changeset 1507:c21d14e48040

Improved validation and property handling. flys-client/trunk@3645 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Wed, 11 Jan 2012 10:21:22 +0000
parents 339f8aa641b5
children 8ac57bbc270c
files flys-client/ChangeLog flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartPropertiesEditor.java flys-client/src/main/java/de/intevation/flys/client/client/utils/DoubleValidator.java flys-client/src/main/java/de/intevation/flys/client/client/utils/IntegerValidator.java flys-client/src/main/java/de/intevation/flys/client/client/utils/Validator.java flys-client/src/main/java/de/intevation/flys/client/server/CollectionHelper.java flys-client/src/main/java/de/intevation/flys/client/shared/model/BooleanProperty.java flys-client/src/main/java/de/intevation/flys/client/shared/model/DoubleProperty.java flys-client/src/main/java/de/intevation/flys/client/shared/model/IntegerProperty.java flys-client/src/main/java/de/intevation/flys/client/shared/model/PropertySetting.java flys-client/src/main/java/de/intevation/flys/client/shared/model/StringProperty.java
diffstat 11 files changed, 172 insertions(+), 137 deletions(-) [+]
line wrap: on
line diff
--- a/flys-client/ChangeLog	Mon Jan 09 18:06:01 2012 +0000
+++ b/flys-client/ChangeLog	Wed Jan 11 10:21:22 2012 +0000
@@ -1,3 +1,27 @@
+2012-01-11  Raimund Renkert <raimund.renkert@intevation.de>
+
+	Improved validation and property handling.
+
+	* src/main/java/de/intevation/flys/client/client/ui/chart/ChartPropertiesEditor.java:
+	  Changed the way of validating and setting values.
+
+	* src/main/java/de/intevation/flys/client/client/utils/Validator.java,
+	  src/main/java/de/intevation/flys/client/client/utils/DoubleValidator.java,
+	  src/main/java/de/intevation/flys/client/client/utils/IntegerValidator.java:
+	  Changed the validator to an interface and removed the ChangedHandler
+	  implementation. The concrete validators implement the new interface.
+
+	* src/main/java/de/intevation/flys/client/shared/model/PropertySetting.java,
+	  src/main/java/de/intevation/flys/client/shared/model/BooleanProperty.java,
+	  src/main/java/de/intevation/flys/client/shared/model/DoubleProperty.java,
+	  src/main/java/de/intevation/flys/client/shared/model/IntegerProperty.java,
+	  src/main/java/de/intevation/flys/client/shared/model/StringProperty.java:
+	  The concrete property classes return the appropriate value type for
+	  better handling.
+
+	  * src/main/java/de/intevation/flys/client/server/CollectionHelper.java:
+	  Set the values via toString() to describe document.
+
 2012-01-09  Raimund Renkert <raimund.renkert@intevation.de>
 
 	Issue 358.
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartPropertiesEditor.java	Mon Jan 09 18:06:01 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartPropertiesEditor.java	Wed Jan 11 10:21:22 2012 +0000
@@ -25,6 +25,8 @@
 import com.smartgwt.client.widgets.events.ClickHandler;
 import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
 import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
+import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
+import com.smartgwt.client.widgets.form.fields.events.BlurEvent;
 
 import com.smartgwt.client.types.Alignment;
 
@@ -192,7 +194,6 @@
             IntegerProperty fontsize =
                 (IntegerProperty)pg.getPropertyByName("font-size");
             FormItem fs = createIntegerProperty(fontsize);
-            fs.addChangedHandler(new IntegerValidator());
             fs.setValue(
                 ((IntegerProperty)
                     origPg.getPropertyByName("font-size")).getValue());
@@ -200,38 +201,25 @@
             DoubleProperty upper =
                 (DoubleProperty)pg.getPropertyByName("upper");
             final FormItem range1 = createDoubleProperty(upper);
-            range1.addChangedHandler(new DoubleValidator());
             range1.setWidth(70);
-            String r1Value = 
-                ((DoubleProperty)origPg.getPropertyByName("upper")).getValue();
+            range1.setValue(
+                ((DoubleProperty)
+                    origPg.getPropertyByName("upper")).toUIString());
+
             DoubleProperty lower =
                 (DoubleProperty)pg.getPropertyByName("lower");
             final FormItem range2 = createDoubleProperty(lower);
-            range2.addChangedHandler(new DoubleValidator());
             range2.setWidth(70);
-            String r2Value =
-                ((DoubleProperty)origPg.getPropertyByName("lower")).getValue();
-            if(locale.equals("de")) {
-                range1.setValue(r1Value.replaceAll("\\.", ","));
-                range2.setValue(r2Value.replaceAll("\\.", ","));
-            }
-            else {
-                range1.setValue(r1Value);
-                range2.setValue(r2Value);
-            }
+            range2.setValue(
+                ((DoubleProperty)
+                    origPg.getPropertyByName("lower")).toUIString());
 
             BooleanProperty fixation =
                 (BooleanProperty)pg.getPropertyByName("fixation");
             FormItem fix = createBooleanProperty(fixation);
-            String fixValue =
-                ((BooleanProperty)
-                    origPg.getPropertyByName("fixation")).getValue();
-            if(fixValue.equals("true")) {
-                fix.setValue(true);
-            }
-            else {
-                fix.setValue(false);
-            }
+            fix.setValue(((BooleanProperty)
+                origPg.getPropertyByName("fixation")).getValue().booleanValue());
+
             fix.addChangedHandler(new ChangedHandler() {
                 public void onChanged(ChangedEvent e) {
                     if ((Boolean)e.getValue()) {
@@ -244,7 +232,7 @@
                     }
                 }
             });
-            if (fixation.getValue().equals("true")) {
+            if (fix.getValue().toString().equals("true")) {
                 range1.enable();
                 range2.enable();
             }
@@ -285,31 +273,14 @@
         FormItem item = new FormItem();
         if (setting instanceof BooleanProperty) {
             item = createBooleanProperty((BooleanProperty)setting);
-            String iValue = ((BooleanProperty)orig).getValue();
-            if(iValue.equals("true")) {
-                item.setValue(true);
-            }
-            else {
-                item.setValue(false);
-            }
+            item.setValue(((BooleanProperty)orig).getValue().toString());
         }
         else if (setting instanceof DoubleProperty) {
             item = createDoubleProperty((DoubleProperty)setting);
-            DoubleValidator validator = new DoubleValidator();
-            item.addChangedHandler(validator);
-            Config config = Config.getInstance();
-            String locale = config.getLocale();
-            String iValue = ((DoubleProperty)orig).getValue();
-            if(locale.equals("de")) {
-                item.setValue(iValue.replaceAll("\\.", ","));
-            }
-            else {
-                item.setValue(iValue);
-            }
+            item.setValue(((DoubleProperty)orig).toUIString());
         }
         else if (setting instanceof IntegerProperty) {
             item = createIntegerProperty((IntegerProperty)setting);
-            item.addChangedHandler(new IntegerValidator());
             item.setValue(((IntegerProperty)orig).getValue());
         }
         else if (setting instanceof StringProperty) {
@@ -332,14 +303,14 @@
         TextItem item = new TextItem();
         item.setTitle(MSG.getString(name));
         item.setTitleAlign(Alignment.LEFT);
-        item.addChangedHandler(new ChangedHandler() {
-            public void onChanged(ChangedEvent e) {
+        item.addBlurHandler(new BlurHandler() {
+            public void onBlur(BlurEvent e) {
                 String val;
-                if (e.getValue() == null) {
+                if (e.getItem().getValue() == null) {
                     val = "";
                 }
                 else {
-                    val = e.getValue().toString();
+                    val = e.getItem().getValue().toString();
                 }
                 sp.setValue(val);
             }
@@ -361,14 +332,14 @@
         item.setLabelAsTitle(true);
         item.setTitleStyle("color:#000;");
         item.setTitleAlign(Alignment.LEFT);
-        item.addChangedHandler(new ChangedHandler() {
-            public void onChanged(ChangedEvent e) {
+        item.addBlurHandler(new BlurHandler() {
+            public void onBlur(BlurEvent e) {
                 String val;
-                if (e.getValue() == null) {
+                if (e.getItem().getValue() == null) {
                     val = "";
                 }
                 else {
-                    val = e.getValue().toString();
+                    val = e.getItem().getValue().toString();
                 }
                 bp.setValue(val);
             }
@@ -389,22 +360,17 @@
         TextItem item = new TextItem();
         item.setTitle(MSG.getString(name));
         item.setTitleAlign(Alignment.LEFT);
-        item.addChangedHandler(new ChangedHandler() {
-            public void onChanged(ChangedEvent e) {
-                String val;
-                if (e.getValue() == null) {
-                    val = "";
-                }
-                else {
-                    val = e.getValue().toString();
-                }
-                DoubleValidator validator = new DoubleValidator();
-
-                Map errors = e.getForm().getErrors();
-                String input = validator.toProtocolString(e.getItem(), errors);
-                if(input != null) {
-                    dp.setValue(input);
-                }
+        item.addBlurHandler(new BlurHandler() {
+            public void onBlur(BlurEvent e) {
+                 DoubleValidator validator = new DoubleValidator();
+                 Map errors = e.getForm().getErrors();
+                 if(validator.validate(e.getItem(), errors)) {
+                     dp.setValueFromUI(e.getItem().getValue().toString());
+                     e.getForm().setErrors(errors, true);
+                 }
+                 else {
+                     e.getForm().setErrors(errors, true);
+                 }
             }
         });
         return item;
@@ -423,16 +389,17 @@
         TextItem item = new TextItem();
         item.setTitle(MSG.getString(name));
         item.setTitleAlign(Alignment.LEFT);
-        item.addChangedHandler(new ChangedHandler() {
-            public void onChanged(ChangedEvent e) {
-                String val;
-                if (e.getValue() == null) {
-                    val = "";
+        item.addBlurHandler(new BlurHandler() {
+            public void onBlur(BlurEvent e) {
+                IntegerValidator validator = new IntegerValidator();
+                Map errors = e.getForm().getErrors();
+                if(validator.validate(e.getItem(), errors)) {
+                    ip.setValue(e.getItem().getValue().toString());
+                    e.getForm().setErrors(errors, true);
                 }
                 else {
-                    val = e.getValue().toString();
+                    e.getForm().setErrors(errors, true);
                 }
-                ip.setValue(val);
             }
         });
         return item;
--- a/flys-client/src/main/java/de/intevation/flys/client/client/utils/DoubleValidator.java	Mon Jan 09 18:06:01 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/utils/DoubleValidator.java	Wed Jan 11 10:21:22 2012 +0000
@@ -2,23 +2,28 @@
 
 import java.util.Map;
 
+import com.google.gwt.core.client.GWT;
 import com.google.gwt.i18n.client.NumberFormat;
 
 import com.smartgwt.client.widgets.form.fields.FormItem;
 
+import de.intevation.flys.client.client.FLYSConstants;
+
 /**
  * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
  */
-public class DoubleValidator extends Validator {
+public class DoubleValidator implements Validator {
+
+    /** The interface that provides i18n messages. */
+    protected FLYSConstants MSG = GWT.create(FLYSConstants.class);
 
     /**
      *
      */
-    @Override
-    protected boolean validate(FormItem item, Map errors) {
+    public boolean validate(FormItem item, Map errors) {
         boolean valid = true;
 
-        String v = (String) item.getValue();
+        String v = item.getValue().toString();
 
         NumberFormat f = NumberFormat.getDecimalFormat();
 
@@ -40,15 +45,5 @@
         }
         return valid;
     }
-
-
-    public String toProtocolString(FormItem item, Map errors) {
-        if(validate(item, errors)) {
-            return item.getValue().toString().replaceAll(",", ".");
-        }
-        else {
-            return null;
-        }
-    }
 }
 
--- a/flys-client/src/main/java/de/intevation/flys/client/client/utils/IntegerValidator.java	Mon Jan 09 18:06:01 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/utils/IntegerValidator.java	Wed Jan 11 10:21:22 2012 +0000
@@ -2,21 +2,27 @@
 
 import java.util.Map;
 
+import com.google.gwt.core.client.GWT;
+
 import com.smartgwt.client.widgets.form.fields.FormItem;
 
+import de.intevation.flys.client.client.FLYSConstants;
+
 /**
  * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
  */
-public class IntegerValidator extends Validator {
+public class IntegerValidator implements Validator {
+
+    /** The interface that provides i18n messages. */
+    protected FLYSConstants MSG = GWT.create(FLYSConstants.class);
 
     /**
      *
      */
-    @Override
-    protected boolean validate(FormItem item, Map errors) {
+    public boolean validate(FormItem item, Map errors) {
         boolean valid = true;
 
-        String v = (String) item.getValue();
+        String v = item.getValue().toString();
 
         try {
             if (v == null) {
--- a/flys-client/src/main/java/de/intevation/flys/client/client/utils/Validator.java	Mon Jan 09 18:06:01 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/utils/Validator.java	Wed Jan 11 10:21:22 2012 +0000
@@ -2,47 +2,15 @@
 
 import java.util.Map;
 
-import com.google.gwt.core.client.GWT;
-
-import com.smartgwt.client.widgets.form.DynamicForm;
 import com.smartgwt.client.widgets.form.fields.FormItem;
-import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
-import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
-
-import de.intevation.flys.client.client.FLYSConstants;
 
 /**
  * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
  *
  * This validator is used for SmartGWT FormItems.
- *
- * To use this validator an attribute named 'internalType' has to be set.
- * Values for 'internalType':
- *   'double'
- *   'integer'
  */
-public class Validator implements ChangedHandler {
-
-    /** The interface that provides i18n messages. */
-    protected FLYSConstants MSG = GWT.create(FLYSConstants.class);
-
+public interface Validator {
 
-    /**
-     *
-     */
-    public void onChanged(ChangedEvent e) {
-        FormItem item = e.getItem();
-        DynamicForm form = e.getForm();
-        Map errors = form.getErrors();
-        String type = item.getAttribute("internalType");
-
-        validate(item, errors);
-        form.setErrors(errors, true);
-    }
-
-
-    protected boolean validate(FormItem item, Map errors) {
-        return true;
-    }
+    boolean validate(FormItem item, Map errors);
 }
 
--- a/flys-client/src/main/java/de/intevation/flys/client/server/CollectionHelper.java	Mon Jan 09 18:06:01 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/CollectionHelper.java	Wed Jan 11 10:21:22 2012 +0000
@@ -374,7 +374,7 @@
             cr.addAttr(e, "type", "string", false);
         }
 
-        e.setTextContent(p.getValue());
+        e.setTextContent(p.getValue().toString());
         cr.addAttr(e, "display", p.getAttribute("display"), false);
         return e;
     }
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/BooleanProperty.java	Mon Jan 09 18:06:01 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/BooleanProperty.java	Wed Jan 11 10:21:22 2012 +0000
@@ -22,14 +22,25 @@
      */
     public BooleanProperty(
         String name,
-        String value)
+        Boolean value)
     {
         this.name = name;
-        this.value = value;
+        this.value = value.toString();
         this.attributes = new HashMap<String, String>();
     }
 
 
+    @Override
+    public Boolean getValue() {
+        return Boolean.valueOf(this.value);
+    }
+
+
+    public void setValue(Boolean value) {
+        this.value = value.toString();
+    }
+
+
     public Object clone() {
         BooleanProperty clone = new BooleanProperty(this.getName(),
                                                     this.getValue());
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/DoubleProperty.java	Mon Jan 09 18:06:01 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/DoubleProperty.java	Wed Jan 11 10:21:22 2012 +0000
@@ -2,6 +2,9 @@
 
 import java.util.HashMap;
 
+import com.google.gwt.i18n.client.NumberFormat;
+import com.google.gwt.core.client.GWT;
+
 /**
  * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
  */
@@ -22,17 +25,57 @@
      */
     public DoubleProperty(
         String name,
-        String value)
+        Double value)
     {
         this.name = name;
-        this.value = value;
+        this.value = value.toString();
         this.attributes = new HashMap<String, String>();
     }
 
+    @Override
+    public Double getValue() {
+        try {
+            GWT.log("returning: " + Double.valueOf(this.value));
+            return Double.valueOf(this.value);
+        }
+        catch(NumberFormatException nfe) {
+            //Should never happen, if property is used correctly.
+            return null;
+        }
+    }
+
+
+    public void setValueFromUI(String value) {
+        NumberFormat nf = NumberFormat.getDecimalFormat();
+        double d;
+        try {
+            d = nf.parse(value);
+            GWT.log("setting " + value + " as " + d);
+            this.value = Double.toString(d);
+        }
+        catch(NumberFormatException nfe) {}
+    }
+
+    public void setValue(Double value) {
+        this.value = value.toString();
+    }
+
+
+    public String toUIString() {
+        double dv;
+        NumberFormat nf = NumberFormat.getDecimalFormat();
+        try {
+            dv = Double.valueOf(this.value).doubleValue();
+        }
+        catch (NumberFormatException nfe) {
+            return null;
+        }
+        return nf.format(dv);
+    }
 
     public Object clone() {
         DoubleProperty clone = new DoubleProperty(this.getName(),
-                                                    this.getValue());
+                                                  this.getValue());
         for(String s: this.getAttributeList()) {
             clone.setAttribute(s, this.getAttribute(s));
         }
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/IntegerProperty.java	Mon Jan 09 18:06:01 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/IntegerProperty.java	Wed Jan 11 10:21:22 2012 +0000
@@ -22,14 +22,29 @@
      */
     public IntegerProperty(
         String name,
-        String value)
+        Integer value)
     {
         this.name = name;
-        this.value = value;
+        this.value = value.toString();
         this.attributes = new HashMap<String, String>();
     }
 
 
+    @Override
+    public Integer getValue() {
+        try {
+            return Integer.valueOf(this.value);
+        }
+        catch(NumberFormatException nfe) {
+            return null;
+        }
+    }
+
+
+    public void setValue(Integer value) {
+        this.value = value.toString();
+    }
+
     public Object clone() {
         IntegerProperty clone = new IntegerProperty(this.getName(),
                                                     this.getValue());
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/PropertySetting.java	Mon Jan 09 18:06:01 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/PropertySetting.java	Wed Jan 11 10:21:22 2012 +0000
@@ -55,7 +55,7 @@
         return this.name;
     }
 
-    public String getValue() {
+    public Object getValue() {
         return this.value;
     }
 
@@ -70,7 +70,7 @@
 
     public Object clone() {
         PropertySetting clone = new PropertySetting(this.getName(),
-                                                    this.getValue());
+                                                    this.getValue().toString());
         for(String s: this.getAttributeList()) {
             clone.setAttribute(s, this.getAttribute(s));
         }
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/StringProperty.java	Mon Jan 09 18:06:01 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/StringProperty.java	Wed Jan 11 10:21:22 2012 +0000
@@ -30,6 +30,12 @@
     }
 
 
+    @Override
+    public String getValue() {
+        return this.value;
+    }
+
+
     public Object clone() {
         StringProperty clone = new StringProperty(this.getName(),
                                                     this.getValue());

http://dive4elements.wald.intevation.org