KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Jakub Jurkiewicz <jakub.jurkiewicz@gmail.com> - Fix for Bug 174737
11  * [IDE] New Plug-in Project wizard status handling is inconsistent
12  *******************************************************************************/

13 package org.eclipse.ui.dialogs;
14
15 import java.net.URI 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.resources.ResourcesPlugin;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Path;
24 import org.eclipse.jface.wizard.WizardPage;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Composite;
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.Text;
33 import org.eclipse.ui.PlatformUI;
34 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
35 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
36 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
37 import org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea;
38 import org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea.IErrorMessageReporter;
39
40 /**
41  * Standard main page for a wizard that is creates a project resource.
42  * <p>
43  * This page may be used by clients as-is; it may be also be subclassed to suit.
44  * </p>
45  * <p>
46  * Example usage:
47  * <pre>
48  * mainPage = new WizardNewProjectCreationPage("basicNewProjectPage");
49  * mainPage.setTitle("Project");
50  * mainPage.setDescription("Create a new project resource.");
51  * </pre>
52  * </p>
53  */

54 public class WizardNewProjectCreationPage extends WizardPage {
55
56        // initial value stores
57
private String JavaDoc initialProjectFieldValue;
58
59     // widgets
60
Text projectNameField;
61
62     private Listener nameModifyListener = new Listener() {
63         public void handleEvent(Event e) {
64             setLocationForSelection();
65             boolean valid = validatePage();
66             setPageComplete(valid);
67                 
68         }
69     };
70
71     private ProjectContentsLocationArea locationArea;
72
73     // constants
74
private static final int SIZING_TEXT_FIELD_WIDTH = 250;
75
76     /**
77      * Creates a new project creation wizard page.
78      *
79      * @param pageName the name of this page
80      */

81     public WizardNewProjectCreationPage(String JavaDoc pageName) {
82         super(pageName);
83         setPageComplete(false);
84     }
85
86     /** (non-Javadoc)
87      * Method declared on IDialogPage.
88      */

89     public void createControl(Composite parent) {
90         Composite composite = new Composite(parent, SWT.NULL);
91         composite.setFont(parent.getFont());
92
93         initializeDialogUnits(parent);
94
95         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite,
96                 IIDEHelpContextIds.NEW_PROJECT_WIZARD_PAGE);
97
98         composite.setLayout(new GridLayout());
99         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
100
101         createProjectNameGroup(composite);
102         locationArea = new ProjectContentsLocationArea(getErrorReporter(), composite);
103         if(initialProjectFieldValue != null) {
104             locationArea.updateProjectName(initialProjectFieldValue);
105         }
106
107         // Scale the button based on the rest of the dialog
108
setButtonLayoutData(locationArea.getBrowseButton());
109         
110         setPageComplete(validatePage());
111         // Show description on opening
112
setErrorMessage(null);
113         setMessage(null);
114         setControl(composite);
115     }
116     
117     /**
118      * Get an error reporter for the receiver.
119      * @return IErrorMessageReporter
120      */

121     private IErrorMessageReporter getErrorReporter() {
122         return new IErrorMessageReporter(){
123             /* (non-Javadoc)
124              * @see org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea.IErrorMessageReporter#reportError(java.lang.String)
125              */

126             public void reportError(String JavaDoc errorMessage) {
127                 setErrorMessage(errorMessage);
128                 boolean valid = errorMessage == null;
129                 if(valid) {
130                     valid = validatePage();
131                 }
132                 
133                 setPageComplete(valid);
134             }
135         };
136     }
137
138     /**
139      * Creates the project name specification controls.
140      *
141      * @param parent the parent composite
142      */

143     private final void createProjectNameGroup(Composite parent) {
144         // project specification group
145
Composite projectGroup = new Composite(parent, SWT.NONE);
146         GridLayout layout = new GridLayout();
147         layout.numColumns = 2;
148         projectGroup.setLayout(layout);
149         projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
150
151         // new project label
152
Label projectLabel = new Label(projectGroup, SWT.NONE);
153         projectLabel.setText(IDEWorkbenchMessages.WizardNewProjectCreationPage_nameLabel);
154         projectLabel.setFont(parent.getFont());
155
156         // new project name entry field
157
projectNameField = new Text(projectGroup, SWT.BORDER);
158         GridData data = new GridData(GridData.FILL_HORIZONTAL);
159         data.widthHint = SIZING_TEXT_FIELD_WIDTH;
160         projectNameField.setLayoutData(data);
161         projectNameField.setFont(parent.getFont());
162
163         // Set the initial value first before listener
164
// to avoid handling an event during the creation.
165
if (initialProjectFieldValue != null) {
166             projectNameField.setText(initialProjectFieldValue);
167         }
168         projectNameField.addListener(SWT.Modify, nameModifyListener);
169     }
170
171
172     /**
173      * Returns the current project location path as entered by
174      * the user, or its anticipated initial value.
175      * Note that if the default has been returned the path
176      * in a project description used to create a project
177      * should not be set.
178      *
179      * @return the project location path or its anticipated initial value.
180      */

181     public IPath getLocationPath() {
182         return new Path(locationArea.getProjectLocation());
183     }
184     
185     /**
186     /**
187      * Returns the current project location URI as entered by
188      * the user, or <code>null</code> if a valid project location
189      * has not been entered.
190      *
191      * @return the project location URI, or <code>null</code>
192      * @since 3.2
193      */

194     public URI JavaDoc getLocationURI() {
195         return locationArea.getProjectLocationURI();
196     }
197
198     /**
199      * Creates a project resource handle for the current project name field
200      * value. The project handle is created relative to the workspace root.
201      * <p>
202      * This method does not create the project resource; this is the
203      * responsibility of <code>IProject::create</code> invoked by the new
204      * project resource wizard.
205      * </p>
206      *
207      * @return the new project resource handle
208      */

209     public IProject getProjectHandle() {
210         return ResourcesPlugin.getWorkspace().getRoot().getProject(
211                 getProjectName());
212     }
213
214     /**
215      * Returns the current project name as entered by the user, or its anticipated
216      * initial value.
217      *
218      * @return the project name, its anticipated initial value, or <code>null</code>
219      * if no project name is known
220      */

221     public String JavaDoc getProjectName() {
222         if (projectNameField == null) {
223             return initialProjectFieldValue;
224         }
225
226         return getProjectNameFieldValue();
227     }
228
229     /**
230      * Returns the value of the project name field
231      * with leading and trailing spaces removed.
232      *
233      * @return the project name in the field
234      */

235     private String JavaDoc getProjectNameFieldValue() {
236         if (projectNameField == null) {
237             return ""; //$NON-NLS-1$
238
}
239
240         return projectNameField.getText().trim();
241     }
242
243     /**
244      * Sets the initial project name that this page will use when
245      * created. The name is ignored if the createControl(Composite)
246      * method has already been called. Leading and trailing spaces
247      * in the name are ignored.
248      * Providing the name of an existing project will not necessarily
249      * cause the wizard to warn the user. Callers of this method
250      * should first check if the project name passed already exists
251      * in the workspace.
252      *
253      * @param name initial project name for this page
254      *
255      * @see IWorkspace#validateName(String, int)
256      *
257      */

258     public void setInitialProjectName(String JavaDoc name) {
259         if (name == null) {
260             initialProjectFieldValue = null;
261         } else {
262             initialProjectFieldValue = name.trim();
263             if(locationArea != null) {
264                 locationArea.updateProjectName(name.trim());
265             }
266         }
267     }
268
269     /**
270      * Set the location to the default location if we are set to useDefaults.
271      */

272     void setLocationForSelection() {
273         locationArea.updateProjectName(getProjectNameFieldValue());
274     }
275
276   
277     /**
278      * Returns whether this page's controls currently all contain valid
279      * values.
280      *
281      * @return <code>true</code> if all controls are valid, and
282      * <code>false</code> if at least one is invalid
283      */

284     protected boolean validatePage() {
285         IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace();
286
287         String JavaDoc projectFieldContents = getProjectNameFieldValue();
288         if (projectFieldContents.equals("")) { //$NON-NLS-1$
289
setErrorMessage(null);
290             setMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectNameEmpty);
291             return false;
292         }
293
294         IStatus nameStatus = workspace.validateName(projectFieldContents,
295                 IResource.PROJECT);
296         if (!nameStatus.isOK()) {
297             setErrorMessage(nameStatus.getMessage());
298             return false;
299         }
300
301         IProject handle = getProjectHandle();
302         if (handle.exists()) {
303             setErrorMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectExistsMessage);
304             return false;
305         }
306         
307         /*
308          * If not using the default value validate the location.
309          */

310         if (!locationArea.isDefault()) {
311             String JavaDoc validLocationMessage = locationArea.checkValidLocation();
312             if (validLocationMessage != null) { //there is no destination location given
313
setErrorMessage(validLocationMessage);
314                 return false;
315             }
316         }
317
318         setErrorMessage(null);
319         setMessage(null);
320         return true;
321     }
322
323     /*
324      * see @DialogPage.setVisible(boolean)
325      */

326     public void setVisible(boolean visible) {
327         super.setVisible(visible);
328         if (visible) {
329             projectNameField.setFocus();
330         }
331     }
332
333     /**
334      * Returns the useDefaults.
335      * @return boolean
336      */

337     public boolean useDefaults() {
338         return locationArea.isDefault();
339     }
340
341 }
342
Popular Tags