|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
DragEventData.java | 55.8% | 73.7% | 71.4% | 67.9% |
|
1 |
package junit.extensions.jfcunit.eventdata;
|
|
2 |
|
|
3 |
import junit.extensions.jfcunit.JFCTestCase;
|
|
4 |
|
|
5 |
import org.w3c.dom.Element;
|
|
6 |
|
|
7 |
import java.awt.AWTEvent;
|
|
8 |
import java.awt.Component;
|
|
9 |
import java.awt.Point;
|
|
10 |
import java.awt.event.MouseEvent;
|
|
11 |
|
|
12 |
import java.util.List;
|
|
13 |
import java.util.Vector;
|
|
14 |
|
|
15 |
|
|
16 |
/**
|
|
17 |
* DragEventSource is a wrapper for Drag Events.
|
|
18 |
* The event encapsulates the source and destination
|
|
19 |
* locations for the drag with xxxMouseEventData
|
|
20 |
* types.
|
|
21 |
*
|
|
22 |
* @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
|
|
23 |
*/
|
|
24 |
public class DragEventData extends AbstractEventData { |
|
25 |
/**
|
|
26 |
* Destination of the drag event.
|
|
27 |
*/
|
|
28 |
private AbstractMouseEventData m_dest;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* Source of the drag event.
|
|
32 |
*/
|
|
33 |
private AbstractMouseEventData m_source;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* List of points in the drag path.
|
|
37 |
*/
|
|
38 |
private List m_points = new Vector(); |
|
39 |
|
|
40 |
/**
|
|
41 |
* Original reference location of component.
|
|
42 |
*/
|
|
43 |
private Point m_origPoint = null; |
|
44 |
|
|
45 |
/**
|
|
46 |
* Constructor: Assumes a null destination and default sleep time.
|
|
47 |
*
|
|
48 |
* @param testCase {@link JFCTestCase} to fire the sleeps upon.
|
|
49 |
* @param source {@link AbstractMouseEventData} indicating the
|
|
50 |
* starting location of the drag event.
|
|
51 |
*/
|
|
52 | 29 |
public DragEventData(final JFCTestCase testCase,
|
53 |
final AbstractMouseEventData source) { |
|
54 | 29 |
this(testCase, source, null, DEFAULT_SLEEPTIME); |
55 |
} |
|
56 |
|
|
57 |
/**
|
|
58 |
* Constructor for a drag event. Assumes the
|
|
59 |
* default sleepTime.
|
|
60 |
*
|
|
61 |
* @param testCase {@link JFCTestCase} to fire the sleeps upon.
|
|
62 |
* @param source {@link AbstractMouseEventData} indicating the
|
|
63 |
* starting location of the drag event.
|
|
64 |
* @param dest {@link AbstractMouseEventData} indicating the
|
|
65 |
* ending location of the drag event.
|
|
66 |
*/
|
|
67 | 86 |
public DragEventData(final JFCTestCase testCase,
|
68 |
final AbstractMouseEventData source, final AbstractMouseEventData dest) { |
|
69 | 86 |
this(testCase, source, dest, DEFAULT_SLEEPTIME);
|
70 |
} |
|
71 |
|
|
72 |
/**
|
|
73 |
* Constructor for a drag event.
|
|
74 |
*
|
|
75 |
* @param testCase TestCase.
|
|
76 |
* @param source {@link AbstractMouseEventData} indicating the
|
|
77 |
* starting location of the drag event.
|
|
78 |
* @param dest {@link AbstractMouseEventData} indicating the
|
|
79 |
* ending location of the drag event.
|
|
80 |
* @param delay sleepTime of the event.
|
|
81 |
*/
|
|
82 | 122 |
public DragEventData(final JFCTestCase testCase,
|
83 |
final AbstractMouseEventData source, final AbstractMouseEventData dest, |
|
84 |
final long delay) {
|
|
85 | 122 |
setTestCase(testCase); |
86 | 122 |
setSource(source); |
87 | 122 |
setDest(dest); |
88 | 122 |
setSleepTime(delay); |
89 |
|
|
90 | 122 |
if ((getSource() != null) && getSource().isValid()) { |
91 | 122 |
setValid(true);
|
92 |
} |
|
93 |
} |
|
94 |
|
|
95 |
/**
|
|
96 |
* This method is provided here to close the
|
|
97 |
* abstract base class.
|
|
98 |
*
|
|
99 |
* @return null This method always returns null.
|
|
100 |
*/
|
|
101 | 0 |
public final Component getComponent() {
|
102 | 0 |
return null; |
103 |
} |
|
104 |
|
|
105 |
/**
|
|
106 |
* Get the default modifiers.
|
|
107 |
* @return default modifiers for the drag event.
|
|
108 |
*/
|
|
109 | 0 |
public final int getDefaultModifiers() { |
110 | 0 |
return m_source.getDefaultModifiers();
|
111 |
} |
|
112 |
|
|
113 |
/**
|
|
114 |
* Set the Destination of the drag event.
|
|
115 |
*
|
|
116 |
* @param dest destination {@link AbstractMouseEventData}.
|
|
117 |
*/
|
|
118 | 122 |
public final void setDest(final AbstractMouseEventData dest) { |
119 | 122 |
m_dest = dest; |
120 |
} |
|
121 |
|
|
122 |
/**
|
|
123 |
* Get the destination MouseEventData.
|
|
124 |
*
|
|
125 |
* @return The destination {@link AbstractMouseEventData}.
|
|
126 |
*/
|
|
127 | 8 |
public final AbstractMouseEventData getDest() {
|
128 | 8 |
return m_dest;
|
129 |
} |
|
130 |
|
|
131 |
/**
|
|
132 |
* Get the modifier text for the current.
|
|
133 |
*
|
|
134 |
* @return String modifier text.
|
|
135 |
*/
|
|
136 | 0 |
public final String getModifierText() {
|
137 | 0 |
return m_source.getModifierText();
|
138 |
} |
|
139 |
|
|
140 |
/**
|
|
141 |
* Set the points in the drag path.
|
|
142 |
*
|
|
143 |
* @param points Set of points to be hit in the
|
|
144 |
* drag path.
|
|
145 |
*/
|
|
146 | 0 |
public final void setPoints(final Point[] points) { |
147 | 0 |
this.m_points.clear();
|
148 |
|
|
149 | 0 |
for (int i = 0; i < points.length; i++) { |
150 | 0 |
this.m_points.add(points[i]);
|
151 |
} |
|
152 |
} |
|
153 |
|
|
154 |
/**
|
|
155 |
* Get the list of points in the drag path.
|
|
156 |
*
|
|
157 |
* @return {@link java.awt.Point}[] list of points.
|
|
158 |
*/
|
|
159 | 84 |
public final Point[] getPoints() {
|
160 | 84 |
return (Point[]) m_points.toArray(new Point[0]); |
161 |
} |
|
162 |
|
|
163 |
/**
|
|
164 |
* Set the Source of the drag event.
|
|
165 |
*
|
|
166 |
* @param source {@link AbstractMouseEventData} indicating the
|
|
167 |
* starting location of the drag event.
|
|
168 |
*/
|
|
169 | 122 |
public final void setSource(final AbstractMouseEventData source) { |
170 | 122 |
m_source = source; |
171 |
|
|
172 | 122 |
if ((m_source != null) |
173 |
&& (m_source.getComponent() != null)
|
|
174 |
&& m_source.getComponent().isVisible()) { |
|
175 | 119 |
this.m_origPoint = m_source.getLocationOnScreen();
|
176 |
} |
|
177 |
} |
|
178 |
|
|
179 |
/**
|
|
180 |
* Get the Source of the drag event.
|
|
181 |
*
|
|
182 |
* @return The source {@link AbstractMouseEventData}.
|
|
183 |
*/
|
|
184 | 739 |
public final AbstractMouseEventData getSource() {
|
185 | 739 |
return m_source;
|
186 |
} |
|
187 |
|
|
188 |
/**
|
|
189 |
* Add a {@link java.awt.Point} to the drag path.
|
|
190 |
*
|
|
191 |
* @param p {@link java.awt.Point} to be hit along the drag path.
|
|
192 |
*/
|
|
193 | 193 |
public final void addPoint(final Point p) { |
194 | 193 |
m_points.add(p); |
195 |
} |
|
196 |
|
|
197 |
/**
|
|
198 |
* This prepares the source and dest components if set,
|
|
199 |
* and inserts/appends the screen locations onto the list
|
|
200 |
* of points.
|
|
201 |
*
|
|
202 |
* @return boolean true if the source and destination events
|
|
203 |
* were able to "prepare" their components.
|
|
204 |
*/
|
|
205 | 84 |
public final boolean prepareComponent() { |
206 | 84 |
boolean results = true; |
207 | 84 |
boolean resultd = true; |
208 |
|
|
209 | 84 |
if (m_source != null) { |
210 | 84 |
results = m_source.prepareComponent(); |
211 | 84 |
m_points.add( |
212 |
0, |
|
213 |
m_source.getLocationOnScreen()); |
|
214 |
} |
|
215 |
|
|
216 | 84 |
Point compLocation = m_source.getComponent().getLocationOnScreen(); |
217 |
|
|
218 | 84 |
if (m_points.size() > 1) {
|
219 | 0 |
for (int i = 1; i < m_points.size(); i++) { |
220 | 0 |
Point p = (Point) m_points.get(i); |
221 | 0 |
p.translate(compLocation.x, compLocation.y); |
222 |
} |
|
223 |
} |
|
224 |
|
|
225 | 84 |
if (m_dest != null) { |
226 | 84 |
resultd = m_dest.prepareComponent(); |
227 | 84 |
m_points.add(m_dest.getLocationOnScreen()); |
228 |
} |
|
229 |
|
|
230 | 84 |
return results & resultd;
|
231 |
} |
|
232 |
|
|
233 |
/**
|
|
234 |
* Check if this event can consume the {@link java.awt.AWTEvent}.
|
|
235 |
*
|
|
236 |
* @param ae {@link java.awt.AWTEvent} to be consumed.
|
|
237 |
* @return boolean true if the event can be consumed.
|
|
238 |
*/
|
|
239 | 195 |
public boolean canConsume(final AWTEvent ae) { |
240 | 195 |
if (!(ae instanceof MouseEvent)) { |
241 | 0 |
return false; |
242 |
} |
|
243 |
|
|
244 | 195 |
MouseEvent me = (MouseEvent) ae; |
245 |
|
|
246 | 195 |
if ((me.getID() == MouseEvent.MOUSE_DRAGGED)
|
247 |
|| (me.getID() == MouseEvent.MOUSE_EXITED) |
|
248 |
|| (me.getID() == MouseEvent.MOUSE_ENTERED)) { |
|
249 | 168 |
return true; |
250 |
} |
|
251 |
|
|
252 | 27 |
if (isValid()) {
|
253 | 27 |
return getSource().canConsume(ae);
|
254 |
} |
|
255 |
|
|
256 | 0 |
return false; |
257 |
} |
|
258 |
|
|
259 |
/**
|
|
260 |
* Consume the {@link java.awt.AWTEvent}.
|
|
261 |
*
|
|
262 |
* @param ae Event to be consumed.
|
|
263 |
* @return boolean true if the event was consumed.
|
|
264 |
*/
|
|
265 | 222 |
public boolean consume(final AWTEvent ae) { |
266 | 222 |
if (!(ae instanceof MouseEvent)) { |
267 | 0 |
return false; |
268 |
} |
|
269 |
|
|
270 | 222 |
MouseEvent me = (MouseEvent) ae; |
271 |
|
|
272 | 222 |
if ((me.getID() == MouseEvent.MOUSE_ENTERED)
|
273 |
|| (me.getID() == MouseEvent.MOUSE_EXITED)) { |
|
274 | 2 |
return true; |
275 |
} |
|
276 |
|
|
277 | 220 |
if (me.getID() == MouseEvent.MOUSE_DRAGGED) {
|
278 | 193 |
Point p = convertToSource( |
279 |
me.getComponent(), |
|
280 |
me.getPoint()); |
|
281 | 193 |
addPoint(p); |
282 |
} else {
|
|
283 | 27 |
return getSource().consume(ae);
|
284 |
} |
|
285 |
|
|
286 | 193 |
return true; |
287 |
} |
|
288 |
|
|
289 |
/**
|
|
290 |
* Equals comparison.
|
|
291 |
*
|
|
292 |
* @param o Object to be compared.
|
|
293 |
* @return true if the objects are the same.
|
|
294 |
*/
|
|
295 | 6 |
public boolean equals(final Object o) { |
296 | 6 |
if (!(o instanceof DragEventData)) { |
297 | 2 |
return false; |
298 |
} |
|
299 |
|
|
300 | 4 |
DragEventData ded = (DragEventData) o; |
301 |
|
|
302 | 4 |
if (m_source == null) { |
303 | 0 |
if (ded.getSource() != null) { |
304 | 0 |
return false; |
305 |
} |
|
306 |
} else {
|
|
307 | 4 |
if (!m_source.equals(ded.getSource())) {
|
308 | 2 |
return false; |
309 |
} |
|
310 |
} |
|
311 |
|
|
312 | 2 |
if (m_dest == null) { |
313 | 0 |
if (ded.getDest() != null) { |
314 | 0 |
return false; |
315 |
} |
|
316 |
} else {
|
|
317 | 2 |
if (!m_dest.equals(ded.getDest())) {
|
318 | 1 |
return false; |
319 |
} |
|
320 |
} |
|
321 |
|
|
322 | 1 |
return true; |
323 |
} |
|
324 |
|
|
325 |
/**
|
|
326 |
* Calculate a hashcode based upon the source and the
|
|
327 |
* dest of this class.
|
|
328 |
* @return int hashcode.
|
|
329 |
*/
|
|
330 | 0 |
public int hashCode() { |
331 | 0 |
int hc = super.hashCode(); |
332 |
|
|
333 | 0 |
if (m_source != null) { |
334 | 0 |
hc += m_source.hashCode(); |
335 |
} |
|
336 |
|
|
337 | 0 |
if (m_dest != null) { |
338 | 0 |
hc += m_dest.hashCode(); |
339 |
} |
|
340 |
|
|
341 | 0 |
return hc;
|
342 |
} |
|
343 |
|
|
344 |
/**
|
|
345 |
* Populate the element wiht the data.
|
|
346 |
*
|
|
347 |
* @param e element to be populated.
|
|
348 |
*/
|
|
349 | 0 |
public void populate(final Element e) { |
350 |
} |
|
351 |
|
|
352 |
/**
|
|
353 |
* Return a string representing the eventdata.
|
|
354 |
*
|
|
355 |
* @return String description of the event data.
|
|
356 |
*/
|
|
357 | 11 |
public String toString() {
|
358 | 11 |
StringBuffer buf = new StringBuffer(1000);
|
359 | 11 |
buf.append("DragEventData:");
|
360 |
|
|
361 | 11 |
if (!isValid()) {
|
362 | 0 |
buf.append(" invalid");
|
363 |
|
|
364 | 0 |
return buf.toString();
|
365 |
} |
|
366 |
|
|
367 | 11 |
buf.append("(Source:" + getSource().toString() + ")"); |
368 | 11 |
buf.append("Points:");
|
369 |
|
|
370 | 11 |
for (int i = 0; i < m_points.size(); i++) { |
371 | 81 |
if (i != 0) {
|
372 | 70 |
buf.append(",");
|
373 |
} |
|
374 |
|
|
375 | 81 |
buf.append(m_points.get(i)); |
376 |
} |
|
377 |
|
|
378 | 11 |
return buf.toString();
|
379 |
} |
|
380 |
|
|
381 |
/**
|
|
382 |
* Convert the point to the source coordinate system.
|
|
383 |
* @param comp Souce component.
|
|
384 |
* @param p Point to be converted.
|
|
385 |
* @return point of p related to source coordinates.
|
|
386 |
*/
|
|
387 | 193 |
private Point convertToSource(final Component comp, final Point p) {
|
388 | 193 |
Point screen = comp.getLocationOnScreen(); |
389 | 193 |
screen.translate(p.x, p.y); |
390 |
|
|
391 | 193 |
if (m_origPoint == null) { |
392 | 0 |
return p;
|
393 |
} |
|
394 |
|
|
395 | 193 |
int dx = screen.x - m_origPoint.x;
|
396 | 193 |
int dy = screen.y - m_origPoint.y;
|
397 |
|
|
398 | 193 |
Point delta = new Point(p);
|
399 | 193 |
delta.translate(dx, dy); |
400 |
|
|
401 | 193 |
return delta;
|
402 |
} |
|
403 |
} |
|
404 |
|
|