changeset 123:a0c68ec37305

Validate input in the project wizard
author Omair Majid <omajid@redhat.com>
date Wed, 26 Mar 2014 10:23:30 -0400
parents 3a5d1c6148ed
children f425cf9c32e8
files com.redhat.thermostat.tools.eclipse.plugin.tests/src/com/redhat/thermostat/tools/eclipse/plugin/tests/ValidatorsTest.java com.redhat.thermostat.tools.eclipse.plugin.tests/src/com/redhat/thermostat/tools/eclipse/plugin/tests/editor/ValidatorsTest.java com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/Validators.java com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/editor/CommandEditPage.java com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/editor/OptionsWizard.java com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/editor/Validators.java com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/ThermostatProjectCreationWizard.java com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/ThermostatProjectCreationWizardPage.java
diffstat 8 files changed, 260 insertions(+), 148 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/com.redhat.thermostat.tools.eclipse.plugin.tests/src/com/redhat/thermostat/tools/eclipse/plugin/tests/ValidatorsTest.java	Wed Mar 26 10:23:30 2014 -0400
@@ -0,0 +1,79 @@
+package com.redhat.thermostat.tools.eclipse.plugin.tests;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.eclipse.core.databinding.validation.IValidator;
+import org.eclipse.core.runtime.IStatus;
+import org.junit.Test;
+
+import com.redhat.thermostat.tools.eclipse.plugin.Validators;
+
+public class ValidatorsTest {
+
+    @Test
+    public void testNonEmptyStringRequiredFailsOnEmptyString() {
+        IValidator validator = new Validators.NonEmptyStringRequired("");
+        IStatus result = validator.validate("");
+        assertFalse(result.isOK());
+    }
+
+    @Test
+    public void testNonEmptyStringRequiredPassesOnNonEmptyString() {
+        IValidator validator = new Validators.NonEmptyStringRequired("");
+        IStatus result = validator.validate("foo");
+        assertTrue(result.isOK());
+    }
+
+    @Test
+    public void testPackageNameRequiredFailsOnEmptyString() {
+        IValidator validator = new Validators.NonEmptyStringRequired("");
+        IStatus result = validator.validate("");
+        assertFalse(result.isOK());
+    }
+
+    @Test
+    public void testPackageNameRequiredFailsOnSpaces() {
+        IValidator validator = new Validators.PackageNameRequired();
+        IStatus result = validator.validate("a b");
+        assertFalse(result.isOK());
+    }
+
+    @Test
+    public void testPackageNameRequiredPassesOnValidPackageName() {
+        IValidator validator = new Validators.PackageNameRequired();
+        IStatus result;
+
+        result = validator.validate("a");
+        assertTrue(result.isOK());
+
+        result = validator.validate("a.b");
+        assertTrue(result.isOK());
+
+        result = validator.validate("a.b.c");
+        assertTrue(result.isOK());
+    }
+
+    @Test
+    public void testValidIdRequiredFailsOnEmptyGroupId() {
+        IValidator validator = new Validators.ValidIdRequired("Group Id");
+        IStatus result = validator.validate("");
+        assertFalse(result.isOK());
+    }
+
+    @Test
+    public void testValidIdRequiredPassesOnValidGroupId() {
+        IValidator validator = new Validators.ValidIdRequired("Group Id");
+        IStatus result;
+
+        result = validator.validate("a");
+        assertTrue(result.isOK());
+
+        result = validator.validate("a-b");
+        assertTrue(result.isOK());
+
+        result = validator.validate("a-b-c");
+        assertTrue(result.isOK());
+    }
+
+}
--- a/com.redhat.thermostat.tools.eclipse.plugin.tests/src/com/redhat/thermostat/tools/eclipse/plugin/tests/editor/ValidatorsTest.java	Tue Mar 25 12:25:58 2014 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,27 +0,0 @@
-package com.redhat.thermostat.tools.eclipse.plugin.tests.editor;
-
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
-import org.eclipse.core.databinding.validation.IValidator;
-import org.eclipse.core.runtime.IStatus;
-import org.junit.Test;
-
-import com.redhat.thermostat.tools.eclipse.plugin.editor.Validators;
-
-public class ValidatorsTest {
-
-    @Test
-    public void testNonEmptyStringRequiredFailsOnEmptyString() {
-        IValidator validator = new Validators.NonEmptyStringRequired("");
-        IStatus result = validator.validate("");
-        assertFalse(result.isOK());
-    }
-
-    @Test
-    public void testNonEmptyStringRequiredPassesOnNonEmptyString() {
-        IValidator validator = new Validators.NonEmptyStringRequired("");
-        IStatus result = validator.validate("foo");
-        assertTrue(result.isOK());
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/Validators.java	Wed Mar 26 10:23:30 2014 -0400
@@ -0,0 +1,82 @@
+package com.redhat.thermostat.tools.eclipse.plugin;
+
+import java.util.Objects;
+
+import org.eclipse.core.databinding.validation.IValidator;
+import org.eclipse.core.databinding.validation.ValidationStatus;
+import org.eclipse.core.runtime.IStatus;
+
+public class Validators {
+
+    public static class NonEmptyStringRequired implements IValidator {
+
+        private String errorMessage;
+
+        public NonEmptyStringRequired(String errorMessage) {
+            this.errorMessage = errorMessage;
+        }
+
+        @Override
+        public IStatus validate(Object value) {
+            Objects.requireNonNull(value);
+
+            if (!(value instanceof String)) {
+                throw new AssertionError("Expected String, but got '" + value + "' (" + value.getClass() + ").");
+            }
+            String text = (String) value;
+            if (text.trim().length() == 0) {
+                return ValidationStatus.error(errorMessage);
+            } else {
+                return ValidationStatus.ok();
+            }
+        }
+    }
+
+    public static class PackageNameRequired implements IValidator {
+
+        @Override
+        public IStatus validate(Object value) {
+            Objects.requireNonNull(value);
+            if (!(value instanceof String)) {
+                throw new AssertionError("Expected String, but got '" + value + "' (" + value.getClass() + ").");
+            }
+            String packageName = (String) value;
+            if (packageName.length() == 0) {
+                return ValidationStatus.error("Package name must be specified");
+            }
+            if (packageName.contains(" ")) {
+                return ValidationStatus.error("Package name must be valid");
+            }
+            return ValidationStatus.ok();
+        }
+    }
+
+    /** Validate maven's groupIds and artifactIds */
+    public static class ValidIdRequired implements IValidator {
+
+        private String idName;
+
+        public ValidIdRequired(String idName) {
+            this.idName = idName;
+        }
+
+        @Override
+        public IStatus validate(Object value) {
+            Objects.requireNonNull(value);
+            if (!(value instanceof String)) {
+                throw new AssertionError("Expected String, but got '" + value + "' (" + value.getClass() + ").");
+            }
+            String projectName = (String) value;
+            if (projectName.length() == 0) {
+                return ValidationStatus.error(idName + " must be specified");
+            }
+            if (projectName.replace('\\', '/').indexOf('/', 1) > 0) {
+                return ValidationStatus.error(idName + " must be valid");
+            }
+            if (projectName.contains(" ")) {
+                return ValidationStatus.error(idName + " must be valid");
+            }
+            return ValidationStatus.ok();
+        }
+    }
+}
--- a/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/editor/CommandEditPage.java	Tue Mar 25 12:25:58 2014 -0400
+++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/editor/CommandEditPage.java	Wed Mar 26 10:23:30 2014 -0400
@@ -39,6 +39,7 @@
 
 import com.redhat.thermostat.tools.eclipse.plugin.BundleInformation;
 import com.redhat.thermostat.tools.eclipse.plugin.Messages;
+import com.redhat.thermostat.tools.eclipse.plugin.Validators;
 import com.redhat.thermostat.tools.eclipse.plugin.model.Bundle;
 import com.redhat.thermostat.tools.eclipse.plugin.model.Command;
 import com.redhat.thermostat.tools.eclipse.plugin.model.Environments;
--- a/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/editor/OptionsWizard.java	Tue Mar 25 12:25:58 2014 -0400
+++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/editor/OptionsWizard.java	Wed Mar 26 10:23:30 2014 -0400
@@ -35,6 +35,7 @@
 import org.eclipse.swt.widgets.Text;
 
 import com.redhat.thermostat.tools.eclipse.plugin.Messages;
+import com.redhat.thermostat.tools.eclipse.plugin.Validators;
 import com.redhat.thermostat.tools.eclipse.plugin.model.Command;
 import com.redhat.thermostat.tools.eclipse.plugin.model.Option;
 import com.redhat.thermostat.tools.eclipse.plugin.model.Options;
--- a/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/editor/Validators.java	Tue Mar 25 12:25:58 2014 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,34 +0,0 @@
-package com.redhat.thermostat.tools.eclipse.plugin.editor;
-
-import java.util.Objects;
-
-import org.eclipse.core.databinding.validation.IValidator;
-import org.eclipse.core.databinding.validation.ValidationStatus;
-import org.eclipse.core.runtime.IStatus;
-
-public class Validators {
-
-    public static class NonEmptyStringRequired implements IValidator {
-
-        private String errorMessage;
-
-        public NonEmptyStringRequired(String errorMessage) {
-            this.errorMessage = errorMessage;
-        }
-
-        @Override
-        public IStatus validate(Object value) {
-            Objects.requireNonNull(value);
-
-            if (!(value instanceof String)) {
-                throw new AssertionError("Expected String, but got '" + value + "' (" + value.getClass() + ").");
-            }
-            String text = (String) value;
-            if (text.trim().length() == 0) {
-                return ValidationStatus.error(errorMessage);
-            } else {
-                return ValidationStatus.ok();
-            }
-        }
-    }
-}
--- a/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/ThermostatProjectCreationWizard.java	Tue Mar 25 12:25:58 2014 -0400
+++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/ThermostatProjectCreationWizard.java	Wed Mar 26 10:23:30 2014 -0400
@@ -10,6 +10,7 @@
 import org.eclipse.ui.IWorkbench;
 
 import com.redhat.thermostat.tools.eclipse.plugin.Activator;
+import com.redhat.thermostat.tools.eclipse.plugin.wizards.ThermostatProjectCreationWizardPage.ProjectModel;
 
 public class ThermostatProjectCreationWizard extends Wizard implements INewWizard {
 
@@ -30,12 +31,13 @@
     @Override
     public boolean performFinish() {
         try {
-            String groupId = pageOne.getGroupId();
-            String artifactId = pageOne.getArtifactId();
-            String version = pageOne.getVersion();
+            ProjectModel model = pageOne.getProjectInformation();
+            String groupId = model.getGroupId();
+            String artifactId = model.getArtifactId();
+            String version = model.getVersion();
 
-            String packagePrefix = pageOne.getPackagePrefix();
-            String thermostatVersion = pageOne.getThermostatVersion();
+            String packagePrefix = model.getPackageName();
+            String thermostatVersion = model.getThermostatVersion();
 
             createProject(groupId, artifactId, version, packagePrefix, thermostatVersion);
         } catch (CoreException e) {
--- a/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/ThermostatProjectCreationWizardPage.java	Tue Mar 25 12:25:58 2014 -0400
+++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/ThermostatProjectCreationWizardPage.java	Wed Mar 26 10:23:30 2014 -0400
@@ -1,5 +1,12 @@
 package com.redhat.thermostat.tools.eclipse.plugin.wizards;
 
+import org.eclipse.core.databinding.DataBindingContext;
+import org.eclipse.core.databinding.UpdateValueStrategy;
+import org.eclipse.core.databinding.beans.PojoObservables;
+import org.eclipse.core.databinding.observable.value.IObservableValue;
+import org.eclipse.core.databinding.validation.IValidator;
+import org.eclipse.jface.databinding.swt.WidgetProperties;
+import org.eclipse.jface.databinding.wizard.WizardPageSupport;
 import org.eclipse.jface.wizard.WizardPage;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.layout.GridData;
@@ -8,15 +15,12 @@
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Text;
 
+import com.redhat.thermostat.tools.eclipse.plugin.Validators;
+
 public class ThermostatProjectCreationWizardPage extends WizardPage {
 
-    private Text groupIdText;
-    private Text artifactIdText;
-    private Text versionText;
-
-    private Text packageNameText;
-
-    private Text thermostatVersionText;
+    private ProjectModel model = new ProjectModel();
+    private DataBindingContext context;
 
     public ThermostatProjectCreationWizardPage() {
         super("wizardPage");
@@ -30,95 +34,99 @@
         GridLayout layout = new GridLayout(2, false);
         container.setLayout(layout);
 
-        Label groupIdLabel = new Label(container, SWT.NULL);
-        groupIdLabel.setText("&Group Id:");
-
-        groupIdText = new Text(container, SWT.BORDER | SWT.SINGLE);
-        groupIdText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
+        context = new DataBindingContext();
 
-        Label artifactIdLabel = new Label(container, SWT.NULL);
-        artifactIdLabel.setText("&Artifact Id:");
+        createLabelAndInputField(container, "&Group Id:", "groupId",
+                new Validators.ValidIdRequired("Group Id"));
 
-        artifactIdText = new Text(container, SWT.BORDER | SWT.SINGLE);
-        artifactIdText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
-
-        Label versionLabel = new Label(container, SWT.NULL);
-        versionLabel.setText("&Version:");
+        createLabelAndInputField(container, "&Artifact Id:", "artifactId",
+                new Validators.ValidIdRequired("Artifact Id"));
 
-        versionText = new Text(container, SWT.BORDER | SWT.SINGLE);
-        versionText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
-
-        Label packageNameLabel = new Label(container, SWT.NULL);
-        packageNameLabel.setText("&Package Prefix:");
+        createLabelAndInputField(container, "&Version:", "version",
+                new Validators.NonEmptyStringRequired("Version must not be empty"));
 
-        packageNameText = new Text(container, SWT.BORDER | SWT.SINGLE);
-        packageNameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
+        createLabelAndInputField(container, "&Package Prefix:", "packageName",
+                new Validators.PackageNameRequired());
 
-        Label thermostatVersionLabel = new Label(container, SWT.NULL);
-        thermostatVersionLabel.setText("&Thermostat Version:");
-
-        thermostatVersionText = new Text(container, SWT.BORDER | SWT.SINGLE);
-        thermostatVersionText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
-
-        checkProjectName();
+        createLabelAndInputField(container, "&Thermostat Version:", "thermostatVersion",
+                new Validators.NonEmptyStringRequired("Thermostat Version must not be empty"));
 
         setControl(container);
+
+        WizardPageSupport.create(this, context);
+    }
+
+    private void createLabelAndInputField(Composite container, String labelText, String propertyName, IValidator validator) {
+        Label label = new Label(container, SWT.NULL);
+        label.setText(labelText);
+
+        Text text = new Text(container, SWT.BORDER | SWT.SINGLE);
+        text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
+        IObservableValue widgetValue = WidgetProperties.text(SWT.Modify).observe(text);
+        IObservableValue modelValue = PojoObservables.observeValue(model, propertyName);
+        UpdateValueStrategy targetToModelStrategy = new UpdateValueStrategy();
+        targetToModelStrategy.setBeforeSetValidator(validator);
+        context.bindValue(widgetValue, modelValue, targetToModelStrategy, null);
+    }
+
+    @Override
+    public void dispose() {
+        context.dispose();
+
+        super.dispose();
+    }
+
+    public ProjectModel getProjectInformation() {
+        return model;
     }
 
-    private void checkProjectName() {
-        String projectName = artifactIdText.getText();
-        if (projectName.length() == 0) {
-            updateStatus("Project name must be specified");
-            return;
+    static class ProjectModel {
+        private String groupId;
+        private String artifactId;
+        private String version;
+
+        private String packageName;
+
+        private String thermostatVersion;
+
+        public String getGroupId() {
+            return groupId;
         }
-        if (projectName.replace('\\', '/').indexOf('/', 1) > 0) {
-            updateStatus("Project name must be valid");
-            return;
+
+        public void setGroupId(String groupId) {
+            this.groupId = groupId;
         }
-        if (projectName.contains(" ")) {
-            updateStatus("Project name must be valid");
-            return;
+
+        public String getArtifactId() {
+            return artifactId;
+        }
+
+        public void setArtifactId(String artifactId) {
+            this.artifactId = artifactId;
         }
-        updateStatus(null);
-    }
+
+        public String getVersion() {
+            return version;
+        }
+
+        public void setVersion(String version) {
+            this.version = version;
+        }
 
-    private void dialogChanged() {
-        String packageName = packageNameText.getText();
-        if (packageName.length() == 0) {
-            updateStatus("Package name must be specified");
-            return;
+        public String getPackageName() {
+            return packageName;
+        }
+
+        public void setPackageName(String packageName) {
+            this.packageName = packageName;
         }
-        if (packageName.contains(" ")) {
-            updateStatus("Package name must be valid");
-            return;
+
+        public String getThermostatVersion() {
+            return thermostatVersion;
         }
-        updateStatus(null);
+
+        public void setThermostatVersion(String thermostatVersion) {
+            this.thermostatVersion = thermostatVersion;
+        }
     }
-
-    private void updateStatus(String message) {
-        setErrorMessage(message);
-        setPageComplete(message == null);
-    }
-
-    public String getGroupId() {
-        return groupIdText.getText();
-    }
-
-    public String getArtifactId() {
-        return artifactIdText.getText();
-    }
-
-    public String getVersion() {
-        return versionText.getText();
-    }
-
-    public String getPackagePrefix() {
-        return packageNameText.getText();
-        
-    }
-
-    public String getThermostatVersion() {
-        return thermostatVersionText.getText();
-    }
-
 }