|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
JTextComponentMouseEventData.java | 75% | 90.7% | 92.3% | 89.1% |
|
1 |
package junit.extensions.jfcunit.eventdata;
|
|
2 |
|
|
3 |
import junit.extensions.jfcunit.JFCTestCase;
|
|
4 |
import junit.extensions.jfcunit.xml.JFCXMLConstants;
|
|
5 |
|
|
6 |
import java.awt.AWTEvent;
|
|
7 |
import java.awt.Component;
|
|
8 |
import java.awt.Point;
|
|
9 |
import java.awt.Rectangle;
|
|
10 |
import java.awt.event.MouseEvent;
|
|
11 |
|
|
12 |
import javax.swing.text.BadLocationException;
|
|
13 |
import javax.swing.text.JTextComponent;
|
|
14 |
|
|
15 |
|
|
16 |
/**
|
|
17 |
* Data container class that holds all the data necessary for jfcUnit to
|
|
18 |
* fire mouse events.
|
|
19 |
*
|
|
20 |
* @author <a href="mailto:vraravam@thoughtworks.com">
|
|
21 |
* Vijay Aravamudhan : ThoughtWorks Inc.</a>
|
|
22 |
*/
|
|
23 |
public class JTextComponentMouseEventData extends AbstractMouseEventData { |
|
24 |
/**
|
|
25 |
* The Component on which to trigger the event.
|
|
26 |
*/
|
|
27 |
private JTextComponent m_comp;
|
|
28 |
|
|
29 |
/**
|
|
30 |
* Offset into the text component in characters.
|
|
31 |
*/
|
|
32 |
private int m_offset = INVALID_TEXT_OFFSET; |
|
33 |
|
|
34 |
/**
|
|
35 |
* Constructor.
|
|
36 |
*/
|
|
37 | 131 |
public JTextComponentMouseEventData() {
|
38 | 131 |
super();
|
39 | 131 |
setValid(false);
|
40 |
} |
|
41 |
|
|
42 |
/**
|
|
43 |
* Constructor.
|
|
44 |
*
|
|
45 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code>
|
|
46 |
* has to be invoked.
|
|
47 |
* @param comp The component on which to trigger the event.
|
|
48 |
*/
|
|
49 | 2 |
public JTextComponentMouseEventData(final JFCTestCase testCase,
|
50 |
final JTextComponent comp) { |
|
51 | 2 |
this(testCase, comp, DEFAULT_NUMBEROFCLICKS);
|
52 |
} |
|
53 |
|
|
54 |
/**
|
|
55 |
* Constructor.
|
|
56 |
*
|
|
57 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
|
|
58 |
* @param comp The component on which to trigger the event.
|
|
59 |
* @param numberOfClicks
|
|
60 |
* Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
|
|
61 |
*/
|
|
62 | 6 |
public JTextComponentMouseEventData(final JFCTestCase testCase,
|
63 |
final JTextComponent comp, final int numberOfClicks) {
|
|
64 | 6 |
this(testCase, comp, numberOfClicks, DEFAULT_MOUSE_MODIFIERS);
|
65 |
} |
|
66 |
|
|
67 |
/**
|
|
68 |
* Constructor.
|
|
69 |
*
|
|
70 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code>
|
|
71 |
* has to be invoked.
|
|
72 |
* @param comp The component on which to trigger the event.
|
|
73 |
* @param sleepTime
|
|
74 |
* The wait time in ms between each event.
|
|
75 |
*/
|
|
76 | 2 |
public JTextComponentMouseEventData(final JFCTestCase testCase,
|
77 |
final JTextComponent comp, final long sleepTime) {
|
|
78 | 2 |
this(testCase, comp, DEFAULT_NUMBEROFCLICKS);
|
79 | 2 |
setSleepTime(sleepTime); |
80 |
} |
|
81 |
|
|
82 |
/**
|
|
83 |
* Constructor.
|
|
84 |
*
|
|
85 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code>
|
|
86 |
* has to be invoked.
|
|
87 |
* @param comp The component on which to trigger the event.
|
|
88 |
* @param numberOfClicks
|
|
89 |
* Number of clicks in the MouseEvent (1 for single-click,
|
|
90 |
* 2 for double clicks)
|
|
91 |
* @param modifiers The modifier key values that need to be passed onto the
|
|
92 |
* event.
|
|
93 |
*/
|
|
94 | 8 |
public JTextComponentMouseEventData(final JFCTestCase testCase,
|
95 |
final JTextComponent comp, final int numberOfClicks, final int modifiers) { |
|
96 | 8 |
this(testCase, comp, numberOfClicks, modifiers, DEFAULT_ISPOPUPTRIGGER);
|
97 |
} |
|
98 |
|
|
99 |
/**
|
|
100 |
* Constructor.
|
|
101 |
*
|
|
102 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code>
|
|
103 |
* has to be invoked.
|
|
104 |
* @param comp The component on which to trigger the event.
|
|
105 |
* @param numberOfClicks
|
|
106 |
* Number of clicks in the MouseEvent (1 for single-click,
|
|
107 |
* 2 for double clicks)
|
|
108 |
* @param isPopupTrigger
|
|
109 |
* boolean specifying whether this event will show a popup.
|
|
110 |
*/
|
|
111 | 2 |
public JTextComponentMouseEventData(final JFCTestCase testCase,
|
112 |
final JTextComponent comp, final int numberOfClicks,
|
|
113 |
final boolean isPopupTrigger) {
|
|
114 | 2 |
this(testCase, comp, numberOfClicks,
|
115 |
getDefaultModifiers(isPopupTrigger), isPopupTrigger); |
|
116 |
} |
|
117 |
|
|
118 |
/**
|
|
119 |
* Constructor.
|
|
120 |
*
|
|
121 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code>
|
|
122 |
* has to be invoked.
|
|
123 |
* @param comp The component on which to trigger the event.
|
|
124 |
* @param numberOfClicks
|
|
125 |
* Number of clicks in the MouseEvent (1 for single-click,
|
|
126 |
* 2 for double clicks)
|
|
127 |
* @param sleepTime
|
|
128 |
* The wait time in ms between each event.
|
|
129 |
*/
|
|
130 | 2 |
public JTextComponentMouseEventData(final JFCTestCase testCase,
|
131 |
final JTextComponent comp, final int numberOfClicks,
|
|
132 |
final long sleepTime) {
|
|
133 | 2 |
this(testCase, comp, numberOfClicks, DEFAULT_MOUSE_MODIFIERS,
|
134 |
DEFAULT_ISPOPUPTRIGGER, sleepTime); |
|
135 |
} |
|
136 |
|
|
137 |
/**
|
|
138 |
* Constructor.
|
|
139 |
*
|
|
140 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code>
|
|
141 |
* has to be invoked.
|
|
142 |
* @param comp The component on which to trigger the event.
|
|
143 |
* @param numberOfClicks
|
|
144 |
* Number of clicks in the MouseEvent (1 for single-click,
|
|
145 |
* 2 for double clicks)
|
|
146 |
* @param modifiers The modifier key values that need to be passed onto the
|
|
147 |
* event.
|
|
148 |
* @param isPopupTrigger
|
|
149 |
* boolean specifying whether this event will show a popup.
|
|
150 |
*/
|
|
151 | 12 |
public JTextComponentMouseEventData(final JFCTestCase testCase,
|
152 |
final JTextComponent comp, final int numberOfClicks,
|
|
153 |
final int modifiers, final boolean isPopupTrigger) { |
|
154 | 12 |
this(testCase, comp, numberOfClicks, modifiers, isPopupTrigger,
|
155 |
DEFAULT_SLEEPTIME); |
|
156 |
} |
|
157 |
|
|
158 |
/**
|
|
159 |
* Constructor.
|
|
160 |
*
|
|
161 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code>
|
|
162 |
* has to be invoked.
|
|
163 |
* @param comp The component on which to trigger the event.
|
|
164 |
* @param numberOfClicks
|
|
165 |
* Number of clicks in the MouseEvent (1 for single-click,
|
|
166 |
* 2 for double clicks)
|
|
167 |
* @param isPopupTrigger
|
|
168 |
* boolean specifying whether this event will show a popup.
|
|
169 |
* @param sleepTime
|
|
170 |
* The wait time in ms between each event.
|
|
171 |
*/
|
|
172 | 2 |
public JTextComponentMouseEventData(final JFCTestCase testCase,
|
173 |
final JTextComponent comp, final int numberOfClicks,
|
|
174 |
final boolean isPopupTrigger, final long sleepTime) { |
|
175 | 2 |
this(testCase, comp, numberOfClicks,
|
176 |
getDefaultModifiers(isPopupTrigger), isPopupTrigger, sleepTime); |
|
177 |
} |
|
178 |
|
|
179 |
/**
|
|
180 |
* Constructor.
|
|
181 |
*
|
|
182 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code>
|
|
183 |
* has to be invoked.
|
|
184 |
* @param comp The component on which to trigger the event.
|
|
185 |
* @param numberOfClicks
|
|
186 |
* Number of clicks in the MouseEvent (1 for single-click,
|
|
187 |
* 2 for double clicks)
|
|
188 |
* @param modifiers The modifier key values that need to be passed onto the
|
|
189 |
* event.
|
|
190 |
* @param isPopupTrigger
|
|
191 |
* boolean specifying whether this event will show a popup.
|
|
192 |
* @param sleepTime The wait time in ms between each event.
|
|
193 |
*/
|
|
194 | 18 |
public JTextComponentMouseEventData(final JFCTestCase testCase,
|
195 |
final JTextComponent comp, final int numberOfClicks,
|
|
196 |
final int modifiers, final boolean isPopupTrigger, final long sleepTime) { |
|
197 | 18 |
this(testCase, comp, numberOfClicks, modifiers, isPopupTrigger,
|
198 |
sleepTime, DEFAULT_POSITION, null, INVALID_TEXT_OFFSET);
|
|
199 |
} |
|
200 |
|
|
201 |
/**
|
|
202 |
* Constructor.
|
|
203 |
*
|
|
204 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code>
|
|
205 |
* has to be invoked.
|
|
206 |
* @param comp The component on which to trigger the event.
|
|
207 |
* @param numberOfClicks
|
|
208 |
* Number of clicks in the MouseEvent (1 for single-click,
|
|
209 |
* 2 for double clicks)
|
|
210 |
* @param modifiers The modifier key values that need to be passed onto the
|
|
211 |
* event.
|
|
212 |
* @param isPopupTrigger
|
|
213 |
* boolean specifying whether this event will show a popup.
|
|
214 |
* @param sleepTime
|
|
215 |
* The wait time in ms between each event.
|
|
216 |
* @param position The relative mouse position within the cell.
|
|
217 |
*/
|
|
218 | 2 |
public JTextComponentMouseEventData(final JFCTestCase testCase,
|
219 |
final JTextComponent comp, final int numberOfClicks,
|
|
220 |
final int modifiers, final boolean isPopupTrigger, |
|
221 |
final long sleepTime, final int position) { |
|
222 | 2 |
this(testCase, comp, numberOfClicks, modifiers, isPopupTrigger,
|
223 |
sleepTime, position, null, INVALID_TEXT_OFFSET);
|
|
224 |
} |
|
225 |
|
|
226 |
/**
|
|
227 |
* Constructor.
|
|
228 |
*
|
|
229 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code>
|
|
230 |
* has to be invoked.
|
|
231 |
* @param comp The component on which to trigger the event.
|
|
232 |
* @param numberOfClicks
|
|
233 |
* Number of clicks in the MouseEvent (1 for single-click,
|
|
234 |
* 2 for double clicks)
|
|
235 |
* @param modifiers The modifier key values that need to be passed onto the
|
|
236 |
* event.
|
|
237 |
* @param isPopupTrigger
|
|
238 |
* boolean specifying whether this event will show a popup.
|
|
239 |
* @param sleepTime
|
|
240 |
* The wait time in ms between each event.
|
|
241 |
* @param position This parameter will be ignored. It will be set to OFFSET.
|
|
242 |
* @param offset The offset into the text component.
|
|
243 |
*/
|
|
244 | 74 |
public JTextComponentMouseEventData(final JFCTestCase testCase,
|
245 |
final JTextComponent comp, final int numberOfClicks,
|
|
246 |
final int modifiers, final boolean isPopupTrigger, |
|
247 |
final long sleepTime, final int position, final int offset) { |
|
248 | 74 |
this(testCase, comp, numberOfClicks, modifiers, isPopupTrigger,
|
249 |
sleepTime, OFFSET, null, offset);
|
|
250 |
} |
|
251 |
|
|
252 |
/**
|
|
253 |
* Constructor.
|
|
254 |
*
|
|
255 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code>
|
|
256 |
* has to be invoked.
|
|
257 |
* @param comp The component on which to trigger the event.
|
|
258 |
* @param numberOfClicks
|
|
259 |
* Number of clicks in the MouseEvent (1 for single-click,
|
|
260 |
* 2 for double clicks)
|
|
261 |
* @param modifiers The modifier key values that need to be passed onto the
|
|
262 |
* event.
|
|
263 |
* @param isPopupTrigger
|
|
264 |
* boolean specifying whether this event will show a popup.
|
|
265 |
* @param sleepTime
|
|
266 |
* The wait time in ms between each event.
|
|
267 |
* @param referencePoint The CUSTOM mouse position within the cell.
|
|
268 |
*/
|
|
269 | 2 |
public JTextComponentMouseEventData(final JFCTestCase testCase,
|
270 |
final JTextComponent comp, final int numberOfClicks,
|
|
271 |
final int modifiers, final boolean isPopupTrigger, |
|
272 |
final long sleepTime, final Point referencePoint) {
|
|
273 | 2 |
this(testCase, comp, numberOfClicks, modifiers, isPopupTrigger,
|
274 |
sleepTime, CUSTOM, referencePoint, INVALID_TEXT_OFFSET); |
|
275 |
} |
|
276 |
|
|
277 |
/**
|
|
278 |
* Constructor.
|
|
279 |
*
|
|
280 |
* @param testCase The JFCTestCase on whose thread
|
|
281 |
* <code>awtSleep()</code> has to be invoked.
|
|
282 |
* @param comp The component on which to trigger the event.
|
|
283 |
* @param numberOfClicks
|
|
284 |
* Number of clicks in the MouseEvent (1 for single-click,
|
|
285 |
* 2 for double clicks)
|
|
286 |
* @param modifiers The modifier key values that need to be passed
|
|
287 |
* onto the event.
|
|
288 |
* @param isPopupTrigger
|
|
289 |
* boolean specifying whether this event will show a popup.
|
|
290 |
* @param sleepTime The wait time in ms between each event.
|
|
291 |
* @param position The relative mouse position within the cell.
|
|
292 |
* @param referencePoint
|
|
293 |
* If position is CUSTOM then the point is a offset from
|
|
294 |
* the location of the component. If the position is PERCENT
|
|
295 |
* then the location is a percentage offset of the hight and width.
|
|
296 |
* Otherwise, the referencePoint is unused.
|
|
297 |
* @param offset The character offset into the text component.
|
|
298 |
*/
|
|
299 | 113 |
public JTextComponentMouseEventData(final JFCTestCase testCase,
|
300 |
final JTextComponent comp, final int numberOfClicks,
|
|
301 |
final int modifiers, final boolean isPopupTrigger, |
|
302 |
final long sleepTime, final int position, final Point referencePoint, |
|
303 |
final int offset) {
|
|
304 | 113 |
setTestCase(testCase); |
305 | 113 |
setSource(comp); |
306 | 113 |
setNumberOfClicks(numberOfClicks); |
307 | 113 |
setModifiers(modifiers); |
308 | 113 |
setPopupTrigger(isPopupTrigger); |
309 | 113 |
setSleepTime(sleepTime); |
310 | 113 |
setPosition(position); |
311 | 113 |
setReferencePoint(referencePoint); |
312 | 113 |
setOffset(offset); |
313 | 113 |
setValid(true);
|
314 |
} |
|
315 |
|
|
316 |
/**
|
|
317 |
* Set the offset.
|
|
318 |
*
|
|
319 |
* @param offset The new value of the offset
|
|
320 |
*/
|
|
321 | 196 |
public final void setOffset(final int offset) { |
322 | 196 |
m_offset = offset; |
323 |
} |
|
324 |
|
|
325 |
/**
|
|
326 |
* Get the offset.
|
|
327 |
*
|
|
328 |
* @return int The value of the offset
|
|
329 |
*/
|
|
330 | 56 |
public final int getOffset() { |
331 | 56 |
return m_offset;
|
332 |
} |
|
333 |
|
|
334 |
/**
|
|
335 |
* Set the attribute value.
|
|
336 |
*
|
|
337 |
* @param comp The new value of the attribute
|
|
338 |
*/
|
|
339 | 196 |
public final void setSource(final JTextComponent comp) { |
340 | 196 |
m_comp = comp; |
341 |
} |
|
342 |
|
|
343 |
/**
|
|
344 |
* Get the attribute value.
|
|
345 |
*
|
|
346 |
* @return Component The value of the attribute
|
|
347 |
*/
|
|
348 | 373 |
public final JTextComponent getSource() {
|
349 | 373 |
return m_comp;
|
350 |
} |
|
351 |
|
|
352 |
/**
|
|
353 |
* The component on which the event has to be fired.
|
|
354 |
*
|
|
355 |
* @return The component
|
|
356 |
*/
|
|
357 | 269 |
public Component getComponent() {
|
358 |
// by default, the component is the same as the source
|
|
359 | 269 |
return getSource();
|
360 |
} |
|
361 |
|
|
362 |
/**
|
|
363 |
* Check if this event can consume the event given.
|
|
364 |
* @param ae AWTEvent to be consumed.
|
|
365 |
* @return true if the event may be consumed.
|
|
366 |
*/
|
|
367 | 194 |
public boolean canConsume(final AWTEvent ae) { |
368 | 194 |
if ((ae.getSource() instanceof JTextComponent) |
369 |
&& super.canConsume(ae)
|
|
370 |
&& sameSource(ae)) { |
|
371 | 90 |
return true; |
372 |
} |
|
373 |
|
|
374 | 104 |
return false; |
375 |
} |
|
376 |
|
|
377 |
/**
|
|
378 |
* Consume the event.
|
|
379 |
* @param ae AWTEvent to be consumed.
|
|
380 |
* @return boolean true if the event was consumed.
|
|
381 |
*/
|
|
382 | 90 |
public boolean consume(final AWTEvent ae) { |
383 | 90 |
if (super.consume(ae)) { |
384 | 7 |
return true; |
385 |
} |
|
386 |
|
|
387 | 83 |
MouseEvent me = (MouseEvent) ae; |
388 | 83 |
JTextComponent source = (JTextComponent) me.getSource(); |
389 | 83 |
setSource(source); |
390 | 83 |
setModifiers(me.getModifiers()); |
391 | 83 |
setNumberOfClicks(me.getClickCount()); |
392 | 83 |
setPopupTrigger(me.isPopupTrigger()); |
393 |
|
|
394 | 83 |
Point p = new Point(
|
395 |
me.getX(), |
|
396 |
me.getY()); |
|
397 | 83 |
Point screen = source.getLocationOnScreen(); |
398 | 83 |
screen.translate(p.x, p.y); |
399 | 83 |
setLocationOnScreen(screen); |
400 |
|
|
401 | 83 |
int offset = ((JTextComponent) source).viewToModel(p);
|
402 | 83 |
setOffset(offset); |
403 |
|
|
404 | 83 |
setPosition(CENTER); |
405 | 83 |
setReferencePoint(null);
|
406 | 83 |
setSleepTime(getDefaultSleepTime()); |
407 |
|
|
408 | 83 |
setValid(true);
|
409 |
|
|
410 | 83 |
return true; |
411 |
} |
|
412 |
|
|
413 |
/**
|
|
414 |
* Compare to event datas and deterime if they are equal.
|
|
415 |
*
|
|
416 |
* @param o Object to be compared.
|
|
417 |
* @return true if the events are the same.
|
|
418 |
*/
|
|
419 | 11 |
public boolean equals(final Object o) { |
420 | 11 |
if (!super.equals(o)) { |
421 | 9 |
return false; |
422 |
} |
|
423 |
|
|
424 | 2 |
JTextComponentMouseEventData data = (JTextComponentMouseEventData) o; |
425 |
|
|
426 | 2 |
return (data.getOffset() == getOffset());
|
427 |
} |
|
428 |
|
|
429 |
/**
|
|
430 |
* Get the hashCode.
|
|
431 |
* @return int hashCode.
|
|
432 |
*/
|
|
433 | 0 |
public int hashCode() { |
434 | 0 |
return super.hashCode() + m_offset; |
435 |
} |
|
436 |
|
|
437 |
/**
|
|
438 |
* Populate the XML Element with this objects attributes.
|
|
439 |
* @param e Element to be populated.
|
|
440 |
*/
|
|
441 | 0 |
public void populate(final org.w3c.dom.Element e) { |
442 | 0 |
super.populate(e);
|
443 | 0 |
e.setAttribute(JFCXMLConstants.TYPE, "JTextComponentMouseEventData");
|
444 | 0 |
e.setAttribute(JFCXMLConstants.INDEX, "" + getOffset());
|
445 |
} |
|
446 |
|
|
447 |
/**
|
|
448 |
* Prepare the component to receive the event.
|
|
449 |
*
|
|
450 |
* @return true if the component is ready to receive the event.
|
|
451 |
*/
|
|
452 | 78 |
public boolean prepareComponent() { |
453 | 78 |
if (!isValidForProcessing(getSource())) {
|
454 | 1 |
return false; |
455 |
} |
|
456 |
|
|
457 | 77 |
Point p = null;
|
458 | 77 |
if (getTestCase() != null) { |
459 | 77 |
getTestCase().flushAWT(); |
460 | 77 |
getTestCase().pauseAWT(); |
461 |
} |
|
462 |
|
|
463 | 77 |
if (getPosition() == OFFSET) {
|
464 | 77 |
setPosition(EAST); |
465 |
|
|
466 | 77 |
if ((m_offset == -1)
|
467 |
|| (m_offset > m_comp.getDocument().getLength())) { |
|
468 | 0 |
m_offset = m_comp.getDocument().getLength(); |
469 |
} |
|
470 |
|
|
471 | 77 |
Rectangle rect = null;
|
472 |
|
|
473 | 77 |
try {
|
474 | 77 |
rect = m_comp.modelToView(m_offset); |
475 |
} catch (BadLocationException ex) {
|
|
476 | 0 |
throw new RuntimeException("Invalid Offset:" + m_offset + ":" |
477 |
+ ex); |
|
478 |
} |
|
479 |
|
|
480 | 77 |
p = calculatePoint(rect); |
481 |
} else {
|
|
482 | 0 |
p = calculatePoint(m_comp.getBounds()); |
483 |
} |
|
484 |
|
|
485 | 77 |
Point screen = m_comp.getLocationOnScreen(); |
486 | 77 |
screen.translate(p.x, p.y); |
487 | 77 |
setLocationOnScreen(screen); |
488 |
|
|
489 | 77 |
return true; |
490 |
} |
|
491 |
|
|
492 |
/**
|
|
493 |
* Get string description of event.
|
|
494 |
* @return String description of event.
|
|
495 |
*/
|
|
496 | 26 |
public String toString() {
|
497 | 26 |
if (!isValid()) {
|
498 | 0 |
return super.toString(); |
499 |
} |
|
500 |
|
|
501 | 26 |
StringBuffer buf = new StringBuffer(1000);
|
502 | 26 |
buf.append(super.toString());
|
503 | 26 |
buf.append(" offset: " + getOffset());
|
504 |
|
|
505 | 26 |
return buf.toString();
|
506 |
} |
|
507 |
} |
|
508 |
|
|