comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ZoomboxControl.java @ 540:a866cdf1ca40

Implemented a zoombox control and added it to the chart toolbar. flys-client/trunk@2040 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 01 Jun 2011 13:43:35 +0000
parents
children ed29599e06e5
comparison
equal deleted inserted replaced
539:fea93eebd2fa 540:a866cdf1ca40
1 package de.intevation.flys.client.client.ui.chart;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import com.smartgwt.client.types.Positioning;
7 import com.smartgwt.client.types.SelectionType;
8 import com.smartgwt.client.widgets.Button;
9 import com.smartgwt.client.widgets.Canvas;
10 import com.smartgwt.client.widgets.events.MouseDownEvent;
11 import com.smartgwt.client.widgets.events.MouseDownHandler;
12 import com.smartgwt.client.widgets.events.MouseMoveEvent;
13 import com.smartgwt.client.widgets.events.MouseMoveHandler;
14 import com.smartgwt.client.widgets.events.MouseUpEvent;
15 import com.smartgwt.client.widgets.events.MouseUpHandler;
16
17 import de.intevation.flys.client.client.event.HasZoomHandlers;
18 import de.intevation.flys.client.client.event.ZoomEvent;
19 import de.intevation.flys.client.client.event.ZoomHandler;
20
21
22 /**
23 * This control observes that panel retrieved by ChartOutputTab.getChartPanel().
24 * If activated, a zoombox is drawn. One of the two edges is the position of the
25 * mouse down event on the observed panel. The other edge is specified by the
26 * current mouse position. If the mouse up event occurs, start and end point
27 * relative to the left and upper border of the observed panel is determined and
28 * a ZoomEvent is fired.
29 *
30 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
31 */
32 public class ZoomboxControl
33 extends Button
34 implements MouseDownHandler, MouseUpHandler, MouseMoveHandler, HasZoomHandlers
35 {
36 protected List<ZoomHandler> handlers;
37
38 protected ChartOutputTab chartTab;
39
40 protected Canvas zoombox;
41
42 protected int[] start;
43 protected int[] end;
44
45
46 public ZoomboxControl(ChartOutputTab chartTab) {
47 super("Zoombox");
48
49 this.handlers = new ArrayList<ZoomHandler>();
50 this.chartTab = chartTab;
51 this.start = new int[2];
52 this.end = new int[2];
53 this.zoombox = new Canvas();
54
55 initZoombox();
56
57 setActionType(SelectionType.CHECKBOX);
58 setSelected(false);
59
60 chartTab.getChartPanel().addMouseDownHandler(this);
61 chartTab.getChartPanel().addMouseMoveHandler(this);
62 chartTab.getChartPanel().addMouseUpHandler(this);
63 }
64
65
66 /**
67 * Initializes the zoombox that is displayed over the observed area. The
68 * zoombox has an opaque background. Its height/width and x/y values are
69 * determined by the start point (mouse down) and the current mouse
70 * position.
71 */
72 protected void initZoombox() {
73 zoombox.setPosition(Positioning.ABSOLUTE);
74 zoombox.setBackgroundColor("red");
75 zoombox.setOpacity(50);
76 zoombox.setWidth(0);
77 zoombox.setHeight(0);
78 }
79
80
81 /**
82 * Registers a new ZoomHandler that wants to listen to ZoomEvents.
83 *
84 * @param handler A new ZoomHandler.
85 */
86 public void addZoomHandler(ZoomHandler handler) {
87 if (handler != null) {
88 handlers.add(handler);
89 }
90 }
91
92
93 /**
94 * A mouse down event on the specified area will set the start point for the
95 * zoombox.
96 *
97 * @param event The mouse down event which contains the xy coordinates of
98 * the observed area.
99 */
100 public void onMouseDown(MouseDownEvent event) {
101 if (!isSelected()) {
102 return;
103 }
104
105 start[0] = getRelativeX(event.getX());
106 start[1] = getRelativeY(event.getY());
107
108 end[0] = start[0];
109 end[1] = start[1];
110
111 chartTab.getChartPanel().addChild(zoombox);
112
113 positionZoombox();
114 }
115
116
117 /**
118 * A mouse move event on the specified area will set the end point for the
119 * zoombox. If the end point differs from the start point, an opaque box is
120 * displayed.
121 *
122 * @param event The mouse move event which contains the xy coordinates of
123 * the observed area.
124 */
125 public void onMouseMove(MouseMoveEvent event) {
126 if (!isSelected()) {
127 return;
128 }
129
130 end[0] = getRelativeX(event.getX());
131 end[1] = getRelativeY(event.getY());
132
133 positionZoombox();
134 }
135
136
137 /**
138 * The mouse up event finalizes the zoom operation. It sets the end point
139 * for this operation, clears the zoombox and fires a ZoomEvent.
140 *
141 * @param event The mouse up event which contains the xy coordinates of the
142 * observed area.
143 */
144 public void onMouseUp(MouseUpEvent event) {
145 if (!isSelected()) {
146 return;
147 }
148
149 end[0] = getRelativeX(event.getX());
150 end[1] = getRelativeY(event.getY());
151
152 clearZoombox();
153
154 chartTab.getChartPanel().removeChild(zoombox);
155 }
156
157
158 /**
159 * Returns the X coordinate relative to the left border.
160 *
161 * @param x The X coordinate relative to the window.
162 *
163 * @return the X coordinate relative to the left border.
164 */
165 protected int getRelativeX(int x) {
166 return x - chartTab.getChartPanel().getPageLeft();
167 }
168
169
170 /**
171 * Returns the Y coordinate relative to the top border.
172 *
173 * @param y The Y coordinate relative to the window.
174 *
175 * @return the Y coordinate relative to the top border.
176 */
177 protected int getRelativeY(int y) {
178 return y - chartTab.getChartPanel().getPageTop();
179 }
180
181
182 /**
183 * Returns min and max x/y values based on the stored values in <i>start</i>
184 * and <i>end</i>.
185 *
186 * @return an int[] as follows: [xmin, ymin, xmax, ymax].
187 */
188 protected int[] orderPositions() {
189 int xmin = start[0] < end[0] ? start[0] : end[0];
190 int ymin = start[1] < end[1] ? start[1] : end[1];
191
192 int xmax = start[0] >= end[0] ? start[0] : end[0];
193 int ymax = start[1] >= end[1] ? start[1] : end[1];
194
195 return new int[] { xmin, ymin, xmax, ymax };
196 }
197
198
199 /**
200 * Sets the width, height, x and y values of the zoombox.
201 */
202 protected void positionZoombox() {
203 int[] values = orderPositions();
204
205 zoombox.setLeft(values[0]);
206 zoombox.setTop(values[1]);
207 zoombox.setWidth(values[2] - values[0]);
208 zoombox.setHeight(values[3] - values[1]);
209 }
210
211
212 /**
213 * Clears the zoombox (set position and size to null).
214 */
215 protected void clearZoombox() {
216 zoombox.setLeft(0);
217 zoombox.setTop(0);
218 zoombox.setWidth(0);
219 zoombox.setHeight(0);
220 }
221
222
223 /**
224 * Fires a ZoomEvent to all registered listeners.
225 */
226 protected void fireZoomEvent() {
227 int[] pos = orderPositions();
228
229 ZoomEvent event = new ZoomEvent(pos[0], pos[1], pos[2], pos[3]);
230
231 for (ZoomHandler handler: handlers) {
232 handler.onZoom(event);
233 }
234 }
235 }
236 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org