comparison jface/ShowPrefs.d @ 62:caaf053c44d6

more examples
author Frank Benoit <benoit@tionex.de>
date Fri, 11 Apr 2008 17:08:20 +0200
parents
children 5c1906bfc206
comparison
equal deleted inserted replaced
61:ffb8196501b3 62:caaf053c44d6
1 module jface.ShowPrefs;
2
3 import dwtx.jface.preference.BooleanFieldEditor;
4 import dwtx.jface.preference.ColorFieldEditor;
5 import dwtx.jface.preference.DirectoryFieldEditor;
6 import dwtx.jface.preference.FileFieldEditor;
7 import dwtx.jface.preference.FontFieldEditor;
8 import dwtx.jface.preference.FieldEditorPreferencePage;
9 import dwtx.jface.preference.RadioGroupFieldEditor;
10 import dwtx.jface.preference.PathEditor;
11 import dwtx.jface.preference.IntegerFieldEditor;
12 import dwtx.jface.preference.ScaleFieldEditor;
13 import dwtx.jface.preference.StringFieldEditor;
14 import dwtx.jface.preference.IPreferenceStore;
15 import dwtx.jface.preference.PreferenceManager;
16 import dwtx.jface.preference.PreferencePage;
17 import dwtx.jface.preference.PreferenceNode;
18 import dwtx.jface.preference.PreferenceStore;
19 import dwtx.jface.preference.PreferenceDialog;
20 import dwtx.jface.resource.ImageDescriptor;
21
22 import dwt.widgets.Display;
23 import dwt.widgets.Composite;
24 import dwt.widgets.Label;
25 import dwt.widgets.Button;
26 import dwt.widgets.Text;
27 import dwt.widgets.Control;
28 import dwt.events.SelectionAdapter;
29 import dwt.events.SelectionEvent;
30 import dwt.layout.RowLayout;
31 import dwt.layout.GridLayout;
32 import dwt.layout.GridData;
33 import dwt.DWT;
34 import dwt.dwthelper.utils;
35
36 import tango.io.File;
37 import tango.io.FilePath;
38
39 version(JIVE) import jive.stacktrace;
40
41 const char[] FILENAME = "showprefs";
42
43 /**
44 * This class creates a preference page
45 */
46 public class PrefPageOne : PreferencePage {
47 // Names for preferences
48 private static final String ONE = "one.one";
49 private static final String TWO = "one.two";
50 private static final String THREE = "one.three";
51
52 // Text fields for user to enter preferences
53 private Text fieldOne;
54 private Text fieldTwo;
55 private Text fieldThree;
56
57 /**
58 * Creates the controls for this page
59 */
60 protected Control createContents(Composite parent) {
61 Composite composite = new Composite(parent, DWT.NONE);
62 composite.setLayout(new GridLayout(2, false));
63
64 // Get the preference store
65 IPreferenceStore preferenceStore = getPreferenceStore();
66
67 // Create three text fields.
68 // Set the text in each from the preference store
69 (new Label(composite, DWT.LEFT)).setText("Field One:");
70 fieldOne = new Text(composite, DWT.BORDER);
71 fieldOne.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
72 fieldOne.setText(preferenceStore.getString(ONE));
73
74 (new Label(composite, DWT.LEFT)).setText("Field Two:");
75 fieldTwo = new Text(composite, DWT.BORDER);
76 fieldTwo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
77 fieldTwo.setText(preferenceStore.getString(TWO));
78
79 (new Label(composite, DWT.LEFT)).setText("Field Three:");
80 fieldThree = new Text(composite, DWT.BORDER);
81 fieldThree.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
82 fieldThree.setText(preferenceStore.getString(THREE));
83
84 return composite;
85 }
86
87 /**
88 * Called when user clicks Restore Defaults
89 */
90 protected void performDefaults() {
91 // Get the preference store
92 IPreferenceStore preferenceStore = getPreferenceStore();
93
94 // Reset the fields to the defaults
95 fieldOne.setText(preferenceStore.getDefaultString(ONE));
96 fieldTwo.setText(preferenceStore.getDefaultString(TWO));
97 fieldThree.setText(preferenceStore.getDefaultString(THREE));
98 }
99
100 /**
101 * Called when user clicks Apply or OK
102 *
103 * @return bool
104 */
105 public bool performOk() {
106 // Get the preference store
107 IPreferenceStore preferenceStore = getPreferenceStore();
108
109 // Set the values from the fields
110 if (fieldOne !is null) preferenceStore.setValue(ONE, fieldOne.getText());
111 if (fieldTwo !is null) preferenceStore.setValue(TWO, fieldTwo.getText());
112 if (fieldThree !is null)
113 preferenceStore.setValue(THREE, fieldThree.getText());
114
115 // Return true to allow dialog to close
116 return true;
117 }
118 }
119
120
121 /**
122 * This class creates a preference page
123 */
124 public class PrefPageTwo : PreferencePage {
125 // Names for preferences
126 private static final String ONE = "two.one";
127 private static final String TWO = "two.two";
128 private static final String THREE = "two.three";
129
130 // The checkboxes
131 private Button checkOne;
132 private Button checkTwo;
133 private Button checkThree;
134
135 /**
136 * PrefPageTwo constructor
137 */
138 public this() {
139 super("Two");
140 setDescription("Check the checks");
141 }
142
143 /**
144 * Creates the controls for this page
145 */
146 protected Control createContents(Composite parent) {
147 Composite composite = new Composite(parent, DWT.NONE);
148 composite.setLayout(new RowLayout(DWT.VERTICAL));
149
150 // Get the preference store
151 IPreferenceStore preferenceStore = getPreferenceStore();
152
153 // Create three checkboxes
154 checkOne = new Button(composite, DWT.CHECK);
155 checkOne.setText("Check One");
156 checkOne.setSelection(preferenceStore.getBoolean(ONE));
157
158 checkTwo = new Button(composite, DWT.CHECK);
159 checkTwo.setText("Check Two");
160 checkTwo.setSelection(preferenceStore.getBoolean(TWO));
161
162 checkThree = new Button(composite, DWT.CHECK);
163 checkThree.setText("Check Three");
164 checkThree.setSelection(preferenceStore.getBoolean(THREE));
165
166 return composite;
167 }
168
169 /**
170 * Add buttons
171 *
172 * @param parent the parent composite
173 */
174 protected void contributeButtons(Composite parent) {
175 // Add a select all button
176 Button selectAll = new Button(parent, DWT.PUSH);
177 selectAll.setText("Select All");
178 selectAll.addSelectionListener(new class SelectionAdapter {
179 public void widgetSelected(SelectionEvent event) {
180 checkOne.setSelection(true);
181 checkTwo.setSelection(true);
182 checkThree.setSelection(true);
183 }
184 });
185
186 // Add a select all button
187 Button clearAll = new Button(parent, DWT.PUSH);
188 clearAll.setText("Clear All");
189 clearAll.addSelectionListener(new class SelectionAdapter {
190 public void widgetSelected(SelectionEvent event) {
191 checkOne.setSelection(false);
192 checkTwo.setSelection(false);
193 checkThree.setSelection(false);
194 }
195 });
196
197 // Add two columns to the parent's layout
198 (cast(GridLayout) parent.getLayout()).numColumns += 2;
199 }
200
201 /**
202 * Change the description label
203 */
204 protected Label createDescriptionLabel(Composite parent) {
205 Label label = null;
206 String description = getDescription();
207 if (description != null) {
208 // Upper case the description
209 description = description.toUpperCase();
210
211 // Right-align the label
212 label = new Label(parent, DWT.RIGHT);
213 label.setText(description);
214 }
215 return label;
216 }
217
218 /**
219 * Called when user clicks Restore Defaults
220 */
221 protected void performDefaults() {
222 // Get the preference store
223 IPreferenceStore preferenceStore = getPreferenceStore();
224
225 // Reset the fields to the defaults
226 checkOne.setSelection(preferenceStore.getDefaultBoolean(ONE));
227 checkTwo.setSelection(preferenceStore.getDefaultBoolean(TWO));
228 checkThree.setSelection(preferenceStore.getDefaultBoolean(THREE));
229 }
230
231 /**
232 * Called when user clicks Apply or OK
233 *
234 * @return bool
235 */
236 public bool performOk() {
237 // Get the preference store
238 IPreferenceStore preferenceStore = getPreferenceStore();
239
240 // Set the values from the fields
241 if (checkOne !is null) preferenceStore.setValue(ONE, checkOne.getSelection());
242 if (checkTwo !is null) preferenceStore.setValue(TWO, checkTwo.getSelection());
243 if (checkThree !is null)
244 preferenceStore.setValue(THREE, checkThree.getSelection());
245
246 // Return true to allow dialog to close
247 return true;
248 }
249 }
250
251
252
253 /**
254 * This class demonstrates JFace preferences
255 */
256 public class ShowPrefs {
257 /**
258 * Runs the application
259 */
260 public void run() {
261 // Display display = new Display();
262
263 // Create the preference manager
264 PreferenceManager mgr = new PreferenceManager();
265
266 // Create the nodes
267 PreferenceNode one = new PreferenceNode("one", "One", ImageDescriptor
268 .createFromFile(getImportData!("cancel.gif")), PrefPageOne.classinfo.name );
269 PreferenceNode two = new PreferenceNode("two", new PrefPageTwo());
270
271 // Add the nodes
272 mgr.addToRoot(one);
273 mgr.addTo(one.getId(), two);
274
275 // Create the preferences dialog
276 PreferenceDialog dlg = new PreferenceDialog(null, mgr);
277
278 // Set the preference store
279 PreferenceStore ps = new PreferenceStore( FILENAME );
280 try {
281 ps.load();
282 } catch (IOException e) {
283 // Ignore
284 }
285 dlg.setPreferenceStore(ps);
286
287 // Open the dialog
288 dlg.open();
289
290 try {
291 // Save the preferences
292 ps.save();
293 } catch (IOException e) {
294 ExceptionPrintStackTrace(e);
295 }
296 // display.dispose();
297 }
298
299 /**
300 * The application entry point
301 *
302 * @param args the command line arguments
303 */
304 public static void main(String[] args) {
305 (new ShowPrefs()).run();
306 }
307 }
308
309 void main(){
310 scope fn = new FilePath( FILENAME );
311 if( !fn.exists ){
312 scope prefs = new File( fn );
313 prefs.write( import("jface.showprefs.properties" ));
314 }
315 ShowPrefs.main( null );
316 }
317
318
319
320
321