# HG changeset patch # User Ingo Weinzierl # Date 1274893409 0 # Node ID c7f8a9b4b006018c6eb7ff73550de4b475254df8 # Parent 13de46229f636b18605b3c6c1bce50ad111db401 Improved parsing i18n values of chart/histogram options (issue289). gnv/trunk@1129 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 13de46229f63 -r c7f8a9b4b006 gnv/ChangeLog --- 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 + + 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 Issue272 diff -r 13de46229f63 -r c7f8a9b4b006 gnv/src/main/java/de/intevation/gnv/action/ChangeOptionsAction.java --- 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 op = outputMode .getOutputParameters(); if (op != null) { Iterator 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 : diff -r 13de46229f63 -r c7f8a9b4b006 gnv/src/main/java/de/intevation/gnv/action/sessionmodel/DiagrammOptions.java --- 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 values = new HashMap(); + private Map values = new HashMap(); /** * 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 : diff -r 13de46229f63 -r c7f8a9b4b006 gnv/src/main/webapp/WEB-INF/jsp/includes/display_diagramm_options_inc.jsp --- 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 @@ <%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"); %> /> <%}else{%> - + <%}%> diff -r 13de46229f63 -r c7f8a9b4b006 gnv/src/main/webapp/WEB-INF/jsp/includes/display_histogram_options_inc.jsp --- 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 @@ <%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"); %> /> <%}else{%> - + <%}%>