diff flys-client/src/main/java/de/intevation/flys/client/client/ui/WQAdaptedInputPanel.java @ 565:a078ba1c139d

Introduced a client side input validation for the adapted WQ panel. flys-client/trunk@2112 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 14 Jun 2011 14:00:19 +0000
parents 5274b9317e40
children 3b670af34367
line wrap: on
line diff
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/WQAdaptedInputPanel.java	Tue Jun 14 11:31:33 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/WQAdaptedInputPanel.java	Tue Jun 14 14:00:19 2011 +0000
@@ -1,5 +1,6 @@
 package de.intevation.flys.client.client.ui;
 
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
@@ -7,6 +8,7 @@
 import java.util.Map;
 
 import com.google.gwt.core.client.GWT;
+import com.google.gwt.i18n.client.NumberFormat;
 
 import com.smartgwt.client.types.TitleOrientation;
 import com.smartgwt.client.types.VerticalAlignment;
@@ -26,6 +28,7 @@
 import de.intevation.flys.client.shared.model.DataList;
 import de.intevation.flys.client.shared.model.DefaultData;
 import de.intevation.flys.client.shared.model.DefaultDataItem;
+import de.intevation.flys.client.shared.model.WQDataItem;
 
 import de.intevation.flys.client.client.FLYSConstants;
 
@@ -59,6 +62,12 @@
     /** Stores the input panels related to their keys.*/
     protected Map<String, DoubleArrayPanel> wqranges;
 
+    /** Stores the min/max values for each q range.*/
+    protected Map<String, double[]> qranges;
+
+    /** Stores the min/max values for each w range.*/
+    protected Map<String, double[]> wranges;
+
     /** The RadioGroupItem that determines the w/q input mode.*/
     protected DynamicForm modes;
 
@@ -66,6 +75,8 @@
 
     public WQAdaptedInputPanel() {
         wqranges = new HashMap<String, DoubleArrayPanel>();
+        qranges  = new HashMap<String, double[]>();
+        wranges  = new HashMap<String, double[]>();
     }
 
 
@@ -189,6 +200,111 @@
     }
 
 
+    @Override
+    public List<String> validate() {
+        if (isWMode()) {
+            return validateW();
+        }
+        else {
+            return validateQ();
+        }
+    }
+
+
+    protected List<String> validateW() {
+        List<String> errors = new ArrayList<String>();
+        NumberFormat nf     = NumberFormat.getDecimalFormat();
+
+        Iterator<String> iter = wqranges.keySet().iterator();
+
+        while (iter.hasNext()) {
+            List<String> tmpErrors = new ArrayList<String>();
+
+            String           key = iter.next();
+            DoubleArrayPanel dap = wqranges.get(key);
+            double[]         mm  = wranges.get(key);
+
+            double[] values = dap.getInputValues();
+            double[] good   = new double[values.length];
+
+            int idx = 0;
+
+            for (double value: values) {
+                if (value < mm[0] || value > mm[1]) {
+                    String tmp = MSG.error_validate_range();
+                    tmp = tmp.replace("$1", nf.format(value));
+                    tmp = tmp.replace("$2", nf.format(mm[0]));
+                    tmp = tmp.replace("$3", nf.format(mm[1]));
+                    tmpErrors.add(tmp);
+                }
+                else {
+                    good[idx++] = value;
+                }
+            }
+
+            double[] justGood = new double[idx];
+            for (int i = 0; i < justGood.length; i++) {
+                justGood[i] = good[i];
+            }
+
+            if (!tmpErrors.isEmpty()) {
+                dap.setValues(justGood);
+
+                errors.addAll(tmpErrors);
+            }
+        }
+
+        return errors;
+    }
+
+
+    protected List<String> validateQ() {
+        List<String> errors = new ArrayList<String>();
+        NumberFormat nf     = NumberFormat.getDecimalFormat();
+
+        Iterator<String> iter = wqranges.keySet().iterator();
+
+        while (iter.hasNext()) {
+            List<String> tmpErrors = new ArrayList<String>();
+
+            String           key = iter.next();
+            DoubleArrayPanel dap = wqranges.get(key);
+            double[]         mm  = qranges.get(key);
+
+            double[] values = dap.getInputValues();
+            double[] good   = new double[values.length];
+
+            int idx = 0;
+
+            for (double value: values) {
+                if (value < mm[0] || value > mm[1]) {
+                    String tmp = MSG.error_validate_range();
+                    tmp = tmp.replace("$1", nf.format(value));
+                    tmp = tmp.replace("$2", nf.format(mm[0]));
+                    tmp = tmp.replace("$3", nf.format(mm[1]));
+                    tmpErrors.add(tmp);
+                }
+                else {
+                    good[idx++] = value;
+                }
+            }
+
+            double[] justGood = new double[idx];
+            for (int i = 0; i < justGood.length; i++) {
+                justGood[i] = good[i];
+            }
+
+            if (!tmpErrors.isEmpty()) {
+                dap.setValues(justGood);
+
+                errors.addAll(tmpErrors);
+            }
+        }
+
+        return errors;
+    }
+
+
     protected void initUserDefaults(DataList dataList) {
 
         initUserWQValues(dataList);
@@ -275,6 +391,19 @@
                 createLineTitle(title), null, this, TitleOrientation.LEFT);
 
             wqranges.put(title, dap);
+
+            if (item instanceof WQDataItem) {
+                WQDataItem wq = (WQDataItem) item;
+                double[] mmQ = wq.getQRange();
+                double[] mmW = wq.getWRange();
+
+                GWT.log(title + " Q: " + mmQ[0] + " - " + mmQ[1]);
+                GWT.log(title + " W: " + mmW[0] + " - " + mmW[1]);
+
+                qranges.put(title, mmQ);
+                wranges.put(title, mmW);
+            }
+
             layout.addMember(dap);
         }
 
@@ -340,6 +469,13 @@
     }
 
 
+    public boolean isWMode() {
+        String mode = (String) modes.getValue(FIELD_WQ_MODE);
+
+        return FIELD_WQ_W.equals(mode);
+    }
+
+
     protected Data getWQMode() {
         String wqMode = modes.getValueAsString(FIELD_WQ_MODE);
         DataItem item = new DefaultDataItem("wq_mode", "wq_mode", wqMode);

http://dive4elements.wald.intevation.org