comparison flys-client/src/main/java/org/dive4elements/river/client/client/ui/chart/PanControl.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/PanControl.java@80626c4a5bbf
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.SelectionType;
9 import com.smartgwt.client.widgets.Canvas;
10 import com.smartgwt.client.widgets.Img;
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 start[0] = event.getX();
87 start[1] = event.getY();
88
89 end[0] = start[0];
90 end[1] = start[1];
91 }
92
93
94 /**
95 * This event is used to reposition the chart image based on the current
96 * drag operation.
97 *
98 * @param event The move event which contains the new coordinates to update
99 * the chart image position.
100 */
101 public void onMouseMove(MouseMoveEvent event) {
102 if (!isSelected() || start[0] == -1 || start[1] == -1) {
103 return;
104 }
105
106 int x = event.getX() - end[0];
107 int y = event.getY() - end[1];
108
109 end[0] = end[0] + x;
110 end[1] = end[1] + y;
111
112 Canvas c = chartTab.getChartImg();
113 c.moveBy(x, y);
114 }
115
116
117 /**
118 * This event stops the dragging operation and fires a DragEnd event to the
119 * registered listeners.
120 *
121 * @param event The mouse up event which contains the end coordinates.
122 */
123 public void onMouseUp(MouseUpEvent event) {
124 if (!isSelected()) {
125 return;
126 }
127
128 end[0] = event.getX();
129 end[1] = event.getY();
130
131 Canvas c = chartTab.getChartImg();
132 c.setLeft(0);
133 c.setTop(0);
134
135 fireOnPan();
136
137 start[0] = -1;
138 start[1] = -1;
139 }
140
141
142 /**
143 * This event is used to cancel the current dragging operation.
144 *
145 * @param event The mouse out event.
146 */
147 public void onMouseOut(MouseOutEvent event) {
148 int x = event.getX();
149 int y = event.getY();
150
151 if (!isSelected() || !isMouseOut(x, y) || start[0] == -1) {
152 return;
153 }
154
155 Canvas c = chartTab.getChartImg();
156 c.setLeft(0);
157 c.setTop(0);
158
159 fireOnPan();
160
161 start[0] = -1;
162 start[1] = -1;
163 }
164
165
166 /**
167 * This method is required to check manually if the mouse pointer really
168 * moves out the chart area. The MouseOutEvent is also fired if the mouse
169 * goes down which doesn't seem to be correct. So, we gonna check this
170 * manually.
171 *
172 * @param x The x coordinate.
173 * @param y The y coordinate.
174 *
175 * @return true, if the mouse is really out of the chart area, otherwise
176 * false.
177 */
178 protected boolean isMouseOut(int x, int y) {
179 Canvas chart = chartTab.getChartImg();
180
181 if (chart instanceof Img) {
182 chart = chart.getParentElement();
183 }
184
185 int left = chart.getPageLeft();
186 int right = chart.getPageRight();
187 int top = chart.getPageTop();
188 int bottom = chart.getPageBottom();
189
190 if (x <= left || x >= right || y <= top || y >= bottom) {
191 return true;
192 }
193
194 return false;
195 }
196
197
198 /**
199 * A pan event is fired to inform the registered listeners about a pan
200 * operation has finished.
201 */
202 protected void fireOnPan() {
203 PanEvent event = new PanEvent(start, end);
204
205 for (PanHandler handler: handlers) {
206 handler.onPan(event);
207 }
208 }
209 }
210 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org