changeset 538:75df57220104

Adapted the MousePositionPanel to use the Transform2D object for computing the chart coordinates from image coordinates. flys-client/trunk@2036 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 01 Jun 2011 09:03:18 +0000
parents d2c37ba78feb
children fea93eebd2fa
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/client/ui/chart/ChartToolbar.java flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/MousePositionPanel.java
diffstat 4 files changed, 144 insertions(+), 123 deletions(-) [+]
line wrap: on
line diff
--- a/flys-client/ChangeLog	Wed Jun 01 08:50:40 2011 +0000
+++ b/flys-client/ChangeLog	Wed Jun 01 09:03:18 2011 +0000
@@ -1,3 +1,19 @@
+2011-06-01  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/client/client/ui/chart/ChartOutputTab.java:
+	  After the chart panel is resized, we will now fetch a chart info
+	  document using the ChartInfoService. This service lets us update the
+	  current Transform2D - which is used to display mouse position in chart
+	  coordinates and determine mouse clicks for zooming.
+
+	* src/main/java/de/intevation/flys/client/client/ui/chart/ChartToolbar.java:
+	  Changed the constructor call of the MousePositionPanel. It needs to
+	  ChartOutputTab now.
+
+	* src/main/java/de/intevation/flys/client/client/ui/chart/MousePositionPanel.java:
+	  Changed the way to compute the chart coordinates. We will now use the
+	  Transform2D object that is returned by the ChartInfoService.
+
 2011-06-01  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/client/client/ui/CollectionView.java:
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartOutputTab.java	Wed Jun 01 08:50:40 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartOutputTab.java	Wed Jun 01 09:03:18 2011 +0000
@@ -3,10 +3,10 @@
 import java.util.Date;
 
 import com.google.gwt.core.client.GWT;
+import com.google.gwt.user.client.rpc.AsyncCallback;
 
 import com.smartgwt.client.widgets.Canvas;
 import com.smartgwt.client.widgets.Img;
-import com.smartgwt.client.widgets.Label;
 
 import com.smartgwt.client.widgets.layout.HLayout;
 import com.smartgwt.client.widgets.layout.VLayout;
@@ -14,11 +14,14 @@
 import com.smartgwt.client.widgets.events.ResizedEvent;
 import com.smartgwt.client.widgets.events.ResizedHandler;
 
+import de.intevation.flys.client.shared.Transform2D;
 import de.intevation.flys.client.shared.model.Collection;
 import de.intevation.flys.client.shared.model.OutputMode;
 import de.intevation.flys.client.client.Config;
 import de.intevation.flys.client.client.event.OutputParameterChangeEvent;
 import de.intevation.flys.client.client.event.OutputParameterChangeHandler;
+import de.intevation.flys.client.client.services.ChartInfoService;
+import de.intevation.flys.client.client.services.ChartInfoServiceAsync;
 import de.intevation.flys.client.client.ui.CollectionView;
 import de.intevation.flys.client.client.ui.OutputTab;
 
@@ -37,6 +40,16 @@
 
 
 
+    /** The service that is used to fetch chart information.*/
+    protected ChartInfoServiceAsync info = GWT.create(ChartInfoService.class);
+
+    /** The transformer used to transform image pixels into chart coordinates.*/
+    protected Transform2D transformer;
+
+    /** The collection view.*/
+    protected CollectionView view;
+
+
     /** The canvas that wraps the chart toolbar.*/
     protected Canvas tbarPanel;
 
@@ -62,6 +75,7 @@
     ){
         super(title, collection, mode);
 
+        view      = collectionView;
         left      = new Canvas();
         right     = new Canvas();
         tbarPanel = new ChartToolbar(collectionView, this);
@@ -105,6 +119,7 @@
      */
     public void onResized(ResizedEvent event) {
         updateChartPanel();
+        updateTransformer();
     }
 
 
@@ -119,6 +134,35 @@
     }
 
 
+    /**
+     * Updates the Transform2D object using the chart info service.
+     */
+    public void updateTransformer() {
+        Canvas chart = getChartPanel();
+
+        Config config = Config.getInstance();
+        String url    = config.getServerUrl();
+        String locale = config.getLocale();
+
+        info.getChartInfo(
+            view.getCollection(),
+            url,
+            locale,
+            mode.getName(),
+            chart.getWidth(),
+            chart.getHeight(),
+            new AsyncCallback<Transform2D>() {
+                public void onFailure(Throwable caught) {
+                    GWT.log("ERROR: " + caught.getMessage());
+                }
+
+                public void onSuccess(Transform2D transformer) {
+                    setTransformer(transformer);
+                }
+            });
+    }
+
+
     public void updateChartPanel() {
         Canvas[] children = right.getChildren();
         for (Canvas child: children) {
@@ -130,7 +174,36 @@
 
 
     public Canvas getChartPanel() {
-        return right;
+        Canvas[] children = right.getChildren();
+
+        if (children == null || children.length == 0) {
+            GWT.log("=> No chart image in the panel.");
+            return right;
+        }
+
+        return children[0];
+    }
+
+
+    /**
+     * Returns the Transform2D object used to transform image coordinates into
+     * chart coordinates.
+     *
+     * @return the Transform2D object.
+     */
+    public Transform2D getTransformer() {
+        return transformer;
+    }
+
+
+    /**
+     * Set a new Transform2D object for the chart. This should be the case, only
+     * if the chart image size has changed.
+     *
+     * @param transformer The new Transform2D object.
+     */
+    protected void setTransformer(Transform2D transformer) {
+        this.transformer = transformer;
     }
 
 
@@ -149,15 +222,6 @@
     }
 
 
-    protected Canvas createThemeControlPanel() {
-        Label label = new Label("Themensteuerung");
-        label.setHeight(25);
-        label.setMargin(5);
-
-        return label;
-    }
-
-
     /**
      * Builds the chart image and returns it.
      *
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartToolbar.java	Wed Jun 01 08:50:40 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartToolbar.java	Wed Jun 01 09:03:18 2011 +0000
@@ -2,7 +2,6 @@
 
 import com.google.gwt.core.client.GWT;
 
-import com.smartgwt.client.core.Rectangle;
 import com.smartgwt.client.widgets.Button;
 import com.smartgwt.client.widgets.Label;
 import com.smartgwt.client.widgets.layout.HLayout;
@@ -43,10 +42,12 @@
     public ChartToolbar(CollectionView view, ChartOutputTab chartTab) {
         super();
 
-        this.view     = view;
-        this.chartTab = chartTab;
+        this.view       = view;
+        this.chartTab   = chartTab;
 
         datacage = new Button(MSG.databasket());
+        position = new MousePositionPanel(chartTab);
+
         datacage.addClickHandler(new ClickHandler() {
             public void onClick(ClickEvent event) {
                 GWT.log("Clicked 'datacage' button.");
@@ -54,13 +55,6 @@
             }
         });
 
-        // TODO determine the correct viewport of the chart!
-        position = new MousePositionPanel(
-            chartTab.getChartPanel(),
-            new Rectangle(65, 35, 50, 70));
-        chartTab.getChartPanel().addMouseMoveHandler(position);
-        chartTab.getChartPanel().addResizedHandler(position);
-
         initLayout();
     }
 
@@ -79,9 +73,10 @@
 
         // TODO do this dynamic
         Label spacer = new Label();
-        spacer.setWidth("450px");
+        spacer.setWidth("100px");
         datacage.setWidth("95px");
-        position.setWidth("150px");
+        position.setWidth("200px");
+        position.setLayoutAlign(com.smartgwt.client.types.Alignment.RIGHT);
 
         addMember(datacage);
         addMember(spacer);
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/MousePositionPanel.java	Wed Jun 01 08:50:40 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/MousePositionPanel.java	Wed Jun 01 09:03:18 2011 +0000
@@ -1,39 +1,42 @@
 package de.intevation.flys.client.client.ui.chart;
 
-import com.smartgwt.client.core.Rectangle;
+import com.google.gwt.i18n.client.NumberFormat;
+
 import com.smartgwt.client.types.Alignment;
 import com.smartgwt.client.widgets.Canvas;
 import com.smartgwt.client.widgets.Label;
 import com.smartgwt.client.widgets.layout.HLayout;
 import com.smartgwt.client.widgets.events.MouseMoveEvent;
 import com.smartgwt.client.widgets.events.MouseMoveHandler;
-import com.smartgwt.client.widgets.events.ResizedEvent;
-import com.smartgwt.client.widgets.events.ResizedHandler;
+
+import de.intevation.flys.client.shared.Transform2D;
 
 
 /**
  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
  */
-public class MousePositionPanel
-extends      HLayout
-implements   MouseMoveHandler, ResizedHandler
-{
-    protected Canvas    window;
-    protected Rectangle viewport;
+public class MousePositionPanel extends HLayout implements MouseMoveHandler {
+
+    protected ChartOutputTab chartTab;
+
+    protected NumberFormat nf;
 
     protected Label x;
     protected Label y;
 
 
-    public MousePositionPanel(Canvas window,  Rectangle viewport) {
+    public MousePositionPanel(ChartOutputTab chartTab) {
         super();
 
-        this.window   = window;
-        this.viewport = viewport;
+        this.chartTab = chartTab;
 
         x = new Label();
         y = new Label();
 
+        nf = NumberFormat.getDecimalFormat();
+
+        chartTab.getChartPanel().addMouseMoveHandler(this);
+
         initLayout();
     }
 
@@ -61,117 +64,60 @@
 
 
     /**
-     * Computes the x|y position (in chart coordinates) and updates the position
-     * in the UI.
+     * /Updates the X value displayed in the <i>x</i> label.
      *
-     * @param event The MouseMoveEvent.
+     * @param x the new x value.
      */
-    public void onMouseMove(MouseMoveEvent event) {
-        int posX = event.getX();
-        int posY = event.getY();
-
-        if (!validX(posX) || !validY(posY)) {
-            return;
-        }
-
-        x.setContents(String.valueOf(getXPosition(posX)));
-        y.setContents(String.valueOf(getYPosition(posY)));
-    }
-
-
-    public void onResized(ResizedEvent event) {
-    }
-
-
-    /**
-     * Computes the left coordinate of the chart.
-     *
-     * @return the left coordinate of the chart.
-     */
-    protected int computeMinX() {
-        return window.getPageLeft() + viewport.getLeft();
-    }
-
-
-    /**
-     * Computes the right coordinate of the chart.
-     *
-     * @return the right coordinate of the chart.
-     */
-    protected int computeMaxX() {
-        return window.getPageLeft() + window.getWidth() - viewport.getWidth();
-    }
-
-
-    /**
-     * Computes the upper Y coordinate of the chart.
-     *
-     * @return the lower Y coordinate of the chart.
-     */
-    protected int computeMinY() {
-        return window.getPageTop() + viewport.getTop();
+    public void setX(double x) {
+        this.x.setContents(nf.format(x));
     }
 
 
     /**
-     * Computes the lower Y coordinate of the chart.
+     * /Updates the Y value displayed in the <i>y</i> label.
      *
-     * @return the lower Y coordinate of the chart.
+     * @param y the new y value.
      */
-    protected int computeMaxY() {
-        return window.getPageBottom() - viewport.getHeight();
-    }
-
-
-    /**
-     * Determines if the x value is in the valid range of the chart.
-     *
-     * @param x A pixel.
-     *
-     * @return true if the x position is valid, otherwise false.
-     */
-    protected boolean validX(int x) {
-        return (x > computeMinX() && x < computeMaxX());
+    public void setY(double y) {
+        this.y.setContents(nf.format(y));
     }
 
 
     /**
-     * Determines if the y value is in the valid range of the chart.
-     *
-     * @param y A pixel.
+     * Listens to mouse move events to refresh the xy position.
      *
-     * @return true if the y position is valid, otherwise false.
+     * @param event The move event.
      */
-    protected boolean validY(int y) {
-        return (y > computeMinY() && y < computeMaxY());
+    public void onMouseMove(MouseMoveEvent event) {
+        updateMousePosition(event.getX(), event.getY());
     }
 
 
     /**
-     * Computes the X coordinate in a chart based on the current mouse position.
-     *
-     * @param pixel A pixel on the screen.
+     * This method takes pixel coordinates, transforms those values into chart
+     * coordinates using the Transform2D class and updates the mouse position.
      *
-     * @return the computed X value.
+     * @param x The x part of the pixel.
+     * @param y The y part of the pixel.
      */
-    protected double getXPosition(int pixel) {
-        // TODO Compute the x relative to the chart range
-
-        return pixel - computeMinX();
-    }
+    public void updateMousePosition(double x, double y) {
+        Transform2D transformer = chartTab.getTransformer();
 
+        if (transformer == null) {
+            return;
+        }
 
-    /**
-     * Computes the Y coordinate in a chart based on the current mouse position.
-     *
-     * @param pixel A pixel on the screen.
-     *
-     * @return the computed Y value.
-     */
-    protected double getYPosition(int pixel) {
-        // TODO Compute the y relative to the chart range
+        Canvas chart = chartTab.getChartPanel();
+        int xOffset = chart.getPageLeft();
+        int yOffset = chart.getPageTop();
 
-        return pixel - computeMinY();
+        x = x - xOffset;
+        y = y - yOffset;
+
+        double[] xy = transformer.transform(x,y);
+
+        setX(xy[0]);
+        setY(xy[1]);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org