comparison flys-client/src/main/java/org/dive4elements/river/client/client/ui/chart/ZoomboxControl.java @ 5834:f507086aa94b

Repaired internal references.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:31:32 +0200
parents flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ZoomboxControl.java@f1a559d13eef
children 821a02bbfb4e
comparison
equal deleted inserted replaced
5833:a2bdc0f524e8 5834:f507086aa94b
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.Positioning;
9 import com.smartgwt.client.types.SelectionType;
10 import com.smartgwt.client.widgets.ImgButton;
11 import com.smartgwt.client.widgets.Canvas;
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.HasZoomHandlers;
22 import de.intevation.flys.client.client.event.ZoomEvent;
23 import de.intevation.flys.client.client.event.ZoomHandler;
24
25
26 /**
27 * This control observes that panel retrieved by ChartOutputTab.getChartPanel().
28 * If activated, a zoombox is drawn. One of the two edges is the position of the
29 * mouse down event on the observed panel. The other edge is specified by the
30 * current mouse position. If the mouse up event occurs, start and end point
31 * relative to the left and upper border of the observed panel is determined and
32 * a ZoomEvent is fired.
33 *
34 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
35 */
36 public class ZoomboxControl
37 extends ImgButton
38 implements MouseDownHandler, MouseUpHandler, MouseMoveHandler, HasZoomHandlers,
39 MouseOutHandler
40 {
41 protected List<ZoomHandler> handlers;
42
43 protected ChartOutputTab chartTab;
44
45 protected Canvas zoombox;
46
47 protected int[] start;
48 protected int[] end;
49
50
51 public ZoomboxControl(ChartOutputTab chartTab, String imageUrl) {
52 super();
53
54 this.handlers = new ArrayList<ZoomHandler>();
55 this.chartTab = chartTab;
56 this.start = new int[] { -1, -1 };
57 this.end = new int[2];
58 this.zoombox = new Canvas();
59
60 initZoombox();
61
62 String baseUrl = GWT.getHostPageBaseURL();
63 setSrc(baseUrl + imageUrl);
64 setActionType(SelectionType.CHECKBOX);
65 setSize(20);
66 setShowRollOver(false);
67 setSelected(false);
68
69 Canvas chart = chartTab.getChartPanel();
70 chart.addMouseDownHandler(this);
71 chart.addMouseOutHandler(this);
72 chart.addMouseMoveHandler(this);
73 chart.addMouseUpHandler(this);
74 }
75
76
77 /**
78 * Initializes the zoombox that is displayed over the observed area. The
79 * zoombox has an opaque background. Its height/width and x/y values are
80 * determined by the start point (mouse down) and the current mouse
81 * position.
82 */
83 protected void initZoombox() {
84 Canvas chart = chartTab.getChartPanel();
85 chart.addChild(zoombox);
86
87 zoombox.setPosition(Positioning.ABSOLUTE);
88 zoombox.setBorder("2px solid black");
89 zoombox.setOpacity(50);
90 zoombox.setWidth(1);
91 zoombox.setHeight(1);
92 zoombox.setLeft(-10000);
93 zoombox.setTop(-10000);
94 }
95
96
97 /**
98 * Registers a new ZoomHandler that wants to listen to ZoomEvents.
99 *
100 * @param handler A new ZoomHandler.
101 */
102 public void addZoomHandler(ZoomHandler handler) {
103 if (handler != null) {
104 handlers.add(handler);
105 }
106 }
107
108
109 /**
110 * A mouse down event on the specified area will set the start point for the
111 * zoombox.
112 *
113 * @param event The mouse down event which contains the xy coordinates of
114 * the observed area.
115 */
116 public void onMouseDown(MouseDownEvent event) {
117 if (!isSelected()) {
118 return;
119 }
120
121 start[0] = getRelativeX(event.getX()) - 1;
122 start[1] = getRelativeY(event.getY()) + 1;
123
124 end[0] = start[0];
125 end[1] = start[1];
126 }
127
128
129 /**
130 * A mouse move event on the specified area will set the end point for the
131 * zoombox. If the end point differs from the start point, an opaque box is
132 * displayed.
133 *
134 * @param event The mouse move event which contains the xy coordinates of
135 * the observed area.
136 */
137 public void onMouseMove(MouseMoveEvent event) {
138 if (!isSelected() || !isZooming()) {
139 return;
140 }
141
142 int x = getRelativeX(event.getX());
143 int y = getRelativeY(event.getY());
144
145 end[0] = x > start[0] ? x-1 : x+1;
146 end[1] = y > start[1] ? y-1 : y+1;
147
148 positionZoombox();
149 }
150
151
152 /**
153 * The mouse up event finalizes the zoom operation. It sets the end point
154 * for this operation, clears the zoombox and fires a ZoomEvent.
155 *
156 * @param event The mouse up event which contains the xy coordinates of the
157 * observed area.
158 */
159 public void onMouseUp(MouseUpEvent event) {
160 if (!isSelected()) {
161 return;
162 }
163
164 end[0] = getRelativeX(event.getX());
165 end[1] = getRelativeY(event.getY());
166
167 fireZoomEvent();
168
169 reset();
170 }
171
172
173 /**
174 * The mouse out event is used to cancel an active zoom operation.
175 *
176 * @param event The mouse out event.
177 */
178 public void onMouseOut(MouseOutEvent event) {
179 if (!isSelected() || !isMouseOut(event.getX(), event.getY())) {
180 return;
181 }
182
183 reset();
184 }
185
186
187 /**
188 * Returns the chart panel.
189 *
190 * @return the chart panel.
191 */
192 protected Canvas getChartPanel() {
193 return chartTab.getChartPanel();
194 }
195
196
197 /**
198 * This method is required to check manually if the mouse pointer really
199 * moves out the chart area. The MouseOutEvent is also fired if the mouse
200 * goes down which doesn't seem to be correct. So, we gonna check this
201 * manually.
202 *
203 * @param x The x coordinate.
204 * @param y The y coordinate.
205 *
206 * @return true, if the mouse is really out of the chart area, otherwise
207 * false.
208 */
209 protected boolean isMouseOut(int x, int y) {
210 Canvas chart = getChartPanel();
211
212 int left = chart.getPageLeft();
213 int right = chart.getPageRight();
214 int top = chart.getPageTop();
215 int bottom = chart.getPageBottom();
216
217 if (x <= left || x >= right || y <= top || y >= bottom) {
218 return true;
219 }
220
221 return false;
222 }
223
224
225 /**
226 * Returns true, if a zoom action is in process.
227 *
228 * @return true, if a zoom action is in process.
229 */
230 public boolean isZooming() {
231 return start[0] > 0 && start[1] > 0;
232 }
233
234
235 /**
236 * Returns the X coordinate relative to the left border.
237 *
238 * @param x The X coordinate relative to the window.
239 *
240 * @return the X coordinate relative to the left border.
241 */
242 protected int getRelativeX(int x) {
243 return x - chartTab.getChartPanel().getPageLeft();
244 }
245
246
247 /**
248 * Returns the Y coordinate relative to the top border.
249 *
250 * @param y The Y coordinate relative to the window.
251 *
252 * @return the Y coordinate relative to the top border.
253 */
254 protected int getRelativeY(int y) {
255 return y - chartTab.getChartPanel().getPageTop();
256 }
257
258
259 /**
260 * Returns min and max x/y values based on the stored values in <i>start</i>
261 * and <i>end</i>.
262 *
263 * @return an int[] as follows: [xmin, ymin, xmax, ymax].
264 */
265 protected int[] orderPositions() {
266 int xmin = start[0] < end[0] ? start[0] : end[0];
267 int ymin = start[1] < end[1] ? start[1] : end[1];
268
269 int xmax = start[0] >= end[0] ? start[0] : end[0];
270 int ymax = start[1] >= end[1] ? start[1] : end[1];
271
272 return new int[] { xmin, ymin, xmax, ymax };
273 }
274
275
276 /**
277 * Sets the width, height, x and y values of the zoombox.
278 */
279 protected void positionZoombox() {
280 int[] values = orderPositions();
281
282 zoombox.setLeft(values[0]);
283 zoombox.setTop(values[1]);
284 zoombox.setWidth(values[2] - values[0]);
285 zoombox.setHeight(values[3] - values[1]);
286 }
287
288
289 /**
290 * Clears the zoombox (set position and size to null).
291 */
292 protected void clearZoombox() {
293 zoombox.setLeft(-10000);
294 zoombox.setTop(-10000);
295 zoombox.setWidth(1);
296 zoombox.setHeight(1);
297 }
298
299
300 /**
301 * Resets the zoom control (start point and zoombox).
302 */
303 protected void reset() {
304 start[0] = -1;
305 start[1] = -1;
306
307 clearZoombox();
308 }
309
310
311 /**
312 * Fires a ZoomEvent to all registered listeners.
313 */
314 protected void fireZoomEvent() {
315 int[] pos = orderPositions();
316
317 ZoomEvent event = new ZoomEvent(pos[0], pos[1], pos[2], pos[3]);
318
319 for (ZoomHandler handler: handlers) {
320 handler.onZoom(event);
321 }
322 }
323 }
324 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org