KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > eclipse > enhydraWizard > NewEnhydraWizardPage


1 package org.enhydra.kelp.eclipse.enhydraWizard;
2
3 import java.io.File JavaDoc;
4
5 import org.eclipse.core.resources.IProject;
6 import org.eclipse.core.resources.IResource;
7 import org.eclipse.core.resources.IWorkspace;
8 import org.eclipse.core.resources.ResourcesPlugin;
9 import org.eclipse.core.runtime.IPath;
10 import org.eclipse.core.runtime.IStatus;
11 import org.eclipse.core.runtime.Path;
12 import org.eclipse.core.runtime.Platform;
13 import org.eclipse.jdt.core.dom.ThisExpression;
14 import org.eclipse.jface.resource.ImageDescriptor;
15 import org.eclipse.jface.wizard.WizardPage;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.events.SelectionListener;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.DirectoryDialog;
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.Text;
29 import org.eclipse.ui.help.WorkbenchHelp;
30 import org.eclipse.ui.internal.IHelpContextIds;
31 import org.eclipse.ui.internal.WorkbenchMessages;
32 import org.eclipse.ui.internal.WorkbenchPlugin;
33
34 /**
35  * @author Tweety
36  *
37  * To change this generated comment edit the template variable "typecomment":
38  * Window>Preferences>Java>Templates.
39  * To enable and disable the creation of type comments go to
40  * Window>Preferences>Java>Code Generation.
41  */

42 public class NewEnhydraWizardPage extends WizardPage {
43
44     private boolean useDefaults = true;
45
46     // initial value stores
47
private String JavaDoc initialProjectFieldValue;
48     private IPath initialLocationFieldValue;
49     
50     // the value the user has entered
51
private String JavaDoc customLocationFieldValue;
52     
53     // widgets
54
private Text projectNameField;
55     private Text locationPathField;
56     private Label locationLabel;
57     private Button browseButton;
58     static private String JavaDoc prjRootValue = null;
59     private Listener nameModifyListener = new Listener() {
60         public void handleEvent(Event e) {
61             setLocationForSelection();
62             setPageComplete(validatePage());
63         }
64     };
65
66     private Listener locationModifyListener = new Listener() {
67         public void handleEvent(Event e) {
68             setPageComplete(validatePage());
69         }
70     };
71
72     // constants
73
private static final int SIZING_TEXT_FIELD_WIDTH = 250;
74     private static final int SIZING_INDENTATION_WIDTH = 10;
75
76
77     /**
78      * Constructor for NewEnhydraWizardPage.
79      * @param pageName
80      */

81     public NewEnhydraWizardPage(String JavaDoc pageName) {
82         super(pageName);
83     
84         setPageComplete(false);
85         initialLocationFieldValue = Platform.getLocation();
86         customLocationFieldValue = "";
87     }
88
89     /**
90      * Constructor for NewEnhydraWizardPage.
91      * @param pageName
92      * @param title
93      * @param titleImage
94      */

95     public NewEnhydraWizardPage(
96         String JavaDoc pageName,
97         String JavaDoc title,
98         ImageDescriptor titleImage) {
99         super(pageName, title, titleImage);
100         
101         setPageComplete(false);
102         initialLocationFieldValue = Platform.getLocation();
103         customLocationFieldValue = "";
104     }
105
106     /** (non-Javadoc)
107      * Method declared on IDialogPage.
108      */

109     public void createControl(Composite parent) {
110         Composite composite = new Composite(parent, SWT.NULL);
111     
112         WorkbenchHelp.setHelp(composite, IHelpContextIds.NEW_PROJECT_WIZARD_PAGE);
113         
114         composite.setLayout(new GridLayout());
115         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
116         
117         createProjectNameGroup(composite);
118         createProjectLocationGroup(composite);
119         validatePage();
120         // Show description on opening
121
setErrorMessage(null);
122         setMessage(null);
123         setControl(composite);
124     }
125     /**
126      * Creates the project location specification controls.
127      *
128      * @param parent the parent composite
129      */

130     private final void createProjectLocationGroup(Composite parent) {
131     
132         // project specification group
133
Composite projectGroup = new Composite(parent, SWT.NONE);
134         GridLayout layout = new GridLayout();
135         layout.numColumns = 3;
136         projectGroup.setLayout(layout);
137         projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
138      
139         // new project label
140
Label projectContentsLabel = new Label(projectGroup, SWT.NONE);
141         projectContentsLabel.setText(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.projectContentsLabel"));
142     
143         GridData labelData = new GridData();
144         labelData.horizontalSpan = 3;
145         projectContentsLabel.setLayoutData(labelData);
146     
147         final Button useDefaultsButton = new Button(projectGroup, SWT.CHECK | SWT.RIGHT);
148         useDefaultsButton.setText(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.useDefaultLabel"));
149         useDefaultsButton.setSelection(useDefaults);
150     
151         GridData buttonData = new GridData();
152         buttonData.horizontalSpan = 3;
153         useDefaultsButton.setLayoutData(buttonData);
154     
155         createUserSpecifiedProjectLocationGroup(projectGroup, !useDefaults);
156     
157         SelectionListener listener = new SelectionAdapter() {
158             public void widgetSelected(SelectionEvent e) {
159                 useDefaults = useDefaultsButton.getSelection();
160                 browseButton.setEnabled(!useDefaults);
161                 locationPathField.setEnabled(!useDefaults);
162                 locationLabel.setEnabled(!useDefaults);
163                 if (useDefaults) {
164                     customLocationFieldValue = locationPathField.getText();
165                     setLocationForSelection();
166                 } else {
167                     locationPathField.setText(customLocationFieldValue);
168                 }
169             }
170         };
171         useDefaultsButton.addSelectionListener(listener);
172     }
173     /**
174      * Creates the project name specification controls.
175      *
176      * @param parent the parent composite
177      */

178     private final void createProjectNameGroup(Composite parent) {
179         // project specification group
180
Composite projectGroup = new Composite(parent, SWT.NONE);
181         GridLayout layout = new GridLayout();
182         layout.numColumns = 2;
183         projectGroup.setLayout(layout);
184         projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
185     
186         // new project label
187
Label projectLabel = new Label(projectGroup, SWT.NONE);
188         projectLabel.setText(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.nameLabel"));
189     
190         // new project name entry field
191
projectNameField = new Text(projectGroup, SWT.BORDER);
192         GridData data = new GridData(GridData.FILL_HORIZONTAL);
193         data.widthHint = SIZING_TEXT_FIELD_WIDTH;
194         projectNameField.setLayoutData(data);
195     
196         // Set the initial value first before listener
197
// to avoid handling an event during the creation.
198
if (initialProjectFieldValue != null)
199             projectNameField.setText(initialProjectFieldValue);
200         projectNameField.addListener(SWT.Modify, nameModifyListener);
201     }
202     /**
203      * Creates the project location specification controls.
204      *
205      * @param projectGroup the parent composite
206      * @param boolean - the initial enabled state of the widgets created
207      */

208     private void createUserSpecifiedProjectLocationGroup(Composite projectGroup, boolean enabled) {
209     
210         // location label
211
locationLabel = new Label(projectGroup,SWT.NONE);
212         locationLabel.setText(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.locationLabel"));
213         locationLabel.setEnabled(enabled);
214     
215         // project location entry field
216
locationPathField = new Text(projectGroup, SWT.BORDER);
217         GridData data = new GridData(GridData.FILL_HORIZONTAL);
218         data.widthHint = SIZING_TEXT_FIELD_WIDTH;
219         locationPathField.setLayoutData(data);
220         locationPathField.setEnabled(enabled);
221     
222         // browse button
223
browseButton = new Button(projectGroup, SWT.PUSH);
224         browseButton.setText(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.browseLabel"));
225         browseButton.addSelectionListener(new SelectionAdapter() {
226             public void widgetSelected(SelectionEvent event) {
227                 handleLocationBrowseButtonPressed();
228             }
229         });
230     
231         browseButton.setEnabled(enabled);
232     
233         // Set the initial value first before listener
234
// to avoid handling an event during the creation.
235
if (initialLocationFieldValue != null)
236             locationPathField.setText(initialLocationFieldValue.toOSString());
237         locationPathField.addListener(SWT.Modify, locationModifyListener);
238     }
239     /**
240      * Returns the current project location path as entered by
241      * the user, or its anticipated initial value.
242      * Note that if the default has been returned the path
243      * in a project description used to create a project
244      * should not be set.
245      *
246      * @return the project location path, its anticipated initial value, or <code>null</code>
247      * if no project location path is known
248      */

249     public IPath getLocationPath() {
250         if (useDefaults)
251             return initialLocationFieldValue;
252             
253         return new Path(getProjectLocationFieldValue());
254     }
255     /**
256      * Creates a project resource handle for the current project name field value.
257      * <p>
258      * This method does not create the project resource; this is the responsibility
259      * of <code>IProject::create</code> invoked by the new project resource wizard.
260      * </p>
261      *
262      * @return the new project resource handle
263      */

264     public IProject getProjectHandle() {
265         return ResourcesPlugin.getWorkspace().getRoot().getProject(getProjectName());
266     }
267     /**
268      * Returns the current project name as entered by the user, or its anticipated
269      * initial value.
270      *
271      * @return the project name, its anticipated initial value, or <code>null</code>
272      * if no project name is known
273      */

274     public String JavaDoc getProjectName() {
275         if (projectNameField == null)
276             return initialProjectFieldValue;
277             
278         return getProjectNameFieldValue();
279     }
280     /**
281      * Returns the value of the project name field
282      * with leading and trailing spaces removed.
283      *
284      * @return the project name in the field
285      */

286     private String JavaDoc getProjectNameFieldValue() {
287         if (projectNameField == null)
288             return "";
289         else
290             return projectNameField.getText().trim();
291     }
292     
293      
294     /**
295      * Returns the value of the project location field
296      * with leading and trailing spaces removed.
297      *
298      * @return the project location directory in the field
299      */

300     private String JavaDoc getProjectLocationFieldValue() {
301         if (locationPathField == null)
302             return "";
303         else
304             return locationPathField.getText().trim();
305     }
306     /**
307      * Open an appropriate directory browser
308      */

309     private void handleLocationBrowseButtonPressed() {
310         DirectoryDialog dialog = new DirectoryDialog(locationPathField.getShell());
311         dialog.setMessage(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.directoryLabel"));
312         
313         String JavaDoc dirName = getProjectLocationFieldValue();
314         if (!dirName.equals("")) {
315             File JavaDoc path = new File JavaDoc(dirName);
316             if (path.exists())
317                 dialog.setFilterPath(new Path(dirName).toOSString());
318         }
319         
320         String JavaDoc selectedDirectory = dialog.open();
321         if (selectedDirectory != null) {
322             customLocationFieldValue = selectedDirectory;
323             locationPathField.setText(customLocationFieldValue);
324         }
325     }
326     /**
327      * Sets the initial project name that this page will use when
328      * created. The name is ignored if the createControl(Composite)
329      * method has already been called. Leading and trailing spaces
330      * in the name are ignored.
331      *
332      * @param name initial project name for this page
333      */

334     public void setInitialProjectName(String JavaDoc name) {
335         if (name == null)
336             initialProjectFieldValue = null;
337         else
338             initialProjectFieldValue = name.trim();
339     }
340     /**
341      * Set the location to the default location if we are set to useDefaults.
342      */

343     private void setLocationForSelection() {
344         if (useDefaults) {
345             IPath defaultPath = Platform.getLocation().append(getProjectNameFieldValue());
346             locationPathField.setText(defaultPath.toOSString());
347         }
348     }
349     /**
350      * Returns whether this page's controls currently all contain valid
351      * values.
352      *
353      * @return <code>true</code> if all controls are valid, and
354      * <code>false</code> if at least one is invalid
355      */

356     private boolean validatePage() {
357         IWorkspace workspace = WorkbenchPlugin.getPluginWorkspace();
358     
359         String JavaDoc projectFieldContents = getProjectNameFieldValue();
360         if (projectFieldContents.equals("")) {
361             setErrorMessage(null);
362             setMessage(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.projectNameEmpty"));
363             return false;
364         }
365         
366         IStatus nameStatus =
367             workspace.validateName(projectFieldContents, IResource.PROJECT);
368         if (!nameStatus.isOK()) {
369             setErrorMessage(nameStatus.getMessage());
370             return false;
371         }
372     
373         String JavaDoc locationFieldContents = getProjectLocationFieldValue();
374         
375         if (locationFieldContents.equals("")) {
376             setErrorMessage(null);
377             setMessage(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.projectLocationEmpty"));
378             return false;
379         }
380         //Dacha add check end of project root
381
if (!(locationFieldContents.endsWith("\\"+projectFieldContents) || locationFieldContents.endsWith("/"+projectFieldContents))) {
382             setErrorMessage(null);
383             setMessage(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.projectRootNotEndsName"));
384             return false;
385         }
386         //end Dacha
387
IPath path = new Path("");
388         if (!path.isValidPath(locationFieldContents)) {
389             setErrorMessage(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.locationError"));
390             return false;
391         }
392         if (!useDefaults && Platform.getLocation().isPrefixOf(new Path(locationFieldContents))) {
393             setErrorMessage(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.defaultLocationError"));
394             return false;
395         }
396      
397     
398         if (getProjectHandle().exists()) {
399             setErrorMessage(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.projectExistsMessage"));
400             return false;
401         }
402     
403         setErrorMessage(null);
404         setMessage(null);
405         return true;
406     }
407     
408     /*
409      * see @DialogPage.setVisible(boolean)
410      */

411     public void setVisible(boolean visible) {
412         super.setVisible(visible);
413         if(visible)
414             projectNameField.setFocus();
415     }
416
417 }
418
Popular Tags