KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > preference > ListEditor


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  *******************************************************************************/

11 package org.eclipse.jface.preference;
12
13 import org.eclipse.jface.dialogs.IDialogConstants;
14 import org.eclipse.jface.resource.JFaceResources;
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.DisposeEvent;
18 import org.eclipse.swt.events.DisposeListener;
19 import org.eclipse.swt.events.SelectionAdapter;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.events.SelectionListener;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.Button;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.List;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.swt.widgets.Widget;
30
31 /**
32  * An abstract field editor that manages a list of input values.
33  * The editor displays a list containing the values, buttons for
34  * adding and removing values, and Up and Down buttons to adjust
35  * the order of elements in the list.
36  * <p>
37  * Subclasses must implement the <code>parseString</code>,
38  * <code>createList</code>, and <code>getNewInputObject</code>
39  * framework methods.
40  * </p>
41  */

42 public abstract class ListEditor extends FieldEditor {
43
44     /**
45      * The list widget; <code>null</code> if none
46      * (before creation or after disposal).
47      */

48     private List list;
49
50     /**
51      * The button box containing the Add, Remove, Up, and Down buttons;
52      * <code>null</code> if none (before creation or after disposal).
53      */

54     private Composite buttonBox;
55
56     /**
57      * The Add button.
58      */

59     private Button addButton;
60
61     /**
62      * The Remove button.
63      */

64     private Button removeButton;
65
66     /**
67      * The Up button.
68      */

69     private Button upButton;
70
71     /**
72      * The Down button.
73      */

74     private Button downButton;
75
76     /**
77      * The selection listener.
78      */

79     private SelectionListener selectionListener;
80
81     /**
82      * Creates a new list field editor
83      */

84     protected ListEditor() {
85     }
86
87     /**
88      * Creates a list field editor.
89      *
90      * @param name the name of the preference this field editor works on
91      * @param labelText the label text of the field editor
92      * @param parent the parent of the field editor's control
93      */

94     protected ListEditor(String JavaDoc name, String JavaDoc labelText, Composite parent) {
95         init(name, labelText);
96         createControl(parent);
97     }
98
99     /**
100      * Notifies that the Add button has been pressed.
101      */

102     private void addPressed() {
103         setPresentsDefaultValue(false);
104         String JavaDoc input = getNewInputObject();
105
106         if (input != null) {
107             int index = list.getSelectionIndex();
108             if (index >= 0) {
109                 list.add(input, index + 1);
110             } else {
111                 list.add(input, 0);
112             }
113             selectionChanged();
114         }
115     }
116
117     /* (non-Javadoc)
118      * Method declared on FieldEditor.
119      */

120     protected void adjustForNumColumns(int numColumns) {
121         Control control = getLabelControl();
122         ((GridData) control.getLayoutData()).horizontalSpan = numColumns;
123         ((GridData) list.getLayoutData()).horizontalSpan = numColumns - 1;
124     }
125
126     /**
127      * Creates the Add, Remove, Up, and Down button in the given button box.
128      *
129      * @param box the box for the buttons
130      */

131     private void createButtons(Composite box) {
132         addButton = createPushButton(box, "ListEditor.add");//$NON-NLS-1$
133
removeButton = createPushButton(box, "ListEditor.remove");//$NON-NLS-1$
134
upButton = createPushButton(box, "ListEditor.up");//$NON-NLS-1$
135
downButton = createPushButton(box, "ListEditor.down");//$NON-NLS-1$
136
}
137
138     /**
139      * Combines the given list of items into a single string.
140      * This method is the converse of <code>parseString</code>.
141      * <p>
142      * Subclasses must implement this method.
143      * </p>
144      *
145      * @param items the list of items
146      * @return the combined string
147      * @see #parseString
148      */

149     protected abstract String JavaDoc createList(String JavaDoc[] items);
150
151     /**
152      * Helper method to create a push button.
153      *
154      * @param parent the parent control
155      * @param key the resource name used to supply the button's label text
156      * @return Button
157      */

158     private Button createPushButton(Composite parent, String JavaDoc key) {
159         Button button = new Button(parent, SWT.PUSH);
160         button.setText(JFaceResources.getString(key));
161         button.setFont(parent.getFont());
162         GridData data = new GridData(GridData.FILL_HORIZONTAL);
163         int widthHint = convertHorizontalDLUsToPixels(button,
164                 IDialogConstants.BUTTON_WIDTH);
165         data.widthHint = Math.max(widthHint, button.computeSize(SWT.DEFAULT,
166                 SWT.DEFAULT, true).x);
167         button.setLayoutData(data);
168         button.addSelectionListener(getSelectionListener());
169         return button;
170     }
171
172     /**
173      * Creates a selection listener.
174      */

175     public void createSelectionListener() {
176         selectionListener = new SelectionAdapter() {
177             public void widgetSelected(SelectionEvent event) {
178                 Widget widget = event.widget;
179                 if (widget == addButton) {
180                     addPressed();
181                 } else if (widget == removeButton) {
182                     removePressed();
183                 } else if (widget == upButton) {
184                     upPressed();
185                 } else if (widget == downButton) {
186                     downPressed();
187                 } else if (widget == list) {
188                     selectionChanged();
189                 }
190             }
191         };
192     }
193
194     /* (non-Javadoc)
195      * Method declared on FieldEditor.
196      */

197     protected void doFillIntoGrid(Composite parent, int numColumns) {
198         Control control = getLabelControl(parent);
199         GridData gd = new GridData();
200         gd.horizontalSpan = numColumns;
201         control.setLayoutData(gd);
202
203         list = getListControl(parent);
204         gd = new GridData(GridData.FILL_HORIZONTAL);
205         gd.verticalAlignment = GridData.FILL;
206         gd.horizontalSpan = numColumns - 1;
207         gd.grabExcessHorizontalSpace = true;
208         list.setLayoutData(gd);
209
210         buttonBox = getButtonBoxControl(parent);
211         gd = new GridData();
212         gd.verticalAlignment = GridData.BEGINNING;
213         buttonBox.setLayoutData(gd);
214     }
215
216     /* (non-Javadoc)
217      * Method declared on FieldEditor.
218      */

219     protected void doLoad() {
220         if (list != null) {
221             String JavaDoc s = getPreferenceStore().getString(getPreferenceName());
222             String JavaDoc[] array = parseString(s);
223             for (int i = 0; i < array.length; i++) {
224                 list.add(array[i]);
225             }
226         }
227     }
228
229     /* (non-Javadoc)
230      * Method declared on FieldEditor.
231      */

232     protected void doLoadDefault() {
233         if (list != null) {
234             list.removeAll();
235             String JavaDoc s = getPreferenceStore().getDefaultString(
236                     getPreferenceName());
237             String JavaDoc[] array = parseString(s);
238             for (int i = 0; i < array.length; i++) {
239                 list.add(array[i]);
240             }
241         }
242     }
243
244     /* (non-Javadoc)
245      * Method declared on FieldEditor.
246      */

247     protected void doStore() {
248         String JavaDoc s = createList(list.getItems());
249         if (s != null) {
250             getPreferenceStore().setValue(getPreferenceName(), s);
251         }
252     }
253
254     /**
255      * Notifies that the Down button has been pressed.
256      */

257     private void downPressed() {
258         swap(false);
259     }
260
261     /**
262      * Returns this field editor's button box containing the Add, Remove,
263      * Up, and Down button.
264      *
265      * @param parent the parent control
266      * @return the button box
267      */

268     public Composite getButtonBoxControl(Composite parent) {
269         if (buttonBox == null) {
270             buttonBox = new Composite(parent, SWT.NULL);
271             GridLayout layout = new GridLayout();
272             layout.marginWidth = 0;
273             buttonBox.setLayout(layout);
274             createButtons(buttonBox);
275             buttonBox.addDisposeListener(new DisposeListener() {
276                 public void widgetDisposed(DisposeEvent event) {
277                     addButton = null;
278                     removeButton = null;
279                     upButton = null;
280                     downButton = null;
281                     buttonBox = null;
282                 }
283             });
284
285         } else {
286             checkParent(buttonBox, parent);
287         }
288
289         selectionChanged();
290         return buttonBox;
291     }
292
293     /**
294      * Returns this field editor's list control.
295      *
296      * @param parent the parent control
297      * @return the list control
298      */

299     public List getListControl(Composite parent) {
300         if (list == null) {
301             list = new List(parent, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL
302                     | SWT.H_SCROLL);
303             list.setFont(parent.getFont());
304             list.addSelectionListener(getSelectionListener());
305             list.addDisposeListener(new DisposeListener() {
306                 public void widgetDisposed(DisposeEvent event) {
307                     list = null;
308                 }
309             });
310         } else {
311             checkParent(list, parent);
312         }
313         return list;
314     }
315
316     /**
317      * Creates and returns a new item for the list.
318      * <p>
319      * Subclasses must implement this method.
320      * </p>
321      *
322      * @return a new item
323      */

324     protected abstract String JavaDoc getNewInputObject();
325
326     /* (non-Javadoc)
327      * Method declared on FieldEditor.
328      */

329     public int getNumberOfControls() {
330         return 2;
331     }
332
333     /**
334      * Returns this field editor's selection listener.
335      * The listener is created if nessessary.
336      *
337      * @return the selection listener
338      */

339     private SelectionListener getSelectionListener() {
340         if (selectionListener == null) {
341             createSelectionListener();
342         }
343         return selectionListener;
344     }
345
346     /**
347      * Returns this field editor's shell.
348      * <p>
349      * This method is internal to the framework; subclassers should not call
350      * this method.
351      * </p>
352      *
353      * @return the shell
354      */

355     protected Shell getShell() {
356         if (addButton == null) {
357             return null;
358         }
359         return addButton.getShell();
360     }
361
362     /**
363      * Splits the given string into a list of strings.
364      * This method is the converse of <code>createList</code>.
365      * <p>
366      * Subclasses must implement this method.
367      * </p>
368      *
369      * @param stringList the string
370      * @return an array of <code>String</code>
371      * @see #createList
372      */

373     protected abstract String JavaDoc[] parseString(String JavaDoc stringList);
374
375     /**
376      * Notifies that the Remove button has been pressed.
377      */

378     private void removePressed() {
379         setPresentsDefaultValue(false);
380         int index = list.getSelectionIndex();
381         if (index >= 0) {
382             list.remove(index);
383             selectionChanged();
384         }
385     }
386
387     /**
388      * Notifies that the list selection has changed.
389      */

390     private void selectionChanged() {
391
392         int index = list.getSelectionIndex();
393         int size = list.getItemCount();
394
395         removeButton.setEnabled(index >= 0);
396         upButton.setEnabled(size > 1 && index > 0);
397         downButton.setEnabled(size > 1 && index >= 0 && index < size - 1);
398     }
399
400     /* (non-Javadoc)
401      * Method declared on FieldEditor.
402      */

403     public void setFocus() {
404         if (list != null) {
405             list.setFocus();
406         }
407     }
408
409     /**
410      * Moves the currently selected item up or down.
411      *
412      * @param up <code>true</code> if the item should move up,
413      * and <code>false</code> if it should move down
414      */

415     private void swap(boolean up) {
416         setPresentsDefaultValue(false);
417         int index = list.getSelectionIndex();
418         int target = up ? index - 1 : index + 1;
419
420         if (index >= 0) {
421             String JavaDoc[] selection = list.getSelection();
422             Assert.isTrue(selection.length == 1);
423             list.remove(index);
424             list.add(selection[0], target);
425             list.setSelection(target);
426         }
427         selectionChanged();
428     }
429
430     /**
431      * Notifies that the Up button has been pressed.
432      */

433     private void upPressed() {
434         swap(true);
435     }
436
437     /*
438      * @see FieldEditor.setEnabled(boolean,Composite).
439      */

440     public void setEnabled(boolean enabled, Composite parent) {
441         super.setEnabled(enabled, parent);
442         getListControl(parent).setEnabled(enabled);
443         addButton.setEnabled(enabled);
444         removeButton.setEnabled(enabled);
445         upButton.setEnabled(enabled);
446         downButton.setEnabled(enabled);
447     }
448 }
449
Popular Tags