KickJava   Java API By Example, From Geeks To Geeks.

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


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 font should be
11  * activated and used by other components.
12  *******************************************************************************/

13 package org.eclipse.ui.dialogs;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.jface.dialogs.IDialogConstants;
22 import org.eclipse.jface.viewers.CheckStateChangedEvent;
23 import org.eclipse.jface.viewers.CheckboxTreeViewer;
24 import org.eclipse.jface.viewers.ICheckStateListener;
25 import org.eclipse.jface.viewers.ILabelProvider;
26 import org.eclipse.jface.viewers.ITreeContentProvider;
27 import org.eclipse.jface.viewers.ViewerComparator;
28 import org.eclipse.jface.viewers.ViewerFilter;
29 import org.eclipse.jface.viewers.ViewerSorter;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.custom.BusyIndicator;
32 import org.eclipse.swt.events.SelectionAdapter;
33 import org.eclipse.swt.events.SelectionEvent;
34 import org.eclipse.swt.events.SelectionListener;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.widgets.Button;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40 import org.eclipse.swt.widgets.Label;
41 import org.eclipse.swt.widgets.Shell;
42 import org.eclipse.swt.widgets.Tree;
43 import org.eclipse.ui.PlatformUI;
44 import org.eclipse.ui.internal.WorkbenchMessages;
45
46 /**
47  * A class to select elements out of a tree structure.
48  *
49  * @since 2.0
50  */

51 public class CheckedTreeSelectionDialog extends SelectionStatusDialog {
52     private CheckboxTreeViewer fViewer;
53
54     private ILabelProvider fLabelProvider;
55
56     private ITreeContentProvider fContentProvider;
57
58     private ISelectionStatusValidator fValidator = null;
59
60     private ViewerComparator fComparator;
61
62     private String JavaDoc fEmptyListMessage = WorkbenchMessages.CheckedTreeSelectionDialog_nothing_available;
63
64     private IStatus fCurrStatus = new Status(IStatus.OK, PlatformUI.PLUGIN_ID,
65             0, "", null); //$NON-NLS-1$
66

67     private List JavaDoc fFilters;
68
69     private Object JavaDoc fInput;
70
71     private boolean fIsEmpty;
72
73     private int fWidth = 60;
74
75     private int fHeight = 18;
76
77     private boolean fContainerMode;
78
79     private Object JavaDoc[] fExpandedElements;
80
81     /**
82      * Constructs an instance of <code>ElementTreeSelectionDialog</code>.
83      *
84      * @param parent
85      * The shell to parent from.
86      * @param labelProvider
87      * the label provider to render the entries
88      * @param contentProvider
89      * the content provider to evaluate the tree structure
90      */

91     public CheckedTreeSelectionDialog(Shell parent,
92             ILabelProvider labelProvider, ITreeContentProvider contentProvider) {
93         super(parent);
94         fLabelProvider = labelProvider;
95         fContentProvider = contentProvider;
96         setResult(new ArrayList JavaDoc(0));
97         setStatusLineAboveButtons(true);
98         fContainerMode = false;
99         fExpandedElements = null;
100         int shellStyle = getShellStyle();
101         setShellStyle(shellStyle | SWT.MAX | SWT.RESIZE);
102     }
103
104     /**
105      * If set, the checked /gray state of containers (inner nodes) is derived
106      * from the checked state of its leaf nodes.
107      *
108      * @param containerMode
109      * The containerMode to set
110      */

111     public void setContainerMode(boolean containerMode) {
112         fContainerMode = containerMode;
113     }
114
115     /**
116      * Sets the initial selection. Convenience method.
117      *
118      * @param selection
119      * the initial selection.
120      */

121     public void setInitialSelection(Object JavaDoc selection) {
122         setInitialSelections(new Object JavaDoc[] { selection });
123     }
124
125     /**
126      * Sets the message to be displayed if the list is empty.
127      *
128      * @param message
129      * the message to be displayed.
130      */

131     public void setEmptyListMessage(String JavaDoc message) {
132         fEmptyListMessage = message;
133     }
134
135     /**
136      * Sets the sorter used by the tree viewer.
137      *
138      * @param sorter
139      * @deprecated since 3.3, use
140      * {@link CheckedTreeSelectionDialog#setComparator(ViewerComparator)}
141      * instead
142      */

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

153     public void setComparator(ViewerComparator comparator){
154         fComparator = comparator;
155     }
156
157     /**
158      * Adds a filter to the tree viewer.
159      *
160      * @param filter
161      * a filter.
162      */

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

177     public void setValidator(ISelectionStatusValidator validator) {
178         fValidator = validator;
179     }
180
181     /**
182      * Sets the tree input.
183      *
184      * @param input
185      * the tree input.
186      */

187     public void setInput(Object JavaDoc input) {
188         fInput = input;
189     }
190
191     /**
192      * Expands elements in the tree.
193      *
194      * @param elements
195      * The elements that will be expanded.
196      */

197     public void setExpandedElements(Object JavaDoc[] elements) {
198         fExpandedElements = elements;
199     }
200
201     /**
202      * Sets the size of the tree in unit of characters.
203      *
204      * @param width
205      * the width of the tree.
206      * @param height
207      * the height of the tree.
208      */

209     public void setSize(int width, int height) {
210         fWidth = width;
211         fHeight = height;
212     }
213
214     /**
215      * Validate the receiver and update the status with the result.
216      *
217      */

218     protected void updateOKStatus() {
219         if (!fIsEmpty) {
220             if (fValidator != null) {
221                 fCurrStatus = fValidator.validate(fViewer.getCheckedElements());
222                 updateStatus(fCurrStatus);
223             } else if (!fCurrStatus.isOK()) {
224                 fCurrStatus = new Status(IStatus.OK, PlatformUI.PLUGIN_ID,
225                         IStatus.OK, "", //$NON-NLS-1$
226
null);
227             }
228         } else {
229             fCurrStatus = new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID,
230                     IStatus.OK, fEmptyListMessage, null);
231         }
232         updateStatus(fCurrStatus);
233     }
234
235     /*
236      * (non-Javadoc)
237      * @see org.eclipse.jface.window.Window#open()
238      */

239     public int open() {
240         fIsEmpty = evaluateIfTreeEmpty(fInput);
241         super.open();
242         return getReturnCode();
243     }
244
245     private void access$superCreate() {
246         super.create();
247     }
248
249     /**
250      * Handles cancel button pressed event.
251      */

252     protected void cancelPressed() {
253         setResult(null);
254         super.cancelPressed();
255     }
256
257     /*
258      * @see SelectionStatusDialog#computeResult()
259      */

260     protected void computeResult() {
261         setResult(Arrays.asList(fViewer.getCheckedElements()));
262     }
263
264     /*
265      * (non-Javadoc)
266      * @see org.eclipse.jface.window.Window#create()
267      */

268     public void create() {
269         BusyIndicator.showWhile(null, new Runnable JavaDoc() {
270             public void run() {
271                 access$superCreate();
272                 fViewer.setCheckedElements(getInitialElementSelections()
273                         .toArray());
274                 if (fExpandedElements != null) {
275                     fViewer.setExpandedElements(fExpandedElements);
276                 }
277                 updateOKStatus();
278             }
279         });
280     }
281
282     /*
283      * (non-Javadoc)
284      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
285      */

286     protected Control createDialogArea(Composite parent) {
287         Composite composite = (Composite) super.createDialogArea(parent);
288         Label messageLabel = createMessageArea(composite);
289         CheckboxTreeViewer treeViewer = createTreeViewer(composite);
290         Control buttonComposite = createSelectionButtons(composite);
291         GridData data = new GridData(GridData.FILL_BOTH);
292         data.widthHint = convertWidthInCharsToPixels(fWidth);
293         data.heightHint = convertHeightInCharsToPixels(fHeight);
294         Tree treeWidget = treeViewer.getTree();
295         treeWidget.setLayoutData(data);
296         treeWidget.setFont(parent.getFont());
297         if (fIsEmpty) {
298             messageLabel.setEnabled(false);
299             treeWidget.setEnabled(false);
300             buttonComposite.setEnabled(false);
301         }
302         return composite;
303     }
304
305     /**
306      * Creates the tree viewer.
307      *
308      * @param parent
309      * the parent composite
310      * @return the tree viewer
311      */

312     protected CheckboxTreeViewer createTreeViewer(Composite parent) {
313         if (fContainerMode) {
314             fViewer = new ContainerCheckedTreeViewer(parent, SWT.BORDER);
315         } else {
316             fViewer = new CheckboxTreeViewer(parent, SWT.BORDER);
317         }
318         fViewer.setContentProvider(fContentProvider);
319         fViewer.setLabelProvider(fLabelProvider);
320         fViewer.addCheckStateListener(new ICheckStateListener() {
321             public void checkStateChanged(CheckStateChangedEvent event) {
322                 updateOKStatus();
323             }
324         });
325         fViewer.setComparator(fComparator);
326         if (fFilters != null) {
327             for (int i = 0; i != fFilters.size(); i++) {
328                 fViewer.addFilter((ViewerFilter) fFilters.get(i));
329             }
330         }
331         fViewer.setInput(fInput);
332         return fViewer;
333     }
334
335     /**
336      * Returns the tree viewer.
337      *
338      * @return the tree viewer
339      */

340     protected CheckboxTreeViewer getTreeViewer() {
341         return fViewer;
342     }
343
344     /**
345      * Adds the selection and deselection buttons to the dialog.
346      *
347      * @param composite
348      * the parent composite
349      * @return Composite the composite the buttons were created in.
350      */

351     protected Composite createSelectionButtons(Composite composite) {
352         Composite buttonComposite = new Composite(composite, SWT.RIGHT);
353         GridLayout layout = new GridLayout();
354         layout.numColumns = 2;
355         buttonComposite.setLayout(layout);
356         buttonComposite.setFont(composite.getFont());
357         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_END
358                 | GridData.GRAB_HORIZONTAL);
359         data.grabExcessHorizontalSpace = true;
360         composite.setData(data);
361         Button selectButton = createButton(buttonComposite,
362                 IDialogConstants.SELECT_ALL_ID, WorkbenchMessages.CheckedTreeSelectionDialog_select_all,
363                 false);
364         SelectionListener listener = new SelectionAdapter() {
365             public void widgetSelected(SelectionEvent e) {
366                 Object JavaDoc[] viewerElements = fContentProvider.getElements(fInput);
367                 if (fContainerMode) {
368                     fViewer.setCheckedElements(viewerElements);
369                 } else {
370                     for (int i = 0; i < viewerElements.length; i++) {
371                         fViewer.setSubtreeChecked(viewerElements[i], true);
372                     }
373                 }
374                 updateOKStatus();
375             }
376         };
377         selectButton.addSelectionListener(listener);
378         Button deselectButton = createButton(buttonComposite,
379                 IDialogConstants.DESELECT_ALL_ID, WorkbenchMessages.CheckedTreeSelectionDialog_deselect_all,
380                 false);
381         listener = new SelectionAdapter() {
382             public void widgetSelected(SelectionEvent e) {
383                 fViewer.setCheckedElements(new Object JavaDoc[0]);
384                 updateOKStatus();
385             }
386         };
387         deselectButton.addSelectionListener(listener);
388         return buttonComposite;
389     }
390
391     private boolean evaluateIfTreeEmpty(Object JavaDoc input) {
392         Object JavaDoc[] elements = fContentProvider.getElements(input);
393         if (elements.length > 0) {
394             if (fFilters != null) {
395                 for (int i = 0; i < fFilters.size(); i++) {
396                     ViewerFilter curr = (ViewerFilter) fFilters.get(i);
397                     elements = curr.filter(fViewer, input, elements);
398                 }
399             }
400         }
401         return elements.length == 0;
402     }
403 }
404
Popular Tags