01: package org.uispec4j;
02:
03: import java.awt.*;
04: import java.awt.event.MouseEvent;
05:
06: /**
07: * Utility for simulating mouse inputs.
08: */
09: public class Mouse {
10:
11: private Mouse() {
12: }
13:
14: /**
15: * Clicks in the center of a UIComponent.
16: */
17: public static void click(UIComponent uiComponent) {
18: doClick(uiComponent.getAwtComponent(), 1);
19: }
20:
21: /**
22: * Double clicks in the center of a UIComponent.
23: */
24: public static void doubleClick(UIComponent uiComponent) {
25: doClick(uiComponent.getAwtComponent(), 2);
26: }
27:
28: /**
29: * Clicks in a given area of a UIComponent.
30: */
31: public static void doClickInRectangle(UIComponent uiComponent,
32: Rectangle rect, boolean useRightClick,
33: Key.Modifier keyModifier) {
34: doClickInRectangle(uiComponent.getAwtComponent(), rect,
35: useRightClick, keyModifier);
36: }
37:
38: /**
39: * Clicks in a given area of a Swing component.
40: */
41: public static void doClickInRectangle(Component component,
42: Rectangle rect, boolean useRightClick,
43: Key.Modifier keyModifier) {
44: doClickInRectangle(component, rect, useRightClick, keyModifier,
45: 1);
46: }
47:
48: /**
49: * Double clicks in a given area of a Swing component.
50: */
51: public static void doDoubleClickInRectangle(Component component,
52: Rectangle rect) {
53: doClickInRectangle(component, rect, false, Key.Modifier.NONE, 2);
54: }
55:
56: /**
57: * Double clicks in a given area of a UIComponent.
58: */
59: public static void doDoubleClickInRectangle(
60: UIComponent uiComponent, Rectangle rect) {
61: doDoubleClickInRectangle(uiComponent.getAwtComponent(), rect);
62: }
63:
64: private static void doClickInRectangle(Component component,
65: Rectangle rect, boolean useRightClick,
66: Key.Modifier keyModifier, int nbClicks) {
67: int modifiers = useRightClick ? MouseEvent.BUTTON3_MASK
68: : MouseEvent.BUTTON1_MASK;
69: modifiers |= keyModifier.getCode();
70: final int x = rect.x + (rect.width / 2);
71: final int y = rect.y + (rect.height / 2);
72: component.dispatchEvent(new MouseEvent(component,
73: MouseEvent.MOUSE_PRESSED, 1, modifiers, x, y, nbClicks,
74: false));
75: component.dispatchEvent(new MouseEvent(component,
76: MouseEvent.MOUSE_RELEASED, 1, modifiers, x, y,
77: nbClicks, useRightClick));
78: component.dispatchEvent(new MouseEvent(component,
79: MouseEvent.MOUSE_CLICKED, 1, modifiers, x, y, nbClicks,
80: false));
81: }
82:
83: private static void doClick(Component component, int clickCount) {
84: doClickInRectangle(component, new Rectangle(), false,
85: Key.Modifier.NONE, clickCount);
86: }
87: }
|