Mercurial > projects > dwt2
comparison org.eclipse.jface/src/org/eclipse/jface/preference/ListEditor.d @ 12:bc29606a740c
Added dwt-addons in original directory structure of eclipse.org
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 14 Mar 2009 18:23:29 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
11:43904fec5dca | 12:bc29606a740c |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2006 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module org.eclipse.jface.preference.ListEditor; | |
14 | |
15 import org.eclipse.jface.preference.FieldEditor; | |
16 | |
17 import org.eclipse.swt.SWT; | |
18 import org.eclipse.swt.events.DisposeEvent; | |
19 import org.eclipse.swt.events.DisposeListener; | |
20 import org.eclipse.swt.events.SelectionAdapter; | |
21 import org.eclipse.swt.events.SelectionEvent; | |
22 import org.eclipse.swt.events.SelectionListener; | |
23 import org.eclipse.swt.layout.GridData; | |
24 import org.eclipse.swt.layout.GridLayout; | |
25 import org.eclipse.swt.widgets.Button; | |
26 import org.eclipse.swt.widgets.Composite; | |
27 import org.eclipse.swt.widgets.Control; | |
28 import org.eclipse.swt.widgets.List; | |
29 import org.eclipse.swt.widgets.Shell; | |
30 import org.eclipse.swt.widgets.Widget; | |
31 import org.eclipse.core.runtime.Assert; | |
32 import org.eclipse.jface.dialogs.IDialogConstants; | |
33 import org.eclipse.jface.resource.JFaceResources; | |
34 | |
35 import java.lang.all; | |
36 import java.util.Set; | |
37 | |
38 /** | |
39 * An abstract field editor that manages a list of input values. | |
40 * The editor displays a list containing the values, buttons for | |
41 * adding and removing values, and Up and Down buttons to adjust | |
42 * the order of elements in the list. | |
43 * <p> | |
44 * Subclasses must implement the <code>parseString</code>, | |
45 * <code>createList</code>, and <code>getNewInputObject</code> | |
46 * framework methods. | |
47 * </p> | |
48 */ | |
49 public abstract class ListEditor : FieldEditor { | |
50 | |
51 /** | |
52 * The list widget; <code>null</code> if none | |
53 * (before creation or after disposal). | |
54 */ | |
55 private List list; | |
56 | |
57 /** | |
58 * The button box containing the Add, Remove, Up, and Down buttons; | |
59 * <code>null</code> if none (before creation or after disposal). | |
60 */ | |
61 private Composite buttonBox; | |
62 | |
63 /** | |
64 * The Add button. | |
65 */ | |
66 private Button addButton; | |
67 | |
68 /** | |
69 * The Remove button. | |
70 */ | |
71 private Button removeButton; | |
72 | |
73 /** | |
74 * The Up button. | |
75 */ | |
76 private Button upButton; | |
77 | |
78 /** | |
79 * The Down button. | |
80 */ | |
81 private Button downButton; | |
82 | |
83 /** | |
84 * The selection listener. | |
85 */ | |
86 private SelectionListener selectionListener; | |
87 | |
88 /** | |
89 * Creates a new list field editor | |
90 */ | |
91 protected this() { | |
92 } | |
93 | |
94 /** | |
95 * Creates a list field editor. | |
96 * | |
97 * @param name the name of the preference this field editor works on | |
98 * @param labelText the label text of the field editor | |
99 * @param parent the parent of the field editor's control | |
100 */ | |
101 protected this(String name, String labelText, Composite parent) { | |
102 init(name, labelText); | |
103 createControl(parent); | |
104 } | |
105 | |
106 /** | |
107 * Notifies that the Add button has been pressed. | |
108 */ | |
109 private void addPressed() { | |
110 setPresentsDefaultValue(false); | |
111 String input = getNewInputObject(); | |
112 | |
113 if (input !is null) { | |
114 int index = list.getSelectionIndex(); | |
115 if (index >= 0) { | |
116 list.add(input, index + 1); | |
117 } else { | |
118 list.add(input, 0); | |
119 } | |
120 selectionChanged(); | |
121 } | |
122 } | |
123 | |
124 /* (non-Javadoc) | |
125 * Method declared on FieldEditor. | |
126 */ | |
127 protected override void adjustForNumColumns(int numColumns) { | |
128 Control control = getLabelControl(); | |
129 (cast(GridData) control.getLayoutData()).horizontalSpan = numColumns; | |
130 (cast(GridData) list.getLayoutData()).horizontalSpan = numColumns - 1; | |
131 } | |
132 | |
133 /** | |
134 * Creates the Add, Remove, Up, and Down button in the given button box. | |
135 * | |
136 * @param box the box for the buttons | |
137 */ | |
138 private void createButtons(Composite box) { | |
139 addButton = createPushButton(box, "ListEditor.add");//$NON-NLS-1$ | |
140 removeButton = createPushButton(box, "ListEditor.remove");//$NON-NLS-1$ | |
141 upButton = createPushButton(box, "ListEditor.up");//$NON-NLS-1$ | |
142 downButton = createPushButton(box, "ListEditor.down");//$NON-NLS-1$ | |
143 } | |
144 | |
145 /** | |
146 * Combines the given list of items into a single string. | |
147 * This method is the converse of <code>parseString</code>. | |
148 * <p> | |
149 * Subclasses must implement this method. | |
150 * </p> | |
151 * | |
152 * @param items the list of items | |
153 * @return the combined string | |
154 * @see #parseString | |
155 */ | |
156 protected abstract String createList(String[] items); | |
157 | |
158 /** | |
159 * Helper method to create a push button. | |
160 * | |
161 * @param parent the parent control | |
162 * @param key the resource name used to supply the button's label text | |
163 * @return Button | |
164 */ | |
165 private Button createPushButton(Composite parent, String key) { | |
166 Button button = new Button(parent, SWT.PUSH); | |
167 button.setText(JFaceResources.getString(key)); | |
168 button.setFont(parent.getFont()); | |
169 GridData data = new GridData(GridData.FILL_HORIZONTAL); | |
170 int widthHint = convertHorizontalDLUsToPixels(button, | |
171 IDialogConstants.BUTTON_WIDTH); | |
172 data.widthHint = Math.max(widthHint, button.computeSize(SWT.DEFAULT, | |
173 SWT.DEFAULT, true).x); | |
174 button.setLayoutData(data); | |
175 button.addSelectionListener(getSelectionListener()); | |
176 return button; | |
177 } | |
178 | |
179 /** | |
180 * Creates a selection listener. | |
181 */ | |
182 public void createSelectionListener() { | |
183 selectionListener = new class SelectionAdapter { | |
184 public void widgetSelected(SelectionEvent event) { | |
185 Widget widget = event.widget; | |
186 if (widget is addButton) { | |
187 addPressed(); | |
188 } else if (widget is removeButton) { | |
189 removePressed(); | |
190 } else if (widget is upButton) { | |
191 upPressed(); | |
192 } else if (widget is downButton) { | |
193 downPressed(); | |
194 } else if (widget is list) { | |
195 selectionChanged(); | |
196 } | |
197 } | |
198 }; | |
199 } | |
200 | |
201 /* (non-Javadoc) | |
202 * Method declared on FieldEditor. | |
203 */ | |
204 protected override void doFillIntoGrid(Composite parent, int numColumns) { | |
205 Control control = getLabelControl(parent); | |
206 GridData gd = new GridData(); | |
207 gd.horizontalSpan = numColumns; | |
208 control.setLayoutData(gd); | |
209 | |
210 list = getListControl(parent); | |
211 gd = new GridData(GridData.FILL_HORIZONTAL); | |
212 gd.verticalAlignment = GridData.FILL; | |
213 gd.horizontalSpan = numColumns - 1; | |
214 gd.grabExcessHorizontalSpace = true; | |
215 list.setLayoutData(gd); | |
216 | |
217 buttonBox = getButtonBoxControl(parent); | |
218 gd = new GridData(); | |
219 gd.verticalAlignment = GridData.BEGINNING; | |
220 buttonBox.setLayoutData(gd); | |
221 } | |
222 | |
223 /* (non-Javadoc) | |
224 * Method declared on FieldEditor. | |
225 */ | |
226 protected override void doLoad() { | |
227 if (list !is null) { | |
228 String s = getPreferenceStore().getString(getPreferenceName()); | |
229 String[] array = parseString(s); | |
230 for (int i = 0; i < array.length; i++) { | |
231 list.add(array[i]); | |
232 } | |
233 } | |
234 } | |
235 | |
236 /* (non-Javadoc) | |
237 * Method declared on FieldEditor. | |
238 */ | |
239 protected override void doLoadDefault() { | |
240 if (list !is null) { | |
241 list.removeAll(); | |
242 String s = getPreferenceStore().getDefaultString( | |
243 getPreferenceName()); | |
244 String[] array = parseString(s); | |
245 for (int i = 0; i < array.length; i++) { | |
246 list.add(array[i]); | |
247 } | |
248 } | |
249 } | |
250 | |
251 /* (non-Javadoc) | |
252 * Method declared on FieldEditor. | |
253 */ | |
254 protected override void doStore() { | |
255 String s = createList(list.getItems()); | |
256 if (s !is null) { | |
257 getPreferenceStore().setValue(getPreferenceName(), s); | |
258 } | |
259 } | |
260 | |
261 /** | |
262 * Notifies that the Down button has been pressed. | |
263 */ | |
264 private void downPressed() { | |
265 swap(false); | |
266 } | |
267 | |
268 /** | |
269 * Returns this field editor's button box containing the Add, Remove, | |
270 * Up, and Down button. | |
271 * | |
272 * @param parent the parent control | |
273 * @return the button box | |
274 */ | |
275 public Composite getButtonBoxControl(Composite parent) { | |
276 if (buttonBox is null) { | |
277 buttonBox = new Composite(parent, SWT.NULL); | |
278 GridLayout layout = new GridLayout(); | |
279 layout.marginWidth = 0; | |
280 buttonBox.setLayout(layout); | |
281 createButtons(buttonBox); | |
282 buttonBox.addDisposeListener(new class DisposeListener { | |
283 public void widgetDisposed(DisposeEvent event) { | |
284 addButton = null; | |
285 removeButton = null; | |
286 upButton = null; | |
287 downButton = null; | |
288 buttonBox = null; | |
289 } | |
290 }); | |
291 | |
292 } else { | |
293 checkParent(buttonBox, parent); | |
294 } | |
295 | |
296 selectionChanged(); | |
297 return buttonBox; | |
298 } | |
299 | |
300 /** | |
301 * Returns this field editor's list control. | |
302 * | |
303 * @param parent the parent control | |
304 * @return the list control | |
305 */ | |
306 public List getListControl(Composite parent) { | |
307 if (list is null) { | |
308 list = new List(parent, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL | |
309 | SWT.H_SCROLL); | |
310 list.setFont(parent.getFont()); | |
311 list.addSelectionListener(getSelectionListener()); | |
312 list.addDisposeListener(new class DisposeListener { | |
313 public void widgetDisposed(DisposeEvent event) { | |
314 list = null; | |
315 } | |
316 }); | |
317 } else { | |
318 checkParent(list, parent); | |
319 } | |
320 return list; | |
321 } | |
322 | |
323 /** | |
324 * Creates and returns a new item for the list. | |
325 * <p> | |
326 * Subclasses must implement this method. | |
327 * </p> | |
328 * | |
329 * @return a new item | |
330 */ | |
331 protected abstract String getNewInputObject(); | |
332 | |
333 /* (non-Javadoc) | |
334 * Method declared on FieldEditor. | |
335 */ | |
336 public override int getNumberOfControls() { | |
337 return 2; | |
338 } | |
339 | |
340 /** | |
341 * Returns this field editor's selection listener. | |
342 * The listener is created if nessessary. | |
343 * | |
344 * @return the selection listener | |
345 */ | |
346 private SelectionListener getSelectionListener() { | |
347 if (selectionListener is null) { | |
348 createSelectionListener(); | |
349 } | |
350 return selectionListener; | |
351 } | |
352 | |
353 /** | |
354 * Returns this field editor's shell. | |
355 * <p> | |
356 * This method is internal to the framework; subclassers should not call | |
357 * this method. | |
358 * </p> | |
359 * | |
360 * @return the shell | |
361 */ | |
362 protected Shell getShell() { | |
363 if (addButton is null) { | |
364 return null; | |
365 } | |
366 return addButton.getShell(); | |
367 } | |
368 | |
369 /** | |
370 * Splits the given string into a list of strings. | |
371 * This method is the converse of <code>createList</code>. | |
372 * <p> | |
373 * Subclasses must implement this method. | |
374 * </p> | |
375 * | |
376 * @param stringList the string | |
377 * @return an array of <code>String</code> | |
378 * @see #createList | |
379 */ | |
380 protected abstract String[] parseString(String stringList); | |
381 | |
382 /** | |
383 * Notifies that the Remove button has been pressed. | |
384 */ | |
385 private void removePressed() { | |
386 setPresentsDefaultValue(false); | |
387 int index = list.getSelectionIndex(); | |
388 if (index >= 0) { | |
389 list.remove(index); | |
390 selectionChanged(); | |
391 } | |
392 } | |
393 | |
394 /** | |
395 * Notifies that the list selection has changed. | |
396 */ | |
397 private void selectionChanged() { | |
398 | |
399 int index = list.getSelectionIndex(); | |
400 int size = list.getItemCount(); | |
401 | |
402 removeButton.setEnabled(index >= 0); | |
403 upButton.setEnabled(size > 1 && index > 0); | |
404 downButton.setEnabled(size > 1 && index >= 0 && index < size - 1); | |
405 } | |
406 | |
407 /* (non-Javadoc) | |
408 * Method declared on FieldEditor. | |
409 */ | |
410 public override void setFocus() { | |
411 if (list !is null) { | |
412 list.setFocus(); | |
413 } | |
414 } | |
415 | |
416 /** | |
417 * Moves the currently selected item up or down. | |
418 * | |
419 * @param up <code>true</code> if the item should move up, | |
420 * and <code>false</code> if it should move down | |
421 */ | |
422 private void swap(bool up) { | |
423 setPresentsDefaultValue(false); | |
424 int index = list.getSelectionIndex(); | |
425 int target = up ? index - 1 : index + 1; | |
426 | |
427 if (index >= 0) { | |
428 String[] selection = list.getSelection(); | |
429 Assert.isTrue(selection.length is 1); | |
430 list.remove(index); | |
431 list.add(selection[0], target); | |
432 list.setSelection(target); | |
433 } | |
434 selectionChanged(); | |
435 } | |
436 | |
437 /** | |
438 * Notifies that the Up button has been pressed. | |
439 */ | |
440 private void upPressed() { | |
441 swap(true); | |
442 } | |
443 | |
444 /* | |
445 * @see FieldEditor.setEnabled(bool,Composite). | |
446 */ | |
447 public override void setEnabled(bool enabled, Composite parent) { | |
448 super.setEnabled(enabled, parent); | |
449 getListControl(parent).setEnabled(enabled); | |
450 addButton.setEnabled(enabled); | |
451 removeButton.setEnabled(enabled); | |
452 upButton.setEnabled(enabled); | |
453 downButton.setEnabled(enabled); | |
454 } | |
455 } |