001: /**
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id$
023: */package com.bostechcorp.cbesb.ui.ide.customcomponent.wizard;
024:
025: import java.net.URI;
026: import java.net.URL;
027: import java.util.HashMap;
028: import java.util.regex.Matcher;
029: import java.util.regex.Pattern;
030:
031: import org.eclipse.core.resources.IProject;
032: import org.eclipse.core.resources.IResource;
033: import org.eclipse.core.resources.IWorkspace;
034: import org.eclipse.core.resources.ResourcesPlugin;
035: import org.eclipse.core.runtime.IPath;
036: import org.eclipse.core.runtime.IStatus;
037: import org.eclipse.core.runtime.Path;
038: import org.eclipse.jface.wizard.IWizardPage;
039: import org.eclipse.jface.wizard.WizardPage;
040: import org.eclipse.swt.SWT;
041: import org.eclipse.swt.events.ModifyEvent;
042: import org.eclipse.swt.events.ModifyListener;
043: import org.eclipse.swt.layout.GridData;
044: import org.eclipse.swt.layout.GridLayout;
045: import org.eclipse.swt.widgets.Composite;
046: import org.eclipse.swt.widgets.Event;
047: import org.eclipse.swt.widgets.Label;
048: import org.eclipse.swt.widgets.Listener;
049: import org.eclipse.swt.widgets.Text;
050: import org.eclipse.ui.PlatformUI;
051: import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
052: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
053: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
054: import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
055: import org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea;
056: import org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea.IErrorMessageReporter;
057:
058: import com.bostechcorp.cbesb.common.i18n.I18N;
059: import com.bostechcorp.cbesb.common.i18n.Messages;
060: import com.bostechcorp.cbesb.common.util.custcomponent.ICustComponent.Role;
061:
062: /**
063: * @author LPS
064: */
065: public class CBCCProjectCreationWizardPage extends WizardPage {
066: // initial value stores
067: private String initialProjectFieldValue;
068:
069: // widgets
070: Text projectNameField;
071: private ComponentPropertiesComposite propertiesComposite;
072:
073: private Listener nameModifyListener = new Listener() {
074: public void handleEvent(Event e) {
075: setLocationForSelection();
076: boolean valid = validatePage();
077: setPageComplete(valid);
078:
079: }
080: };
081:
082: private ModifyListener textModifyListener = new ModifyListener() {
083: public void modifyText(ModifyEvent e) {
084: setPageComplete(validatePage());
085: }
086: };
087:
088: private ProjectContentsLocationArea locationArea;
089:
090: private HashMap<String, String> properties = new HashMap<String, String>();
091:
092: // constants
093: private static final int SIZING_TEXT_FIELD_WIDTH = 250;
094:
095: protected CBCCProjectCreationWizardPage(String pageName) {
096: super (pageName);
097: setTitle(I18N.getString(Messages.CBCC_PROPERTIES_PAGE_TITLE));
098: setDescription(I18N
099: .getString(Messages.CBCC_PROPERTIES_PAGE_DESCRIPTION));
100: }
101:
102: /**
103: * (non-Javadoc) Method declared on IDialogPage.
104: */
105: public void createControl(Composite parent) {
106: //TODOO should be inserted the new COmposite
107: Composite composite = new Composite(parent, SWT.NULL);
108: composite.setFont(parent.getFont());
109:
110: initializeDialogUnits(parent);
111:
112: PlatformUI.getWorkbench().getHelpSystem().setHelp(composite,
113: IIDEHelpContextIds.NEW_PROJECT_WIZARD_PAGE);
114:
115: composite.setLayout(new GridLayout());
116: composite.setLayoutData(new GridData(GridData.FILL_BOTH));
117:
118: createProjectNameGroup(composite);
119: locationArea = new ProjectContentsLocationArea(
120: getErrorReporter(), composite);
121: if (initialProjectFieldValue != null) {
122: locationArea.updateProjectName(initialProjectFieldValue);
123: }
124:
125: // Scale the button based on the rest of the dialog
126: setButtonLayoutData(locationArea.getBrowseButton());
127:
128: //Adittional custom component controls
129: propertiesComposite = new ComponentPropertiesComposite(
130: composite, SWT.NONE, properties);
131: propertiesComposite.setLayoutData(new GridData(SWT.FILL,
132: SWT.FILL, false, true));
133: propertiesComposite.setLayout(new GridLayout());
134: //some listeners for other properties
135: propertiesComposite.getText_1().addModifyListener(
136: textModifyListener);
137: propertiesComposite.getText_3().addModifyListener(
138: textModifyListener);
139: propertiesComposite.getText_5().addModifyListener(
140: textModifyListener);
141: propertiesComposite.getText_6().addModifyListener(
142: textModifyListener);
143: propertiesComposite.getText_7().addModifyListener(
144: textModifyListener);
145: propertiesComposite.getText().addModifyListener(
146: textModifyListener);
147:
148: setPageComplete(validatePage());
149: // Show description on opening
150: setErrorMessage(null);
151: setMessage(null);
152: setControl(composite);
153:
154: }
155:
156: /**
157: * Get an error reporter for the receiver.
158: *
159: * @return IErrorMessageReporter
160: */
161: private IErrorMessageReporter getErrorReporter() {
162: return new IErrorMessageReporter() {
163: /*
164: * (non-Javadoc)
165: *
166: * @see org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea.IErrorMessageReporter#reportError(java.lang.String)
167: */
168: public void reportError(String errorMessage) {
169: setErrorMessage(errorMessage);
170: boolean valid = errorMessage == null;
171: if (valid) {
172: valid = validatePage();
173: }
174:
175: setPageComplete(valid);
176: }
177: };
178: }
179:
180: /**
181: * Creates the project name specification controls.
182: *
183: * @param parent
184: * the parent composite
185: */
186: private final void createProjectNameGroup(Composite parent) {
187: // project specification group
188: Composite projectGroup = new Composite(parent, SWT.NONE);
189: GridLayout layout = new GridLayout();
190: layout.numColumns = 2;
191: projectGroup.setLayout(layout);
192: projectGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
193: true, false));
194:
195: // new project label
196: Label projectLabel = new Label(projectGroup, SWT.NONE);
197: projectLabel
198: .setText(IDEWorkbenchMessages.WizardNewProjectCreationPage_nameLabel);
199: projectLabel.setFont(parent.getFont());
200:
201: // new project name entry field
202: projectNameField = new Text(projectGroup, SWT.BORDER);
203: GridData data = new GridData(GridData.FILL_HORIZONTAL);
204: data.widthHint = SIZING_TEXT_FIELD_WIDTH;
205: projectNameField.setLayoutData(data);
206: projectNameField.setFont(parent.getFont());
207:
208: // Set the initial value first before listener
209: // to avoid handling an event during the creation.
210: if (initialProjectFieldValue != null) {
211: projectNameField.setText(initialProjectFieldValue);
212: }
213: projectNameField.addListener(SWT.Modify, nameModifyListener);
214: }
215:
216: /**
217: * Returns the current project location path as entered by the user, or its
218: * anticipated initial value. Note that if the default has been returned the
219: * path in a project description used to create a project should not be set.
220: *
221: * @return the project location path or its anticipated initial value.
222: */
223: public IPath getLocationPath() {
224: return new Path(locationArea.getProjectLocation());
225: }
226:
227: /**
228: * /** Returns the current project location URI as entered by the user, or
229: * <code>null</code> if a valid project location has not been entered.
230: *
231: * @return the project location URI, or <code>null</code>
232: * @since 3.2
233: */
234: public URI getLocationURI() {
235: return locationArea.getProjectLocationURI();
236: }
237:
238: /**
239: * Creates a project resource handle for the current project name field
240: * value.
241: * <p>
242: * This method does not create the project resource; this is the
243: * responsibility of <code>IProject::create</code> invoked by the new
244: * project resource wizard.
245: * </p>
246: *
247: * @return the new project resource handle
248: */
249: public IProject getProjectHandle() {
250: return ResourcesPlugin.getWorkspace().getRoot().getProject(
251: getProjectName());
252: }
253:
254: /**
255: * Returns the current project name as entered by the user, or its
256: * anticipated initial value.
257: *
258: * @return the project name, its anticipated initial value, or
259: * <code>null</code> if no project name is known
260: */
261: public String getProjectName() {
262: if (projectNameField == null) {
263: return initialProjectFieldValue;
264: }
265:
266: return getProjectNameFieldValue();
267: }
268:
269: /**
270: * Returns the value of the project name field with leading and trailing
271: * spaces removed.
272: *
273: * @return the project name in the field
274: */
275: private String getProjectNameFieldValue() {
276: if (projectNameField == null) {
277: return ""; //$NON-NLS-1$
278: }
279:
280: return projectNameField.getText().trim();
281: }
282:
283: /**
284: * Sets the initial project name that this page will use when created. The
285: * name is ignored if the createControl(Composite) method has already been
286: * called. Leading and trailing spaces in the name are ignored. Providing
287: * the name of an existing project will not necessarily cause the wizard to
288: * warn the user. Callers of this method should first check if the project
289: * name passed already exists in the workspace.
290: *
291: * @param name
292: * initial project name for this page
293: * @see IWorkspace#validateName(String, int)
294: */
295: public void setInitialProjectName(String name) {
296: if (name == null) {
297: initialProjectFieldValue = null;
298: } else {
299: initialProjectFieldValue = name.trim();
300: if (locationArea != null) {
301: locationArea.updateProjectName(name.trim());
302: }
303: }
304: }
305:
306: /**
307: * Set the location to the default location if we are set to useDefaults.
308: */
309: void setLocationForSelection() {
310: locationArea.updateProjectName(getProjectNameFieldValue());
311: }
312:
313: public static boolean nameValidate(String name) {
314: Pattern pattern = Pattern.compile("([0-9]|([a-z]|[A-Z])|.).*");
315: Matcher matcher = pattern.matcher(name);
316: if (matcher.matches()) {
317: Pattern pattern2 = Pattern.compile("([a-zA-Z0-9]|.)+");
318: Matcher matcher2 = pattern2.matcher(name);
319: if (matcher2.matches()) {
320: return true;
321: } else
322: return false;
323: } else
324: return false;
325: }
326:
327: /**
328: * Returns whether this page's controls currently all contain valid values.
329: *
330: * @return <code>true</code> if all controls are valid, and
331: * <code>false</code> if at least one is invalid
332: */
333: protected boolean validatePage() {
334: if (!nameValidate(getProjectName())) {
335: setErrorMessage(I18N.getString(Messages.PROJECT_NAME_RULE));
336: return false;
337: }
338: IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace();
339:
340: String projectFieldContents = getProjectNameFieldValue();
341: if (projectFieldContents.equals("")) { //$NON-NLS-1$
342: setErrorMessage(null);
343: setMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectNameEmpty);
344: return false;
345: }
346:
347: IStatus nameStatus = workspace.validateName(
348: projectFieldContents, IResource.PROJECT);
349: if (!nameStatus.isOK()) {
350: setErrorMessage(nameStatus.getMessage());
351: return false;
352: }
353:
354: IProject handle = getProjectHandle();
355: if (handle.exists()) {
356: setErrorMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectExistsMessage);
357: return false;
358: }
359:
360: /*
361: * If not using the default value validate the location.
362: */
363: if (!locationArea.isDefault()) {
364: IStatus locationStatus = workspace
365: .validateProjectLocationURI(handle, locationArea
366: .getProjectLocationURI());
367: if (!locationStatus.isOK()) {
368: setErrorMessage(locationStatus.getMessage());
369: return false;
370: }
371: }
372:
373: //Validate Custom component Controls
374: if (!validCustomControls()) {
375: return false;
376: }
377:
378: setErrorMessage(null);
379: setMessage(null);
380: return true;
381: }
382:
383: private boolean validCustomControls() {
384: if (propertiesComposite.getText_1().getText().trim().equals("")) {
385: setErrorMessage(I18N
386: .getString(Messages.CBCC_ERROR_NAME_EMPTY));
387: return false;
388: }
389: if (propertiesComposite.getText_3().getText().trim().equals("")) {
390: setErrorMessage(I18N
391: .getString(Messages.CBCC_ERROR_COMPONENT_NAME_EMPTY));
392: return false;
393: }
394: if (propertiesComposite.getText().getText().trim().equals("")) {
395: setErrorMessage(I18N
396: .getString(Messages.CBCC_ERROR_COMPONENT_NAME_EMPTY));
397: return false;
398: }
399: if (propertiesComposite.getText_5().getText().trim().equals("")) {
400: setErrorMessage(I18N
401: .getString(Messages.CBCC_ERROR_URL_EMPTY));
402: return false;
403: } else {
404: try {
405: new URL(propertiesComposite.getText_5().getText());
406: new URI(propertiesComposite.getText_5().getText());
407: } catch (Exception e) {
408: setErrorMessage(I18N
409: .getString(Messages.CBCC_ERROR_URL_MALFORMED));
410: return false;
411: }
412: }
413: if (propertiesComposite.getText_6().getText().trim().equals("")) {
414: setErrorMessage(I18N
415: .getString(Messages.CBCC_ERROR_VENDOR_EMPTY));
416: return false;
417: }
418: if (propertiesComposite.getText_7().getText().trim().equals("")) {
419: setErrorMessage(I18N
420: .getString(Messages.CBCC_ERROR_VERSION_EMPTY));
421: return false;
422: }
423: propertiesComposite.viewToModel();
424: return true;
425: }
426:
427: /*
428: * see @DialogPage.setVisible(boolean)
429: */
430: public void setVisible(boolean visible) {
431: super .setVisible(visible);
432: if (visible) {
433: projectNameField.setFocus();
434: }
435: }
436:
437: /**
438: * Returns the useDefaults.
439: *
440: * @return boolean
441: */
442: public boolean useDefaults() {
443: return locationArea.isDefault();
444: }
445:
446: public HashMap<String, String> getProperties() {
447: return propertiesComposite.getProperties();
448: }
449:
450: @Override
451: public IWizardPage getNextPage() {
452: if (getRole().equals(Role.CONSUMER)) {
453: return getWizard().getPage("ConsumerPage");
454: } else if (getRole().equals(Role.PROVIDER)) {
455: return getWizard().getPage("ProviderPage");
456: }
457: return super .getNextPage();
458: }
459:
460: public Role getRole() {
461: if (propertiesComposite.getCombo().getText().equals(
462: ComponentPropertiesComposite.BOTH)) {
463: return Role.BOTH;
464: } else if (propertiesComposite.getCombo().getText().equals(
465: ComponentPropertiesComposite.CONSUMER)) {
466: return Role.CONSUMER;
467:
468: } else if (propertiesComposite.getCombo().getText().equals(
469: ComponentPropertiesComposite.PROVIDER)) {
470: return Role.PROVIDER;
471: }
472: return Role.BOTH;
473: }
474: }
|