view 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 source
package de.intevation.flys.client.client.ui.chart;

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 de.intevation.flys.client.shared.Transform2D;


/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class MousePositionPanel extends HLayout implements MouseMoveHandler {

    protected ChartOutputTab chartTab;

    protected NumberFormat nf;

    protected Label x;
    protected Label y;


    public MousePositionPanel(ChartOutputTab chartTab) {
        super();

        this.chartTab = chartTab;

        x = new Label();
        y = new Label();

        nf = NumberFormat.getDecimalFormat();

        chartTab.getChartPanel().addMouseMoveHandler(this);

        initLayout();
    }


    /**
     * Initializes the layout of this component.
     */
    protected void initLayout() {
        setLayoutAlign(Alignment.RIGHT);
        setMembersMargin(5);

        Label xDesc = new Label("Position: X = ");
        Label yDesc  = new Label("Y = ");

        xDesc.setWidth(60);
        x.setWidth(25);
        yDesc.setWidth(20);
        y.setWidth(25);

        addMember(xDesc);
        addMember(x);
        addMember(yDesc);
        addMember(y);
    }


    /**
     * /Updates the X value displayed in the <i>x</i> label.
     *
     * @param x the new x value.
     */
    public void setX(double x) {
        this.x.setContents(nf.format(x));
    }


    /**
     * /Updates the Y value displayed in the <i>y</i> label.
     *
     * @param y the new y value.
     */
    public void setY(double y) {
        this.y.setContents(nf.format(y));
    }


    /**
     * Listens to mouse move events to refresh the xy position.
     *
     * @param event The move event.
     */
    public void onMouseMove(MouseMoveEvent event) {
        updateMousePosition(event.getX(), event.getY());
    }


    /**
     * This method takes pixel coordinates, transforms those values into chart
     * coordinates using the Transform2D class and updates the mouse position.
     *
     * @param x The x part of the pixel.
     * @param y The y part of the pixel.
     */
    public void updateMousePosition(double x, double y) {
        Transform2D transformer = chartTab.getTransformer();

        if (transformer == null) {
            return;
        }

        Canvas chart = chartTab.getChartPanel();
        int xOffset = chart.getPageLeft();
        int yOffset = chart.getPageTop();

        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