001: package org.enhydra.kelp.eclipse.enhydraWizard;
002:
003: import java.io.File;
004: import java.net.URI;
005: import java.net.URISyntaxException;
006:
007: import org.eclipse.core.resources.IProject;
008: import org.eclipse.core.resources.IResource;
009: import org.eclipse.core.resources.IWorkspace;
010: import org.eclipse.core.resources.ResourcesPlugin;
011: import org.eclipse.core.runtime.IPath;
012: import org.eclipse.core.runtime.IStatus;
013: import org.eclipse.core.runtime.Path;
014: import org.eclipse.core.runtime.Platform;
015: import org.eclipse.jface.resource.ImageDescriptor;
016: import org.eclipse.jface.wizard.WizardPage;
017: import org.eclipse.swt.SWT;
018: import org.eclipse.swt.events.SelectionAdapter;
019: import org.eclipse.swt.events.SelectionEvent;
020: import org.eclipse.swt.events.SelectionListener;
021: import org.eclipse.swt.layout.GridData;
022: import org.eclipse.swt.layout.GridLayout;
023: import org.eclipse.swt.widgets.Button;
024: import org.eclipse.swt.widgets.Composite;
025: import org.eclipse.swt.widgets.DirectoryDialog;
026: import org.eclipse.swt.widgets.Event;
027: import org.eclipse.swt.widgets.Label;
028: import org.eclipse.swt.widgets.Listener;
029: import org.eclipse.swt.widgets.Text;
030: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
031:
032: /**
033: * @author Tweety
034: *
035: * To change this generated comment edit the template variable "typecomment":
036: * Window>Preferences>Java>Templates. To enable and disable the creation of type
037: * comments go to Window>Preferences>Java>Code Generation.
038: */
039: public class NewEnhydraWizardPage extends WizardPage {
040:
041: private boolean useDefaults = true;
042:
043: // initial value stores
044: private String initialProjectFieldValue;
045:
046: private IPath initialLocationFieldValue;
047:
048: // the value the user has entered
049: private String customLocationFieldValue;
050:
051: // widgets
052: private Text projectNameField;
053:
054: private Text locationPathField;
055:
056: private Label locationLabel;
057:
058: private Button browseButton;
059:
060: private Listener nameModifyListener = new Listener() {
061: public void handleEvent(Event e) {
062: setLocationForSelection();
063: setPageComplete(validatePage());
064: }
065: };
066:
067: private Listener locationModifyListener = new Listener() {
068: public void handleEvent(Event e) {
069: setPageComplete(validatePage());
070: }
071: };
072:
073: // constants
074: private static final int SIZING_TEXT_FIELD_WIDTH = 250;
075:
076: private static final int SIZING_INDENTATION_WIDTH = 10;
077:
078: /**
079: * Constructor for NewEnhydraWizardPage.
080: *
081: * @param pageName
082: */
083: public NewEnhydraWizardPage(String pageName) {
084: super (pageName);
085:
086: setPageComplete(false);
087: initialLocationFieldValue = Platform.getLocation();
088: customLocationFieldValue = "";
089: }
090:
091: /**
092: * Constructor for NewEnhydraWizardPage.
093: *
094: * @param pageName
095: * @param title
096: * @param titleImage
097: */
098: public NewEnhydraWizardPage(String pageName, String title,
099: ImageDescriptor titleImage) {
100: super (pageName, title, titleImage);
101:
102: setPageComplete(false);
103: initialLocationFieldValue = Platform.getLocation();
104: customLocationFieldValue = "";
105: }
106:
107: /**
108: * (non-Javadoc) Method declared on IDialogPage.
109: */
110: public void createControl(Composite parent) {
111: Composite composite = new Composite(parent, SWT.NULL);
112:
113: composite.setLayout(new GridLayout());
114: composite.setLayoutData(new GridData(GridData.FILL_BOTH));
115:
116: createProjectNameGroup(composite);
117: createProjectLocationGroup(composite);
118: validatePage();
119:
120: // Show description on opening
121: setErrorMessage(null);
122: setMessage(null);
123: setControl(composite);
124: }
125:
126: /**
127: * Creates the project location specification controls.
128: *
129: * @param parent
130: * the parent composite
131: */
132: private final void createProjectLocationGroup(Composite parent) {
133:
134: // project specification group
135: Composite projectGroup = new Composite(parent, SWT.NONE);
136: GridLayout layout = new GridLayout();
137: layout.numColumns = 3;
138: projectGroup.setLayout(layout);
139: projectGroup.setLayoutData(new GridData(
140: GridData.FILL_HORIZONTAL));
141:
142: // new project label
143: Label projectContentsLabel = new Label(projectGroup, SWT.NONE);
144: projectContentsLabel
145: .setText(NewEnhydraWizardMessages
146: .getString("NewEnhydraWizardPage.projectContentsLabel"));
147:
148: GridData labelData = new GridData();
149: labelData.horizontalSpan = 3;
150: projectContentsLabel.setLayoutData(labelData);
151:
152: final Button useDefaultsButton = new Button(projectGroup,
153: SWT.CHECK | SWT.RIGHT);
154: useDefaultsButton.setText(NewEnhydraWizardMessages
155: .getString("NewEnhydraWizardPage.useDefaultLabel"));
156: useDefaultsButton.setSelection(useDefaults);
157:
158: GridData buttonData = new GridData();
159: buttonData.horizontalSpan = 3;
160: useDefaultsButton.setLayoutData(buttonData);
161:
162: createUserSpecifiedProjectLocationGroup(projectGroup,
163: !useDefaults);
164:
165: SelectionListener listener = new SelectionAdapter() {
166: public void widgetSelected(SelectionEvent e) {
167: useDefaults = useDefaultsButton.getSelection();
168: browseButton.setEnabled(!useDefaults);
169: locationPathField.setEnabled(!useDefaults);
170: locationLabel.setEnabled(!useDefaults);
171: if (useDefaults) {
172: customLocationFieldValue = locationPathField
173: .getText();
174: setLocationForSelection();
175: } else {
176: locationPathField.setText(customLocationFieldValue);
177: }
178: }
179: };
180: useDefaultsButton.addSelectionListener(listener);
181: }
182:
183: /**
184: * Creates the project name specification controls.
185: *
186: * @param parent
187: * the parent composite
188: */
189: private final void createProjectNameGroup(Composite parent) {
190: // project specification group
191: Composite projectGroup = new Composite(parent, SWT.NONE);
192: GridLayout layout = new GridLayout();
193: layout.numColumns = 2;
194: projectGroup.setLayout(layout);
195: projectGroup.setLayoutData(new GridData(
196: GridData.FILL_HORIZONTAL));
197:
198: // new project label
199: Label projectLabel = new Label(projectGroup, SWT.NONE);
200: projectLabel.setText(NewEnhydraWizardMessages
201: .getString("NewEnhydraWizardPage.nameLabel"));
202:
203: // new project name entry field
204: projectNameField = new Text(projectGroup, SWT.BORDER);
205: GridData data = new GridData(GridData.FILL_HORIZONTAL);
206: data.widthHint = SIZING_TEXT_FIELD_WIDTH;
207: projectNameField.setLayoutData(data);
208:
209: // Set the initial value first before listener
210: // to avoid handling an event during the creation.
211: if (initialProjectFieldValue != null)
212: projectNameField.setText(initialProjectFieldValue);
213: projectNameField.addListener(SWT.Modify, nameModifyListener);
214: }
215:
216: /**
217: * Creates the project location specification controls.
218: *
219: * @param projectGroup
220: * the parent composite
221: * @param boolean -
222: * the initial enabled state of the widgets created
223: */
224: private void createUserSpecifiedProjectLocationGroup(
225: Composite projectGroup, boolean enabled) {
226:
227: // location label
228: locationLabel = new Label(projectGroup, SWT.NONE);
229: locationLabel.setText(NewEnhydraWizardMessages
230: .getString("NewEnhydraWizardPage.locationLabel"));
231: locationLabel.setEnabled(enabled);
232:
233: // project location entry field
234: locationPathField = new Text(projectGroup, SWT.BORDER);
235: GridData data = new GridData(GridData.FILL_HORIZONTAL);
236: data.widthHint = SIZING_TEXT_FIELD_WIDTH;
237: locationPathField.setLayoutData(data);
238: locationPathField.setEnabled(enabled);
239:
240: // browse button
241: browseButton = new Button(projectGroup, SWT.PUSH);
242: browseButton.setText(NewEnhydraWizardMessages
243: .getString("NewEnhydraWizardPage.browseLabel"));
244: browseButton.addSelectionListener(new SelectionAdapter() {
245: public void widgetSelected(SelectionEvent event) {
246: handleLocationBrowseButtonPressed();
247: }
248: });
249:
250: browseButton.setEnabled(enabled);
251:
252: // Set the initial value first before listener
253: // to avoid handling an event during the creation.
254: if (initialLocationFieldValue != null)
255: locationPathField.setText(initialLocationFieldValue
256: .toOSString());
257: locationPathField.addListener(SWT.Modify,
258: locationModifyListener);
259: }
260:
261: /**
262: * Returns the current project location path as entered by
263: * the user, or its anticipated initial value.
264: * Note that if the default has been returned the path
265: * in a project description used to create a project
266: * should not be set.
267: *
268: * @return the project location path, its anticipated initial value, or <code>null</code>
269: * if no project location path is known
270: */
271: public IPath getLocationPath() {
272: if (useDefaults)
273: return initialLocationFieldValue;
274:
275: return new Path(getProjectLocationFieldValue());
276: }
277:
278: /**
279: * Returns the current project location path as entered by the user, or its
280: * anticipated initial value. Note that if the default has been returned the
281: * path in a project description used to create a project should not be set.
282: *
283: * @return the project location path, its anticipated initial value, or
284: * <code>null</code> if no project location path is known
285: */
286: public URI getLocationURI() {
287: try {
288: if (useDefaults) {
289: System.out
290: .println("1. initialLocationFieldValue.toOSString() - "
291: + initialLocationFieldValue
292: .toOSString());
293: return new URI(initialLocationFieldValue.toOSString());
294: }
295: System.out.println("2. getProjectLocationFieldValue() - "
296: + getProjectLocationFieldValue());
297: return new URI(getProjectLocationFieldValue());
298:
299: } catch (URISyntaxException usex) {
300: usex.printStackTrace();
301: return null;
302: }
303: }
304:
305: /**
306: * Creates a project resource handle for the current project name field
307: * value.
308: * <p>
309: * This method does not create the project resource; this is the
310: * responsibility of <code>IProject::create</code> invoked by the new
311: * project resource wizard.
312: * </p>
313: *
314: * @return the new project resource handle
315: */
316: public IProject getProjectHandle() {
317: return ResourcesPlugin.getWorkspace().getRoot().getProject(
318: getProjectName());
319: }
320:
321: /**
322: * Returns the current project name as entered by the user, or its
323: * anticipated initial value.
324: *
325: * @return the project name, its anticipated initial value, or
326: * <code>null</code> if no project name is known
327: */
328: public String getProjectName() {
329: if (projectNameField == null)
330: return initialProjectFieldValue;
331:
332: return getProjectNameFieldValue();
333: }
334:
335: /**
336: * Returns the value of the project name field with leading and trailing
337: * spaces removed.
338: *
339: * @return the project name in the field
340: */
341: private String getProjectNameFieldValue() {
342: if (projectNameField == null)
343: return "";
344: else
345: return projectNameField.getText().trim();
346: }
347:
348: /**
349: * Returns the value of the project location field with leading and trailing
350: * spaces removed.
351: *
352: * @return the project location directory in the field
353: */
354: private String getProjectLocationFieldValue() {
355: if (locationPathField == null)
356: return "";
357: else
358: return locationPathField.getText().trim();
359: }
360:
361: /**
362: * Open an appropriate directory browser
363: */
364: private void handleLocationBrowseButtonPressed() {
365: DirectoryDialog dialog = new DirectoryDialog(locationPathField
366: .getShell());
367: dialog.setMessage(NewEnhydraWizardMessages
368: .getString("NewEnhydraWizardPage.directoryLabel"));
369:
370: String dirName = getProjectLocationFieldValue();
371: if (!dirName.equals("")) {
372: File path = new File(dirName);
373: if (path.exists())
374: dialog.setFilterPath(new Path(dirName).toOSString());
375: }
376:
377: String selectedDirectory = dialog.open();
378: if (selectedDirectory != null) {
379: customLocationFieldValue = selectedDirectory;
380: locationPathField.setText(customLocationFieldValue);
381: }
382: }
383:
384: /**
385: * Sets the initial project name that this page will use when created. The
386: * name is ignored if the createControl(Composite) method has already been
387: * called. Leading and trailing spaces in the name are ignored.
388: *
389: * @param name
390: * initial project name for this page
391: */
392: public void setInitialProjectName(String name) {
393: if (name == null)
394: initialProjectFieldValue = null;
395: else
396: initialProjectFieldValue = name.trim();
397: }
398:
399: /**
400: * Set the location to the default location if we are set to useDefaults.
401: */
402: private void setLocationForSelection() {
403: if (useDefaults) {
404: IPath defaultPath = Platform.getLocation().append(
405: getProjectNameFieldValue());
406: locationPathField.setText(defaultPath.toOSString());
407: }
408: }
409:
410: /**
411: * Returns whether this page's controls currently all contain valid values.
412: *
413: * @return <code>true</code> if all controls are valid, and
414: * <code>false</code> if at least one is invalid
415: */
416: private boolean validatePage() {
417: // SLOBODAN IWorkspace workspace = WorkbenchPlugin.getPluginWorkspace();
418: IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace();
419: String projectFieldContents = getProjectNameFieldValue();
420: if (projectFieldContents.equals("")) {
421: setErrorMessage(null);
422: setMessage(NewEnhydraWizardMessages
423: .getString("NewEnhydraWizardPage.projectNameEmpty"));
424: return false;
425: }
426: IStatus nameStatus = workspace.validateName(
427: projectFieldContents, IResource.PROJECT);
428: if (!nameStatus.isOK()) {
429: setErrorMessage(nameStatus.getMessage());
430: return false;
431: }
432: String locationFieldContents = getProjectLocationFieldValue();
433: if (locationFieldContents.equals("")) {
434: setErrorMessage(null);
435: setMessage(NewEnhydraWizardMessages
436: .getString("NewEnhydraWizardPage.projectLocationEmpty"));
437: return false;
438: }
439: //Dacha add check end of project root
440: if (!(locationFieldContents.endsWith("\\"
441: + projectFieldContents) || locationFieldContents
442: .endsWith("/" + projectFieldContents))) {
443: setErrorMessage(null);
444: setMessage(NewEnhydraWizardMessages
445: .getString("NewEnhydraWizardPage.projectRootNotEndsName"));
446: return false;
447: }
448: //end Dacha
449: IPath path = new Path("");
450: if (!path.isValidPath(locationFieldContents)) {
451: setErrorMessage(NewEnhydraWizardMessages
452: .getString("NewEnhydraWizardPage.locationError"));
453: return false;
454: }
455: if (!useDefaults
456: && Platform.getLocation().isPrefixOf(
457: new Path(locationFieldContents))) {
458: setErrorMessage(NewEnhydraWizardMessages
459: .getString("NewEnhydraWizardPage.defaultLocationError"));
460: return false;
461: }
462:
463: if (getProjectHandle().exists()) {
464: setErrorMessage(NewEnhydraWizardMessages
465: .getString("NewEnhydraWizardPage.projectExistsMessage"));
466: return false;
467: }
468:
469: setErrorMessage(null);
470: setMessage(null);
471: return true;
472: }
473:
474: /*
475: * see @DialogPage.setVisible(boolean)
476: */
477: public void setVisible(boolean visible) {
478: super.setVisible(visible);
479: if (visible)
480: projectNameField.setFocus();
481: }
482:
483: }
|