view flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/MousePositionPanel.java @ 1598:ef745bc6bed9

Distinguish between numbers and dates while parsing chart info documents; the MousePosition panel now displays the position on date axes correctly. flys-client/trunk@3931 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 06 Feb 2012 14:43:27 +0000
parents 78907f0fb939
children 936e3e6cd9b9
line wrap: on
line source
package de.intevation.flys.client.client.ui.chart;

import com.google.gwt.i18n.client.NumberFormat;

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. <b>Note:</b> This layout has a
     * fixed width of 195px plus a margin of 5px.
     */
    protected void initLayout() {
        setMembersMargin(5);

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

        HLayout xLayout = new HLayout();
        xLayout.setWidth(125);
        xLayout.addMember(xDesc);
        xLayout.addMember(x);

        HLayout yLayout = new HLayout();
        yLayout.setWidth(70);
        yLayout.addMember(yDesc);
        yLayout.addMember(y);

        xDesc.setWidth(70);
        x.setWidth(55);
        yDesc.setWidth(20);
        y.setWidth(50);

        addMember(xLayout);
        addMember(yLayout);
    }


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


    /**
     * /Updates the Y value displayed in the <i>y</i> label.
     *
     * @param y the new y value.
     */
    public void setY(String y) {
        this.y.setContents(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);
        String[] xyStr = transformer.format(new Number[] {
            new Double(xy[0]), new Double(xy[1]) });

        setX(xyStr[0]);
        setY(xyStr[1]);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org