KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > misc > ContainerSelectionGroup


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  * Igor Fedorenko <igorfie@yahoo.com> -
11  * Fix for Bug 136921 [IDE] New File dialog locks for 20 seconds
12  *******************************************************************************/

13 package org.eclipse.ui.internal.ide.misc;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.resources.IContainer;
19 import org.eclipse.core.resources.ResourcesPlugin;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.jface.dialogs.Dialog;
23 import org.eclipse.jface.viewers.DoubleClickEvent;
24 import org.eclipse.jface.viewers.IDoubleClickListener;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.ISelectionChangedListener;
27 import org.eclipse.jface.viewers.IStructuredSelection;
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.swt.SWT;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Event;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.swt.widgets.Listener;
39 import org.eclipse.swt.widgets.Text;
40 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
41 import org.eclipse.ui.model.WorkbenchLabelProvider;
42 import org.eclipse.ui.part.DrillDownComposite;
43
44 /**
45  * Workbench-level composite for choosing a container.
46  */

47 public class ContainerSelectionGroup extends Composite {
48     // The listener to notify of events
49
private Listener listener;
50
51     // Enable user to type in new container name
52
private boolean allowNewContainerName = true;
53
54     // show all projects by default
55
private boolean showClosedProjects = true;
56
57     // Last selection made by user
58
private IContainer selectedContainer;
59
60     // handle on parts
61
private Text containerNameField;
62
63     TreeViewer treeViewer;
64
65     // the message to display at the top of this dialog
66
private static final String JavaDoc DEFAULT_MSG_NEW_ALLOWED = IDEWorkbenchMessages.ContainerGroup_message;
67
68     private static final String JavaDoc DEFAULT_MSG_SELECT_ONLY = IDEWorkbenchMessages.ContainerGroup_selectFolder;
69
70     // sizing constants
71
private static final int SIZING_SELECTION_PANE_WIDTH = 320;
72
73     private static final int SIZING_SELECTION_PANE_HEIGHT = 300;
74
75     /**
76      * Creates a new instance of the widget.
77      *
78      * @param parent
79      * The parent widget of the group.
80      * @param listener
81      * A listener to forward events to. Can be null if no listener is
82      * required.
83      * @param allowNewContainerName
84      * Enable the user to type in a new container name instead of
85      * just selecting from the existing ones.
86      */

87     public ContainerSelectionGroup(Composite parent, Listener listener,
88             boolean allowNewContainerName) {
89         this(parent, listener, allowNewContainerName, null);
90     }
91
92     /**
93      * Creates a new instance of the widget.
94      *
95      * @param parent
96      * The parent widget of the group.
97      * @param listener
98      * A listener to forward events to. Can be null if no listener is
99      * required.
100      * @param allowNewContainerName
101      * Enable the user to type in a new container name instead of
102      * just selecting from the existing ones.
103      * @param message
104      * The text to present to the user.
105      */

106     public ContainerSelectionGroup(Composite parent, Listener listener,
107             boolean allowNewContainerName, String JavaDoc message) {
108         this(parent, listener, allowNewContainerName, message, true);
109     }
110
111     /**
112      * Creates a new instance of the widget.
113      *
114      * @param parent
115      * The parent widget of the group.
116      * @param listener
117      * A listener to forward events to. Can be null if no listener is
118      * required.
119      * @param allowNewContainerName
120      * Enable the user to type in a new container name instead of
121      * just selecting from the existing ones.
122      * @param message
123      * The text to present to the user.
124      * @param showClosedProjects
125      * Whether or not to show closed projects.
126      */

127     public ContainerSelectionGroup(Composite parent, Listener listener,
128             boolean allowNewContainerName, String JavaDoc message,
129             boolean showClosedProjects) {
130         this(parent, listener, allowNewContainerName, message,
131                 showClosedProjects, SIZING_SELECTION_PANE_HEIGHT,
132                 SIZING_SELECTION_PANE_WIDTH);
133     }
134
135     /**
136      * Creates a new instance of the widget.
137      *
138      * @param parent
139      * The parent widget of the group.
140      * @param listener
141      * A listener to forward events to. Can be null if no listener is
142      * required.
143      * @param allowNewContainerName
144      * Enable the user to type in a new container name instead of
145      * just selecting from the existing ones.
146      * @param message
147      * The text to present to the user.
148      * @param showClosedProjects
149      * Whether or not to show closed projects.
150      * @param heightHint
151      * height hint for the drill down composite
152      * @param widthHint
153      * width hint for the drill down composite
154      */

155     public ContainerSelectionGroup(Composite parent, Listener listener,
156             boolean allowNewContainerName, String JavaDoc message,
157             boolean showClosedProjects, int heightHint, int widthHint) {
158         super(parent, SWT.NONE);
159         this.listener = listener;
160         this.allowNewContainerName = allowNewContainerName;
161         this.showClosedProjects = showClosedProjects;
162         if (message != null) {
163             createContents(message, heightHint, widthHint);
164         } else if (allowNewContainerName) {
165             createContents(DEFAULT_MSG_NEW_ALLOWED, heightHint, widthHint);
166         } else {
167             createContents(DEFAULT_MSG_SELECT_ONLY, heightHint, widthHint);
168         }
169     }
170
171     /**
172      * The container selection has changed in the tree view. Update the
173      * container name field value and notify all listeners.
174      *
175      * @param container
176      * The container that changed
177      */

178     public void containerSelectionChanged(IContainer container) {
179         selectedContainer = container;
180
181         if (allowNewContainerName) {
182             if (container == null) {
183                 containerNameField.setText("");//$NON-NLS-1$
184
} else {
185                 String JavaDoc text = container.getFullPath().makeRelative().toString();
186                 containerNameField.setText(text);
187                 containerNameField.setToolTipText(text);
188             }
189         }
190
191         // fire an event so the parent can update its controls
192
if (listener != null) {
193             Event changeEvent = new Event();
194             changeEvent.type = SWT.Selection;
195             changeEvent.widget = this;
196             listener.handleEvent(changeEvent);
197         }
198     }
199
200     /**
201      * Creates the contents of the composite.
202      *
203      * @param message
204      */

205     public void createContents(String JavaDoc message) {
206         createContents(message, SIZING_SELECTION_PANE_HEIGHT,
207                 SIZING_SELECTION_PANE_WIDTH);
208     }
209
210     /**
211      * Creates the contents of the composite.
212      *
213      * @param message
214      * @param heightHint
215      * @param widthHint
216      */

217     public void createContents(String JavaDoc message, int heightHint, int widthHint) {
218         GridLayout layout = new GridLayout();
219         layout.marginWidth = 0;
220         setLayout(layout);
221         setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
222
223         Label label = new Label(this, SWT.WRAP);
224         label.setText(message);
225         label.setFont(this.getFont());
226
227         if (allowNewContainerName) {
228             containerNameField = new Text(this, SWT.SINGLE | SWT.BORDER);
229             GridData gd = new GridData(GridData.FILL_HORIZONTAL);
230             gd.widthHint = widthHint;
231             containerNameField.setLayoutData(gd);
232             containerNameField.addListener(SWT.Modify, listener);
233             containerNameField.setFont(this.getFont());
234         } else {
235             // filler...
236
new Label(this, SWT.NONE);
237         }
238
239         createTreeViewer(heightHint);
240         Dialog.applyDialogFont(this);
241     }
242
243     /**
244      * Returns a new drill down viewer for this dialog.
245      *
246      * @param heightHint
247      * height hint for the drill down composite
248      */

249     protected void createTreeViewer(int heightHint) {
250         // Create drill down.
251
DrillDownComposite drillDown = new DrillDownComposite(this, SWT.BORDER);
252         GridData spec = new GridData(SWT.FILL, SWT.FILL, true, true);
253         spec.widthHint = SIZING_SELECTION_PANE_WIDTH;
254         spec.heightHint = heightHint;
255         drillDown.setLayoutData(spec);
256
257         // Create tree viewer inside drill down.
258
treeViewer = new TreeViewer(drillDown, SWT.NONE);
259         drillDown.setChildTree(treeViewer);
260         ContainerContentProvider cp = new ContainerContentProvider();
261         cp.showClosedProjects(showClosedProjects);
262         treeViewer.setContentProvider(cp);
263         treeViewer.setLabelProvider(WorkbenchLabelProvider
264                 .getDecoratingWorkbenchLabelProvider());
265         treeViewer.setComparator(new ViewerComparator());
266         treeViewer.setUseHashlookup(true);
267         treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
268             public void selectionChanged(SelectionChangedEvent event) {
269                 IStructuredSelection selection = (IStructuredSelection) event
270                         .getSelection();
271                 containerSelectionChanged((IContainer) selection
272                         .getFirstElement()); // allow null
273
}
274         });
275         treeViewer.addDoubleClickListener(new IDoubleClickListener() {
276             public void doubleClick(DoubleClickEvent event) {
277                 ISelection selection = event.getSelection();
278                 if (selection instanceof IStructuredSelection) {
279                     Object JavaDoc item = ((IStructuredSelection) selection)
280                             .getFirstElement();
281                     if (item == null) {
282                         return;
283                     }
284                     if (treeViewer.getExpandedState(item)) {
285                         treeViewer.collapseToLevel(item, 1);
286                     } else {
287                         treeViewer.expandToLevel(item, 1);
288                     }
289                 }
290             }
291         });
292
293         // This has to be done after the viewer has been laid out
294
treeViewer.setInput(ResourcesPlugin.getWorkspace());
295     }
296
297     /**
298      * Returns the currently entered container name. Null if the field is empty.
299      * Note that the container may not exist yet if the user entered a new
300      * container name in the field.
301      * @return IPath
302      */

303     public IPath getContainerFullPath() {
304         if (allowNewContainerName) {
305             String JavaDoc pathName = containerNameField.getText();
306             if (pathName == null || pathName.length() < 1) {
307                 return null;
308             }
309             // The user may not have made this absolute so do it for them
310
return (new Path(pathName)).makeAbsolute();
311
312         }
313         if (selectedContainer == null)
314             return null;
315         return selectedContainer.getFullPath();
316
317     }
318
319     /**
320      * Gives focus to one of the widgets in the group, as determined by the
321      * group.
322      */

323     public void setInitialFocus() {
324         if (allowNewContainerName) {
325             containerNameField.setFocus();
326         } else {
327             treeViewer.getTree().setFocus();
328         }
329     }
330
331     /**
332      * Sets the selected existing container.
333      * @param container
334      */

335     public void setSelectedContainer(IContainer container) {
336         selectedContainer = container;
337
338         // expand to and select the specified container
339
List JavaDoc itemsToExpand = new ArrayList JavaDoc();
340         IContainer parent = container.getParent();
341         while (parent != null) {
342             itemsToExpand.add(0, parent);
343             parent = parent.getParent();
344         }
345         treeViewer.setExpandedElements(itemsToExpand.toArray());
346         treeViewer.setSelection(new StructuredSelection(container), true);
347     }
348 }
349
Popular Tags