Debian Codegraph @

tests/test-extensions/net/sourceforge/jnlp/awt/awtactions/MouseActions.java

raw
1 /* MouseActions.java
2 Copyright (C) 2012 Red Hat, Inc.
3
4 This file is part of IcedTea.
5
6 IcedTea is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, version 2.
9
10 IcedTea is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with IcedTea; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301 USA.
19
20 Linking this library statically or dynamically with other modules is
21 making a combined work based on this library. Thus, the terms and
22 conditions of the GNU General Public License cover the whole
23 combination.
24
25 As a special exception, the copyright holders of this library give you
26 permission to link this library with independent modules to produce an
27 executable, regardless of the license terms of these independent
28 modules, and to copy and distribute the resulting executable under
29 terms of your choice, provided that you also meet, for each linked
30 independent module, the terms and conditions of the license of that
31 module. An independent module is a module which is not derived from
32 or based on this library. If you modify this library, you may extend
33 this exception to your version of the library, but you are not
34 obligated to do so. If you do not wish to do so, delete this
35 exception statement from your version.
36 */
37
38
39 package net.sourceforge.jnlp.awt.awtactions;
40
41 import java.awt.Rectangle;
42 import java.awt.Robot;
43 import java.awt.event.InputEvent;
44
45 /**
46 * class MouseActions
47 *
48 * static methods for manipulating the mouse via AWT robot
49 */
50 public class MouseActions {
51 private static final int defaultDelay = 250;
52
53 /**
54 * method click presses and releases given mouse keys
55 * with reasonable delay before the event
56 *
57 * @param mouseKeyMask
58 * @param robot
59 * @param delayMs
60 */
61 public static void click(int mouseKeyMask, Robot robot, int delayMs){
62 robot.delay(delayMs);
63 robot.mousePress(mouseKeyMask);
64 robot.delay(delayMs);
65 robot.mouseRelease(mouseKeyMask);
66 }
67
68 public static void click(int mouseKeyMask, Robot robot){
69 robot.delay(defaultDelay);
70 robot.mousePress(mouseKeyMask);
71 robot.delay(defaultDelay);
72 robot.mouseRelease(mouseKeyMask);
73 }
74
75 /**
76 * method doubleClick presses and releases given mouse keys
77 * two times with reasonable delays
78 *
79 * @param mouseKeyMask
80 * @param robot
81 * @param delayMs
82 */
83
84 public static void doubleClick(int mouseKeyMask, Robot robot, int delayMs){
85 click(mouseKeyMask, robot, delayMs);
86 click(mouseKeyMask, robot, delayMs);
87 }
88
89 public static void doubleClick(int mouseKeyMask, Robot robot){
90 click(mouseKeyMask, robot, defaultDelay);
91 click(mouseKeyMask, robot, defaultDelay);
92 }
93
94 /**
95 * method drag presses the right mouse key,
96 * drags the mouse to a point, and releases the mouse key
97 * with reasonable delays
98 *
99 * @param xTo
100 * @param yTo
101 * @param robot
102 * @param delayMs
103 */
104 public static void drag(int xTo, int yTo, Robot robot, int delayMs){
105 robot.delay(delayMs);
106 robot.mousePress(InputEvent.BUTTON1_MASK);
107 robot.delay(delayMs);
108 robot.mouseMove(xTo, yTo);
109 }
110
111 public static void drag(int xTo, int yTo, Robot robot){
112 robot.delay(defaultDelay);
113 robot.mousePress(InputEvent.BUTTON1_MASK);
114 robot.delay(defaultDelay);
115 robot.mouseMove(xTo, yTo);
116 }
117
118 /**
119 * method dragFromRectangle clicks in the middle
120 * of the given rectangle and drags the mouse from the rectangle
121 * with reasonable delays
122 *
123 * @param rectangle
124 * @param robot
125 * @param delayMs
126 */
127
128 public static void dragFromRectangle(Rectangle rectangle, Robot robot, int delayMs){
129 int x1 = rectangle.x + rectangle.width/2;
130 int y1 = rectangle.y + rectangle.height/2;
131 int x2 = x1 + 2*rectangle.width;
132 int y2 = y1 + 2*rectangle.height;
133 robot.delay(delayMs);
134 robot.mouseMove(x1, y1);
135 drag(x2,y2, robot);
136 }
137
138 public static void dragFromRectangle(Rectangle rectangle, Robot robot){
139 dragFromRectangle(rectangle, robot, defaultDelay);
140 }
141
142 /**
143 * method moveInsideRectangle places the mouse in the middle
144 * of the given rectangle and moves the mouse inside the rectangle
145 * with reasonable delays
146 *
147 * @param rectangle
148 * @param robot
149 * @param delayMs
150 */
151
152 public static void moveInsideRectangle(Rectangle rectangle, Robot robot, int delayMs){
153 int x1 = rectangle.x + rectangle.width/2;
154 int y1 = rectangle.y + rectangle.height/2;
155 int x2 = x1 + rectangle.width/4;
156 int y2 = y1 + rectangle.height/4;
157 robot.delay(delayMs);
158 robot.mouseMove(x1, y1);
159 robot.delay(delayMs);
160 robot.mouseMove(x2, y2);
161 }
162
163 public static void moveInsideRectangle(Rectangle rectangle, Robot robot){
164 moveInsideRectangle(rectangle, robot, defaultDelay);
165 }
166
167 /**
168 *
169 * @param rectangle
170 * @param robot
171 * @param delayMs
172 */
173 public static void moveMouseToMiddle(Rectangle rectangle, Robot robot, int delayMs){
174 robot.delay(delayMs);
175 int x = rectangle.x + (rectangle.width/2);
176 int y = rectangle.y + (rectangle.height/2);
177 robot.mouseMove(x,y);
178 }
179
180 public static void moveMouseToMiddle(Rectangle rectangle, Robot robot){
181 moveMouseToMiddle(rectangle, robot, defaultDelay);
182 }
183
184 /**
185 *
186 * @param rectangle
187 * @param robot
188 * @param delayMs
189 */
190 public static void moveMouseOutside(Rectangle rectangle, Robot robot, int delayMs){
191 robot.delay(delayMs);
192 int x = rectangle.x + 2*rectangle.width;
193 int y = rectangle.y + 2*rectangle.height;
194 robot.mouseMove(x,y);
195 }
196
197 public static void moveMouseOutside(Rectangle rectangle, Robot robot){
198 moveMouseOutside(rectangle, robot, defaultDelay);
199 }
200
201
202 /**
203 * method clickInside moves the mouse in the middle point
204 * of a given rectangle and clicks with reasonable delay
205
206 *
207 * @param rectangle
208 * @param robot
209 * @param delayMs
210 */
211 public static void clickInside(int mouseKey, Rectangle rectangle, Robot robot, int delayMs){
212 moveMouseToMiddle(rectangle, robot, delayMs);
213 robot.delay(delayMs);
214 click(mouseKey, robot, delayMs);
215 }
216
217 public static void clickInside(int mouseKey, Rectangle rectangle, Robot robot){
218 clickInside(mouseKey, rectangle, robot, defaultDelay);
219 }
220
221 public static void clickInside(Rectangle rectangle, Robot robot, int delayMs){
222 clickInside(InputEvent.BUTTON1_MASK, rectangle, robot, delayMs);
223 }
224
225 public static void clickInside(Rectangle rectangle, Robot robot){
226 clickInside(rectangle, robot, defaultDelay);
227 }
228 }