diff flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/PanControl.java @ 551:13c7f90917fc

Added a PanControl to pan charts. flys-client/trunk@2064 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 07 Jun 2011 13:07:27 +0000
parents
children c5fc3fa02edb
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/PanControl.java	Tue Jun 07 13:07:27 2011 +0000
@@ -0,0 +1,166 @@
+package de.intevation.flys.client.client.ui.chart;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.google.gwt.core.client.GWT;
+
+import com.smartgwt.client.types.Overflow;
+import com.smartgwt.client.types.SelectionType;
+import com.smartgwt.client.widgets.Canvas;
+import com.smartgwt.client.widgets.ImgButton;
+import com.smartgwt.client.widgets.events.MouseDownEvent;
+import com.smartgwt.client.widgets.events.MouseDownHandler;
+import com.smartgwt.client.widgets.events.MouseMoveEvent;
+import com.smartgwt.client.widgets.events.MouseMoveHandler;
+import com.smartgwt.client.widgets.events.MouseOutEvent;
+import com.smartgwt.client.widgets.events.MouseOutHandler;
+import com.smartgwt.client.widgets.events.MouseUpEvent;
+import com.smartgwt.client.widgets.events.MouseUpHandler;
+
+import de.intevation.flys.client.client.event.HasPanHandlers;
+import de.intevation.flys.client.client.event.PanEvent;
+import de.intevation.flys.client.client.event.PanHandler;
+
+
+/**
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+public class PanControl
+extends      ImgButton
+implements   MouseDownHandler, MouseMoveHandler, MouseUpHandler,
+             MouseOutHandler, HasPanHandlers
+{
+    protected ChartOutputTab chartTab;
+
+    protected List<PanHandler> handlers;
+
+    protected int[] start;
+    protected int[] end;
+
+
+    public PanControl(ChartOutputTab chartTab, String imageUrl) {
+        super();
+
+        this.chartTab = chartTab;
+        this.handlers = new ArrayList<PanHandler>();
+        this.start    = new int[] { -1, -1 };
+        this.end      = new int[] { -1, -1 };
+
+        String baseUrl = GWT.getHostPageBaseURL();
+        setSrc(baseUrl + imageUrl);
+        setActionType(SelectionType.CHECKBOX);
+        setSize(20);
+        setShowRollOver(false);
+        setSelected(false);
+
+        chartTab.getChartPanel().addMouseDownHandler(this);
+        chartTab.getChartPanel().addMouseMoveHandler(this);
+        chartTab.getChartPanel().addMouseUpHandler(this);
+        chartTab.getChartPanel().addMouseOutHandler(this);
+    }
+
+
+    /**
+     * Method used to register a new PanHandler.
+     *
+     * @param handler A new PanHandler.
+     */
+    public void addPanHandler(PanHandler handler) {
+        if (handler != null) {
+            handlers.add(handler);
+        }
+    }
+
+
+    /**
+     * This event starts the dragging operation if the control is activated.
+     *
+     * @param event The mouse down event which contains the start coordinates.
+     */
+    public void onMouseDown(MouseDownEvent event) {
+        if (!isSelected()) {
+            return;
+        }
+
+        Canvas parent = chartTab.getChartPanel().getParentElement();
+        parent.setOverflow(Overflow.HIDDEN);
+
+        start[0] = event.getX();
+        start[1] = event.getY();
+
+        end[0] = start[0];
+        end[1] = start[1];
+    }
+
+
+    /**
+     * This event is used to reposition the chart image based on the current
+     * drag operation.
+     *
+     * @param event The move event which contains the new coordinates to update
+     * the chart image position.
+     */
+    public void onMouseMove(MouseMoveEvent event) {
+        if (!isSelected() || start[0] == -1 || start[1] == -1) {
+            return;
+        }
+
+        int x = event.getX() - end[0];
+        int y = event.getY() - end[1];
+
+        end[0] = end[0] + x;
+        end[1] = end[1] + y;
+
+        Canvas c = chartTab.getChartPanel();
+        c.moveBy(x, y);
+    }
+
+
+    /**
+     * This event stops the dragging operation and fires a DragEnd event to the
+     * registered listeners.
+     *
+     * @param event The mouse up event which contains the end coordinates.
+     */
+    public void onMouseUp(MouseUpEvent event) {
+        if (!isSelected()) {
+            return;
+        }
+
+        end[0] = event.getX();
+        end[1] = event.getY();
+
+        Canvas parent = chartTab.getChartPanel().getParentElement();
+        parent.setOverflow(Overflow.AUTO);
+
+        fireOnPan();
+
+        start[0] = -1;
+        start[1] = -1;
+    }
+
+
+    /**
+     * This event is used to cancel the current dragging operation.
+     *
+     * @param event The mouse out event.
+     */
+    public void onMouseOut(MouseOutEvent event) {
+        // TODO Cancel dragging if mouse pointer leaves chart area
+    }
+
+
+    /**
+     * A pan event is fired to inform the registered listeners about a pan
+     * operation has finished.
+     */
+    protected void fireOnPan() {
+        PanEvent event = new PanEvent(start, end);
+
+        for (PanHandler handler: handlers) {
+            handler.onPan(event);
+        }
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org