changeset 564:55a90afaf513

Introduced a client side validation for the WQ panel. flys-client/trunk@2105 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 14 Jun 2011 11:31:33 +0000 (2011-06-14)
parents 469528551b78
children a078ba1c139d
files flys-client/ChangeLog flys-client/src/main/java/de/intevation/flys/client/client/ui/WQInputPanel.java
diffstat 2 files changed, 114 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-client/ChangeLog	Tue Jun 14 10:42:34 2011 +0000
+++ b/flys-client/ChangeLog	Tue Jun 14 11:31:33 2011 +0000
@@ -1,3 +1,9 @@
+2011-06-14  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/client/client/ui/WQInputPanel.java: The
+	  values entered by the user are validated on client side now taking
+	  account on the format and the river's W/Q ranges.
+
 2011-06-14  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/client/client/ui/DoubleArrayPanel.java:
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/WQInputPanel.java	Tue Jun 14 10:42:34 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/WQInputPanel.java	Tue Jun 14 11:31:33 2011 +0000
@@ -1,9 +1,11 @@
 package de.intevation.flys.client.client.ui;
 
+import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
 
 import com.google.gwt.core.client.GWT;
+import com.google.gwt.i18n.client.NumberFormat;
 import com.google.gwt.user.client.rpc.AsyncCallback;
 
 import com.smartgwt.client.data.Record;
@@ -664,6 +666,112 @@
     }
 
 
+    @Override
+    public List<String> validate() {
+        if (isRangeMode()) {
+            return validateRangeValues();
+        }
+        else {
+            return validateSingleValues();
+        }
+    }
+
+
+    protected List<String> validateRangeValues() {
+        if (isWMode()) {
+            return validateRange(wRangePanel, minW, maxW);
+        }
+        else {
+            return validateRange(qRangePanel, minQ, maxQ);
+        }
+    }
+
+
+    protected List<String> validateSingleValues() {
+        if (isWMode()) {
+            return validateSingle(wArrayPanel, minW, maxW);
+        }
+        else {
+            return validateSingle(qArrayPanel, minQ, maxQ);
+        }
+    }
+
+
+    protected List<String> validateRange(
+        DoubleRangePanel panel,
+        double min, double max)
+    {
+        List<String> errors = new ArrayList<String>();
+        NumberFormat nf     = NumberFormat.getDecimalFormat();
+
+        if (!panel.validateForm()) {
+            errors.add(MESSAGE.wrongFormat());
+        }
+
+        double from = panel.getFrom();
+        double to   = panel.getTo();
+
+        if (from < min || from > max) {
+            String tmp = MESSAGE.error_validate_lower_range();
+            tmp = tmp.replace("$1", nf.format(from));
+            tmp = tmp.replace("$2", nf.format(min));
+            errors.add(tmp);
+            from = min;
+        }
+
+        if (to < min || to > max) {
+            String tmp = MESSAGE.error_validate_upper_range();
+            tmp = tmp.replace("$1", nf.format(to));
+            tmp = tmp.replace("$2", nf.format(max));
+            errors.add(tmp);
+            to = max;
+        }
+
+        return errors;
+    }
+
+
+    protected List<String> validateSingle(
+        DoubleArrayPanel panel,
+        double min, double max)
+    {
+        List<String> errors = new ArrayList<String>();
+        NumberFormat nf     = NumberFormat.getDecimalFormat();
+
+        if (!panel.validateForm()) {
+            errors.add(MESSAGE.wrongFormat());
+        }
+
+        double[] values = panel.getInputValues();
+        double[] good   = new double[values.length];
+        int      idx    = 0;
+
+        for (double value: values) {
+            if (value < min || value > max) {
+                String tmp = MESSAGE.error_validate_range();
+                tmp = tmp.replace("$1", nf.format(value));
+                tmp = tmp.replace("$2", nf.format(min));
+                tmp = tmp.replace("$3", nf.format(max));
+                errors.add(tmp);
+            }
+            else {
+                good[idx++] = value;
+            }
+        }
+
+        double[] justGood = new double[idx];
+        for (int i = 0; i < justGood.length; i++) {
+            justGood[i] = good[i];
+        }
+
+        if (!errors.isEmpty()) {
+            panel.setValues(justGood);
+        }
+
+        return errors;
+    }
+
+
     /**
      * This method returns the selected data.
      *

http://dive4elements.wald.intevation.org