Mercurial > projects > dwt-addons
annotate dwtx/jface/preference/FieldEditorPreferencePage.d @ 104:04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
These new wrappers now use the tango.util.containers instead of the tango.util.collections.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 07 Aug 2008 15:01:33 +0200 |
parents | 46a6e0e6ccd4 |
children |
rev | line source |
---|---|
34 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2007 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Chris Tilt (chris@tilts.net) - Bug 38547 - [Preferences] Changing preferences | |
11 * ignored after "Restore defaults" pressed. | |
12 * Port to the D programming language: | |
13 * Frank Benoit <benoit@tionex.de> | |
14 *******************************************************************************/ | |
15 module dwtx.jface.preference.FieldEditorPreferencePage; | |
16 | |
17 import dwtx.jface.preference.PreferencePage; | |
18 import dwtx.jface.preference.FieldEditor; | |
19 import dwtx.jface.preference.FieldEditorPreferencePage; | |
20 | |
21 // import java.util.Iterator; | |
22 // import java.util.List; | |
23 | |
24 import dwt.DWT; | |
25 import dwt.layout.GridData; | |
26 import dwt.layout.GridLayout; | |
27 import dwt.widgets.Composite; | |
28 import dwt.widgets.Control; | |
29 import dwtx.jface.resource.ImageDescriptor; | |
30 import dwtx.jface.util.IPropertyChangeListener; | |
31 import dwtx.jface.util.PropertyChangeEvent; | |
32 | |
33 import dwt.dwthelper.utils; | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
34 import dwtx.dwtxhelper.Collection; |
34 | 35 |
36 /** | |
37 * A special abstract preference page to host field editors. | |
38 * <p> | |
39 * Subclasses must implement the <code>createFieldEditors</code> method | |
40 * and should override <code>createLayout</code> if a special layout of the field | |
41 * editors is needed. | |
42 * </p> | |
43 */ | |
44 public abstract class FieldEditorPreferencePage : PreferencePage, | |
45 IPropertyChangeListener { | |
46 | |
47 /** | |
48 * Layout constant (value <code>0</code>) indicating that | |
49 * each field editor is handled as a single component. | |
50 */ | |
51 public static const int FLAT = 0; | |
52 | |
53 /** | |
54 * Layout constant (value <code>1</code>) indicating that | |
55 * the field editors' basic controls are put into a grid layout. | |
56 */ | |
57 public static const int GRID = 1; | |
58 | |
59 /** | |
60 * The vertical spacing used by layout styles <code>FLAT</code> | |
61 * and <code>GRID</code>. | |
62 */ | |
63 protected static const int VERTICAL_SPACING = 10; | |
64 | |
65 /** | |
66 * The margin width used by layout styles <code>FLAT</code> | |
67 * and <code>GRID</code>. | |
68 */ | |
69 protected static const int MARGIN_WIDTH = 0; | |
70 | |
71 /** | |
72 * The margin height used by layout styles <code>FLAT</code> | |
73 * and <code>GRID</code>. | |
74 */ | |
75 protected static const int MARGIN_HEIGHT = 0; | |
76 | |
77 /** | |
78 * The field editors, or <code>null</code> if not created yet. | |
79 */ | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
80 private List fields = null; |
34 | 81 |
82 /** | |
83 * The layout style; either <code>FLAT</code> or <code>GRID</code>. | |
84 */ | |
85 private int style; | |
86 | |
87 /** | |
88 * The first invalid field editor, or <code>null</code> | |
89 * if all field editors are valid. | |
90 */ | |
91 private FieldEditor invalidFieldEditor = null; | |
92 | |
93 /** | |
94 * The parent composite for field editors | |
95 */ | |
96 private Composite fieldEditorParent; | |
97 | |
98 /** | |
99 * Create a new instance of the reciever. | |
100 */ | |
101 public this() { | |
102 this(FLAT); | |
103 } | |
104 | |
105 /** | |
106 * Creates a new field editor preference page with the given style, | |
107 * an empty title, and no image. | |
108 * | |
109 * @param style either <code>GRID</code> or <code>FLAT</code> | |
110 */ | |
111 protected this(int style) { | |
112 super(); | |
113 this.style = style; | |
114 } | |
115 | |
116 /** | |
117 * Creates a new field editor preference page with the given title | |
118 * and style, but no image. | |
119 * | |
120 * @param title the title of this preference page | |
121 * @param style either <code>GRID</code> or <code>FLAT</code> | |
122 */ | |
123 protected this(String title, int style) { | |
124 super(title); | |
125 this.style = style; | |
126 } | |
127 | |
128 /** | |
129 * Creates a new field editor preference page with the given title, | |
130 * image, and style. | |
131 * | |
132 * @param title the title of this preference page | |
133 * @param image the image for this preference page, or | |
134 * <code>null</code> if none | |
135 * @param style either <code>GRID</code> or <code>FLAT</code> | |
136 */ | |
137 protected this(String title, ImageDescriptor image, | |
138 int style) { | |
139 super(title, image); | |
140 this.style = style; | |
141 } | |
142 | |
143 /** | |
144 * Adds the given field editor to this page. | |
145 * | |
146 * @param editor the field editor | |
147 */ | |
148 protected void addField(FieldEditor editor) { | |
149 if (fields is null) { | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
150 fields = new ArrayList(); |
34 | 151 } |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
152 fields.add(editor); |
34 | 153 } |
154 | |
155 /** | |
156 * Adjust the layout of the field editors so that | |
157 * they are properly aligned. | |
158 */ | |
159 protected void adjustGridLayout() { | |
160 int numColumns = calcNumberOfColumns(); | |
161 (cast(GridLayout) fieldEditorParent.getLayout()).numColumns = numColumns; | |
162 if (fields !is null) { | |
163 for (int i = 0; i < fields.size(); i++) { | |
164 FieldEditor fieldEditor = cast(FieldEditor) fields.get(i); | |
165 fieldEditor.adjustForNumColumns_package(numColumns); | |
166 } | |
167 } | |
168 } | |
169 | |
170 /** | |
171 * Applys the font to the field editors managed by this page. | |
172 */ | |
173 protected void applyFont() { | |
174 if (fields !is null) { | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
175 Iterator e = fields.iterator(); |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
176 while (e.hasNext()) { |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
177 FieldEditor pe = cast(FieldEditor) e.next(); |
34 | 178 pe.applyFont_package(); |
179 } | |
180 } | |
181 } | |
182 | |
183 /** | |
184 * Calculates the number of columns needed to host all field editors. | |
185 * | |
186 * @return the number of columns | |
187 */ | |
188 private int calcNumberOfColumns() { | |
189 int result = 0; | |
190 if (fields !is null) { | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
191 Iterator e = fields.iterator(); |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
192 while (e.hasNext()) { |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
193 FieldEditor pe = cast(FieldEditor) e.next(); |
34 | 194 result = Math.max(result, pe.getNumberOfControls()); |
195 } | |
196 } | |
197 return result; | |
198 } | |
199 | |
200 /** | |
201 * Recomputes the page's error state by calling <code>isValid</code> for | |
202 * every field editor. | |
203 */ | |
204 protected void checkState() { | |
205 bool valid = true; | |
206 invalidFieldEditor = null; | |
207 // The state can only be set to true if all | |
208 // field editors contain a valid value. So we must check them all | |
209 if (fields !is null) { | |
210 int size = fields.size(); | |
211 for (int i = 0; i < size; i++) { | |
212 FieldEditor editor = cast(FieldEditor) fields.get(i); | |
213 valid = valid && editor.isValid(); | |
214 if (!valid) { | |
215 invalidFieldEditor = editor; | |
216 break; | |
217 } | |
218 } | |
219 } | |
220 setValid(valid); | |
221 } | |
222 | |
223 /* (non-Javadoc) | |
224 * Method declared on PreferencePage. | |
225 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
34
diff
changeset
|
226 protected override Control createContents(Composite parent) { |
34 | 227 fieldEditorParent = new Composite(parent, DWT.NULL); |
228 GridLayout layout = new GridLayout(); | |
229 layout.numColumns = 1; | |
230 layout.marginHeight = 0; | |
231 layout.marginWidth = 0; | |
232 fieldEditorParent.setLayout(layout); | |
233 fieldEditorParent.setFont(parent.getFont()); | |
234 | |
235 createFieldEditors(); | |
236 | |
237 if (style is GRID) { | |
238 adjustGridLayout(); | |
239 } | |
240 | |
241 initialize(); | |
242 checkState(); | |
243 return fieldEditorParent; | |
244 } | |
245 | |
246 /** | |
247 * Creates the page's field editors. | |
248 * <p> | |
249 * The default implementation of this framework method | |
250 * does nothing. Subclass must implement this method to | |
251 * create the field editors. | |
252 * </p> | |
253 * <p> | |
254 * Subclasses should call <code>getFieldEditorParent</code> | |
255 * to obtain the parent control for each field editor. | |
256 * This same parent should not be used for more than | |
257 * one editor as the parent may change for each field | |
258 * editor depending on the layout style of the page | |
259 * </p> | |
260 */ | |
261 protected abstract void createFieldEditors(); | |
262 | |
263 /** | |
264 * The field editor preference page implementation of an <code>IDialogPage</code> | |
265 * method disposes of this page's controls and images. | |
266 * Subclasses may override to release their own allocated DWT | |
267 * resources, but must call <code>super.dispose</code>. | |
268 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
34
diff
changeset
|
269 public override void dispose() { |
34 | 270 super.dispose(); |
271 if (fields !is null) { | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
272 Iterator e = fields.iterator(); |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
273 while (e.hasNext()) { |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
274 FieldEditor pe = cast(FieldEditor) e.next(); |
34 | 275 pe.setPage(null); |
276 pe.setPropertyChangeListener(null); | |
277 pe.setPreferenceStore(null); | |
278 } | |
279 } | |
280 } | |
281 | |
282 /** | |
283 * Returns a parent composite for a field editor. | |
284 * <p> | |
285 * This value must not be cached since a new parent | |
286 * may be created each time this method called. Thus | |
287 * this method must be called each time a field editor | |
288 * is constructed. | |
289 * </p> | |
290 * | |
291 * @return a parent | |
292 */ | |
293 protected Composite getFieldEditorParent() { | |
294 if (style is FLAT) { | |
295 // Create a new parent for each field editor | |
296 Composite parent = new Composite(fieldEditorParent, DWT.NULL); | |
297 parent.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); | |
298 return parent; | |
299 } | |
300 // Just return the parent | |
301 return fieldEditorParent; | |
302 } | |
303 | |
304 /** | |
305 * Initializes all field editors. | |
306 */ | |
307 protected void initialize() { | |
308 if (fields !is null) { | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
309 Iterator e = fields.iterator(); |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
310 while (e.hasNext()) { |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
311 FieldEditor pe = cast(FieldEditor) e.next(); |
34 | 312 pe.setPage(this); |
313 pe.setPropertyChangeListener(this); | |
314 pe.setPreferenceStore(getPreferenceStore()); | |
315 pe.load(); | |
316 } | |
317 } | |
318 } | |
319 | |
320 /** | |
321 * The field editor preference page implementation of a <code>PreferencePage</code> | |
322 * method loads all the field editors with their default values. | |
323 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
34
diff
changeset
|
324 protected override void performDefaults() { |
34 | 325 if (fields !is null) { |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
326 Iterator e = fields.iterator(); |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
327 while (e.hasNext()) { |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
328 FieldEditor pe = cast(FieldEditor) e.next(); |
34 | 329 pe.loadDefault(); |
330 } | |
331 } | |
332 // Force a recalculation of my error state. | |
333 checkState(); | |
334 super.performDefaults(); | |
335 } | |
336 | |
337 /** | |
338 * The field editor preference page implementation of this | |
339 * <code>PreferencePage</code> method saves all field editors by | |
340 * calling <code>FieldEditor.store</code>. Note that this method | |
341 * does not save the preference store itself; it just stores the | |
342 * values back into the preference store. | |
343 * | |
344 * @see FieldEditor#store() | |
345 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
34
diff
changeset
|
346 public override bool performOk() { |
34 | 347 if (fields !is null) { |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
348 Iterator e = fields.iterator(); |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
349 while (e.hasNext()) { |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
350 FieldEditor pe = cast(FieldEditor) e.next(); |
34 | 351 pe.store(); |
352 pe.setPresentsDefaultValue_package(false); | |
353 } | |
354 } | |
355 return true; | |
356 } | |
357 | |
358 /** | |
359 * The field editor preference page implementation of this <code>IPreferencePage</code> | |
360 * (and <code>IPropertyChangeListener</code>) method intercepts <code>IS_VALID</code> | |
361 * events but passes other events on to its superclass. | |
362 */ | |
363 public void propertyChange(PropertyChangeEvent event) { | |
364 | |
365 if (event.getProperty().equals(FieldEditor.IS_VALID)) { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
366 bool newValue = (cast(Boolean) event.getNewValue()).booleanValue(); |
34 | 367 // If the new value is true then we must check all field editors. |
368 // If it is false, then the page is invalid in any case. | |
369 if (newValue) { | |
370 checkState(); | |
371 } else { | |
372 invalidFieldEditor = cast(FieldEditor) event.getSource(); | |
373 setValid(newValue); | |
374 } | |
375 } | |
376 } | |
377 | |
378 /* (non-Javadoc) | |
379 * Method declared on IDialog. | |
380 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
34
diff
changeset
|
381 public override void setVisible(bool visible) { |
34 | 382 super.setVisible(visible); |
383 if (visible && invalidFieldEditor !is null) { | |
384 invalidFieldEditor.setFocus(); | |
385 } | |
386 } | |
387 } |