view flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/MousePositionPanel.java @ 4198:1cdbd8a0c994

Added two new tables ClickableQDTable and ClickableWTable and made Ws and Qs clickable in historical discharge calculation. The new tables define listener interfaces (clicked lower or upper icon) to listen to user clicks. In addition to this, there is an enum ClickMode with NONE, SINGLE and RANGE options, which allows to specifiy, which icons are displayed in the tables. NONE means no icon for user clicks, SINGLE has 1 icon, RANGE 2 icons for lower and upper.
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 22 Oct 2012 13:31:25 +0200
parents 0189d6b0973a
children 651b93c10dc5
line wrap: on
line source
package de.intevation.flys.client.client.ui.chart;

import java.util.ArrayList;

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;


/**
 * Panel showing the mouse position in data space.
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class MousePositionPanel extends HLayout implements MouseMoveHandler {

    /** Is associated to a ChartTab. */
    protected ChartOutputTab chartTab;

    protected HLayout xLayout;
    protected ArrayList<HLayout> yLayouts;


    public MousePositionPanel(ChartOutputTab chartTab) {
        super();

        this.chartTab = chartTab;

        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);

        xLayout = null;
        yLayouts = new ArrayList<HLayout>();
    }


    /**
     * 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) {
        int transformerCount = chartTab.getTransformerCount();

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

        x = x - xOffset;
        y = y - yOffset;

        // Create Layout for x coordinates.
        if (xLayout == null){
            Label xDesc  = new Label("Position: X = ");
            Label xLabel = new Label();
            xLayout      = new HLayout();
            xLayout.setWidth(125);
            xLayout.addMember(xDesc);
            xLayout.addMember(xLabel);
            xDesc.setWidth(70);
            xLabel.setWidth(55);
            addMember(xLayout);
        }

        ArrayList<String> yCoordinates = new ArrayList<String>();
        String xCoordinate = "";
        for (int i = 0; i < transformerCount; i++) {
            HLayout yLayout = null;
            // If no layout exists for this y axis, create one.
            // else use the existing one.
            if (yLayouts.size() <= i) {
                Label yDesc     = new Label("Y" + (i+1) + " = ");
                Label yLabel    = new Label();
                yLayout = new HLayout();
                yLayout.setWidth(80);
                yLayout.addMember(yDesc);
                yLayout.addMember(yLabel);
                yDesc.setWidth(30);
                yLabel.setWidth(50);
                addMember(yLayout);
                yLayouts.add(i, yLayout);
            }
            else {
                yLayout = yLayouts.get(i);
            }

            Transform2D transformer = chartTab.getTransformer(i);

            if (transformer == null) {
                return;
            }

            // Get the label for the coordinates.
            Canvas xLabel = xLayout.getMember(1);
            Canvas yLabel = yLayout.getMember(1);

            double[] xy    = transformer.transform(x, y);
            String[] xyStr = transformer.format(new Number[] {
                new Double(xy[0]), new Double(xy[1]) });
            // Set the coordinates.
            xLabel.setContents(xyStr[0]);
            yLabel.setContents(xyStr[1]);
        }

        // Remove y coordinates.
        if (yLayouts.size() > transformerCount) {
            for (int i = yLayouts.size() - 1; i >= transformerCount; i--) {
                removeMember(yLayouts.get(i));
                yLayouts.remove(i);
            }
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org