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

Added a first implementation of a MousePositionPanel - work is not finished yet. flys-client/trunk@2025 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 27 May 2011 15:38:24 +0000
parents
children 75df57220104
line wrap: on
line source
package de.intevation.flys.client.client.ui.chart;

import com.smartgwt.client.core.Rectangle;
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;


/**
 * @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;

    protected Label x;
    protected Label y;


    public MousePositionPanel(Canvas window,  Rectangle viewport) {
        super();

        this.window   = window;
        this.viewport = viewport;

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

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


    /**
     * Computes the x|y position (in chart coordinates) and updates the position
     * in the UI.
     *
     * @param event The MouseMoveEvent.
     */
    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();
    }


    /**
     * Computes the lower Y coordinate of the chart.
     *
     * @return the lower Y coordinate of the chart.
     */
    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());
    }


    /**
     * Determines if the y value is in the valid range of the chart.
     *
     * @param y A pixel.
     *
     * @return true if the y position is valid, otherwise false.
     */
    protected boolean validY(int y) {
        return (y > computeMinY() && y < computeMaxY());
    }


    /**
     * Computes the X coordinate in a chart based on the current mouse position.
     *
     * @param pixel A pixel on the screen.
     *
     * @return the computed X value.
     */
    protected double getXPosition(int pixel) {
        // TODO Compute the x relative to the chart range

        return pixel - computeMinX();
    }


    /**
     * 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

        return pixel - computeMinY();
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org