Mercurial > dive4elements > river
comparison 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 |
comparison
equal
deleted
inserted
replaced
550:605debcbb6a0 | 551:13c7f90917fc |
---|---|
1 package de.intevation.flys.client.client.ui.chart; | |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.List; | |
5 | |
6 import com.google.gwt.core.client.GWT; | |
7 | |
8 import com.smartgwt.client.types.Overflow; | |
9 import com.smartgwt.client.types.SelectionType; | |
10 import com.smartgwt.client.widgets.Canvas; | |
11 import com.smartgwt.client.widgets.ImgButton; | |
12 import com.smartgwt.client.widgets.events.MouseDownEvent; | |
13 import com.smartgwt.client.widgets.events.MouseDownHandler; | |
14 import com.smartgwt.client.widgets.events.MouseMoveEvent; | |
15 import com.smartgwt.client.widgets.events.MouseMoveHandler; | |
16 import com.smartgwt.client.widgets.events.MouseOutEvent; | |
17 import com.smartgwt.client.widgets.events.MouseOutHandler; | |
18 import com.smartgwt.client.widgets.events.MouseUpEvent; | |
19 import com.smartgwt.client.widgets.events.MouseUpHandler; | |
20 | |
21 import de.intevation.flys.client.client.event.HasPanHandlers; | |
22 import de.intevation.flys.client.client.event.PanEvent; | |
23 import de.intevation.flys.client.client.event.PanHandler; | |
24 | |
25 | |
26 /** | |
27 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
28 */ | |
29 public class PanControl | |
30 extends ImgButton | |
31 implements MouseDownHandler, MouseMoveHandler, MouseUpHandler, | |
32 MouseOutHandler, HasPanHandlers | |
33 { | |
34 protected ChartOutputTab chartTab; | |
35 | |
36 protected List<PanHandler> handlers; | |
37 | |
38 protected int[] start; | |
39 protected int[] end; | |
40 | |
41 | |
42 public PanControl(ChartOutputTab chartTab, String imageUrl) { | |
43 super(); | |
44 | |
45 this.chartTab = chartTab; | |
46 this.handlers = new ArrayList<PanHandler>(); | |
47 this.start = new int[] { -1, -1 }; | |
48 this.end = new int[] { -1, -1 }; | |
49 | |
50 String baseUrl = GWT.getHostPageBaseURL(); | |
51 setSrc(baseUrl + imageUrl); | |
52 setActionType(SelectionType.CHECKBOX); | |
53 setSize(20); | |
54 setShowRollOver(false); | |
55 setSelected(false); | |
56 | |
57 chartTab.getChartPanel().addMouseDownHandler(this); | |
58 chartTab.getChartPanel().addMouseMoveHandler(this); | |
59 chartTab.getChartPanel().addMouseUpHandler(this); | |
60 chartTab.getChartPanel().addMouseOutHandler(this); | |
61 } | |
62 | |
63 | |
64 /** | |
65 * Method used to register a new PanHandler. | |
66 * | |
67 * @param handler A new PanHandler. | |
68 */ | |
69 public void addPanHandler(PanHandler handler) { | |
70 if (handler != null) { | |
71 handlers.add(handler); | |
72 } | |
73 } | |
74 | |
75 | |
76 /** | |
77 * This event starts the dragging operation if the control is activated. | |
78 * | |
79 * @param event The mouse down event which contains the start coordinates. | |
80 */ | |
81 public void onMouseDown(MouseDownEvent event) { | |
82 if (!isSelected()) { | |
83 return; | |
84 } | |
85 | |
86 Canvas parent = chartTab.getChartPanel().getParentElement(); | |
87 parent.setOverflow(Overflow.HIDDEN); | |
88 | |
89 start[0] = event.getX(); | |
90 start[1] = event.getY(); | |
91 | |
92 end[0] = start[0]; | |
93 end[1] = start[1]; | |
94 } | |
95 | |
96 | |
97 /** | |
98 * This event is used to reposition the chart image based on the current | |
99 * drag operation. | |
100 * | |
101 * @param event The move event which contains the new coordinates to update | |
102 * the chart image position. | |
103 */ | |
104 public void onMouseMove(MouseMoveEvent event) { | |
105 if (!isSelected() || start[0] == -1 || start[1] == -1) { | |
106 return; | |
107 } | |
108 | |
109 int x = event.getX() - end[0]; | |
110 int y = event.getY() - end[1]; | |
111 | |
112 end[0] = end[0] + x; | |
113 end[1] = end[1] + y; | |
114 | |
115 Canvas c = chartTab.getChartPanel(); | |
116 c.moveBy(x, y); | |
117 } | |
118 | |
119 | |
120 /** | |
121 * This event stops the dragging operation and fires a DragEnd event to the | |
122 * registered listeners. | |
123 * | |
124 * @param event The mouse up event which contains the end coordinates. | |
125 */ | |
126 public void onMouseUp(MouseUpEvent event) { | |
127 if (!isSelected()) { | |
128 return; | |
129 } | |
130 | |
131 end[0] = event.getX(); | |
132 end[1] = event.getY(); | |
133 | |
134 Canvas parent = chartTab.getChartPanel().getParentElement(); | |
135 parent.setOverflow(Overflow.AUTO); | |
136 | |
137 fireOnPan(); | |
138 | |
139 start[0] = -1; | |
140 start[1] = -1; | |
141 } | |
142 | |
143 | |
144 /** | |
145 * This event is used to cancel the current dragging operation. | |
146 * | |
147 * @param event The mouse out event. | |
148 */ | |
149 public void onMouseOut(MouseOutEvent event) { | |
150 // TODO Cancel dragging if mouse pointer leaves chart area | |
151 } | |
152 | |
153 | |
154 /** | |
155 * A pan event is fired to inform the registered listeners about a pan | |
156 * operation has finished. | |
157 */ | |
158 protected void fireOnPan() { | |
159 PanEvent event = new PanEvent(start, end); | |
160 | |
161 for (PanHandler handler: handlers) { | |
162 handler.onPan(event); | |
163 } | |
164 } | |
165 } | |
166 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |