changeset 562:9f16ac843dda

Introduced a client side validation before new user data are sent to the server - the range/location panel already implements this validation. flys-client/trunk@2097 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 09 Jun 2011 14:06:10 +0000
parents 460b8e0f0563
children 469528551b78
files flys-client/ChangeLog flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.java flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.properties flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_de.properties flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_en.properties flys-client/src/main/java/de/intevation/flys/client/client/ui/AbstractUIProvider.java flys-client/src/main/java/de/intevation/flys/client/client/ui/DoubleRangePanel.java flys-client/src/main/java/de/intevation/flys/client/client/ui/LocationDistancePanel.java
diffstat 8 files changed, 159 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/flys-client/ChangeLog	Thu Jun 09 10:57:42 2011 +0000
+++ b/flys-client/ChangeLog	Thu Jun 09 14:06:10 2011 +0000
@@ -1,3 +1,22 @@
+2011-06-09  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/client/client/FLYSConstants.properties,
+	  src/main/java/de/intevation/flys/client/client/FLYSConstants_en.properties,
+	  src/main/java/de/intevation/flys/client/client/FLYSConstants_de.properties,
+	  src/main/java/de/intevation/flys/client/client/FLYSConstants.java:
+	  Added error strings for the range range/location panel.
+
+	* src/main/java/de/intevation/flys/client/client/ui/AbstractUIProvider.java:
+	  Added a validate() method that is called before the event to step
+	  forward is fired. If the method returns a list with errors, the event is
+	  not fired. In that case, an error dialog is displayed.
+
+	* src/main/java/de/intevation/flys/client/client/ui/LocationDistancePanel.java:
+	  Override the validate() method of AbstractUIProvider.
+
+	* src/main/java/de/intevation/flys/client/client/ui/DoubleRangePanel.java:
+	  Added a method that validates the form.
+
 2011-06-09  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/client/server/ChartInfoServiceImpl.java:
--- a/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.java	Thu Jun 09 10:57:42 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.java	Thu Jun 09 14:06:10 2011 +0000
@@ -190,6 +190,12 @@
 
     // ERRORS
 
+    String error_validate_range();
+
+    String error_validate_lower_range();
+
+    String error_validate_upper_range();
+
     String error_create_artifact();
 
     String error_describe_artifact();
--- a/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.properties	Thu Jun 09 10:57:42 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.properties	Thu Jun 09 14:06:10 2011 +0000
@@ -91,6 +91,9 @@
 wst = WST
 chart_themepanel_header_themes = Theme
 
+error_validate_range = The value $1 needs to be smaller than $3 and bigger than $2.
+error_validate_lower_range = The lower value $1 needs to be bigger than $2.
+error_validate_upper_range = The upper value $1 needs to be smaller than $2.
 error_empty_state = Required inputs are missing.
 error_invalid_double_value = Wrong format for a floating point number.
 error_create_artifact = Error while creating new project.
--- a/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_de.properties	Thu Jun 09 10:57:42 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_de.properties	Thu Jun 09 14:06:10 2011 +0000
@@ -91,6 +91,9 @@
 wst = WST
 chart_themepanel_header_themes = Thema
 
+error_validate_range = Der untere Wert $1 muss kleiner als $3 und gr\u00f6\u00dfer als $2 sein.
+error_validate_lower_range = Der untere Wert $1 muss gr\u00f6\u00dfer sein als $2.
+error_validate_upper_range = Der obere Wert $1 muss kleiner sein als $2.
 error_empty_state = Es wurden nicht alle ben\u00d6tigten Daten eingegeben.
 error_invalid_double_value = Falsches Format f\u00fc eine Flie\u00dfkommazahl.
 error_create_artifact = Fehler beim Erstellen eines neuen Projekts.
--- a/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_en.properties	Thu Jun 09 10:57:42 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_en.properties	Thu Jun 09 14:06:10 2011 +0000
@@ -91,6 +91,9 @@
 wst = WST
 chart_themepanel_header_themes = Theme
 
+error_validate_range = The value $1 needs to be smaller than $3 and bigger than $2.
+error_validate_lower_range = The lower value $1 needs to be bigger than $2.
+error_validate_upper_range = The upper value $1 needs to be smaller than $2.
 error_empty_state = Required inputs are missing.
 error_invalid_double_value = Wrong format for a floating point number.
 error_create_artifact = Error while creating new project.
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/AbstractUIProvider.java	Thu Jun 09 10:57:42 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/AbstractUIProvider.java	Thu Jun 09 14:06:10 2011 +0000
@@ -5,6 +5,7 @@
 
 import com.google.gwt.core.client.GWT;
 
+import com.smartgwt.client.util.SC;
 import com.smartgwt.client.widgets.Canvas;
 import com.smartgwt.client.widgets.Img;
 import com.smartgwt.client.widgets.events.ClickEvent;
@@ -116,9 +117,21 @@
      * @param e The click event.
      */
     public void onClick(ClickEvent e) {
-        Data[] data = getData();
+        List<String> errors = validate();
+        if (errors == null || errors.isEmpty()) {
+            Data[] data = getData();
+            fireStepForwardEvent(new StepForwardEvent(data));
+        }
+        else {
+            StringBuilder sb = new StringBuilder();
 
-        fireStepForwardEvent(new StepForwardEvent(data));
+            for (String error: errors) {
+                sb.append(error);
+                sb.append("<br>");
+            }
+
+            SC.warn(sb.toString());
+        }
     }
 
 
@@ -216,6 +229,11 @@
     }
 
 
+    public List<String> validate() {
+        return new ArrayList<String>();
+    }
+
+
     /**
      * This method needs to be implemented by concrete subclasses. It should
      * create a new Canvas object with a representation of <i>data</i>.
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/DoubleRangePanel.java	Thu Jun 09 10:57:42 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/DoubleRangePanel.java	Thu Jun 09 14:06:10 2011 +0000
@@ -120,6 +120,14 @@
         stepItem.setValue(f.format(steps));
     }
 
+
+    public boolean validateForm() {
+        return
+            validateForm(fromItem) &&
+            validateForm(toItem) &&
+            validateForm(stepItem);
+    }
+
     /**
      * This method validates the entered text in the input fields. If
      * there are values that doesn't represent a valid float, an error is
@@ -140,6 +148,10 @@
         Map errors     = getErrors();
 
         try {
+            if (v == null) {
+                throw new NumberFormatException("empty");
+            }
+
             double value = f.parse(v);
 
             errors.remove(item.getFieldName());
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/LocationDistancePanel.java	Thu Jun 09 10:57:42 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/LocationDistancePanel.java	Thu Jun 09 14:06:10 2011 +0000
@@ -1,10 +1,12 @@
 package de.intevation.flys.client.client.ui;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 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.widgets.Canvas;
@@ -538,6 +540,97 @@
     }
 
 
+    @Override
+    public List<String> validate() {
+        if (isLocationMode()) {
+            return validateLocations();
+        }
+        else {
+            return validateRange();
+        }
+    }
+
+
+    protected List<String> validateLocations() {
+        List<String> errors = new ArrayList<String>();
+        NumberFormat nf     = NumberFormat.getDecimalFormat();
+
+        try {
+            saveLocationValues(locationPanel);
+        }
+        catch (Exception e) {
+            errors.add(MESSAGES.wrongFormat());
+        }
+
+        double[] values = getLocationValues();
+        double[] good   = new double[values.length];
+        int      idx    = 0;
+
+        for (double value: values) {
+            if (value < min || value > max) {
+                String tmp = MESSAGES.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()) {
+            locationPanel.setValues(justGood);
+        }
+
+        return errors;
+    }
+
+
+    protected List<String> validateRange() {
+        List<String> errors = new ArrayList<String>();
+        NumberFormat nf     = NumberFormat.getDecimalFormat();
+
+        try {
+            saveDistanceValues(distancePanel);
+        }
+        catch (Exception e) {
+            errors.add(MESSAGES.wrongFormat());
+        }
+
+        double from = getFrom();
+        double to   = getTo();
+        double step = getStep();
+
+        if (from < min || from > max) {
+            String tmp = MESSAGES.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 = MESSAGES.error_validate_upper_range();
+            tmp = tmp.replace("$1", nf.format(to));
+            tmp = tmp.replace("$2", nf.format(max));
+            errors.add(tmp);
+            to = max;
+        }
+
+        if (!errors.isEmpty()) {
+            distancePanel.setValues(from, to, step);
+        }
+
+        return errors;
+    }
+
+
     /**
      * This method returns the selected data.
      *

http://dive4elements.wald.intevation.org