KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > dialogs > WizardNewProjectNameAndLocationPage


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.internal.ide.dialogs;
14
15 import org.eclipse.core.resources.IProject;
16 import org.eclipse.core.resources.IProjectDescription;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.IWorkspace;
19 import org.eclipse.core.resources.ResourcesPlugin;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.jface.wizard.WizardPage;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.events.SelectionListener;
29 import org.eclipse.swt.graphics.Font;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Button;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.DirectoryDialog;
35 import org.eclipse.swt.widgets.Event;
36 import org.eclipse.swt.widgets.Label;
37 import org.eclipse.swt.widgets.Listener;
38 import org.eclipse.swt.widgets.Text;
39 import org.eclipse.ui.PlatformUI;
40 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
41 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
42 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
43
44 /**
45  * First page for the new project creation wizard. This page
46  * collects the name and location of the new project.
47  * <p>
48  * Example usage:
49  * <pre>
50  * mainPage = new WizardNewProjectNameAndLocationPage("wizardNewProjectNameAndLocationPage");
51  * mainPage.setTitle("Project");
52  * mainPage.setDescription("Create a new project.");
53  * </pre>
54  * </p>
55  */

56 public class WizardNewProjectNameAndLocationPage extends WizardPage {
57     // Whether to use default or custom project location
58
private boolean useDefaults = true;
59
60     // initial value stores
61
private String JavaDoc initialProjectFieldValue;
62
63     private IPath initialLocationFieldValue;
64
65     // the value the user has entered
66
private String JavaDoc customLocationFieldValue;
67
68     // widgets
69
private Text projectNameField;
70
71     private Text locationPathField;
72
73     private Label locationLabel;
74
75     private Button browseButton;
76
77     private Listener nameModifyListener = new Listener() {
78         public void handleEvent(Event e) {
79             setLocationForSelection();
80             setPageComplete(validatePage());
81         }
82     };
83
84     private Listener locationModifyListener = new Listener() {
85         public void handleEvent(Event e) {
86             setPageComplete(validatePage());
87         }
88     };
89
90     // constants
91
private static final int SIZING_TEXT_FIELD_WIDTH = 250;
92
93     /**
94      * Creates a new project creation wizard page.
95      *
96      * @param pageName the name of this page
97      */

98     public WizardNewProjectNameAndLocationPage(String JavaDoc pageName) {
99         super(pageName);
100         setPageComplete(false);
101         initialLocationFieldValue = Platform.getLocation();
102         customLocationFieldValue = ""; //$NON-NLS-1$
103
}
104
105     /* (non-Javadoc)
106      * Method declared on IWizardPage
107      */

108     public boolean canFlipToNextPage() {
109         // Already know there is a next page...
110
return isPageComplete();
111     }
112
113     /* (non-Javadoc)
114      * Method declared on IDialogPage.
115      */

116     public void createControl(Composite parent) {
117         Composite composite = new Composite(parent, SWT.NULL);
118         composite.setLayout(new GridLayout());
119         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
120         composite.setFont(parent.getFont());
121
122         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite,
123                 IIDEHelpContextIds.NEW_PROJECT_WIZARD_PAGE);
124
125         createProjectNameGroup(composite);
126         createProjectLocationGroup(composite);
127
128         validatePage();
129
130         // Show description on opening
131
setErrorMessage(null);
132         setMessage(null);
133         setControl(composite);
134     }
135
136     /**
137      * Creates the project location specification controls.
138      *
139      * @param parent the parent composite
140      */

141     private final void createProjectLocationGroup(Composite parent) {
142         Font font = parent.getFont();
143         // project specification group
144
Composite projectGroup = new Composite(parent, SWT.NONE);
145         GridLayout layout = new GridLayout();
146         layout.numColumns = 3;
147         projectGroup.setLayout(layout);
148         projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
149         projectGroup.setFont(font);
150
151         // new project label
152
Label projectContentsLabel = new Label(projectGroup, SWT.NONE);
153         projectContentsLabel.setFont(font);
154         projectContentsLabel
155                 .setText(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectContentsLabel);
156
157         GridData labelData = new GridData();
158         labelData.horizontalSpan = 3;
159         projectContentsLabel.setLayoutData(labelData);
160
161         final Button useDefaultsButton = new Button(projectGroup, SWT.CHECK
162                 | SWT.RIGHT);
163         useDefaultsButton.setText(IDEWorkbenchMessages.WizardNewProjectCreationPage_useDefaultLabel);
164         useDefaultsButton.setSelection(useDefaults);
165         useDefaultsButton.setFont(font);
166
167         GridData buttonData = new GridData();
168         buttonData.horizontalSpan = 3;
169         useDefaultsButton.setLayoutData(buttonData);
170
171         createUserSpecifiedProjectLocationGroup(projectGroup, !useDefaults);
172
173         SelectionListener listener = new SelectionAdapter() {
174             public void widgetSelected(SelectionEvent e) {
175                 useDefaults = useDefaultsButton.getSelection();
176                 browseButton.setEnabled(!useDefaults);
177                 locationPathField.setEnabled(!useDefaults);
178                 locationLabel.setEnabled(!useDefaults);
179                 if (useDefaults) {
180                     customLocationFieldValue = locationPathField.getText();
181                     setLocationForSelection();
182                 } else {
183                     locationPathField.setText(customLocationFieldValue);
184                 }
185             }
186         };
187         useDefaultsButton.addSelectionListener(listener);
188     }
189
190     /**
191      * Creates the project name specification controls.
192      *
193      * @param parent the parent composite
194      */

195     private final void createProjectNameGroup(Composite parent) {
196         Font font = parent.getFont();
197         // project specification group
198
Composite projectGroup = new Composite(parent, SWT.NONE);
199         GridLayout layout = new GridLayout();
200         layout.numColumns = 2;
201         projectGroup.setLayout(layout);
202         projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
203
204         // new project label
205
Label projectLabel = new Label(projectGroup, SWT.NONE);
206         projectLabel.setFont(font);
207         projectLabel.setText(IDEWorkbenchMessages.WizardNewProjectCreationPage_nameLabel);
208
209         // new project name entry field
210
projectNameField = new Text(projectGroup, SWT.BORDER);
211         GridData data = new GridData(GridData.FILL_HORIZONTAL);
212         data.widthHint = SIZING_TEXT_FIELD_WIDTH;
213         projectNameField.setLayoutData(data);
214         projectNameField.setFont(font);
215
216         // Set the initial value first before listener
217
// to avoid handling an event during the creation.
218
if (initialProjectFieldValue != null) {
219             projectNameField.setText(initialProjectFieldValue);
220         }
221         projectNameField.addListener(SWT.Modify, nameModifyListener);
222     }
223
224     /**
225      * Creates the project location specification controls.
226      *
227      * @param projectGroup the parent composite
228      * @param enabled the initial enabled state of the widgets created
229      */

230     private void createUserSpecifiedProjectLocationGroup(
231             Composite projectGroup, boolean enabled) {
232         Font font = projectGroup.getFont();
233         // location label
234
locationLabel = new Label(projectGroup, SWT.NONE);
235         locationLabel.setFont(font);
236         locationLabel.setText(IDEWorkbenchMessages.WizardNewProjectCreationPage_locationLabel);
237         locationLabel.setEnabled(enabled);
238
239         // project location entry field
240
locationPathField = new Text(projectGroup, SWT.BORDER);
241         GridData data = new GridData(GridData.FILL_HORIZONTAL);
242         data.widthHint = SIZING_TEXT_FIELD_WIDTH;
243         locationPathField.setLayoutData(data);
244         locationPathField.setFont(font);
245         locationPathField.setEnabled(enabled);
246
247         // browse button
248
browseButton = new Button(projectGroup, SWT.PUSH);
249         browseButton.setFont(font);
250         browseButton.setText(IDEWorkbenchMessages.WizardNewProjectCreationPage_browseLabel);
251         browseButton.addSelectionListener(new SelectionAdapter() {
252             public void widgetSelected(SelectionEvent event) {
253                 handleLocationBrowseButtonPressed();
254             }
255         });
256
257         browseButton.setEnabled(enabled);
258
259         // Set the initial value first before listener
260
// to avoid handling an event during the creation.
261
if (initialLocationFieldValue != null) {
262             locationPathField.setText(initialLocationFieldValue.toOSString());
263         }
264         locationPathField.addListener(SWT.Modify, locationModifyListener);
265     }
266
267     /**
268      * Returns the current project location path as entered by
269      * the user, or its anticipated initial value.
270      *
271      * @return the project location path, its anticipated initial value, or <code>null</code>
272      * if no project location path is known
273      */

274     /* package */IPath getLocationPath() {
275         if (useDefaults) {
276             return initialLocationFieldValue;
277         }
278
279         return new Path(getProjectLocationFieldValue());
280     }
281
282     /**
283      * Creates a project resource handle for the current project name field value.
284      * <p>
285      * This method does not create the project resource; this is the responsibility
286      * of <code>IProject::create</code> invoked by the new project resource wizard.
287      * </p>
288      *
289      * @return the new project resource handle
290      */

291     /* package */IProject getProjectHandle() {
292         return ResourcesPlugin.getWorkspace().getRoot().getProject(
293                 getProjectName());
294     }
295
296     /**
297      * Returns the current project name as entered by the user, or its anticipated
298      * initial value.
299      *
300      * @return the project name, its anticipated initial value, or <code>null</code>
301      * if no project name is known
302      */

303     /* package */String JavaDoc getProjectName() {
304         if (projectNameField == null) {
305             return initialProjectFieldValue;
306         }
307
308         return getProjectNameFieldValue();
309     }
310
311     /**
312      * Returns the value of the project name field
313      * with leading and trailing spaces removed.
314      *
315      * @return the project name in the field
316      */

317     private String JavaDoc getProjectNameFieldValue() {
318         if (projectNameField == null) {
319             return IDEResourceInfoUtils.EMPTY_STRING;
320         }
321        return projectNameField.getText().trim();
322     }
323
324     /**
325      * Returns the value of the project location field
326      * with leading and trailing spaces removed.
327      *
328      * @return the project location directory in the field
329      */

330     private String JavaDoc getProjectLocationFieldValue() {
331         if (locationPathField == null) {
332             return IDEResourceInfoUtils.EMPTY_STRING;
333         }
334        return locationPathField.getText().trim();
335     }
336
337     /**
338      * Open an appropriate directory browser
339      */

340     private void handleLocationBrowseButtonPressed() {
341         DirectoryDialog dialog = new DirectoryDialog(locationPathField
342                 .getShell());
343         dialog.setMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_directoryLabel);
344
345         String JavaDoc dirName = getProjectLocationFieldValue();
346         if (dirName.length() > 0) {
347             if (IDEResourceInfoUtils.exists(dirName)) {
348                 dialog.setFilterPath(new Path(dirName).toOSString());
349             }
350         }
351
352         String JavaDoc selectedDirectory = dialog.open();
353         if (selectedDirectory != null) {
354             customLocationFieldValue = selectedDirectory;
355             locationPathField.setText(customLocationFieldValue);
356         }
357     }
358
359     /**
360      * Returns whether the currently specified project
361      * content directory points to an exising project
362      */

363     private boolean isExistingProjectLocation() {
364         IPath path = getLocationPath();
365         path = path.append(IProjectDescription.DESCRIPTION_FILE_NAME);
366         return path.toFile().exists();
367     }
368
369     /**
370      * Sets the initial project name that this page will use when
371      * created. The name is ignored if the createControl(Composite)
372      * method has already been called. Leading and trailing spaces
373      * in the name are ignored.
374      *
375      * @param name initial project name for this page
376      */

377     /* package */void setInitialProjectName(String JavaDoc name) {
378         if (name == null) {
379             initialProjectFieldValue = null;
380         } else {
381             initialProjectFieldValue = name.trim();
382         }
383     }
384
385     /**
386      * Set the location to the default location if we are set to useDefaults.
387      */

388     private void setLocationForSelection() {
389         if (useDefaults) {
390             IPath defaultPath = Platform.getLocation().append(
391                     getProjectNameFieldValue());
392             locationPathField.setText(defaultPath.toOSString());
393         }
394     }
395
396     /**
397      * Returns whether this page's controls currently all contain valid
398      * values.
399      *
400      * @return <code>true</code> if all controls are valid, and
401      * <code>false</code> if at least one is invalid
402      */

403     private boolean validatePage() {
404         IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace();
405
406         String JavaDoc projectFieldContents = getProjectNameFieldValue();
407         if (projectFieldContents.equals("")) { //$NON-NLS-1$
408
setErrorMessage(null);
409             setMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectNameEmpty);
410             return false;
411         }
412
413         IStatus nameStatus = workspace.validateName(projectFieldContents,
414                 IResource.PROJECT);
415         if (!nameStatus.isOK()) {
416             setErrorMessage(nameStatus.getMessage());
417             return false;
418         }
419
420         String JavaDoc locationFieldContents = getProjectLocationFieldValue();
421
422         if (locationFieldContents.equals("")) { //$NON-NLS-1$
423
setErrorMessage(null);
424             setMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectLocationEmpty);
425             return false;
426         }
427
428         IPath path = new Path(""); //$NON-NLS-1$
429
if (!path.isValidPath(locationFieldContents)) {
430             setErrorMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_locationError);
431             return false;
432         }
433         if (!useDefaults
434                 && Platform.getLocation().isPrefixOf(
435                         new Path(locationFieldContents))) {
436             setErrorMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_defaultLocationError);
437             return false;
438         }
439
440         if (getProjectHandle().exists()) {
441             setErrorMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectExistsMessage);
442             return false;
443         }
444
445         if (isExistingProjectLocation()) {
446             setErrorMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectLocationExistsMessage);
447             return false;
448         }
449
450         setErrorMessage(null);
451         setMessage(null);
452         return true;
453     }
454
455     /*
456      * see @DialogPage.setVisible(boolean)
457      */

458     public void setVisible(boolean visible) {
459         super.setVisible(visible);
460         if (visible) {
461             projectNameField.setFocus();
462         }
463     }
464
465 }
466
Popular Tags