KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > editors > ListFieldEditor


1 package com.bull.eclipse.jonas.editors;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.StringTokenizer JavaDoc;
5
6 import org.eclipse.jface.dialogs.IDialogConstants;
7 import org.eclipse.jface.dialogs.InputDialog;
8 import org.eclipse.jface.preference.ListEditor;
9 import org.eclipse.jface.util.Assert;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.events.DisposeEvent;
12 import org.eclipse.swt.events.DisposeListener;
13 import org.eclipse.swt.events.SelectionAdapter;
14 import org.eclipse.swt.events.SelectionEvent;
15 import org.eclipse.swt.events.SelectionListener;
16 import org.eclipse.swt.layout.GridData;
17 import org.eclipse.swt.layout.GridLayout;
18 import org.eclipse.swt.widgets.Button;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.List;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.swt.widgets.Widget;
24
25 import com.bull.eclipse.jonas.JonasPluginResources;
26
27
28 public class ListFieldEditor extends ListEditor implements JonasPluginResources {
29
30     /**
31      * The list widget; <code>null</code> if none
32      * (before creation or after disposal).
33      */

34     protected List JavaDoc list;
35
36     /**
37      * The button box containing the Add, Remove, Up, and Down buttons;
38      * <code>null</code> if none (before creation or after disposal).
39      */

40     protected Composite buttonBox;
41
42     protected Button addButton;
43     protected Button removeButton;
44     protected Button updateButton;
45     protected Button upButton;
46     protected Button downButton;
47
48     /**
49      * The selection listener.
50      */

51     protected SelectionListener selectionListener;
52     /**
53      * Creates a new list field editor
54      */

55     public ListFieldEditor() {
56     }
57     /**
58      * Creates a list field editor.
59      *
60      * @param name the name of the preference this field editor works on
61      * @param labelText the label text of the field editor
62      * @param parent the parent of the field editor's control
63      */

64     public ListFieldEditor(String JavaDoc name, String JavaDoc labelText, Composite parent) {
65         init(name, labelText);
66         createControl(parent);
67     }
68     
69     
70     /**
71      * Notifies that the Add button has been pressed.
72      */

73     protected void addPressed() {
74         setPresentsDefaultValue(false);
75         String JavaDoc input = getNewInputObject();
76
77         if (input != null) {
78             int index = list.getSelectionIndex();
79             if (index >= 0)
80                 list.add(input, index + 1);
81             else
82                 list.add(input, 0);
83             selectionChanged();
84         }
85     }
86
87     /**
88      * Notifies that the Add button has been pressed.
89      */

90     protected void updatePressed() {
91         String JavaDoc input = getNewInputObject();
92
93         if (input != null) {
94             int index = list.getSelectionIndex();
95             if (index >= 0)
96                 list.setItem(index, input);
97             selectionChanged();
98         }
99     }
100     
101     /* (non-Javadoc)
102      * Method declared on FieldEditor.
103      */

104     protected void adjustForNumColumns(int numColumns) {
105         Control control = getLabelControl();
106         ((GridData) control.getLayoutData()).horizontalSpan = numColumns;
107         ((GridData) list.getLayoutData()).horizontalSpan = numColumns - 1;
108     }
109     /**
110      * Creates the Add, Remove, Up, and Down button in the given button box.
111      *
112      * @param buttonBox the box for the buttons
113      */

114     protected void createButtons(Composite buttonBox) {
115         addButton = createPushButton(buttonBox, PREF_PAGE_ADDBUTTON_LABEL); //$NON-NLS-1$
116
removeButton = createPushButton(buttonBox, PREF_PAGE_REMOVEBUTTON_LABEL); //$NON-NLS-1$
117
updateButton = createPushButton(buttonBox, PREF_PAGE_UPDATEBUTTON_LABEL); //$NON-NLS-1$
118
upButton = createPushButton(buttonBox, PREF_PAGE_UPBUTTON_LABEL); //$NON-NLS-1$
119
downButton = createPushButton(buttonBox, PREF_PAGE_DOWNBUTTON_LABEL); //$NON-NLS-1$
120
}
121
122     /**
123      * Helper method to create a push button.
124      *
125      * @param parent the parent control
126      * @param key the resource name used to supply the button's label text
127      */

128     protected Button createPushButton(Composite parent, String JavaDoc label) {
129         Button button = new Button(parent, SWT.PUSH);
130         button.setText(label);
131         GridData data = new GridData(GridData.FILL_HORIZONTAL);
132         data.heightHint = convertVerticalDLUsToPixels(button, IDialogConstants.BUTTON_HEIGHT);
133         int widthHint = convertHorizontalDLUsToPixels(button, IDialogConstants.BUTTON_WIDTH);
134         data.widthHint = Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
135         // data.horizontalSpan = hs; use to have multiple buttons on the same row
136
button.setLayoutData(data);
137         button.addSelectionListener(getSelectionListener());
138         return button;
139     }
140     /**
141      * Creates a selection listener.
142      */

143     public void createSelectionListener() {
144         selectionListener = new SelectionAdapter() {
145             public void widgetSelected(SelectionEvent event) {
146                 Widget widget = event.widget;
147                 if (widget == addButton) {
148                     addPressed();
149                 } else if (widget == removeButton) {
150                     removePressed();
151                 } else if (widget == updateButton) {
152                     updatePressed();
153                 } else if (widget == upButton) {
154                     upPressed();
155                 } else if (widget == downButton) {
156                     downPressed();
157                 } else if (widget == list) {
158                     selectionChanged();
159                 }
160             }
161         };
162     }
163     /* (non-Javadoc)
164      * Method declared on FieldEditor.
165      */

166     protected void doFillIntoGrid(Composite parent, int numColumns) {
167
168         Control control = getLabelControl(parent);
169         GridData gd = new GridData();
170         gd.horizontalSpan = numColumns;
171         control.setLayoutData(gd);
172
173         list = getListControl(parent);
174         gd = new GridData(GridData.FILL_HORIZONTAL);
175         gd.verticalAlignment = GridData.FILL;
176         gd.horizontalSpan = numColumns - 1;
177         gd.horizontalAlignment = GridData.FILL;
178         gd.grabExcessHorizontalSpace = true;
179         list.setLayoutData(gd);
180
181         buttonBox = getButtonBoxControl(parent);
182         gd = new GridData();
183         gd.verticalAlignment = GridData.BEGINNING;
184         buttonBox.setLayoutData(gd);
185     }
186     /* (non-Javadoc)
187      * Method declared on FieldEditor.
188      */

189     protected void doLoad() {
190         if (list != null) {
191             String JavaDoc s = getPreferenceStore().getString(getPreferenceName());
192             String JavaDoc[] array = parseString(s);
193             for (int i = 0; i < array.length; i++) {
194                 list.add(array[i]);
195             }
196         }
197     }
198     /* (non-Javadoc)
199      * Method declared on FieldEditor.
200      */

201     protected void doLoadDefault() {
202         if (list != null) {
203             list.removeAll();
204             String JavaDoc s = getPreferenceStore().getDefaultString(getPreferenceName());
205             String JavaDoc[] array = parseString(s);
206             for (int i = 0; i < array.length; i++) {
207                 list.add(array[i]);
208             }
209         }
210     }
211     /* (non-Javadoc)
212      * Method declared on FieldEditor.
213      */

214     protected void doStore() {
215         String JavaDoc s = createList(list.getItems());
216         if (s != null)
217             getPreferenceStore().setValue(getPreferenceName(), s);
218     }
219     /**
220      * Notifies that the Down button has been pressed.
221      */

222     protected void downPressed() {
223         swap(false);
224     }
225     /**
226      * Returns this field editor's button box containing the Add, Remove,
227      * Up, and Down button.
228      *
229      * @param parent the parent control
230      * @return the button box
231      */

232     public Composite getButtonBoxControl(Composite parent) {
233         if (buttonBox == null) {
234             buttonBox = new Composite(parent, SWT.NULL);
235             GridLayout layout = new GridLayout();
236             layout.numColumns = 1; // change value to have multiple buttons on the same row
237
layout.marginWidth = 0;
238             layout.marginHeight = 0;
239             layout.verticalSpacing = 0;
240             buttonBox.setLayout(layout);
241             createButtons(buttonBox);
242             buttonBox.addDisposeListener(new DisposeListener() {
243                 public void widgetDisposed(DisposeEvent event) {
244                     addButton = null;
245                     removeButton = null;
246                     upButton = null;
247                     downButton = null;
248                     buttonBox = null;
249                 }
250             });
251
252         } else {
253             checkParent(buttonBox, parent);
254         }
255
256         selectionChanged();
257         return buttonBox;
258     }
259     /**
260      * Returns this field editor's list control.
261      *
262      * @param parent the parent control
263      * @return the list control
264      */

265     public List JavaDoc getListControl(Composite parent) {
266         if (list == null) {
267             list = new List JavaDoc(parent, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL | SWT.H_SCROLL);
268             list.addSelectionListener(getSelectionListener());
269             list.addDisposeListener(new DisposeListener() {
270                 public void widgetDisposed(DisposeEvent event) {
271                     list = null;
272                 }
273             });
274         } else {
275             checkParent(list, parent);
276         }
277         return list;
278     }
279
280     /* (non-Javadoc)
281      * Method declared on FieldEditor.
282      */

283     public int getNumberOfControls() {
284         return 2;
285     }
286     /**
287      * Returns this field editor's selection listener.
288      * The listener is created if nessessary.
289      *
290      * @return the selection listener
291      */

292     protected SelectionListener getSelectionListener() {
293         if (selectionListener == null)
294             createSelectionListener();
295         return selectionListener;
296     }
297     /**
298      * Returns this field editor's shell.
299      * <p>
300      * This method is internal to the framework; subclassers should not call
301      * this method.
302      * </p>
303      *
304      * @return the shell
305      */

306     protected Shell getShell() {
307         if (addButton == null)
308             return null;
309         return addButton.getShell();
310     }
311
312     /**
313      * Notifies that the Remove button has been pressed.
314      */

315     protected void removePressed() {
316         setPresentsDefaultValue(false);
317         int index = list.getSelectionIndex();
318         if (index >= 0) {
319             list.remove(index);
320             selectionChanged();
321         }
322     }
323     /**
324      * Notifies that the list selection has changed.
325      */

326     protected void selectionChanged() {
327
328         int index = list.getSelectionIndex();
329         int size = list.getItemCount();
330
331         removeButton.setEnabled(index >= 0);
332         upButton.setEnabled(size > 1 && index > 0);
333         downButton.setEnabled(size > 1 && index >= 0 && index < size - 1);
334     }
335     /* (non-Javadoc)
336      * Method declared on FieldEditor.
337      */

338     public void setFocus() {
339         if (list != null) {
340             list.setFocus();
341         }
342     }
343     /**
344      * Moves the currently selected item up or down.
345      *
346      * @param up <code>true</code> if the item should move up,
347      * and <code>false</code> if it should move down
348      */

349     protected void swap(boolean up) {
350         setPresentsDefaultValue(false);
351         int index = list.getSelectionIndex();
352         int target = up ? index - 1 : index + 1;
353
354         if (index >= 0) {
355             String JavaDoc[] selection = list.getSelection();
356             Assert.isTrue(selection.length == 1);
357             list.remove(index);
358             list.add(selection[0], target);
359             list.setSelection(target);
360         }
361         selectionChanged();
362     }
363     /**
364      * Notifies that the Up button has been pressed.
365      */

366     protected void upPressed() {
367         swap(true);
368     }
369
370     /**
371      * Combines the given list of items into a single string.
372      * This method is the converse of <code>parseString</code>.
373      * <p>
374      * Subclasses must implement this method.
375      * </p>
376      *
377      * @param items the list of items
378      * @return the combined string
379      * @see #parseString
380      */

381
382     protected String JavaDoc createList(String JavaDoc[] items) {
383         StringBuffer JavaDoc path = new StringBuffer JavaDoc("");
384
385         for (int i = 0; i < items.length; i++) {
386             path.append(items[i]);
387             path.append(JonasPluginResources.PREF_PAGE_LIST_SEPARATOR);
388         }
389         return path.toString();
390     }
391
392     /**
393      * Creates and returns a new item for the list.
394      * <p>
395      * Subclasses must implement this method.
396      * </p>
397      *
398      * @return a new item
399      */

400     protected String JavaDoc getNewInputObject() {
401
402         String JavaDoc defaultValue = "";
403         if (list.getSelection().length != 0) {
404             defaultValue = list.getSelection()[0];
405         }
406
407         InputDialog dialog = new InputDialog(getShell(), "New Jonas JVM paramater", "Enter a JVM parameter", defaultValue, null);
408         String JavaDoc param = null;
409         int dialogCode = dialog.open();
410         if (dialogCode == InputDialog.OK) {
411             param = dialog.getValue();
412             if (param != null) {
413                 param = param.trim();
414                 if (param.length() == 0)
415                     return null;
416             }
417         }
418         return param;
419     }
420
421     /**
422      * Splits the given string into a list of strings.
423      * This method is the converse of <code>createList</code>.
424      * <p>
425      * Subclasses must implement this method.
426      * </p>
427      *
428      * @param stringList the string
429      * @return an array of <code>String</code>
430      * @see #createList
431      */

432     protected String JavaDoc[] parseString(String JavaDoc stringList) {
433         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(stringList, JonasPluginResources.PREF_PAGE_LIST_SEPARATOR); //$NON-NLS-1$
434
ArrayList JavaDoc v = new ArrayList JavaDoc();
435         while (st.hasMoreElements()) {
436             v.add(st.nextElement());
437         }
438         return (String JavaDoc[]) v.toArray(new String JavaDoc[v.size()]);
439     }
440
441 }
442
Popular Tags