Validation was added to new project creation wizard
authorEugene Ostroukhov <eostroukhov@symbian.org>
Mon, 11 Jan 2010 13:15:16 -0800
changeset 17 f1b0259bb410
parent 16 718b6c524eba
child 18 f887dc7c2ae0
Validation was added to new project creation wizard
org.symbian.tools.wrttools/bin/org/symbian/tools/wrttools/util/CompoundValidator.class
org.symbian.tools.wrttools/bin/org/symbian/tools/wrttools/util/RegexpValidator.class
org.symbian.tools.wrttools/bin/org/symbian/tools/wrttools/util/Util.class
org.symbian.tools.wrttools/bin/org/symbian/tools/wrttools/wizards/AbstractDataBindingPage$NonEmptyStringValidator.class
org.symbian.tools.wrttools/bin/org/symbian/tools/wrttools/wizards/AbstractDataBindingPage.class
org.symbian.tools.wrttools/bin/org/symbian/tools/wrttools/wizards/WRTProjectDetailsWizardPage.class
org.symbian.tools.wrttools/bin/org/symbian/tools/wrttools/wizards/WRTProjectFilesWizardPage.class
org.symbian.tools.wrttools/bin/org/symbian/tools/wrttools/wizards/WizardContext.class
org.symbian.tools.wrttools/bin/org/symbian/tools/wrttools/wizards/WrtWidgetWizard.class
org.symbian.tools.wrttools/plugin.xml
org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/util/CompoundValidator.java
org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/util/RegexpValidator.java
org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/util/Util.java
org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/wizards/AbstractDataBindingPage.java
org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/wizards/WRTProjectDetailsWizardPage.java
org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/wizards/WizardContext.java
org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/wizards/WrtWidgetWizard.java
Binary file org.symbian.tools.wrttools/bin/org/symbian/tools/wrttools/util/CompoundValidator.class has changed
Binary file org.symbian.tools.wrttools/bin/org/symbian/tools/wrttools/util/RegexpValidator.class has changed
Binary file org.symbian.tools.wrttools/bin/org/symbian/tools/wrttools/util/Util.class has changed
Binary file org.symbian.tools.wrttools/bin/org/symbian/tools/wrttools/wizards/AbstractDataBindingPage$NonEmptyStringValidator.class has changed
Binary file org.symbian.tools.wrttools/bin/org/symbian/tools/wrttools/wizards/AbstractDataBindingPage.class has changed
Binary file org.symbian.tools.wrttools/bin/org/symbian/tools/wrttools/wizards/WRTProjectDetailsWizardPage.class has changed
Binary file org.symbian.tools.wrttools/bin/org/symbian/tools/wrttools/wizards/WRTProjectFilesWizardPage.class has changed
Binary file org.symbian.tools.wrttools/bin/org/symbian/tools/wrttools/wizards/WizardContext.class has changed
Binary file org.symbian.tools.wrttools/bin/org/symbian/tools/wrttools/wizards/WrtWidgetWizard.class has changed
--- a/org.symbian.tools.wrttools/plugin.xml	Fri Jan 08 17:41:28 2010 -0800
+++ b/org.symbian.tools.wrttools/plugin.xml	Mon Jan 11 13:15:16 2010 -0800
@@ -70,6 +70,16 @@
             Creates a new Symbian WRT widget project using one of the provided templates
          </description>
       </wizard>
+      <wizard
+            category="org.symbian.tools.wrttools.core.category"
+            class="org.symbian.tools.wrttools.wizards.AptanaProjectsImportWizard"
+            finalPerspective="org.eclipse.wst.jsdt.ui.JavaPerspective"
+            hasPages="true"
+            icon="icons/main16.gif"
+            id="org.symbian.tools.wrttools.newImportAptanaProject"
+            name="Import Aptana IDE WRT Project"
+            project="true">
+      </wizard>
    </extension>
    <extension
          point="org.symbian.tools.wrttools.projectTemplates">
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/util/CompoundValidator.java	Mon Jan 11 13:15:16 2010 -0800
@@ -0,0 +1,57 @@
+/**
+ * Copyright (c) 2010 Symbian Foundation and/or its subsidiary(-ies).
+ * All rights reserved.
+ * This component and the accompanying materials are made available
+ * under the terms of the License "Eclipse Public License v1.0"
+ * which accompanies this distribution, and is available
+ * at the URL "http://www.eclipse.org/legal/epl-v10.html".
+ *
+ * Initial Contributors:
+ * Symbian Foundation - initial contribution.
+ * Contributors:
+ * Description:
+ * Overview:
+ * Details:
+ * Platforms/Drives/Compatibility:
+ * Assumptions/Requirement/Pre-requisites:
+ * Failures and causes:
+ */
+package org.symbian.tools.wrttools.util;
+
+import org.eclipse.core.databinding.validation.IValidator;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.symbian.tools.wrttools.wizards.AbstractDataBindingPage.NonEmptyStringValidator;
+
+
+public class CompoundValidator implements IValidator {
+	private final IValidator[] validators;
+
+	public CompoundValidator(IValidator ... validators) {
+		this.validators = validators;
+	}
+	
+	public CompoundValidator(NonEmptyStringValidator validator,
+			IValidator[] validators) {
+		this.validators = new IValidator[validators.length + 1];
+		this.validators[0] = validator;
+		System.arraycopy(validators, 0, this.validators, 1, validators.length);
+	}
+
+	@Override
+	public IStatus validate(Object value) {
+		IStatus status = Status.OK_STATUS;
+		
+		for (IValidator validator : validators) {
+			IStatus s = validator.validate(value);
+			switch (s.getSeverity()) {
+			case IStatus.ERROR:
+				return s;
+			case IStatus.WARNING:
+				status = s;
+			}
+		}
+		return status;
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/util/RegexpValidator.java	Mon Jan 11 13:15:16 2010 -0800
@@ -0,0 +1,54 @@
+/**
+ * Copyright (c) 2010 Symbian Foundation and/or its subsidiary(-ies).
+ * All rights reserved.
+ * This component and the accompanying materials are made available
+ * under the terms of the License "Eclipse Public License v1.0"
+ * which accompanies this distribution, and is available
+ * at the URL "http://www.eclipse.org/legal/epl-v10.html".
+ *
+ * Initial Contributors:
+ * Symbian Foundation - initial contribution.
+ * Contributors:
+ * Description:
+ * Overview:
+ * Details:
+ * Platforms/Drives/Compatibility:
+ * Assumptions/Requirement/Pre-requisites:
+ * Failures and causes:
+ */
+package org.symbian.tools.wrttools.util;
+
+import java.text.MessageFormat;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.eclipse.core.databinding.validation.IValidator;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.symbian.tools.wrttools.Activator;
+
+public class RegexpValidator implements IValidator {
+	private final String errorMessage;
+	private Pattern pattern;
+	private final boolean match;
+
+	public RegexpValidator(String pattern, String errorMessage, boolean match) {
+		this.errorMessage = errorMessage;
+		this.match = match;
+		this.pattern = Pattern.compile(pattern);
+	}
+
+	@Override
+	public IStatus validate(Object value) {
+		String string = value.toString();
+		Matcher matcher = pattern.matcher(string);
+		if (match && !matcher.matches()) {
+			return new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format(errorMessage, string));
+		} else if (!match && matcher.find()) {
+			return new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format(errorMessage, string.substring(matcher.start(), matcher.end())));
+		} else {
+			return Status.OK_STATUS;
+		}
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/util/Util.java	Mon Jan 11 13:15:16 2010 -0800
@@ -0,0 +1,31 @@
+/**
+ * Copyright (c) 2010 Symbian Foundation and/or its subsidiary(-ies).
+ * All rights reserved.
+ * This component and the accompanying materials are made available
+ * under the terms of the License "Eclipse Public License v1.0"
+ * which accompanies this distribution, and is available
+ * at the URL "http://www.eclipse.org/legal/epl-v10.html".
+ *
+ * Initial Contributors:
+ * Symbian Foundation - initial contribution.
+ * Contributors:
+ * Description:
+ * Overview:
+ * Details:
+ * Platforms/Drives/Compatibility:
+ * Assumptions/Requirement/Pre-requisites:
+ * Failures and causes:
+ */
+package org.symbian.tools.wrttools.util;
+
+public class Util {
+
+	public static String removeSpaces(String widgetName) {
+		return widgetName != null ? widgetName.replace(" ", "") : null;
+	}
+
+	public static String removeNonAlphaNum(String projectName) {
+		return projectName != null ? projectName.replaceAll("[^a-zA-Z0-9 ]", "") : null;
+	}
+
+}
--- a/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/wizards/AbstractDataBindingPage.java	Fri Jan 08 17:41:28 2010 -0800
+++ b/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/wizards/AbstractDataBindingPage.java	Mon Jan 11 13:15:16 2010 -0800
@@ -39,6 +39,7 @@
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Text;
 import org.symbian.tools.wrttools.Activator;
+import org.symbian.tools.wrttools.util.CompoundValidator;
 
 public abstract class AbstractDataBindingPage extends WizardPage {
 	public class NonEmptyStringValidator implements IValidator {
@@ -78,9 +79,9 @@
 	}
 
 	protected Text createText(Composite root, String property,
-			String propertyName) {
+			String propertyName, IValidator... validators) {
 		return createText(root, BeansObservables
-				.observeValue(context, property), propertyName);
+				.observeValue(context, property), propertyName, validators);
 	}
 
 	protected Text createTextForExt(Composite root, String property,
@@ -92,14 +93,16 @@
 	}
 
 	private Text createText(Composite root, IObservableValue model,
-			String propertyName) {
+			String propertyName, IValidator... validators) {
 		Text text = new Text(root, SWT.BORDER);
 		text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
 		ISWTObservableValue view = SWTObservables.observeText(text, SWT.Modify);
 		UpdateValueStrategy strategy = new UpdateValueStrategy(
 				UpdateValueStrategy.POLICY_UPDATE);
-		strategy
-				.setBeforeSetValidator(new NonEmptyStringValidator(propertyName));
+		NonEmptyStringValidator validator = new NonEmptyStringValidator(
+				propertyName);
+		strategy.setBeforeSetValidator(validators.length == 0 ? validator
+				: new CompoundValidator(validator, validators));
 		bindingContext.bindValue(view, model, strategy, null);
 		return text;
 	}
--- a/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/wizards/WRTProjectDetailsWizardPage.java	Fri Jan 08 17:41:28 2010 -0800
+++ b/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/wizards/WRTProjectDetailsWizardPage.java	Mon Jan 11 13:15:16 2010 -0800
@@ -27,6 +27,7 @@
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
+import org.symbian.tools.wrttools.util.RegexpValidator;
 
 public class WRTProjectDetailsWizardPage extends AbstractDataBindingPage {
 	private boolean isActive;
@@ -46,7 +47,7 @@
 		root.setLayout(new GridLayout(2, false));
 		createLabel(root, "Widget name:");
 		
-		createText(root, WizardContext.WIDGET_NAME, "widget name");
+		createText(root, WizardContext.WIDGET_NAME, "widget name", new RegexpValidator("[^\\w\\. ]", "Widget name cannot contain {0} character", false));
 		
 		createLabel(root, "");
 		createLabel(root, "This will be the widget's display name on the device");
@@ -54,7 +55,7 @@
 		createLabel(root, "");
 		createLabel(root, "Widget identifier:");
 
-		createText(root, WizardContext.WIDGET_ID, "widget identifier");
+		createText(root, WizardContext.WIDGET_ID, "widget identifier", new RegexpValidator("[\\w]*(\\.\\w[\\w]*)*", "{0} is not a valid widget ID", true));
 		createLabel(root, "");
 		createLabel(root, "This id should be unique for succesful installation of widget on the device");
 		createLabel(root, "");
@@ -70,7 +71,6 @@
 		IObservableValue model = BeansObservables.observeValue(context, WizardContext.HOME_SCREEN);
 		bindingContext.bindValue(view, model);
 		
-		
 		setControl(root);
 	}
 
--- a/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/wizards/WizardContext.java	Fri Jan 08 17:41:28 2010 -0800
+++ b/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/wizards/WizardContext.java	Mon Jan 11 13:15:16 2010 -0800
@@ -26,6 +26,7 @@
 import java.util.TreeMap;
 
 import org.symbian.tools.wrttools.core.ProjectTemplate;
+import org.symbian.tools.wrttools.util.Util;
 
 public class WizardContext {
 	public static final String PROJECT_URI = "projectUri";
@@ -71,7 +72,7 @@
 	private String getDefaultWidgetId() {
 		if (template != null) {
 			return MessageFormat
-					.format(template.getIdFormat(), getWidgetName());
+					.format(template.getIdFormat(), Util.removeSpaces(getWidgetName()));
 		} else {
 			return null;
 		}
@@ -111,7 +112,7 @@
 	}
 
 	public String getWidgetName() {
-		return widgetName != null ? widgetName : getProjectName();
+		return widgetName != null ? widgetName : Util.removeNonAlphaNum(getProjectName());
 	}
 
 	public void removePropertyChangeListener(PropertyChangeListener arg0) {
@@ -234,9 +235,9 @@
 		
 		vars.put("widgetName", getWidgetName());
 		vars.put("widgetId", getWidgetId());
-		vars.put("mainHtml", getHtmlFile());
-		vars.put("mainCss", getCssFile());
-		vars.put("mainJs", getJsFile());
+		vars.put("mainHtml", getHtmlFileName());
+		vars.put("mainCss", getCssFileName());
+		vars.put("mainJs", getJsFileName());
 		vars.put("homeScreen", String.valueOf(isHomeScreen()));
 		vars.putAll(extensions);
 		
@@ -252,4 +253,26 @@
 		this.homeScreen = homeScreen;
 		propertySupport.firePropertyChange(HOME_SCREEN, old, homeScreen);
 	}
+	
+	public String getHtmlFileName() {
+		return stripExtension(getHtmlFile(), "htm", "html");
+	}
+	
+	public String getJsFileName() {
+		return stripExtension(getJsFile(), "js");
+	}
+
+	public String getCssFileName() {
+		return stripExtension(getCssFile(), "css");
+	}
+	
+	private String stripExtension(String fileName, String... extensions) {
+		for (String extension : extensions) {
+			String extensionAndDot = "." + extension;
+			if (fileName.endsWith(extensionAndDot)) {
+				return fileName.substring(0, fileName.length() - extensionAndDot.length());
+			}
+		}
+		return fileName;
+	}
 }
--- a/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/wizards/WrtWidgetWizard.java	Fri Jan 08 17:41:28 2010 -0800
+++ b/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/wizards/WrtWidgetWizard.java	Mon Jan 11 13:15:16 2010 -0800
@@ -166,7 +166,7 @@
 				}
 			}
 		}
-		ProjectUtils.addPreviewer(project, new Path(context.getHtmlFile()));
+		ProjectUtils.addPreviewer(project, new Path(context.getHtmlFileName()));
 	}
 
 	private void copyTemplate(IProject project, String name,