001: package com.bostechcorp.cbesb.ui.ide.wizards;
002:
003: /*******************************************************************************
004: * Copyright (c) 2000, 2006 IBM Corporation and others.
005: * All rights reserved. This program and the accompanying materials
006: * are made available under the terms of the Eclipse Public License v1.0
007: * which accompanies this distribution, and is available at
008: * http://www.eclipse.org/legal/epl-v10.html
009: *
010: * Contributors:
011: * IBM Corporation - initial API and implementation
012: *******************************************************************************/
013:
014: import java.net.URI;
015: import java.util.regex.Matcher;
016: import java.util.regex.Pattern;
017:
018: import org.eclipse.core.resources.IProject;
019: import org.eclipse.core.resources.IResource;
020: import org.eclipse.core.resources.IWorkspace;
021: import org.eclipse.core.resources.ResourcesPlugin;
022: import org.eclipse.core.runtime.IPath;
023: import org.eclipse.core.runtime.IStatus;
024: import org.eclipse.core.runtime.Path;
025: import org.eclipse.jface.wizard.WizardPage;
026: import org.eclipse.swt.SWT;
027: import org.eclipse.swt.layout.GridData;
028: import org.eclipse.swt.layout.GridLayout;
029: import org.eclipse.swt.widgets.Composite;
030: import org.eclipse.swt.widgets.Event;
031: import org.eclipse.swt.widgets.Label;
032: import org.eclipse.swt.widgets.Listener;
033: import org.eclipse.swt.widgets.Text;
034: import org.eclipse.ui.PlatformUI;
035: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
036: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
037: import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
038: import org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea;
039: import org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea.IErrorMessageReporter;
040:
041: import com.bostechcorp.cbesb.common.i18n.I18N;
042: import com.bostechcorp.cbesb.common.i18n.Messages;
043:
044: /**
045: * Standard main page for a wizard that is creates a project resource.
046: * <p>
047: * This page may be used by clients as-is; it may be also be subclassed to suit.
048: * </p>
049: * <p>
050: * Example usage:
051: *
052: * <pre>
053: * mainPage = new WizardNewProjectCreationPage("basicNewProjectPage");
054: * mainPage.setTitle("Project");
055: * mainPage.setDescription("Create a new project resource.");
056: * </pre>
057: *
058: * </p>
059: */
060: public class WizardNewProjectCreationPage extends WizardPage {
061:
062: // initial value stores
063: private String initialProjectFieldValue;
064:
065: // widgets
066: Text projectNameField;
067:
068: private Listener nameModifyListener = new Listener() {
069: public void handleEvent(Event e) {
070: setLocationForSelection();
071: boolean valid = validatePage();
072: setPageComplete(valid);
073:
074: }
075: };
076:
077: private ProjectContentsLocationArea locationArea;
078:
079: // constants
080: private static final int SIZING_TEXT_FIELD_WIDTH = 250;
081:
082: /**
083: * Creates a new project creation wizard page.
084: *
085: * @param pageName
086: * the name of this page
087: */
088: public WizardNewProjectCreationPage(String pageName) {
089: super (pageName);
090: setPageComplete(false);
091: }
092:
093: /**
094: * (non-Javadoc) Method declared on IDialogPage.
095: */
096: public void createControl(Composite parent) {
097: Composite composite = new Composite(parent, SWT.NULL);
098: composite.setFont(parent.getFont());
099:
100: initializeDialogUnits(parent);
101:
102: PlatformUI.getWorkbench().getHelpSystem().setHelp(composite,
103: IIDEHelpContextIds.NEW_PROJECT_WIZARD_PAGE);
104:
105: composite.setLayout(new GridLayout());
106: composite.setLayoutData(new GridData(GridData.FILL_BOTH));
107:
108: createProjectNameGroup(composite);
109: locationArea = new ProjectContentsLocationArea(
110: getErrorReporter(), composite);
111: if (initialProjectFieldValue != null) {
112: locationArea.updateProjectName(initialProjectFieldValue);
113: }
114:
115: // Scale the button based on the rest of the dialog
116: setButtonLayoutData(locationArea.getBrowseButton());
117:
118: setPageComplete(validatePage());
119: // Show description on opening
120: setErrorMessage(null);
121: setMessage(null);
122: setControl(composite);
123: }
124:
125: /**
126: * Get an error reporter for the receiver.
127: *
128: * @return IErrorMessageReporter
129: */
130: private IErrorMessageReporter getErrorReporter() {
131: return new IErrorMessageReporter() {
132: /*
133: * (non-Javadoc)
134: *
135: * @see org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea.IErrorMessageReporter#reportError(java.lang.String)
136: */
137: public void reportError(String errorMessage) {
138: setErrorMessage(errorMessage);
139: boolean valid = errorMessage == null;
140: if (valid) {
141: valid = validatePage();
142: }
143:
144: setPageComplete(valid);
145: }
146: };
147: }
148:
149: /**
150: * Creates the project name specification controls.
151: *
152: * @param parent
153: * the parent composite
154: */
155: private final void createProjectNameGroup(Composite parent) {
156: // project specification group
157: Composite projectGroup = new Composite(parent, SWT.NONE);
158: GridLayout layout = new GridLayout();
159: layout.numColumns = 2;
160: projectGroup.setLayout(layout);
161: projectGroup.setLayoutData(new GridData(
162: GridData.FILL_HORIZONTAL));
163:
164: // new project label
165: Label projectLabel = new Label(projectGroup, SWT.NONE);
166: projectLabel
167: .setText(IDEWorkbenchMessages.WizardNewProjectCreationPage_nameLabel);
168: projectLabel.setFont(parent.getFont());
169:
170: // new project name entry field
171: projectNameField = new Text(projectGroup, SWT.BORDER);
172: GridData data = new GridData(GridData.FILL_HORIZONTAL);
173: data.widthHint = SIZING_TEXT_FIELD_WIDTH;
174: projectNameField.setLayoutData(data);
175: projectNameField.setFont(parent.getFont());
176:
177: // Set the initial value first before listener
178: // to avoid handling an event during the creation.
179: if (initialProjectFieldValue != null) {
180: projectNameField.setText(initialProjectFieldValue);
181: }
182: projectNameField.addListener(SWT.Modify, nameModifyListener);
183: }
184:
185: /**
186: * Returns the current project location path as entered by the user, or its
187: * anticipated initial value. Note that if the default has been returned the
188: * path in a project description used to create a project should not be set.
189: *
190: * @return the project location path or its anticipated initial value.
191: */
192: public IPath getLocationPath() {
193: return new Path(locationArea.getProjectLocation());
194: }
195:
196: /**
197: * /** Returns the current project location URI as entered by the user, or
198: * <code>null</code> if a valid project location has not been entered.
199: *
200: * @return the project location URI, or <code>null</code>
201: * @since 3.2
202: */
203: public URI getLocationURI() {
204: return locationArea.getProjectLocationURI();
205: }
206:
207: /**
208: * Creates a project resource handle for the current project name field
209: * value.
210: * <p>
211: * This method does not create the project resource; this is the
212: * responsibility of <code>IProject::create</code> invoked by the new
213: * project resource wizard.
214: * </p>
215: *
216: * @return the new project resource handle
217: */
218: public IProject getProjectHandle() {
219: return ResourcesPlugin.getWorkspace().getRoot().getProject(
220: getProjectName());
221: }
222:
223: /**
224: * Returns the current project name as entered by the user, or its
225: * anticipated initial value.
226: *
227: * @return the project name, its anticipated initial value, or
228: * <code>null</code> if no project name is known
229: */
230: public String getProjectName() {
231: if (projectNameField == null) {
232: return initialProjectFieldValue;
233: }
234:
235: return getProjectNameFieldValue();
236: }
237:
238: /**
239: * Returns the value of the project name field with leading and trailing
240: * spaces removed.
241: *
242: * @return the project name in the field
243: */
244: private String getProjectNameFieldValue() {
245: if (projectNameField == null) {
246: return ""; //$NON-NLS-1$
247: }
248:
249: return projectNameField.getText().trim();
250: }
251:
252: /**
253: * Sets the initial project name that this page will use when created. The
254: * name is ignored if the createControl(Composite) method has already been
255: * called. Leading and trailing spaces in the name are ignored. Providing
256: * the name of an existing project will not necessarily cause the wizard to
257: * warn the user. Callers of this method should first check if the project
258: * name passed already exists in the workspace.
259: *
260: * @param name
261: * initial project name for this page
262: *
263: * @see IWorkspace#validateName(String, int)
264: *
265: */
266: public void setInitialProjectName(String name) {
267: if (name == null) {
268: initialProjectFieldValue = null;
269: } else {
270: initialProjectFieldValue = name.trim();
271: if (locationArea != null) {
272: locationArea.updateProjectName(name.trim());
273: }
274: }
275: }
276:
277: /**
278: * Set the location to the default location if we are set to useDefaults.
279: */
280: void setLocationForSelection() {
281: locationArea.updateProjectName(getProjectNameFieldValue());
282: }
283:
284: public static boolean nameValidate(String name) {
285: Pattern pattern = Pattern.compile("([0-9]|([a-z]|[A-Z])|.).*");
286: Matcher matcher = pattern.matcher(name);
287: if (matcher.matches()) {
288: Pattern pattern2 = Pattern.compile("([a-zA-Z0-9]|.)+");
289: Matcher matcher2 = pattern2.matcher(name);
290: if (matcher2.matches()) {
291: if (!name.contains("_")) {
292: return true;
293: } else {
294: return false;
295: }
296: } else
297: return false;
298: } else
299: return false;
300: }
301:
302: /**
303: * Returns whether this page's controls currently all contain valid values.
304: *
305: * @return <code>true</code> if all controls are valid, and
306: * <code>false</code> if at least one is invalid
307: */
308: protected boolean validatePage() {
309:
310: if (!nameValidate(getProjectName())) {
311: setErrorMessage(I18N.getString(Messages.PROJECT_NAME_RULE));
312: return false;
313: }
314:
315: IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace();
316:
317: String projectFieldContents = getProjectNameFieldValue();
318: if (projectFieldContents.equals("")) { //$NON-NLS-1$
319: setErrorMessage(null);
320: setMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectNameEmpty);
321: return false;
322: }
323:
324: IStatus nameStatus = workspace.validateName(
325: projectFieldContents, IResource.PROJECT);
326: if (!nameStatus.isOK()) {
327: setErrorMessage(nameStatus.getMessage());
328: return false;
329: }
330:
331: IProject handle = getProjectHandle();
332: if (handle.exists()) {
333: setErrorMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectExistsMessage);
334: return false;
335: }
336:
337: /*
338: * If not using the default value validate the location.
339: */
340: if (!locationArea.isDefault()) {
341: IStatus locationStatus = workspace
342: .validateProjectLocationURI(handle, locationArea
343: .getProjectLocationURI());
344: if (!locationStatus.isOK()) {
345: setErrorMessage(locationStatus.getMessage());
346: return false;
347: }
348: }
349:
350: setErrorMessage(null);
351: setMessage(null);
352: return true;
353: }
354:
355: /*
356: * see @DialogPage.setVisible(boolean)
357: */
358: public void setVisible(boolean visible) {
359: super .setVisible(visible);
360: if (visible) {
361: projectNameField.setFocus();
362: }
363: }
364:
365: /**
366: * Returns the useDefaults.
367: *
368: * @return boolean
369: */
370: public boolean useDefaults() {
371: return locationArea.isDefault();
372: }
373:
374: }
|