KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.jface.dialogs.IDialogConstants;
21 import org.eclipse.jface.viewers.DoubleClickEvent;
22 import org.eclipse.jface.viewers.IDoubleClickListener;
23 import org.eclipse.jface.viewers.ILabelProvider;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.ISelectionChangedListener;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.jface.viewers.ITreeContentProvider;
28 import org.eclipse.jface.viewers.SelectionChangedEvent;
29 import org.eclipse.jface.viewers.StructuredSelection;
30 import org.eclipse.jface.viewers.TreeViewer;
31 import org.eclipse.jface.viewers.ViewerComparator;
32 import org.eclipse.jface.viewers.ViewerFilter;
33 import org.eclipse.jface.viewers.ViewerSorter;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.custom.BusyIndicator;
36 import org.eclipse.swt.events.SelectionAdapter;
37 import org.eclipse.swt.events.SelectionEvent;
38 import org.eclipse.swt.layout.GridData;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Control;
41 import org.eclipse.swt.widgets.Label;
42 import org.eclipse.swt.widgets.Shell;
43 import org.eclipse.swt.widgets.Tree;
44 import org.eclipse.ui.PlatformUI;
45 import org.eclipse.ui.internal.WorkbenchMessages;
46
47 /**
48  * A class to select elements out of a tree structure.
49  *
50  * @since 2.0
51  */

52 public class ElementTreeSelectionDialog extends SelectionStatusDialog {
53
54     private TreeViewer fViewer;
55
56     private ILabelProvider fLabelProvider;
57
58     private ITreeContentProvider fContentProvider;
59
60     private ISelectionStatusValidator fValidator = null;
61
62     private ViewerComparator fComparator;
63
64     private boolean fAllowMultiple = true;
65
66     private boolean fDoubleClickSelects = true;
67
68     private String JavaDoc fEmptyListMessage = WorkbenchMessages.ElementTreeSelectionDialog_nothing_available;
69
70     private IStatus fCurrStatus = new Status(IStatus.OK, PlatformUI.PLUGIN_ID,
71             IStatus.OK, "", null); //$NON-NLS-1$
72

73     private List JavaDoc fFilters;
74
75     private Object JavaDoc fInput;
76
77     private boolean fIsEmpty;
78
79     private int fWidth = 60;
80
81     private int fHeight = 18;
82
83     /**
84      * Constructs an instance of <code>ElementTreeSelectionDialog</code>.
85      * @param parent The parent shell for the dialog
86      * @param labelProvider the label provider to render the entries
87      * @param contentProvider the content provider to evaluate the tree structure
88      */

89     public ElementTreeSelectionDialog(Shell parent,
90             ILabelProvider labelProvider, ITreeContentProvider contentProvider) {
91         super(parent);
92
93         fLabelProvider = labelProvider;
94         fContentProvider = contentProvider;
95
96         setResult(new ArrayList JavaDoc(0));
97         setStatusLineAboveButtons(true);
98
99         int shellStyle = getShellStyle();
100         setShellStyle(shellStyle | SWT.MAX | SWT.RESIZE);
101     }
102
103     /**
104      * Sets the initial selection.
105      * Convenience method.
106      * @param selection the initial selection.
107      */

108     public void setInitialSelection(Object JavaDoc selection) {
109         setInitialSelections(new Object JavaDoc[] { selection });
110     }
111
112     /**
113      * Sets the message to be displayed if the list is empty.
114      * @param message the message to be displayed.
115      */

116     public void setEmptyListMessage(String JavaDoc message) {
117         fEmptyListMessage = message;
118     }
119
120     /**
121      * Specifies if multiple selection is allowed.
122      * @param allowMultiple
123      */

124     public void setAllowMultiple(boolean allowMultiple) {
125         fAllowMultiple = allowMultiple;
126     }
127
128     /**
129      * Specifies if default selected events (double click) are created.
130      * @param doubleClickSelects
131      */

132     public void setDoubleClickSelects(boolean doubleClickSelects) {
133         fDoubleClickSelects = doubleClickSelects;
134     }
135
136     /**
137      * Sets the sorter used by the tree viewer.
138      * @param sorter
139      * @deprecated as of 3.3, use {@link ElementTreeSelectionDialog#setComparator(ViewerComparator)} instead
140      */

141     public void setSorter(ViewerSorter sorter) {
142         fComparator = sorter;
143     }
144     
145     /**
146      * Sets the comparator used by the tree viewer.
147      * @param comparator
148      * @since 3.3
149      */

150     public void setComparator(ViewerComparator comparator){
151         fComparator = comparator;
152     }
153
154     /**
155      * Adds a filter to the tree viewer.
156      * @param filter a filter.
157      */

158     public void addFilter(ViewerFilter filter) {
159         if (fFilters == null) {
160             fFilters = new ArrayList JavaDoc(4);
161         }
162
163         fFilters.add(filter);
164     }
165
166     /**
167      * Sets an optional validator to check if the selection is valid.
168      * The validator is invoked whenever the selection changes.
169      * @param validator the validator to validate the selection.
170      */

171     public void setValidator(ISelectionStatusValidator validator) {
172         fValidator = validator;
173     }
174
175     /**
176      * Sets the tree input.
177      * @param input the tree input.
178      */

179     public void setInput(Object JavaDoc input) {
180         fInput = input;
181     }
182
183     /**
184      * Sets the size of the tree in unit of characters.
185      * @param width the width of the tree.
186      * @param height the height of the tree.
187      */

188     public void setSize(int width, int height) {
189         fWidth = width;
190         fHeight = height;
191     }
192
193     /**
194      * Validate the receiver and update the ok status.
195      *
196      */

197     protected void updateOKStatus() {
198         if (!fIsEmpty) {
199             if (fValidator != null) {
200                 fCurrStatus = fValidator.validate(getResult());
201                 updateStatus(fCurrStatus);
202             } else {
203                 fCurrStatus = new Status(IStatus.OK, PlatformUI.PLUGIN_ID,
204                         IStatus.OK, "", //$NON-NLS-1$
205
null);
206             }
207         } else {
208             fCurrStatus = new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID,
209                     IStatus.ERROR, fEmptyListMessage, null);
210         }
211         updateStatus(fCurrStatus);
212     }
213
214     /*
215      * (non-Javadoc)
216      * @see org.eclipse.jface.window.Window#open()
217      */

218     public int open() {
219         fIsEmpty = evaluateIfTreeEmpty(fInput);
220         super.open();
221         return getReturnCode();
222     }
223
224     private void access$superCreate() {
225         super.create();
226     }
227
228     /**
229      * Handles cancel button pressed event.
230      */

231     protected void cancelPressed() {
232         setResult(null);
233         super.cancelPressed();
234     }
235
236     /*
237      * @see SelectionStatusDialog#computeResult()
238      */

239     protected void computeResult() {
240         setResult(((IStructuredSelection) fViewer.getSelection()).toList());
241     }
242
243     /*
244      * (non-Javadoc)
245      * @see org.eclipse.jface.window.Window#create()
246      */

247     public void create() {
248         BusyIndicator.showWhile(null, new Runnable JavaDoc() {
249             public void run() {
250                 access$superCreate();
251                 fViewer.setSelection(new StructuredSelection(
252                         getInitialElementSelections()), true);
253                 updateOKStatus();
254             }
255         });
256     }
257
258     /*
259      * @see Dialog#createDialogArea(Composite)
260      */

261     protected Control createDialogArea(Composite parent) {
262         Composite composite = (Composite) super.createDialogArea(parent);
263
264         Label messageLabel = createMessageArea(composite);
265         TreeViewer treeViewer = createTreeViewer(composite);
266
267         GridData data = new GridData(GridData.FILL_BOTH);
268         data.widthHint = convertWidthInCharsToPixels(fWidth);
269         data.heightHint = convertHeightInCharsToPixels(fHeight);
270
271         Tree treeWidget = treeViewer.getTree();
272         treeWidget.setLayoutData(data);
273         treeWidget.setFont(parent.getFont());
274
275         if (fIsEmpty) {
276             messageLabel.setEnabled(false);
277             treeWidget.setEnabled(false);
278         }
279
280         return composite;
281     }
282
283     /**
284      * Creates the tree viewer.
285      *
286      * @param parent the parent composite
287      * @return the tree viewer
288      */

289     protected TreeViewer createTreeViewer(Composite parent) {
290         int style = SWT.BORDER | (fAllowMultiple ? SWT.MULTI : SWT.SINGLE);
291
292         fViewer = new TreeViewer(new Tree(parent, style));
293         fViewer.setContentProvider(fContentProvider);
294         fViewer.setLabelProvider(fLabelProvider);
295         fViewer.addSelectionChangedListener(new ISelectionChangedListener() {
296             public void selectionChanged(SelectionChangedEvent event) {
297                 access$setResult(((IStructuredSelection) event.getSelection())
298                         .toList());
299                 updateOKStatus();
300             }
301         });
302
303         fViewer.setComparator(fComparator);
304         if (fFilters != null) {
305             for (int i = 0; i != fFilters.size(); i++) {
306                 fViewer.addFilter((ViewerFilter) fFilters.get(i));
307             }
308         }
309
310         if (fDoubleClickSelects) {
311             Tree tree = fViewer.getTree();
312             tree.addSelectionListener(new SelectionAdapter() {
313                 public void widgetDefaultSelected(SelectionEvent e) {
314                     updateOKStatus();
315                     if (fCurrStatus.isOK()) {
316                         access$superButtonPressed(IDialogConstants.OK_ID);
317                     }
318                 }
319             });
320         }
321         fViewer.addDoubleClickListener(new IDoubleClickListener() {
322             public void doubleClick(DoubleClickEvent event) {
323                 updateOKStatus();
324
325                 //If it is not OK or if double click does not
326
//select then expand
327
if (!(fDoubleClickSelects && fCurrStatus.isOK())) {
328                     ISelection selection = event.getSelection();
329                     if (selection instanceof IStructuredSelection) {
330                         Object JavaDoc item = ((IStructuredSelection) selection)
331                                 .getFirstElement();
332                         if (fViewer.getExpandedState(item)) {
333                             fViewer.collapseToLevel(item, 1);
334                         } else {
335                             fViewer.expandToLevel(item, 1);
336                         }
337                     }
338                 }
339             }
340         });
341
342         fViewer.setInput(fInput);
343
344         return fViewer;
345     }
346
347     /**
348      * Returns the tree viewer.
349      *
350      * @return the tree viewer
351      */

352     protected TreeViewer getTreeViewer() {
353         return fViewer;
354     }
355
356     private boolean evaluateIfTreeEmpty(Object JavaDoc input) {
357         Object JavaDoc[] elements = fContentProvider.getElements(input);
358         if (elements.length > 0) {
359             if (fFilters != null) {
360                 for (int i = 0; i < fFilters.size(); i++) {
361                     ViewerFilter curr = (ViewerFilter) fFilters.get(i);
362                     elements = curr.filter(fViewer, input, elements);
363                 }
364             }
365         }
366         return elements.length == 0;
367     }
368
369     /**
370      * Set the result using the super class implementation of
371      * buttonPressed.
372      * @param id
373      * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
374      */

375     protected void access$superButtonPressed(int id) {
376         super.buttonPressed(id);
377     }
378
379     /**
380      * Set the result using the super class implementation of
381      * setResult.
382      * @param result
383      * @see SelectionStatusDialog#setResult(int, Object)
384      */

385     protected void access$setResult(List JavaDoc result) {
386         super.setResult(result);
387     }
388
389     /**
390      * @see org.eclipse.jface.window.Window#handleShellCloseEvent()
391      */

392     protected void handleShellCloseEvent() {
393         super.handleShellCloseEvent();
394
395         //Handle the closing of the shell by selecting the close icon
396
if (getReturnCode() == CANCEL) {
397             setResult(null);
398         }
399     }
400
401 }
402
Popular Tags