KickJava   Java API By Example, From Geeks To Geeks.

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


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.ui.dialogs;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IAdaptable;
20 import org.eclipse.jface.viewers.CheckStateChangedEvent;
21 import org.eclipse.jface.viewers.ICheckStateListener;
22 import org.eclipse.jface.viewers.ITreeContentProvider;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.ControlEvent;
25 import org.eclipse.swt.events.ControlListener;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.swt.widgets.TableColumn;
30 import org.eclipse.ui.PlatformUI;
31 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
32 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
33 import org.eclipse.ui.internal.ide.misc.CheckboxTreeAndListGroup;
34 import org.eclipse.ui.model.WorkbenchContentProvider;
35 import org.eclipse.ui.model.WorkbenchLabelProvider;
36
37 /**
38  * A standard resource selection dialog which solicits a list of resources from
39  * the user. The <code>getResult</code> method returns the selected resources.
40  * <p>
41  * This class may be instantiated; it is not intended to be subclassed.
42  * </p>
43  * <p>
44  * Example:
45  * <pre>
46  * ResourceSelectionDialog dialog =
47  * new ResourceSelectionDialog(getShell(), rootResource, msg);
48  * dialog.setInitialSelections(selectedResources);
49  * dialog.open();
50  * return dialog.getResult();
51  * </pre>
52  * </p>
53  */

54 public class ResourceSelectionDialog extends SelectionDialog {
55     // the root element to populate the viewer with
56
private IAdaptable root;
57
58     // the visual selection widget group
59
private CheckboxTreeAndListGroup selectionGroup;
60
61     // constants
62
private final static int SIZING_SELECTION_WIDGET_WIDTH = 400;
63
64     private final static int SIZING_SELECTION_WIDGET_HEIGHT = 300;
65
66     /**
67      * Creates a resource selection dialog rooted at the given element.
68      *
69      * @param parentShell the parent shell
70      * @param rootElement the root element to populate this dialog with
71      * @param message the message to be displayed at the top of this dialog, or
72      * <code>null</code> to display a default message
73      */

74     public ResourceSelectionDialog(Shell parentShell, IAdaptable rootElement,
75             String JavaDoc message) {
76         super(parentShell);
77         setTitle(IDEWorkbenchMessages.ResourceSelectionDialog_title);
78         root = rootElement;
79         if (message != null) {
80             setMessage(message);
81         } else {
82             setMessage(IDEWorkbenchMessages.ResourceSelectionDialog_message);
83         }
84         setShellStyle(getShellStyle() | SWT.RESIZE);
85     }
86
87     /**
88      * Visually checks the previously-specified elements in the container (left)
89      * portion of this dialog's resource selection viewer.
90      */

91     private void checkInitialSelections() {
92         Iterator JavaDoc itemsToCheck = getInitialElementSelections().iterator();
93
94         while (itemsToCheck.hasNext()) {
95             IResource currentElement = (IResource) itemsToCheck.next();
96
97             if (currentElement.getType() == IResource.FILE) {
98                 selectionGroup.initialCheckListItem(currentElement);
99             } else {
100                 selectionGroup.initialCheckTreeItem(currentElement);
101             }
102         }
103     }
104
105
106     /**
107      * @param event the event
108      */

109     public void checkStateChanged(CheckStateChangedEvent event) {
110         getOkButton().setEnabled(selectionGroup.getCheckedElementCount() > 0);
111     }
112
113     /* (non-Javadoc)
114      * Method declared in Window.
115      */

116     protected void configureShell(Shell shell) {
117         super.configureShell(shell);
118         PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
119                 IIDEHelpContextIds.RESOURCE_SELECTION_DIALOG);
120     }
121
122     public void create() {
123         super.create();
124         initializeDialog();
125     }
126
127     /* (non-Javadoc)
128      * Method declared on Dialog.
129      */

130     protected Control createDialogArea(Composite parent) {
131         // page group
132
Composite composite = (Composite) super.createDialogArea(parent);
133
134         //create the input element, which has the root resource
135
//as its only child
136
ArrayList JavaDoc input = new ArrayList JavaDoc();
137         input.add(root);
138
139         createMessageArea(composite);
140         selectionGroup = new CheckboxTreeAndListGroup(composite, input,
141                 getResourceProvider(IResource.FOLDER | IResource.PROJECT
142                         | IResource.ROOT), WorkbenchLabelProvider
143                         .getDecoratingWorkbenchLabelProvider(),
144                 getResourceProvider(IResource.FILE), WorkbenchLabelProvider
145                         .getDecoratingWorkbenchLabelProvider(), SWT.NONE,
146                 // since this page has no other significantly-sized
147
// widgets we need to hardcode the combined widget's
148
// size, otherwise it will open too small
149
SIZING_SELECTION_WIDGET_WIDTH, SIZING_SELECTION_WIDGET_HEIGHT);
150
151         composite.addControlListener(new ControlListener() {
152             public void controlMoved(ControlEvent e) {
153             }
154
155             public void controlResized(ControlEvent e) {
156                 //Also try and reset the size of the columns as appropriate
157
TableColumn[] columns = selectionGroup.getListTable()
158                         .getColumns();
159                 for (int i = 0; i < columns.length; i++) {
160                     columns[i].pack();
161                 }
162             }
163         });
164
165         return composite;
166     }
167
168     /**
169      * Returns a content provider for <code>IResource</code>s that returns
170      * only children of the given resource type.
171      */

172     private ITreeContentProvider getResourceProvider(final int resourceType) {
173         return new WorkbenchContentProvider() {
174             public Object JavaDoc[] getChildren(Object JavaDoc o) {
175                 if (o instanceof IContainer) {
176                     IResource[] members = null;
177                     try {
178                         members = ((IContainer) o).members();
179                     } catch (CoreException e) {
180                         //just return an empty set of children
181
return new Object JavaDoc[0];
182                     }
183
184                     //filter out the desired resource types
185
ArrayList JavaDoc results = new ArrayList JavaDoc();
186                     for (int i = 0; i < members.length; i++) {
187                         //And the test bits with the resource types to see if they are what we want
188
if ((members[i].getType() & resourceType) > 0) {
189                             results.add(members[i]);
190                         }
191                     }
192                     return results.toArray();
193                 }
194                 //input element case
195
if (o instanceof ArrayList JavaDoc) {
196                     return ((ArrayList JavaDoc) o).toArray();
197                 }
198                 return new Object JavaDoc[0];
199             }
200         };
201     }
202
203     /**
204      * Initializes this dialog's controls.
205      */

206     private void initializeDialog() {
207         selectionGroup.addCheckStateListener(new ICheckStateListener() {
208             public void checkStateChanged(CheckStateChangedEvent event) {
209                 getOkButton().setEnabled(
210                         selectionGroup.getCheckedElementCount() > 0);
211             }
212         });
213
214         if (getInitialElementSelections().isEmpty()) {
215             getOkButton().setEnabled(false);
216         } else {
217             checkInitialSelections();
218         }
219     }
220
221     /**
222      * The <code>ResourceSelectionDialog</code> implementation of this
223      * <code>Dialog</code> method builds a list of the selected resources for later
224      * retrieval by the client and closes this dialog.
225      */

226     protected void okPressed() {
227         Iterator JavaDoc resultEnum = selectionGroup.getAllCheckedListItems();
228         ArrayList JavaDoc list = new ArrayList JavaDoc();
229         while (resultEnum.hasNext()) {
230             list.add(resultEnum.next());
231         }
232         setResult(list);
233         super.okPressed();
234     }
235 }
236
Popular Tags