KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > dialogs > AbstractElementListSelectionDialog


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  * Sebastian Davids <sdavids@gmx.de> - Fix for bug 19346 - Dialog
11  * font should be activated and used by other components.
12  *******************************************************************************/

13 package org.eclipse.ui.dialogs;
14
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.jface.dialogs.IDialogConstants;
19 import org.eclipse.jface.viewers.ILabelProvider;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.custom.BusyIndicator;
22 import org.eclipse.swt.events.KeyEvent;
23 import org.eclipse.swt.events.KeyListener;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.events.SelectionListener;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.widgets.Button;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Event;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.swt.widgets.Listener;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.swt.widgets.Text;
34 import org.eclipse.ui.PlatformUI;
35
36 /**
37  * An abstract class to select elements out of a list of elements.
38  *
39  * @since 2.0
40  */

41 public abstract class AbstractElementListSelectionDialog extends
42         SelectionStatusDialog {
43
44     private ILabelProvider fRenderer;
45
46     private boolean fIgnoreCase = true;
47
48     private boolean fIsMultipleSelection = false;
49
50     private boolean fMatchEmptyString = true;
51
52     private boolean fAllowDuplicates = true;
53
54     private Label fMessage;
55
56     protected FilteredList fFilteredList;
57
58     private Text fFilterText;
59
60     private ISelectionStatusValidator fValidator;
61
62     private String JavaDoc fFilter = null;
63
64     private String JavaDoc fEmptyListMessage = ""; //$NON-NLS-1$
65

66     private String JavaDoc fEmptySelectionMessage = ""; //$NON-NLS-1$
67

68     private int fWidth = 60;
69
70     private int fHeight = 18;
71
72     private Object JavaDoc[] fSelection = new Object JavaDoc[0];
73
74     /**
75      * Constructs a list selection dialog.
76      * @param parent The parent for the list.
77      * @param renderer ILabelProvider for the list
78      */

79     protected AbstractElementListSelectionDialog(Shell parent,
80             ILabelProvider renderer) {
81         super(parent);
82         fRenderer = renderer;
83
84         int shellStyle = getShellStyle();
85         setShellStyle(shellStyle | SWT.MAX | SWT.RESIZE);
86     }
87
88     /**
89      * Handles default selection (double click).
90      * By default, the OK button is pressed.
91      */

92     protected void handleDefaultSelected() {
93         if (validateCurrentSelection()) {
94             buttonPressed(IDialogConstants.OK_ID);
95         }
96     }
97
98     /**
99      * Specifies if sorting, filtering and folding is case sensitive.
100      * @param ignoreCase
101      */

102     public void setIgnoreCase(boolean ignoreCase) {
103         fIgnoreCase = ignoreCase;
104     }
105
106     /**
107      * Returns if sorting, filtering and folding is case sensitive.
108      * @return boolean
109      */

110     public boolean isCaseIgnored() {
111         return fIgnoreCase;
112     }
113
114     /**
115      * Specifies whether everything or nothing should be filtered on
116      * empty filter string.
117      * @param matchEmptyString boolean
118      */

119     public void setMatchEmptyString(boolean matchEmptyString) {
120         fMatchEmptyString = matchEmptyString;
121     }
122
123     /**
124      * Specifies if multiple selection is allowed.
125      * @param multipleSelection
126      */

127     public void setMultipleSelection(boolean multipleSelection) {
128         fIsMultipleSelection = multipleSelection;
129     }
130
131     /**
132      * Specifies whether duplicate entries are displayed or not.
133      * @param allowDuplicates
134      */

135     public void setAllowDuplicates(boolean allowDuplicates) {
136         fAllowDuplicates = allowDuplicates;
137     }
138
139     /**
140      * Sets the list size in unit of characters.
141      * @param width the width of the list.
142      * @param height the height of the list.
143      */

144     public void setSize(int width, int height) {
145         fWidth = width;
146         fHeight = height;
147     }
148
149     /**
150      * Sets the message to be displayed if the list is empty.
151      * @param message the message to be displayed.
152      */

153     public void setEmptyListMessage(String JavaDoc message) {
154         fEmptyListMessage = message;
155     }
156
157     /**
158      * Sets the message to be displayed if the selection is empty.
159      * @param message the message to be displayed.
160      */

161     public void setEmptySelectionMessage(String JavaDoc message) {
162         fEmptySelectionMessage = message;
163     }
164
165     /**
166      * Sets an optional validator to check if the selection is valid.
167      * The validator is invoked whenever the selection changes.
168      * @param validator the validator to validate the selection.
169      */

170     public void setValidator(ISelectionStatusValidator validator) {
171         fValidator = validator;
172     }
173
174     /**
175      * Sets the elements of the list (widget).
176      * To be called within open().
177      * @param elements the elements of the list.
178      */

179     protected void setListElements(Object JavaDoc[] elements) {
180         Assert.isNotNull(fFilteredList);
181         fFilteredList.setElements(elements);
182     }
183
184     /**
185      * Sets the filter pattern.
186      * @param filter the filter pattern.
187      */

188     public void setFilter(String JavaDoc filter) {
189         if (fFilterText == null) {
190             fFilter = filter;
191         } else {
192             fFilterText.setText(filter);
193         }
194     }
195
196     /**
197      * Returns the current filter pattern.
198      * @return returns the current filter pattern or <code>null<code> if filter was not set.
199      */

200     public String JavaDoc getFilter() {
201         if (fFilteredList == null) {
202             return fFilter;
203         } else {
204             return fFilteredList.getFilter();
205         }
206     }
207
208     /**
209      * Returns the indices referring the current selection.
210      * To be called within open().
211      * @return returns the indices of the current selection.
212      */

213     protected int[] getSelectionIndices() {
214         Assert.isNotNull(fFilteredList);
215         return fFilteredList.getSelectionIndices();
216     }
217
218     /**
219      * Returns an index referring the first current selection.
220      * To be called within open().
221      * @return returns the indices of the current selection.
222      */

223     protected int getSelectionIndex() {
224         Assert.isNotNull(fFilteredList);
225         return fFilteredList.getSelectionIndex();
226     }
227
228     /**
229      * Sets the selection referenced by an array of elements.
230      * Empty or null array removes selection.
231      * To be called within open().
232      * @param selection the indices of the selection.
233      */

234     protected void setSelection(Object JavaDoc[] selection) {
235         Assert.isNotNull(fFilteredList);
236         fFilteredList.setSelection(selection);
237     }
238
239     /**
240      * Returns an array of the currently selected elements.
241      * To be called within or after open().
242      * @return returns an array of the currently selected elements.
243      */

244     protected Object JavaDoc[] getSelectedElements() {
245         Assert.isNotNull(fFilteredList);
246         return fFilteredList.getSelection();
247     }
248
249     /**
250      * Returns all elements which are folded together to one entry in the list.
251      * @param index the index selecting the entry in the list.
252      * @return returns an array of elements folded together.
253      */

254     public Object JavaDoc[] getFoldedElements(int index) {
255         Assert.isNotNull(fFilteredList);
256         return fFilteredList.getFoldedElements(index);
257     }
258
259     /**
260      * Creates the message text widget and sets layout data.
261      * @param composite the parent composite of the message area.
262      */

263     protected Label createMessageArea(Composite composite) {
264         Label label = super.createMessageArea(composite);
265
266         GridData data = new GridData();
267         data.grabExcessVerticalSpace = false;
268         data.grabExcessHorizontalSpace = true;
269         data.horizontalAlignment = GridData.FILL;
270         data.verticalAlignment = GridData.BEGINNING;
271         label.setLayoutData(data);
272
273         fMessage = label;
274
275         return label;
276     }
277
278     /**
279      * Handles a selection changed event.
280      * By default, the current selection is validated.
281      */

282     protected void handleSelectionChanged() {
283         validateCurrentSelection();
284     }
285
286     /**
287      * Validates the current selection and updates the status line
288      * accordingly.
289      * @return boolean <code>true</code> if the current selection is
290      * valid.
291      */

292     protected boolean validateCurrentSelection() {
293         Assert.isNotNull(fFilteredList);
294
295         IStatus status;
296         Object JavaDoc[] elements = getSelectedElements();
297
298         if (elements.length > 0) {
299             if (fValidator != null) {
300                 status = fValidator.validate(elements);
301             } else {
302                 status = new Status(IStatus.OK, PlatformUI.PLUGIN_ID,
303                         IStatus.OK, "", //$NON-NLS-1$
304
null);
305             }
306         } else {
307             if (fFilteredList.isEmpty()) {
308                 status = new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID,
309                         IStatus.ERROR, fEmptyListMessage, null);
310             } else {
311                 status = new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID,
312                         IStatus.ERROR, fEmptySelectionMessage, null);
313             }
314         }
315
316         updateStatus(status);
317
318         return status.isOK();
319     }
320
321     /*
322      * @see Dialog#cancelPressed
323      */

324     protected void cancelPressed() {
325         setResult(null);
326         super.cancelPressed();
327     }
328
329     /**
330      * Creates a filtered list.
331      * @param parent the parent composite.
332      * @return returns the filtered list widget.
333      */

334     protected FilteredList createFilteredList(Composite parent) {
335         int flags = SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL
336                 | (fIsMultipleSelection ? SWT.MULTI : SWT.SINGLE);
337
338         FilteredList list = new FilteredList(parent, flags, fRenderer,
339                 fIgnoreCase, fAllowDuplicates, fMatchEmptyString);
340
341         GridData data = new GridData();
342         data.widthHint = convertWidthInCharsToPixels(fWidth);
343         data.heightHint = convertHeightInCharsToPixels(fHeight);
344         data.grabExcessVerticalSpace = true;
345         data.grabExcessHorizontalSpace = true;
346         data.horizontalAlignment = GridData.FILL;
347         data.verticalAlignment = GridData.FILL;
348         list.setLayoutData(data);
349         list.setFont(parent.getFont());
350         list.setFilter((fFilter == null ? "" : fFilter)); //$NON-NLS-1$
351

352         list.addSelectionListener(new SelectionListener() {
353             public void widgetDefaultSelected(SelectionEvent e) {
354                 handleDefaultSelected();
355             }
356
357             public void widgetSelected(SelectionEvent e) {
358                 handleWidgetSelected();
359             }
360         });
361
362         fFilteredList = list;
363
364         return list;
365     }
366
367     // 3515
368
private void handleWidgetSelected() {
369         Object JavaDoc[] newSelection = fFilteredList.getSelection();
370
371         if (newSelection.length != fSelection.length) {
372             fSelection = newSelection;
373             handleSelectionChanged();
374         } else {
375             for (int i = 0; i != newSelection.length; i++) {
376                 if (!newSelection[i].equals(fSelection[i])) {
377                     fSelection = newSelection;
378                     handleSelectionChanged();
379                     break;
380                 }
381             }
382         }
383     }
384
385     protected Text createFilterText(Composite parent) {
386         Text text = new Text(parent, SWT.BORDER);
387
388         GridData data = new GridData();
389         data.grabExcessVerticalSpace = false;
390         data.grabExcessHorizontalSpace = true;
391         data.horizontalAlignment = GridData.FILL;
392         data.verticalAlignment = GridData.BEGINNING;
393         text.setLayoutData(data);
394         text.setFont(parent.getFont());
395
396         text.setText((fFilter == null ? "" : fFilter)); //$NON-NLS-1$
397

398         Listener listener = new Listener() {
399             public void handleEvent(Event e) {
400                 fFilteredList.setFilter(fFilterText.getText());
401             }
402         };
403         text.addListener(SWT.Modify, listener);
404
405         text.addKeyListener(new KeyListener() {
406             public void keyPressed(KeyEvent e) {
407                 if (e.keyCode == SWT.ARROW_DOWN) {
408                     fFilteredList.setFocus();
409                 }
410             }
411
412             public void keyReleased(KeyEvent e) {
413             }
414         });
415
416         fFilterText = text;
417
418         return text;
419     }
420
421     /*
422      * (non-Javadoc)
423      * @see org.eclipse.jface.window.Window#open()
424      */

425     public int open() {
426         super.open();
427         return getReturnCode();
428     }
429
430     private void access$superCreate() {
431         super.create();
432     }
433
434     /*
435      * (non-Javadoc)
436      * @see org.eclipse.jface.window.Window#create()
437      */

438     public void create() {
439
440         BusyIndicator.showWhile(null, new Runnable JavaDoc() {
441             public void run() {
442                 access$superCreate();
443
444                 Assert.isNotNull(fFilteredList);
445
446                 if (fFilteredList.isEmpty()) {
447                     handleEmptyList();
448                 } else {
449                     validateCurrentSelection();
450                     fFilterText.selectAll();
451                     fFilterText.setFocus();
452                 }
453             }
454         });
455
456     }
457
458     /**
459      * Handles empty list by disabling widgets.
460      */

461     protected void handleEmptyList() {
462         fMessage.setEnabled(false);
463         fFilterText.setEnabled(false);
464         fFilteredList.setEnabled(false);
465         updateOkState();
466     }
467
468     /**
469      * Update the enablement of the OK button based on whether or not there
470      * is a selection.
471      *
472      */

473     protected void updateOkState() {
474         Button okButton = getOkButton();
475         if (okButton != null) {
476             okButton.setEnabled(getSelectedElements().length != 0);
477         }
478     }
479 }
480
Popular Tags