001: /*
002: * uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004, Refractions Research Inc. This
003: * library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
004: * License as published by the Free Software Foundation; version 2.1 of the License. This library is distributed in the
005: * hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
006: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
007: */
008: package net.refractions.udig.project.ui.internal.wizard;
009:
010: import java.io.File;
011: import java.net.MalformedURLException;
012: import java.net.URL;
013:
014: import net.refractions.udig.project.ui.internal.ImageConstants;
015: import net.refractions.udig.project.ui.internal.Images;
016: import net.refractions.udig.project.ui.internal.Messages;
017:
018: import org.eclipse.core.runtime.IPath;
019: import org.eclipse.core.runtime.Platform;
020: import org.eclipse.jface.preference.DirectoryFieldEditor;
021: import org.eclipse.jface.preference.StringFieldEditor;
022: import org.eclipse.jface.wizard.WizardPage;
023: import org.eclipse.swt.SWT;
024: import org.eclipse.swt.layout.GridData;
025: import org.eclipse.swt.widgets.Composite;
026: import org.eclipse.swt.widgets.Text;
027:
028: /**
029: * A wizard page to create a new project.
030: *
031: * @author vitalus
032: *
033: * @since 0.3
034: */
035: public class NewProjectWizardPage extends WizardPage {
036:
037: DirectoryFieldEditor projectDirectoryEditor;
038:
039: StringFieldEditor projectNameEditor;
040:
041: /**
042: * Construct <code>NewProjectWizardPage</code>.
043: */
044: public NewProjectWizardPage() {
045: super (Messages.NewProjectWizardPage_newProject,
046: Messages.NewProjectWizardPage_newProject,
047: Images.getDescriptor(ImageConstants.NEWPROJECT_WIZBAN));
048: setDescription(Messages.NewProjectWizardPage_newProject_description);
049: }
050:
051: /**
052: * Set up this page for use.
053: *
054: * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
055: * @param parent
056: */
057: public void createControl(Composite parent) {
058:
059: Composite composite = new Composite(parent, SWT.NONE);
060:
061: projectNameEditor = new StringFieldEditor(
062: "newproject.name", Messages.NewProjectWizardPage_label_projectName, composite); //$NON-NLS-1$
063: projectNameEditor.setPage(this );
064: projectNameEditor
065: .setValidateStrategy(StringFieldEditor.VALIDATE_ON_KEY_STROKE);
066: Text textControl = projectNameEditor.getTextControl(composite);
067: GridData gd = new GridData(SWT.LEFT, SWT.NONE, false, false);
068: gd.widthHint = 100;
069: gd.horizontalSpan = 2;
070: textControl.setLayoutData(gd);
071:
072: projectDirectoryEditor = new DirectoryFieldEditor(
073: "newproject.directory", Messages.NewProjectWizardPage_label_projectDir, composite); //$NON-NLS-1$
074: projectDirectoryEditor.setPage(this );
075: projectDirectoryEditor.fillIntoGrid(composite, 3);
076:
077: String defaultProjectName = Messages.NewProjectWizardPage_default_name;
078:
079: final IPath homepath = Platform.getLocation();
080: String projectPath = new File(homepath.toString()
081: + File.separatorChar + defaultProjectName + ".udig").getAbsolutePath(); //$NON-NLS-1$
082:
083: projectNameEditor.setStringValue(defaultProjectName);
084: projectDirectoryEditor.setStringValue(projectPath);
085:
086: composite.pack();
087:
088: setControl(composite);
089: setPageComplete(true);
090: }
091:
092: /**
093: * Returns specified project name.
094: *
095: * @return
096: */
097: public String getProjectName() {
098: return projectNameEditor.getStringValue();
099: }
100:
101: /**
102: * Returns specified project path.
103: *
104: * @return
105: */
106: public String getProjectPath() {
107: return projectDirectoryEditor.getStringValue();
108: }
109:
110: /**
111: * Validates the form with project name and path.
112: *
113: * @return
114: * <code>true</code> if valid
115: */
116: public boolean validate() {
117:
118: if (!projectNameEditor.isValid()) {
119: setErrorMessage(Messages.NewProjectWizardPage_err_project_name);
120: return false;
121: }
122:
123: final String projectPath = getProjectPath();
124: final String projectName = getProjectName();
125:
126: if (projectPath == null || projectPath.length() == 0) {
127: setErrorMessage(Messages.NewProjectWizardPage_err_project_dir_valid);
128: return false;
129: }
130:
131: File projectPathFolder = null;
132: try {
133: URL projectURL = new URL("file:///" + projectPath); //$NON-NLS-1$
134: projectPathFolder = new File(projectURL.getFile());
135:
136: String absolutePath = projectPathFolder.getAbsolutePath();
137: if (!projectPath.equals(absolutePath)) {
138: setErrorMessage(Messages.NewProjectWizardPage_err_project_dir_absolute);
139: return false;
140: }
141:
142: } catch (MalformedURLException e) {
143: e.printStackTrace();
144: return false;
145: }
146:
147: if (projectPathFolder.exists()) {
148: String projectFileAbsolutePath = projectPathFolder
149: .getAbsolutePath()
150: + File.separatorChar + "project.uprj"; //$NON-NLS-1$;
151: File projectFile = new File(projectFileAbsolutePath);
152: if (projectFile.exists()) {
153: setErrorMessage(Messages.NewProjectWizardPage_err_project_exists);
154: return false;
155: }
156: }
157:
158: if (projectName == null || projectName.length() == 0) {
159: setErrorMessage(Messages.NewProjectWizardPage_err_project_name);
160: return false;
161: }
162: return true;
163: }
164:
165: }
|