changeset 96:10ffc183f39e

Introduce some validators
author Omair Majid <omajid@redhat.com>
date Thu, 06 Feb 2014 15:32:06 -0500
parents 6fb474ebe666
children 1c835942f3c2
files com.redhat.thermostat.tools.eclipse.plugin.tests/META-INF/MANIFEST.MF 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/BundleInformation.java com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/BundleInformationExtractor.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/ExtensionEditPage.java com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/editor/NewBundleDialog.java com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/editor/Validators.java
diffstat 8 files changed, 116 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/com.redhat.thermostat.tools.eclipse.plugin.tests/META-INF/MANIFEST.MF	Thu Feb 06 13:50:10 2014 -0500
+++ b/com.redhat.thermostat.tools.eclipse.plugin.tests/META-INF/MANIFEST.MF	Thu Feb 06 15:32:06 2014 -0500
@@ -13,4 +13,5 @@
 Import-Package: com.redhat.thermostat.tools.eclipse.plugin,
  com.redhat.thermostat.tools.eclipse.plugin.editor,
  com.redhat.thermostat.tools.eclipse.plugin.model,
- com.redhat.thermostat.tools.eclipse.plugin.wizards
+ com.redhat.thermostat.tools.eclipse.plugin.wizards,
+ org.eclipse.core.databinding.validation
--- /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/editor/ValidatorsTest.java	Thu Feb 06 15:32:06 2014 -0500
@@ -0,0 +1,27 @@
+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/BundleInformation.java	Thu Feb 06 15:32:06 2014 -0500
@@ -0,0 +1,31 @@
+package com.redhat.thermostat.tools.eclipse.plugin;
+
+public class BundleInformation {
+
+    private String name;
+    private String version;
+
+    public BundleInformation() {
+    }
+
+    public BundleInformation(String name, String version) {
+        this.name = name;
+        this.version = version;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setVersion(String version) {
+        this.version = version;
+    }
+
+    public String getVersion() {
+        return version;
+    }
+}
--- a/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/BundleInformationExtractor.java	Thu Feb 06 13:50:10 2014 -0500
+++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/BundleInformationExtractor.java	Thu Feb 06 15:32:06 2014 -0500
@@ -6,18 +6,16 @@
 import java.util.jar.Attributes;
 import java.util.jar.JarInputStream;
 
-import com.redhat.thermostat.tools.eclipse.plugin.model.Bundle;
-
 public class BundleInformationExtractor {
 
-    public Bundle extract(String path) throws FileNotFoundException, IOException {
+    public BundleInformation extract(String path) throws FileNotFoundException, IOException {
 
         try (JarInputStream in = new JarInputStream(new FileInputStream(path))) {
             Attributes attr = in.getManifest().getMainAttributes();
             String name = attr.getValue("Bundle-SymbolicName"); //$NON-NLS-1$
             String version = attr.getValue("Bundle-Version"); //$NON-NLS-1$
 
-            return new Bundle(name, version);
+            return new BundleInformation(name, version);
         }
     }
 
--- a/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/editor/CommandEditPage.java	Thu Feb 06 13:50:10 2014 -0500
+++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/editor/CommandEditPage.java	Thu Feb 06 15:32:06 2014 -0500
@@ -1,9 +1,12 @@
 package com.redhat.thermostat.tools.eclipse.plugin.editor;
 
+import org.eclipse.core.databinding.Binding;
 import org.eclipse.core.databinding.DataBindingContext;
+import org.eclipse.core.databinding.UpdateValueStrategy;
 import org.eclipse.core.databinding.beans.BeanProperties;
 import org.eclipse.core.databinding.observable.list.IObservableList;
 import org.eclipse.core.databinding.observable.value.IObservableValue;
+import org.eclipse.jface.databinding.fieldassist.ControlDecorationSupport;
 import org.eclipse.jface.databinding.swt.WidgetProperties;
 import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
 import org.eclipse.jface.viewers.ArrayContentProvider;
@@ -31,6 +34,7 @@
 import org.eclipse.ui.forms.widgets.TableWrapData;
 import org.eclipse.ui.forms.widgets.TableWrapLayout;
 
+import com.redhat.thermostat.tools.eclipse.plugin.BundleInformation;
 import com.redhat.thermostat.tools.eclipse.plugin.Messages;
 import com.redhat.thermostat.tools.eclipse.plugin.model.Bundle;
 import com.redhat.thermostat.tools.eclipse.plugin.model.Command;
@@ -222,7 +226,8 @@
                 NewBundleDialog dialog = new NewBundleDialog(sectionContents.getShell());
                 dialog.create();
                 if (dialog.open() == Window.OK) {
-                    commandModel.addBundle(dialog.getBundle());
+                    BundleInformation bundleInfo = dialog.getBundleInformation();
+                    commandModel.addBundle(new Bundle(bundleInfo.getName(), bundleInfo.getVersion()));
                 }
             }
         });
@@ -252,7 +257,10 @@
 
         IObservableValue descriptionWidgetValue = WidgetProperties.text(SWT.Modify).observe(commandDescription);
         IObservableValue descriptionModelValue = BeanProperties.value(Command.class, "description").observe(commandModel); //$NON-NLS-1$
-        bindingContext.bindValue(descriptionWidgetValue, descriptionModelValue);
+        UpdateValueStrategy descriptionStrategy = new UpdateValueStrategy();
+        descriptionStrategy.setBeforeSetValidator(new Validators.NonEmptyStringRequired("Description can not be empty"));
+        Binding descriptionBinding = bindingContext.bindValue(descriptionWidgetValue, descriptionModelValue, descriptionStrategy, null);
+        ControlDecorationSupport.create(descriptionBinding, SWT.LEFT | SWT.TOP);
 
         IObservableValue environmentsModelValue = BeanProperties.value(Command.class, "environments").observe(commandModel); //$NON-NLS-1$
 
--- a/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/editor/ExtensionEditPage.java	Thu Feb 06 13:50:10 2014 -0500
+++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/editor/ExtensionEditPage.java	Thu Feb 06 15:32:06 2014 -0500
@@ -27,6 +27,7 @@
 import org.eclipse.ui.forms.widgets.TableWrapData;
 import org.eclipse.ui.forms.widgets.TableWrapLayout;
 
+import com.redhat.thermostat.tools.eclipse.plugin.BundleInformation;
 import com.redhat.thermostat.tools.eclipse.plugin.Messages;
 import com.redhat.thermostat.tools.eclipse.plugin.model.Bundle;
 import com.redhat.thermostat.tools.eclipse.plugin.model.Extension;
@@ -170,8 +171,8 @@
                 NewBundleDialog dialog = new NewBundleDialog(sectionContents.getShell());
                 dialog.create();
                 if (dialog.open() == Window.OK) {
-                    Bundle newBundle = dialog.getBundle();
-                    extensionModel.addBundle(newBundle);
+                    BundleInformation bundleInfo = dialog.getBundleInformation();
+                    extensionModel.addBundle(new Bundle(bundleInfo.getName(), bundleInfo.getVersion()));
                 }
             }
         });
--- a/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/editor/NewBundleDialog.java	Thu Feb 06 13:50:10 2014 -0500
+++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/editor/NewBundleDialog.java	Thu Feb 06 15:32:06 2014 -0500
@@ -16,9 +16,9 @@
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Text;
 
+import com.redhat.thermostat.tools.eclipse.plugin.BundleInformation;
 import com.redhat.thermostat.tools.eclipse.plugin.BundleInformationExtractor;
 import com.redhat.thermostat.tools.eclipse.plugin.Messages;
-import com.redhat.thermostat.tools.eclipse.plugin.model.Bundle;
 
 /**
  * Prompt the user to enter or select a bundle name/version.
@@ -27,7 +27,7 @@
  */
 public class NewBundleDialog extends TitleAreaDialog {
 
-    private Bundle bundleInfo;
+    private BundleInformation bundleInfo = new BundleInformation();
 
     private Button manualSelectionButton;
     private Text nameText;
@@ -143,11 +143,13 @@
 
     private void saveInput() {
         if (manualSelectionButton.getSelection()) {
-            bundleInfo = new Bundle(nameText.getText(), versionText.getText());
+            bundleInfo = new BundleInformation(nameText.getText(), versionText.getText());
         } else {
             try {
                 BundleInformationExtractor extractor = new BundleInformationExtractor();
-                bundleInfo = extractor.extract(fileLocationText.getText());
+                BundleInformation extractedInfo = extractor.extract(fileLocationText.getText());
+                bundleInfo.setName(extractedInfo.getName());
+                bundleInfo.setVersion(extractedInfo.getVersion());
             } catch (IOException e) {
                 // FIXME doing something saner with this exception
                 e.printStackTrace();
@@ -155,8 +157,7 @@
         }
     }
 
-    public Bundle getBundle() {
+    public BundleInformation getBundleInformation() {
         return bundleInfo;
     }
-
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/editor/Validators.java	Thu Feb 06 15:32:06 2014 -0500
@@ -0,0 +1,34 @@
+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();
+            }
+        }
+    }
+}