KickJava   Java API By Example, From Geeks To Geeks.

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


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
14 package org.eclipse.ui.dialogs;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.resources.IContainer;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Event;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.swt.widgets.Listener;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
31 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
32 import org.eclipse.ui.internal.ide.misc.ContainerSelectionGroup;
33
34 /**
35  * A standard selection dialog which solicits a container resource from the user.
36  * The <code>getResult</code> method returns the selected container resource.
37  * <p>
38  * This class may be instantiated; it is not intended to be subclassed.
39  * </p>
40  * <p>
41  * Example:
42  * <pre>
43  * ContainerSelectionDialog dialog =
44  * new ContainerSelectionDialog(getShell(), initialSelection, allowNewContainerName(), msg);
45  * dialog.open();
46  * Object[] result = dialog.getResult();
47  * </pre>
48  * </p>
49  */

50 public class ContainerSelectionDialog extends SelectionDialog {
51     /**
52      *
53      */

54     private static final String JavaDoc EMPTY_STRING = ""; //$NON-NLS-1$
55

56     // the widget group;
57
ContainerSelectionGroup group;
58
59     // the root resource to populate the viewer with
60
private IContainer initialSelection;
61
62     // allow the user to type in a new container name
63
private boolean allowNewContainerName = true;
64
65     // the validation message
66
Label statusMessage;
67
68     //for validating the selection
69
ISelectionValidator validator;
70
71     // show closed projects by default
72
private boolean showClosedProjects = true;
73
74     /**
75      * Creates a resource container selection dialog rooted at the given resource.
76      * All selections are considered valid.
77      *
78      * @param parentShell the parent shell
79      * @param initialRoot the initial selection in the tree
80      * @param allowNewContainerName <code>true</code> to enable the user to type in
81      * a new container name, and <code>false</code> to restrict the user to just
82      * selecting from existing ones
83      * @param message the message to be displayed at the top of this dialog, or
84      * <code>null</code> to display a default message
85      */

86     public ContainerSelectionDialog(Shell parentShell, IContainer initialRoot,
87             boolean allowNewContainerName, String JavaDoc message) {
88         super(parentShell);
89         setTitle(IDEWorkbenchMessages.ContainerSelectionDialog_title);
90         this.initialSelection = initialRoot;
91         this.allowNewContainerName = allowNewContainerName;
92         if (message != null) {
93             setMessage(message);
94         } else {
95             setMessage(IDEWorkbenchMessages.ContainerSelectionDialog_message);
96         }
97         setShellStyle(getShellStyle() | SWT.RESIZE);
98     }
99
100     /* (non-Javadoc)
101      * Method declared in Window.
102      */

103     protected void configureShell(Shell shell) {
104         super.configureShell(shell);
105         PlatformUI.getWorkbench().getHelpSystem()
106                 .setHelp(shell, IIDEHelpContextIds.CONTAINER_SELECTION_DIALOG);
107     }
108
109     /* (non-Javadoc)
110      * Method declared on Dialog.
111      */

112     protected Control createDialogArea(Composite parent) {
113         // create composite
114
Composite area = (Composite) super.createDialogArea(parent);
115
116         Listener listener = new Listener() {
117             public void handleEvent(Event event) {
118                 if (statusMessage != null && validator != null) {
119                     String JavaDoc errorMsg = validator.isValid(group
120                             .getContainerFullPath());
121                     if (errorMsg == null || errorMsg.equals(EMPTY_STRING)) {
122                         statusMessage.setText(EMPTY_STRING);
123                         getOkButton().setEnabled(true);
124                     } else {
125                         statusMessage.setText(errorMsg);
126                         getOkButton().setEnabled(false);
127                     }
128                 }
129             }
130         };
131
132         // container selection group
133
group = new ContainerSelectionGroup(area, listener,
134                 allowNewContainerName, getMessage(), showClosedProjects);
135         if (initialSelection != null) {
136             group.setSelectedContainer(initialSelection);
137         }
138
139         statusMessage = new Label(area, SWT.WRAP);
140         statusMessage.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
141         statusMessage.setText(" \n "); //$NON-NLS-1$
142
statusMessage.setFont(parent.getFont());
143
144         return dialogArea;
145     }
146
147     /**
148      * The <code>ContainerSelectionDialog</code> implementation of this
149      * <code>Dialog</code> method builds a list of the selected resource containers
150      * for later retrieval by the client and closes this dialog.
151      */

152     protected void okPressed() {
153
154         List JavaDoc chosenContainerPathList = new ArrayList JavaDoc();
155         IPath returnValue = group.getContainerFullPath();
156         if (returnValue != null) {
157             chosenContainerPathList.add(returnValue);
158         }
159         setResult(chosenContainerPathList);
160         super.okPressed();
161     }
162
163     /**
164      * Sets the validator to use.
165      *
166      * @param validator A selection validator
167      */

168     public void setValidator(ISelectionValidator validator) {
169         this.validator = validator;
170     }
171
172     /**
173      * Set whether or not closed projects should be shown
174      * in the selection dialog.
175      *
176      * @param show Whether or not to show closed projects.
177      */

178     public void showClosedProjects(boolean show) {
179         this.showClosedProjects = show;
180     }
181 }
182
Popular Tags