001: /*
002:
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
004: *
005: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
006: *
007: * The contents of this file are subject to the terms of either the GNU
008: * General Public License Version 2 only ("GPL") or the Common
009: * Development and Distribution License("CDDL") (collectively, the
010: * "License"). You may not use this file except in compliance with the
011: * License. You can obtain a copy of the License at
012: * http://www.netbeans.org/cddl-gplv2.html
013: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
014: * specific language governing permissions and limitations under the
015: * License. When distributing the software, include this License Header
016: * Notice in each file and include the License file at
017: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
018: * particular file as subject to the "Classpath" exception as provided
019: * by Sun in the GPL Version 2 section of the License file that
020: * accompanied this code. If applicable, add the following below the
021: * License Header, with the fields enclosed by brackets [] replaced by
022: * your own identifying information:
023: * "Portions Copyrighted [year] [name of copyright owner]"
024: *
025: * Contributor(s): Alexandre Iline.
026:
027: *
028:
029: * The Original Software is the Jemmy library.
030:
031: * The Initial Developer of the Original Software is Alexandre Iline.
032:
033: * All Rights Reserved.
034: *
035: * If you wish your version of this file to be governed by only the CDDL
036: * or only the GPL Version 2, indicate your decision by adding
037: * "[Contributor] elects to include this software in this distribution
038: * under the [CDDL or GPL Version 2] license." If you do not indicate a
039: * single choice of license, a recipient has the option to distribute
040: * your version of this file under either the CDDL, the GPL Version 2 or
041: * to extend the choice of license to its licensees as provided above.
042: * However, if you add GPL Version 2 code and therefore, elected the GPL
043: * Version 2 license, then the option applies only if the new code is
044: * made subject to such option by the copyright holder.
045:
046: *
047:
048: *
049:
050: *
051:
052: * $Id$ $Revision$ $Date$
053:
054: *
055:
056: */
057:
058: package org.netbeans.jemmy.drivers.input;
059:
060: import java.awt.Component;
061:
062: import java.awt.event.MouseEvent;
063:
064: import org.netbeans.jemmy.Timeout;
065:
066: import org.netbeans.jemmy.drivers.MouseDriver;
067:
068: import org.netbeans.jemmy.operators.ComponentOperator;
069:
070: import org.netbeans.jemmy.operators.Operator;
071:
072: /**
073:
074: * MouseDriver using event dispatching.
075:
076: *
077:
078: * @author Alexandre Iline(alexandre.iline@sun.com)
079:
080: */
081:
082: public class MouseEventDriver extends EventDriver implements
083: MouseDriver {
084:
085: /**
086:
087: * Constructs a MouseEventDriver object.
088:
089: * @param supported an array of supported class names
090:
091: */
092:
093: public MouseEventDriver(String[] supported) {
094:
095: super (supported);
096:
097: }
098:
099: /**
100:
101: * Constructs a MouseEventDriver object.
102:
103: */
104:
105: public MouseEventDriver() {
106:
107: super ();
108:
109: }
110:
111: public void pressMouse(ComponentOperator oper, int x, int y,
112: int mouseButton, int modifiers) {
113:
114: dispatchEvent(oper.getSource(),
115:
116: MouseEvent.MOUSE_PRESSED,
117:
118: modifiers, x, y, 1,
119:
120: mouseButton);
121:
122: }
123:
124: public void releaseMouse(ComponentOperator oper, int x, int y,
125: int mouseButton, int modifiers) {
126:
127: dispatchEvent(oper.getSource(),
128:
129: MouseEvent.MOUSE_RELEASED,
130:
131: modifiers, x, y, 1,
132:
133: mouseButton);
134:
135: }
136:
137: public void moveMouse(ComponentOperator oper, int x, int y) {
138:
139: dispatchEvent(oper.getSource(),
140:
141: MouseEvent.MOUSE_MOVED,
142:
143: 0, x, y, 0,
144:
145: Operator.getDefaultMouseButton());
146:
147: }
148:
149: public void clickMouse(ComponentOperator oper, int x, int y,
150: int clickCount, int mouseButton,
151:
152: int modifiers, Timeout mouseClick) {
153:
154: moveMouse(oper, x, y);
155:
156: dispatchEvent(oper.getSource(),
157:
158: MouseEvent.MOUSE_ENTERED,
159:
160: 0, x, y, 0,
161:
162: Operator.getDefaultMouseButton());
163:
164: dispatchEvent(oper.getSource(),
165:
166: MouseEvent.MOUSE_PRESSED,
167:
168: modifiers, x, y, 1,
169:
170: mouseButton);
171:
172: for (int i = 1; i < clickCount; i++) {
173:
174: dispatchEvent(oper.getSource(),
175:
176: MouseEvent.MOUSE_RELEASED,
177:
178: modifiers, x, y, i,
179:
180: mouseButton);
181:
182: dispatchEvent(oper.getSource(),
183:
184: MouseEvent.MOUSE_CLICKED,
185:
186: modifiers, x, y, i,
187:
188: mouseButton);
189:
190: dispatchEvent(oper.getSource(),
191:
192: MouseEvent.MOUSE_PRESSED,
193:
194: modifiers, x, y, i + 1,
195:
196: mouseButton);
197:
198: }
199:
200: mouseClick.sleep();
201:
202: dispatchEvent(oper.getSource(),
203:
204: MouseEvent.MOUSE_RELEASED,
205:
206: modifiers, x, y, clickCount,
207:
208: mouseButton);
209:
210: dispatchEvent(oper.getSource(),
211:
212: MouseEvent.MOUSE_CLICKED,
213:
214: modifiers, x, y, clickCount,
215:
216: mouseButton);
217:
218: exitMouse(oper);
219:
220: }
221:
222: public void dragMouse(ComponentOperator oper, int x, int y,
223: int mouseButton, int modifiers) {
224:
225: dispatchEvent(oper.getSource(),
226:
227: MouseEvent.MOUSE_DRAGGED,
228:
229: modifiers, x, y, 1,
230:
231: mouseButton);
232:
233: }
234:
235: public void dragNDrop(ComponentOperator oper, int start_x,
236: int start_y, int end_x, int end_y,
237:
238: int mouseButton, int modifiers, Timeout before,
239: Timeout after) {
240:
241: dispatchEvent(oper.getSource(),
242:
243: MouseEvent.MOUSE_ENTERED,
244:
245: 0, start_x, start_y, 0,
246:
247: Operator.getDefaultMouseButton());
248:
249: dispatchEvent(oper.getSource(),
250:
251: MouseEvent.MOUSE_PRESSED,
252:
253: modifiers, start_x, start_y, 1,
254:
255: mouseButton);
256:
257: before.sleep();
258:
259: dragMouse(oper, end_x, end_y, mouseButton, modifiers);
260:
261: after.sleep();
262:
263: dispatchEvent(oper.getSource(),
264:
265: MouseEvent.MOUSE_RELEASED,
266:
267: modifiers, end_x, end_y, 1,
268:
269: mouseButton);
270:
271: exitMouse(oper);
272:
273: }
274:
275: public void enterMouse(ComponentOperator oper) {
276:
277: dispatchEvent(oper.getSource(),
278:
279: MouseEvent.MOUSE_ENTERED,
280:
281: 0, oper.getCenterX(), oper.getCenterY(), 0,
282:
283: Operator.getDefaultMouseButton());
284:
285: }
286:
287: public void exitMouse(ComponentOperator oper) {
288:
289: dispatchEvent(oper.getSource(),
290:
291: MouseEvent.MOUSE_EXITED,
292:
293: 0, oper.getCenterX(), oper.getCenterY(), 0,
294:
295: Operator.getDefaultMouseButton());
296:
297: }
298:
299: /**
300:
301: * Dispatches a mouse event to the component.
302:
303: * @param comp Component to dispatch events to.
304:
305: * @param id an event id.
306:
307: * @param modifiers a combination of <code>InputEvent.*_MASK</code> fields.
308:
309: * @param x relative x coordinate of event point
310:
311: * @param y relative y coordinate of event point
312:
313: * @param clickCount click count
314:
315: * @param mouseButton mouse button.
316:
317: */
318:
319: protected void dispatchEvent(Component comp, int id, int modifiers,
320: int x, int y, int clickCount, int mouseButton) {
321:
322: dispatchEvent(comp,
323:
324: new MouseEvent(comp,
325:
326: id,
327:
328: System.currentTimeMillis(),
329:
330: modifiers | mouseButton, x, y, clickCount,
331:
332: mouseButton == Operator.getPopupMouseButton() &&
333:
334: id == MouseEvent.MOUSE_PRESSED));
335:
336: }
337:
338: }
|