diff flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/MousePositionPanel.java @ 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 e08777967bad
children fea93eebd2fa
line wrap: on
line diff
--- 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