changeset 1597:8bbaa0d173cf

Save zoom parameters as Number; added new Axis types NumberAxis and DateAxis. flys-client/trunk@3923 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 06 Feb 2012 13:27:05 +0000
parents 0685a4969e93
children ef745bc6bed9
files flys-client/ChangeLog flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartOutputTab.java flys-client/src/main/java/de/intevation/flys/client/server/ChartInfoServiceImpl.java flys-client/src/main/java/de/intevation/flys/client/shared/model/Axis.java flys-client/src/main/java/de/intevation/flys/client/shared/model/DateAxis.java flys-client/src/main/java/de/intevation/flys/client/shared/model/NumberAxis.java flys-client/src/main/java/de/intevation/flys/client/shared/model/ZoomObj.java
diffstat 7 files changed, 328 insertions(+), 125 deletions(-) [+]
line wrap: on
line diff
--- a/flys-client/ChangeLog	Mon Feb 06 12:38:48 2012 +0000
+++ b/flys-client/ChangeLog	Mon Feb 06 13:27:05 2012 +0000
@@ -1,3 +1,26 @@
+2012-02-06  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/client/shared/model/Axis.java: Axis is an
+	  interface now. The code moved to NumberAxis which should be the default
+	  Axis.
+
+	* src/main/java/de/intevation/flys/client/shared/model/NumberAxis.java: New.
+	  This axis stores the parameter min, max, from and to as double values.
+
+	* src/main/java/de/intevation/flys/client/shared/model/DateAxis.java: New.
+	  This axis stores the parameter min, max, from and to as long values. These
+	  values represent the miliseconds until January 1, 1970.
+
+	* src/main/java/de/intevation/flys/client/server/ChartInfoServiceImpl.java:
+	  Create new DateAxis instances if the axis type is set to "date". In this
+	  case, the parameters from, to, min and max are parsed as Long values. The
+	  default is to create new instances of NumberAxis and to parse those values
+	  as Double.
+
+	* src/main/java/de/intevation/flys/client/client/ui/chart/ChartOutputTab.java,
+	  src/main/java/de/intevation/flys/client/shared/model/ZoomObj.java: Save
+	  the zoom values as Number.
+
 2012-02-06  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/client/client/FLYSConstants_en.properties,
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartOutputTab.java	Mon Feb 06 12:38:48 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartOutputTab.java	Mon Feb 06 13:27:05 2012 +0000
@@ -92,7 +92,7 @@
 
     /** Stack of ZoomObj to allow 'redo last zoom'-kind of actions. */
     protected Stack<ZoomObj> zoomStack;
-    protected double[]       zoom;
+    protected Number[]       zoom;
 
 
     /**
@@ -116,9 +116,12 @@
         right     = new Canvas();
         xrange    = new int[2];
         yrange    = new int[2];
-        zoom      = new double[4];
         zoomStack = new Stack<ZoomObj>();
 
+        zoom = new Number[] {
+            new Integer(0), new Integer(1),
+            new Integer(0), new Integer(1) };
+
         left.setBorder("1px solid black");
         left.setWidth(THEMEPANEL_MIN_WIDTH);
         left.setMinWidth(THEMEPANEL_MIN_WIDTH);
@@ -223,7 +226,7 @@
     }
 
 
-    protected double[] translateCoordinates() {
+    protected Number[] translateCoordinates() {
         if (xrange == null || (xrange[0] == 0 && xrange[1] == 0)) {
             zoom[0] = 0d;
             zoom[1] = 1d;
@@ -247,34 +250,34 @@
     protected void translateXCoordinates() {
         Axis xAxis = chartInfo.getXAxis(0);
 
-        double xmin   = xAxis.getMin();
-        double xmax   = xAxis.getMax();
-        double xRange = xmax - xmin;
+        Number xmin   = xAxis.getMin();
+        Number xmax   = xAxis.getMax();
+        Number xRange = subtract(xmax, xmin);
 
         Transform2D transformer = getTransformer(0);
 
         double[] start = transformer.transform(xrange[0], yrange[0]);
         double[] end   = transformer.transform(xrange[1], yrange[1]);
 
-        zoom[0] = (start[0] - xmin) / xRange;
-        zoom[1] = (end[0] - xmin) / xRange;
+        zoom[0] = divide(subtract(start[0], xmin), xRange);
+        zoom[1] = divide(subtract(end[0], xmin), xRange);
     }
 
 
     protected void translateYCoordinates() {
         Axis yAxis = chartInfo.getYAxis(0);
 
-        double ymin   = yAxis.getMin();
-        double ymax   = yAxis.getMax();
-        double yRange = ymax - ymin;
+        Number ymin   = yAxis.getMin();
+        Number ymax   = yAxis.getMax();
+        Number yRange = subtract(ymax, ymin);
 
         Transform2D transformer = getTransformer(0);
 
         double[] start = transformer.transform(xrange[0], yrange[0]);
         double[] end   = transformer.transform(xrange[1], yrange[1]);
 
-        zoom[2] = (start[1] - ymin) / yRange;
-        zoom[3] = (end[1] - ymin) / yRange;
+        zoom[2] = divide(subtract(start[1], ymin), yRange);
+        zoom[3] = divide(subtract(end[1], ymin), yRange);
     }
 
 
@@ -297,8 +300,8 @@
         Axis xAxis = chartInfo.getXAxis(0);
         Axis yAxis = chartInfo.getYAxis(0);
 
-        double[] x = panAxis(xAxis, diffX);
-        double[] y = panAxis(yAxis, diffY);
+        Number[] x = panAxis(xAxis, diffX);
+        Number[] y = panAxis(yAxis, diffY);
 
         zoom[0] = x[0];
         zoom[1] = x[1];
@@ -310,12 +313,12 @@
     }
 
 
-    protected double[] panAxis(Axis axis, double diff) {
-        double min = axis.getFrom();
-        double max = axis.getTo();
+    protected Number[] panAxis(Axis axis, double diff) {
+        Number min = axis.getFrom();
+        Number max = axis.getTo();
 
-        min += diff;
-        max += diff;
+        min = add(min, diff);
+        max = add(max, diff);
 
         return computeZoom(axis, min, max);
     }
@@ -349,8 +352,8 @@
         Axis xAxis = chartInfo.getXAxis(0);
         Axis yAxis = chartInfo.getYAxis(0);
 
-        double[] x = zoomAxis(xAxis, factor);
-        double[] y = zoomAxis(yAxis, factor);
+        Number[] x = zoomAxis(xAxis, factor);
+        Number[] y = zoomAxis(yAxis, factor);
 
         zoom[0] = x[0];
         zoom[1] = x[1];
@@ -379,17 +382,19 @@
     }
 
 
-    public double[] zoomAxis(Axis axis, int factor) {
+    public Number[] zoomAxis(Axis axis, int factor) {
         GWT.log("Prepare Axis for zooming (factor: " + factor + ")");
 
-        double min   = axis.getMin();
-        double max   = axis.getMax();
-        double range = max > min ? max - min : min - max;
+        Number min   = axis.getMin();
+        Number max   = axis.getMax();
+        Number range = isBigger(max, min) ? subtract(max, min) : subtract(min, max);
 
-        double curFrom = axis.getFrom();
-        double curTo   = axis.getTo();
+        Number curFrom = axis.getFrom();
+        Number curTo   = axis.getTo();
 
-        double diff = curTo > curFrom ? curTo - curFrom : curFrom - curTo;
+        Number diff = isBigger(curTo, curFrom)
+            ? subtract(curTo, curFrom)
+            : subtract(curFrom, curTo);
 
         GWT.log("    max from    : " + min);
         GWT.log("    max to      : " + max);
@@ -398,28 +403,30 @@
         GWT.log("    current to  : " + curTo);
         GWT.log("    current diff: " + diff);
 
-        double newFrom = curFrom - (diff * factor / 100);
-        double newTo   = curTo + (diff * factor / 100);
+        Number newFrom = subtract(curFrom, divide(multi(diff, factor), 100));
+        Number newTo   = add(curTo, divide(multi(diff, factor), 100));
 
         GWT.log("    new from: " + newFrom);
         GWT.log("    new to  : " + newTo);
 
-        return new double[] {
-            (newFrom - min) / range,
-            (newTo - min) / range
+        return new Number[] {
+            divide(subtract(newFrom, min), range),
+            divide(subtract(newTo, min), range)
         };
     }
 
 
-    public static double[] computeZoom(Axis axis, double min, double max) {
-        double[] zoom = new double[2];
+    public static Number[] computeZoom(Axis axis, Number min, Number max) {
+        Number[] zoom = new Number[2];
 
-        double absMin = axis.getMin();
-        double absMax = axis.getMax();
-        double diff   = absMax > absMin ? absMax - absMin : absMin - absMax;
+        Number absMin = axis.getMin();
+        Number absMax = axis.getMax();
+        Number diff   = isBigger(absMax, absMin)
+            ? subtract(absMax, absMin)
+            : subtract(absMin, absMax);
 
-        zoom[0] = (min - absMin) / diff;
-        zoom[1] = (max - absMin) / diff;
+        zoom[0] = divide(subtract(min, absMin), diff);
+        zoom[1] = divide(subtract(max, absMin), diff);
 
         return zoom;
     }
@@ -577,20 +584,20 @@
         imgUrl += "&width=" + Integer.toString(width);
         imgUrl += "&height=" + Integer.toString(height);
 
-        double[] zoom = getZoomValues();
+        Number[] zoom = getZoomValues();
 
         if (zoom != null) {
-            if (zoom[0] != 0 || zoom[1] != 1) {
+            if (zoom[0].intValue() != 0 || zoom[1].intValue() != 1) {
                 // a zoom range of 0-1 means displaying the whole range. In such
                 // case we don't need to zoom.
-                imgUrl += "&minx=" + Double.toString(zoom[0]);
-                imgUrl += "&maxx=" + Double.toString(zoom[1]);
+                imgUrl += "&minx=" + zoom[0];
+                imgUrl += "&maxx=" + zoom[1];
             }
-            if (zoom[2] != 0 || zoom[3] != 1) {
+            if (zoom[2].intValue() != 0 || zoom[3].intValue() != 1) {
                 // a zoom range of 0-1 means displaying the whole range. In such
                 // case we don't need to zoom.
-                imgUrl += "&miny=" + Double.toString(zoom[2]);
-                imgUrl += "&maxy=" + Double.toString(zoom[3]);
+                imgUrl += "&miny=" + zoom[2];
+                imgUrl += "&maxy=" + zoom[3];
             }
         }
 
@@ -614,20 +621,20 @@
         attr.put("width",  chart.getWidth().toString());
         attr.put("height", chart.getHeight().toString());
 
-        double[] zoom = getZoomValues();
+        Number[] zoom = getZoomValues();
 
         if (zoom != null) {
-            if (zoom[0] != 0 || zoom[1] != 1) {
+            if (zoom[0].intValue() != 0 || zoom[1].intValue() != 1) {
                 // a zoom range of 0-1 means displaying the whole range. In such
                 // case we don't need to zoom.
-                attr.put("minx", Double.toString(zoom[0]));
-                attr.put("maxx", Double.toString(zoom[1]));
+                attr.put("minx", zoom[0].toString());
+                attr.put("maxx", zoom[1].toString());
             }
-            if (zoom[2] != 0 || zoom[3] != 1) {
+            if (zoom[2].intValue() != 0 || zoom[3].intValue() != 1) {
                 // a zoom range of 0-1 means displaying the whole range. In such
                 // case we don't need to zoom.
-                attr.put("miny", Double.toString(zoom[2]));
-                attr.put("maxy", Double.toString(zoom[3]));
+                attr.put("miny", zoom[2].toString());
+                attr.put("maxy", zoom[3].toString());
             }
         }
 
@@ -635,7 +642,7 @@
     }
 
 
-    protected double[] getZoomValues() {
+    protected Number[] getZoomValues() {
         return zoom;
     }
 
@@ -644,5 +651,70 @@
     public CollectionView getView() {
         return this.view;
     }
+
+
+    public static Number subtract(Number left, Number right) {
+        if (left instanceof Double) {
+            return new Double(left.doubleValue() - right.doubleValue());
+        }
+        else if (left instanceof Long) {
+            return new Long(left.longValue() - right.longValue());
+        }
+        else {
+            return new Integer(left.intValue() - right.intValue());
+        }
+    }
+
+
+    public static Number add(Number left, Number right) {
+        if (left instanceof Double) {
+            return new Double(left.doubleValue() + right.doubleValue());
+        }
+        else if (left instanceof Long) {
+            return new Long(left.longValue() + right.longValue());
+        }
+        else {
+            return new Integer(left.intValue() + right.intValue());
+        }
+    }
+
+
+    public static Number divide(Number left, Number right) {
+        if (left instanceof Double) {
+            return new Double(left.doubleValue() / right.doubleValue());
+        }
+        else if (left instanceof Long) {
+            return new Long(left.longValue() / right.longValue());
+        }
+        else {
+            return new Integer(left.intValue() / right.intValue());
+        }
+    }
+
+
+    public static Number multi(Number left, Number right) {
+        if (left instanceof Double) {
+            return new Double(left.doubleValue() * right.doubleValue());
+        }
+        else if (left instanceof Long) {
+            return new Long(left.longValue() * right.longValue());
+        }
+        else {
+            return new Integer(left.intValue() * right.intValue());
+        }
+    }
+
+
+    public static boolean isBigger(Number left, Number right) {
+        if (left instanceof Double) {
+            return left.doubleValue() > right.doubleValue();
+        }
+        else if (left instanceof Long) {
+            return left.longValue() > right.longValue();
+        }
+        else {
+            return left.intValue() > right.intValue();
+        }
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/java/de/intevation/flys/client/server/ChartInfoServiceImpl.java	Mon Feb 06 12:38:48 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/ChartInfoServiceImpl.java	Mon Feb 06 13:27:05 2012 +0000
@@ -3,12 +3,14 @@
 import java.io.InputStream;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
 import javax.xml.xpath.XPathConstants;
 
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
@@ -26,6 +28,8 @@
 import de.intevation.flys.client.shared.Transform2D;
 import de.intevation.flys.client.shared.exceptions.ServerException;
 import de.intevation.flys.client.shared.model.Axis;
+import de.intevation.flys.client.shared.model.DateAxis;
+import de.intevation.flys.client.shared.model.NumberAxis;
 import de.intevation.flys.client.shared.model.ChartInfo;
 import de.intevation.flys.client.shared.model.Collection;
 
@@ -147,30 +151,20 @@
 
         Axis[] result = new Axis[count];
 
-        for (int i = 0; i < count; i++) {
-            Node node = axes.item(i);
-
-            String posStr = XMLUtils.xpathString(
-                node, "@art:pos", ArtifactNamespaceContext.INSTANCE);
+        String ns = ArtifactNamespaceContext.NAMESPACE_URI;
 
-            String fromStr = XMLUtils.xpathString(
-                node, "@art:from", ArtifactNamespaceContext.INSTANCE);
+        for (int i = 0; i < count; i++) {
+            Element node = (Element) axes.item(i);
 
-            String toStr = XMLUtils.xpathString(
-                node, "@art:to", ArtifactNamespaceContext.INSTANCE);
-
-            String minStr = XMLUtils.xpathString(
-                node, "@art:min", ArtifactNamespaceContext.INSTANCE);
-
-            String maxStr = XMLUtils.xpathString(
-                node, "@art:max", ArtifactNamespaceContext.INSTANCE);
+            String posStr   = node.getAttributeNS(ns, "pos");
+            String fromStr  = node.getAttributeNS(ns, "from");
+            String toStr    = node.getAttributeNS(ns, "to");
+            String minStr   = node.getAttributeNS(ns, "min");
+            String maxStr   = node.getAttributeNS(ns, "max");
+            String axisType = node.getAttributeNS(ns, "axistype");
 
             try {
                 int    pos  = Integer.parseInt(posStr);
-                double from = Double.parseDouble(fromStr);
-                double to   = Double.parseDouble(toStr);
-                double min  = Double.parseDouble(minStr);
-                double max  = Double.parseDouble(maxStr);
 
                 if (pos >= result.length) {
                     // this should never happen
@@ -178,7 +172,29 @@
                     continue;
                 }
 
-                result[pos] = new Axis(pos, from, to, min, max);
+                if (axisType != null && axisType.equals(DateAxis.TYPE)) {
+                    long from = Long.parseLong(fromStr);
+                    long to   = Long.parseLong(toStr);
+                    long min  = Long.parseLong(minStr);
+                    long max  = Long.parseLong(maxStr);
+
+                    if (logger.isDebugEnabled()) {
+                        logger.debug("date axis from: " + new Date(from));
+                        logger.debug("date axis to  : " + new Date(to));
+                        logger.debug("date axis min : " + new Date(min));
+                        logger.debug("date axis max : " + new Date(max));
+                    }
+
+                    result[pos] = new DateAxis(pos, from, to, min, max);
+                }
+                else {
+                    double from = Double.parseDouble(fromStr);
+                    double to   = Double.parseDouble(toStr);
+                    double min  = Double.parseDouble(minStr);
+                    double max  = Double.parseDouble(maxStr);
+
+                    result[pos] = new NumberAxis(pos, from, to, min, max);
+                }
             }
             catch (NumberFormatException nfe) {
                 nfe.printStackTrace();
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/Axis.java	Mon Feb 06 12:38:48 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/Axis.java	Mon Feb 06 13:27:05 2012 +0000
@@ -6,52 +6,16 @@
 /**
  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
  */
-public class Axis implements Serializable {
-
-    protected int pos;
-
-    protected double from;
-    protected double to;
-
-    protected double min;
-    protected double max;
-
-
-    public Axis() {
-    }
-
-
-    public Axis(int pos, double from, double to, double min, double max) {
-        this.pos  = pos;
-        this.from = from;
-        this.to   = to;
-        this.min  = min;
-        this.max  = max;
-    }
+public interface Axis extends Serializable {
 
-
-    public int getPos() {
-        return pos;
-    }
-
-
-    public double getFrom() {
-        return from;
-    }
-
+    int getPos();
 
-    public double getTo() {
-        return to;
-    }
-
+    Number getFrom();
 
-    public double getMin() {
-        return min;
-    }
+    Number getTo();
 
+    Number getMin();
 
-    public double getMax() {
-        return max;
-    }
+    Number getMax();
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/DateAxis.java	Mon Feb 06 13:27:05 2012 +0000
@@ -0,0 +1,66 @@
+package de.intevation.flys.client.shared.model;
+
+import java.io.Serializable;
+
+
+/**
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+public class DateAxis implements Axis {
+
+    public static final String TYPE = "date";
+
+
+    protected int pos;
+
+    protected long from;
+    protected long to;
+
+    protected long min;
+    protected long max;
+
+
+    public DateAxis() {
+    }
+
+
+    public DateAxis(int pos, long from, long to, long min, long max) {
+        this.pos  = pos;
+        this.from = from;
+        this.to   = to;
+        this.min  = min;
+        this.max  = max;
+    }
+
+
+    @Override
+    public int getPos() {
+        return pos;
+    }
+
+
+    @Override
+    public Number getFrom() {
+        return from;
+    }
+
+
+    @Override
+    public Number getTo() {
+        return to;
+    }
+
+
+    @Override
+    public Number getMin() {
+        return min;
+    }
+
+
+    @Override
+    public Number getMax() {
+        return max;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/NumberAxis.java	Mon Feb 06 13:27:05 2012 +0000
@@ -0,0 +1,62 @@
+package de.intevation.flys.client.shared.model;
+
+import java.io.Serializable;
+
+
+/**
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+public class NumberAxis implements Axis {
+
+    protected int pos;
+
+    protected double from;
+    protected double to;
+
+    protected double min;
+    protected double max;
+
+
+    public NumberAxis() {
+    }
+
+
+    public NumberAxis(int pos, double from, double to, double min, double max) {
+        this.pos  = pos;
+        this.from = from;
+        this.to   = to;
+        this.min  = min;
+        this.max  = max;
+    }
+
+
+    @Override
+    public int getPos() {
+        return pos;
+    }
+
+
+    @Override
+    public Number getFrom() {
+        return from;
+    }
+
+
+    @Override
+    public Number getTo() {
+        return to;
+    }
+
+
+    @Override
+    public Number getMin() {
+        return min;
+    }
+
+
+    @Override
+    public Number getMax() {
+        return max;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/ZoomObj.java	Mon Feb 06 12:38:48 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/ZoomObj.java	Mon Feb 06 13:27:05 2012 +0000
@@ -5,17 +5,17 @@
 
 public class ZoomObj implements Serializable {
 
-    protected double a;
-    protected double b;
-    protected double c;
-    protected double d;
+    protected Number a;
+    protected Number b;
+    protected Number c;
+    protected Number d;
 
 
     public ZoomObj() {
     }
 
 
-    public ZoomObj(double a, double b, double c, double d) {
+    public ZoomObj(Number a, Number b, Number c, Number d) {
         this.a = a;
         this.b = b;
         this.c = c;
@@ -23,8 +23,8 @@
     }
 
 
-    public double[] getZoom() {
-        return new double[] { a, b, c, d };
+    public Number[] getZoom() {
+        return new Number[] { a, b, c, d };
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org