1/* 2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23package org.netbeans.jemmy.drivers.input; 24 25import java.awt.Component; 26import java.awt.event.MouseEvent; 27import org.netbeans.jemmy.Timeout; 28import org.netbeans.jemmy.drivers.MouseDriver; 29import org.netbeans.jemmy.operators.ComponentOperator; 30import org.netbeans.jemmy.operators.Operator; 31 32/** 33 * MouseDriver using event dispatching. 34 * 35 * @author Alexandre Iline(alexandre.iline@oracle.com) 36 */ 37public class MouseEventDriver extends EventDriver implements MouseDriver { 38 39 /** 40 * Constructs a MouseEventDriver object. 41 * 42 * @param supported an array of supported class names 43 */ 44 public MouseEventDriver(String[] supported) { 45 super(supported); 46 } 47 48 /** 49 * Constructs a MouseEventDriver object. 50 */ 51 public MouseEventDriver() { 52 super(); 53 } 54 55 @Override 56 public void pressMouse(ComponentOperator oper, int x, int y, int mouseButton, int modifiers) { 57 dispatchEvent(oper.getSource(), 58 MouseEvent.MOUSE_PRESSED, 59 modifiers, x, y, 1, 60 mouseButton); 61 } 62 63 @Override 64 public void releaseMouse(ComponentOperator oper, int x, int y, int mouseButton, int modifiers) { 65 dispatchEvent(oper.getSource(), 66 MouseEvent.MOUSE_RELEASED, 67 modifiers, x, y, 1, 68 mouseButton); 69 } 70 71 @Override 72 public void moveMouse(ComponentOperator oper, int x, int y) { 73 dispatchEvent(oper.getSource(), 74 MouseEvent.MOUSE_MOVED, 75 0, x, y, 0, 76 Operator.getDefaultMouseButton()); 77 } 78 79 @Override 80 public void clickMouse(ComponentOperator oper, int x, int y, int clickCount, int mouseButton, 81 int modifiers, Timeout mouseClick) { 82 83 moveMouse(oper, x, y); 84 dispatchEvent(oper.getSource(), 85 MouseEvent.MOUSE_ENTERED, 86 0, x, y, 0, 87 Operator.getDefaultMouseButton()); 88 dispatchEvent(oper.getSource(), 89 MouseEvent.MOUSE_PRESSED, 90 modifiers, x, y, 1, 91 mouseButton); 92 93 for (int i = 1; i < clickCount; i++) { 94 dispatchEvent(oper.getSource(), 95 MouseEvent.MOUSE_RELEASED, 96 modifiers, x, y, i, 97 mouseButton); 98 dispatchEvent(oper.getSource(), 99 MouseEvent.MOUSE_CLICKED, 100 modifiers, x, y, i, 101 mouseButton); 102 dispatchEvent(oper.getSource(), 103 MouseEvent.MOUSE_PRESSED, 104 modifiers, x, y, i + 1, 105 mouseButton); 106 } 107 108 mouseClick.sleep(); 109 dispatchEvent(oper.getSource(), 110 MouseEvent.MOUSE_RELEASED, 111 modifiers, x, y, clickCount, 112 mouseButton); 113 dispatchEvent(oper.getSource(), 114 MouseEvent.MOUSE_CLICKED, 115 modifiers, x, y, clickCount, 116 mouseButton); 117 exitMouse(oper); 118 } 119 120 @Override 121 public void dragMouse(ComponentOperator oper, int x, int y, int mouseButton, int modifiers) { 122 dispatchEvent(oper.getSource(), 123 MouseEvent.MOUSE_DRAGGED, 124 modifiers, x, y, 1, 125 mouseButton); 126 } 127 128 @Override 129 public void dragNDrop(ComponentOperator oper, int start_x, int start_y, int end_x, int end_y, 130 int mouseButton, int modifiers, Timeout before, Timeout after) { 131 132 dispatchEvent(oper.getSource(), 133 MouseEvent.MOUSE_ENTERED, 134 0, start_x, start_y, 0, 135 Operator.getDefaultMouseButton()); 136 dispatchEvent(oper.getSource(), 137 MouseEvent.MOUSE_PRESSED, 138 modifiers, start_x, start_y, 1, 139 mouseButton); 140 before.sleep(); 141 dragMouse(oper, end_x, end_y, mouseButton, modifiers); 142 after.sleep(); 143 dispatchEvent(oper.getSource(), 144 MouseEvent.MOUSE_RELEASED, 145 modifiers, end_x, end_y, 1, 146 mouseButton); 147 exitMouse(oper); 148 } 149 150 @Override 151 public void enterMouse(ComponentOperator oper) { 152 dispatchEvent(oper.getSource(), 153 MouseEvent.MOUSE_ENTERED, 154 0, oper.getCenterX(), oper.getCenterY(), 0, 155 Operator.getDefaultMouseButton()); 156 } 157 158 @Override 159 public void exitMouse(ComponentOperator oper) { 160 dispatchEvent(oper.getSource(), 161 MouseEvent.MOUSE_EXITED, 162 0, oper.getCenterX(), oper.getCenterY(), 0, 163 Operator.getDefaultMouseButton()); 164 } 165 166 /** 167 * Dispatches a mouse event to the component. 168 * 169 * @param comp Component to dispatch events to. 170 * @param id an event id. 171 * @param modifiers a combination of {@code InputEvent.*_MASK} fields. 172 * @param x relative x coordinate of event point 173 * @param y relative y coordinate of event point 174 * @param clickCount click count 175 * @param mouseButton mouse button. 176 */ 177 protected void dispatchEvent(Component comp, int id, int modifiers, int x, int y, int clickCount, int mouseButton) { 178 dispatchEvent(comp, 179 new MouseEvent(comp, 180 id, 181 System.currentTimeMillis(), 182 modifiers | mouseButton, x, y, clickCount, 183 mouseButton == Operator.getPopupMouseButton() 184 && id == MouseEvent.MOUSE_PRESSED)); 185 } 186} 187