diff flys-client/src/main/java/de/intevation/flys/client/shared/model/DoubleProperty.java @ 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 3304608baf35
children bf080e932d54
line wrap: on
line diff
--- 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));
         }

http://dive4elements.wald.intevation.org