KickJava   Java API By Example, From Geeks To Geeks.

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


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

13 package org.eclipse.ui.dialogs;
14
15 import java.util.ArrayList JavaDoc;
16
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.IWorkspace;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.osgi.util.NLS;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.graphics.Font;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Event;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.swt.widgets.Listener;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.swt.widgets.Text;
34 import org.eclipse.ui.PlatformUI;
35 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
36 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
37 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
38 import org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea;
39 import org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea.IErrorMessageReporter;
40
41 /**
42  * The ProjectLocationSelectionDialog is the dialog used to select the name and
43  * location of a project for copying.
44  */

45 public class ProjectLocationSelectionDialog extends SelectionStatusDialog {
46     // widgets
47
private Text projectNameField;
48
49     private IProject project;
50
51     private ProjectContentsLocationArea locationArea;
52
53     private static String JavaDoc PROJECT_NAME_LABEL = IDEWorkbenchMessages.ProjectLocationSelectionDialog_nameLabel;
54
55     private static String JavaDoc PROJECT_LOCATION_SELECTION_TITLE = IDEWorkbenchMessages.ProjectLocationSelectionDialog_selectionTitle;
56
57     // constants
58
private static final int SIZING_TEXT_FIELD_WIDTH = 250;
59
60     private boolean firstLocationCheck;
61
62     /**
63      * Create a ProjectLocationSelectionDialog on the supplied project parented
64      * by the parentShell.
65      *
66      * @param parentShell
67      * @param existingProject
68      */

69     public ProjectLocationSelectionDialog(Shell parentShell,
70             IProject existingProject) {
71         super(parentShell);
72         setShellStyle(getShellStyle() | SWT.RESIZE);
73         setTitle(PROJECT_LOCATION_SELECTION_TITLE);
74         setStatusLineAboveButtons(true);
75         this.project = existingProject;
76     }
77
78     /**
79      * Check the message. If it is null then continue otherwise inform the user
80      * via the status value and disable the OK.
81      *
82      * @param errorMsg
83      * the error message to show if it is not <code>null</code>
84      */

85     private void applyValidationResult(String JavaDoc errorMsg) {
86         int code;
87         boolean allowFinish = false;
88
89         if (errorMsg == null) {
90             code = IStatus.OK;
91             errorMsg = ""; //$NON-NLS-1$
92
allowFinish = true;
93         } else if (firstLocationCheck) {
94             code = IStatus.OK;
95         } else {
96             code = IStatus.ERROR;
97         }
98
99         updateStatus(new Status(code, IDEWorkbenchPlugin.IDE_WORKBENCH, code,
100                 errorMsg, null));
101         getOkButton().setEnabled(allowFinish);
102     }
103
104     /**
105      * Check whether the entries are valid. If so return null. Otherwise return
106      * a string that indicates the problem.
107      */

108     private String JavaDoc checkValid() {
109         String JavaDoc valid = checkValidName();
110         if (valid != null) {
111             return valid;
112         }
113         return locationArea.checkValidLocation();
114     }
115
116     /**
117      * Check if the entries in the widget are valid. If they are return null
118      * otherwise return a string that indicates the problem.
119      */

120     private String JavaDoc checkValidName() {
121
122         String JavaDoc name = this.projectNameField.getText();
123         IWorkspace workspace = getProject().getWorkspace();
124         IStatus nameStatus = workspace.validateName(name, IResource.PROJECT);
125         if (!nameStatus.isOK()) {
126             return nameStatus.getMessage();
127         }
128         IProject newProject = workspace.getRoot().getProject(name);
129         if (newProject.exists()) {
130             return NLS.bind(
131                     IDEWorkbenchMessages.CopyProjectAction_alreadyExists, name);
132         }
133
134         return null;
135     }
136
137     /**
138      * The <code>ProjectLocationSelectionDialog</code> implementation of this
139      * <code>SelectionStatusDialog</code> method builds a two element list -
140      * the first element is the project name and the second one is the location.
141      */

142     protected void computeResult() {
143
144         ArrayList JavaDoc list = new ArrayList JavaDoc();
145         list.add(this.projectNameField.getText());
146         list.add(locationArea.getProjectLocation());
147         setResult(list);
148     }
149
150     /*
151      * (non-Javadoc) Method declared in Window.
152      */

153     protected void configureShell(Shell shell) {
154         super.configureShell(shell);
155         PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
156                 IIDEHelpContextIds.PROJECT_LOCATION_SELECTION_DIALOG);
157     }
158
159     /*
160      * (non-Javadoc) Method declared on Dialog.
161      */

162     protected Control createDialogArea(Composite parent) {
163         // page group
164
Composite composite = (Composite) super.createDialogArea(parent);
165
166         composite.setLayout(new GridLayout());
167         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
168
169         createProjectNameGroup(composite);
170         locationArea = new ProjectContentsLocationArea(getErrorReporter(),
171                 composite, project);
172         locationArea.updateProjectName(projectNameField.getText());
173         return composite;
174     }
175
176     /**
177      * Create the listener that is used to validate the entries for the receiver
178      */

179     private void createNameListener() {
180
181         Listener listener = new Listener() {
182             public void handleEvent(Event event) {
183                 setLocationForSelection();
184                 applyValidationResult(checkValid());
185             }
186         };
187
188         this.projectNameField.addListener(SWT.Modify, listener);
189     }
190
191     /**
192      * Creates the project name specification controls.
193      *
194      * @param parent
195      * the parent composite
196      */

197     private void createProjectNameGroup(Composite parent) {
198         Font font = parent.getFont();
199         // project specification group
200
Composite projectGroup = new Composite(parent, SWT.NONE);
201         GridLayout layout = new GridLayout();
202         layout.numColumns = 2;
203         projectGroup.setLayout(layout);
204         projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
205
206         // new project label
207
Label projectLabel = new Label(projectGroup, SWT.NONE);
208         projectLabel.setFont(font);
209         projectLabel.setText(PROJECT_NAME_LABEL);
210
211         // new project name entry field
212
projectNameField = new Text(projectGroup, SWT.BORDER);
213         GridData data = new GridData(GridData.FILL_HORIZONTAL);
214         data.widthHint = SIZING_TEXT_FIELD_WIDTH;
215         projectNameField.setLayoutData(data);
216         projectNameField.setFont(font);
217
218         // Set the initial value first before listener
219
// to avoid handling an event during the creation.
220
projectNameField.setText(getCopyNameFor(getProject().getName()));
221         projectNameField.selectAll();
222
223         createNameListener();
224
225     }
226
227     /**
228      * Generates a new name for the project that does not have any collisions.
229      */

230     private String JavaDoc getCopyNameFor(String JavaDoc projectName) {
231
232         IWorkspace workspace = getProject().getWorkspace();
233         if (!workspace.getRoot().getProject(projectName).exists()) {
234             return projectName;
235         }
236
237         int counter = 1;
238         while (true) {
239             String JavaDoc nameSegment;
240             if (counter > 1) {
241                 nameSegment = NLS.bind(
242                         IDEWorkbenchMessages.CopyProjectAction_copyNameTwoArgs,
243                         new Integer JavaDoc(counter), projectName);
244             } else {
245                 nameSegment = NLS.bind(
246                         IDEWorkbenchMessages.CopyProjectAction_copyNameOneArg,
247                         projectName);
248             }
249
250             if (!workspace.getRoot().getProject(nameSegment).exists()) {
251                 return nameSegment;
252             }
253
254             counter++;
255         }
256
257     }
258
259     /**
260      * Get the project being manipulated.
261      */

262     private IProject getProject() {
263         return this.project;
264     }
265
266     /**
267      * Set the location to the default location if we are set to useDefaults.
268      */

269     private void setLocationForSelection() {
270         locationArea.updateProjectName(projectNameField.getText());
271     }
272
273     /**
274      * Get an error reporter for the receiver.
275      *
276      * @return IErrorMessageReporter
277      */

278     private IErrorMessageReporter getErrorReporter() {
279         return new IErrorMessageReporter() {
280             /*
281              * (non-Javadoc)
282              *
283              * @see org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea.IErrorMessageReporter#reportError(java.lang.String)
284              */

285             public void reportError(String JavaDoc errorMessage) {
286                 setMessage(errorMessage);
287
288             }
289         };
290     }
291 }
292
Popular Tags