changeset 967:c7f8a9b4b006

Improved parsing i18n values of chart/histogram options (issue289). gnv/trunk@1129 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 26 May 2010 17:03:29 +0000
parents 13de46229f63
children 7a93e4114e6d
files gnv/ChangeLog gnv/src/main/java/de/intevation/gnv/action/ChangeOptionsAction.java gnv/src/main/java/de/intevation/gnv/action/sessionmodel/DiagrammOptions.java gnv/src/main/webapp/WEB-INF/jsp/includes/display_diagramm_options_inc.jsp gnv/src/main/webapp/WEB-INF/jsp/includes/display_histogram_options_inc.jsp
diffstat 5 files changed, 76 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/gnv/ChangeLog	Tue May 25 14:30:25 2010 +0000
+++ b/gnv/ChangeLog	Wed May 26 17:03:29 2010 +0000
@@ -1,3 +1,24 @@
+2010-05-26  Ingo Weinzierl <ingo.weinzierl@intevation.de>
+
+	  Issue289
+
+	* src/main/java/de/intevation/gnv/action/ChangeOptionsAction.java: Store
+	  chart/histogram options inserted by the user in the SessionModel. In the
+	  case of a wrong input we are able to reuse the previous value. Options
+	  inserted by the user are stored as native objects - no longer as string.
+
+	* src/main/java/de/intevation/gnv/action/sessionmodel/DiagrammOptions.java:
+	  Store options as Object instead of String to keep the information about
+	  the type of the option (string, integer, double, etc). Furthermore there
+	  is a new method getValue(String key, Locale locale) that returns the value
+	  as string. Double values are formatted using the locale object. This
+	  method is used in the gui to display a well formatted i18n string.
+
+	* src/main/webapp/WEB-INF/jsp/includes/display_histogram_options_inc.jsp,
+	  src/main/webapp/WEB-INF/jsp/includes/display_diagramm_options_inc.jsp:
+	  Make use of the new method in DiagrammOptions to retrieve the value as
+	  formatted string.
+
 2010-05-25  Ingo Weinzierl <ingo.weinzierl@intevation.de>
 
 	  Issue272
--- a/gnv/src/main/java/de/intevation/gnv/action/ChangeOptionsAction.java	Tue May 25 14:30:25 2010 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/action/ChangeOptionsAction.java	Wed May 26 17:03:29 2010 +0000
@@ -55,19 +55,23 @@
 
             ArtifactDescription ad = sm.getArtifactDescription();
 
-            String target = request.getParameter("target");
+            String target         = request.getParameter("target");
             OutputMode outputMode = sm.getOutputMode(target);
+            Locale locale         = sm.getCurrentLocale();
+
             if (outputMode != null) {
-
                 Collection<OutputParameter> op = outputMode
                         .getOutputParameters();
                 if (op != null) {
                     Iterator<OutputParameter> it = op.iterator();
+                    DiagrammOptions oldOptions      = sm.getDiagrammOptions();
                     DiagrammOptions diagrammOptions = new DiagrammOptions();
                     while (it.hasNext()) {
                         OutputParameter parameter = it.next();
                         String name  = parameter.getName();
-                        String old   = parameter.getValue();
+                        Object old   = oldOptions != null
+                            ? oldOptions.getValue(name)
+                            : parameter.getValue();
                         String value = request.getParameter(name);
                         String type  = parameter.getType();
 
@@ -108,10 +112,14 @@
                             }
                         }
                         else if (type.equalsIgnoreCase("double")) {
-                            if (validDouble(request.getLocale(), value)) {
-                                diagrammOptions.setValue(name, value);
+                            try {
+                                double val    = parseDoubleValue(locale, value);
+                                String valStr = Double.toString(val);
+                                log.debug("Change diagram options ["
+                                    + name +"] to " + val);
+                                diagrammOptions.setValue(name, val);
                             }
-                            else {
+                            catch (ParseException pe) {
                                 log.warn("Text is not a valid double: "+value);
                                 diagrammOptions.setValue(name, old);
 
@@ -178,19 +186,14 @@
         }
     }
 
-    protected boolean validDouble(Locale locale, String value) {
-        try {
-            NumberFormat format = NumberFormat.getInstance(locale);
-            Number       number = format.parse(value);
+    protected double parseDoubleValue(Locale locale, String value)
+    throws ParseException
+    {
+        log.error("LOCALE FOR DOUBLE PARSING: " + locale.toString());
+        NumberFormat format = NumberFormat.getNumberInstance(locale);
+        Number       number = format.parse(value);
 
-            if (number.doubleValue() < 0)
-                return false;
-
-            return (number instanceof Double) || validInteger(locale, value);
-        }
-        catch (ParseException pe) {
-            return false;
-        }
+        return number.doubleValue();
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/gnv/src/main/java/de/intevation/gnv/action/sessionmodel/DiagrammOptions.java	Tue May 25 14:30:25 2010 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/action/sessionmodel/DiagrammOptions.java	Wed May 26 17:03:29 2010 +0000
@@ -1,6 +1,9 @@
 package de.intevation.gnv.action.sessionmodel;
 
+import java.text.NumberFormat;
+
 import java.util.HashMap;
+import java.util.Locale;
 import java.util.Map;
 
 /**
@@ -11,7 +14,7 @@
  */
 public class DiagrammOptions {
 
-    private Map<String, String> values = new HashMap<String, String>();
+    private Map<String, Object> values = new HashMap<String, Object>();
 
     /**
      * Constructor
@@ -25,19 +28,38 @@
      * @param  key The key of the value that should be returned.
      * @return the value.
      */
-    public String getValue(String key) {
+    public Object getValue(String key) {
         return this.values.get(key);
     }
 
 
     /**
+     * Returns the value as string.
+     * @param key The key of the value that should be returned.
+     * @param locale A locale object used to format numbers.
+     * @return the value as string.
+     */
+    public String getValue(String key, Locale locale) {
+        Object obj = values.get(key);
+
+        if (obj instanceof Double && locale != null) {
+            Double value        = (Double) obj;
+            NumberFormat format = NumberFormat.getNumberInstance(locale);
+
+            return format.format(value);
+        }
+
+        return (String) obj;
+    }
+
+    /**
      * Set a value with the given key.
      *
      * @param key The given key.
      * @param value The value to be stored.
      */
-    public void setValue(String key, String value) {
-        this.values.put(key, value);
+    public void setValue(String key, Object value) {
+        values.put(key, value);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/gnv/src/main/webapp/WEB-INF/jsp/includes/display_diagramm_options_inc.jsp	Tue May 25 14:30:25 2010 +0000
+++ b/gnv/src/main/webapp/WEB-INF/jsp/includes/display_diagramm_options_inc.jsp	Wed May 26 17:03:29 2010 +0000
@@ -9,6 +9,7 @@
 <%@page import="de.intevation.gnv.artifactdatabase.objects.ExportMode"%>
 <%@page import="java.util.Collection"%>
 <%@page import="java.util.Iterator"%>
+<%@page import="java.util.Locale"%>
 <%@page import="java.net.URLEncoder"%>
 <% 
     String exceptionMsg             = (String)request.getAttribute(CommunicationKeys.REQUEST_EXCEPTION_MESSAGE);
@@ -18,6 +19,7 @@
     String targetSVG                = "svg";
     String targetIMG                = "img";
     SessionModel sm                 = SessionModelFactory.getInstance().getSessionModel(request);
+    Locale locale                   = sm.getCurrentLocale();
     OutputMode outputMode           = sm.getOutputMode(target);
     DiagrammOptions diagrammOptions = sm.getDiagrammOptions();
 
@@ -77,13 +79,13 @@
                   </td>
                   <td>
                       <%if (om.getType().equalsIgnoreCase("boolean")){
-                          boolean checked = useDiagrammOptions ? "true".equalsIgnoreCase(diagrammOptions.getValue(om.getName())) : om.getValue().equalsIgnoreCase("true");
+                          boolean checked = useDiagrammOptions ? "true".equalsIgnoreCase(diagrammOptions.getValue(om.getName(), locale)) : om.getValue().equalsIgnoreCase("true");
                       %>
                             <input type="checkbox" 
                                    name="<%=om.getName() %>" 
                                    <%=checked ? " value=\"true\" checked=\"checked\"": "" %>/>
                       <%}else{%>
-                            <input type="text" name="<%=om.getName() %>" value="<%=useDiagrammOptions ? diagrammOptions.getValue(om.getName()): om.getValue()%>"/>
+                            <input type="text" name="<%=om.getName() %>" value="<%=useDiagrammOptions ? diagrammOptions.getValue(om.getName(), locale): om.getValue()%>"/>
                       <%}%>
                   </td>
               </tr>
--- a/gnv/src/main/webapp/WEB-INF/jsp/includes/display_histogram_options_inc.jsp	Tue May 25 14:30:25 2010 +0000
+++ b/gnv/src/main/webapp/WEB-INF/jsp/includes/display_histogram_options_inc.jsp	Wed May 26 17:03:29 2010 +0000
@@ -9,6 +9,7 @@
 <%@page import="de.intevation.gnv.artifactdatabase.objects.ExportMode"%>
 <%@page import="java.util.Collection"%>
 <%@page import="java.util.Iterator"%>
+<%@page import="java.util.Locale"%>
 <%@page import="java.net.URLEncoder"%>
 <%
     String exceptionMsg             = (String)request.getAttribute(CommunicationKeys.REQUEST_EXCEPTION_MESSAGE);
@@ -18,6 +19,7 @@
     String targetSVG                = "svg";
     String targetIMG                = "img";
     SessionModel sm                 = SessionModelFactory.getInstance().getSessionModel(request);
+    Locale locale                   = sm.getCurrentLocale();
     OutputMode outputMode           = sm.getOutputMode(target);
     DiagrammOptions diagrammOptions = sm.getDiagrammOptions();
 
@@ -102,13 +104,13 @@
                   </td>
                   <td>
                       <%if (om.getType().equalsIgnoreCase("boolean")){
-                          boolean checked = useDiagrammOptions ? "true".equalsIgnoreCase(diagrammOptions.getValue(om.getName())) : om.getValue().equalsIgnoreCase("true");
+                          boolean checked = useDiagrammOptions ? "true".equalsIgnoreCase(diagrammOptions.getValue(om.getName(), locale)) : om.getValue().equalsIgnoreCase("true");
                       %>
                             <input type="checkbox" 
                                    name="<%=om.getName() %>" 
                                    <%=checked ? " value=\"true\" checked=\"checked\"": "" %>/>
                       <%}else{%>
-                            <input type="text" name="<%=om.getName() %>" value="<%=useDiagrammOptions ? diagrammOptions.getValue(om.getName()): om.getValue()%>"/>
+                            <input type="text" name="<%=om.getName() %>" value="<%=useDiagrammOptions ? diagrammOptions.getValue(om.getName(), locale): om.getValue()%>"/>
                       <%}%>
                   </td>
               </tr>

http://dive4elements.wald.intevation.org