1 /* 2 * Copyright (c) 2007, 2017, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package org.jemmy.input; 26 27 import org.jemmy.Rectangle; 28 import org.jemmy.Point; 29 import java.util.Arrays; 30 import org.jemmy.action.Action; 31 import org.jemmy.control.Wrap; 32 import org.jemmy.env.Environment; 33 import org.jemmy.env.Timeout; 34 import org.jemmy.interfaces.Modifier; 35 import org.jemmy.interfaces.Mouse; 36 import static org.jemmy.interfaces.Mouse.CLICK; 37 import org.jemmy.interfaces.Showable; 38 39 /** 40 * 41 * @author shura 42 */ 43 public class MouseImpl implements Mouse { 44 45 private Wrap<?> target; 46 private RobotDriver robotDriver; 47 private boolean detached = false; 48 49 static { 50 if (Environment.getEnvironment().getTimeout(CLICK) == null) { 51 Environment.getEnvironment().setTimeout(MouseImpl.CLICK); 52 } 53 } 54 55 public MouseImpl(Wrap<?> target) { 56 this.target = target; 57 this.robotDriver = new RobotDriver(new Timeout("", 10)); 58 } 59 60 public Mouse detached() { 61 this.detached = true; 62 return this; 63 } 64 65 private void runAction(Action action) { 66 if (detached) { 67 target.getEnvironment().getExecutor().executeDetached(target.getEnvironment(), false, action); 68 } else { 69 target.getEnvironment().getExecutor().execute(target.getEnvironment(), false, action); 70 } 71 } 72 73 @Override 74 public void press() { 75 press(MouseButtons.BUTTON1); 76 } 77 78 @Override 79 public void press(MouseButton button) { 80 press(button, new Modifier[]{}); 81 } 82 83 @Override 84 public void press(final MouseButton button, final Modifier... modifiers) { 85 runAction(new Action() { 86 87 public void run(Object... parameters) { 88 robotDriver.pressMouse(button, modifiers); 89 } 90 91 @Override 92 public String toString() { 93 return "pressing mouse button " + button + " with " + modifiers + " modifiers"; 94 } 95 }); 96 } 97 98 public void release() { 99 release(MouseButtons.BUTTON1); 100 } 101 102 @Override 103 public void release(MouseButton button) { 104 release(button, new Modifier[]{}); 105 } 106 107 @Override 108 public void release(final MouseButton button, final Modifier... modifiers) { 109 runAction(new Action() { 110 111 public void run(Object... parameters) { 112 robotDriver.releaseMouse(button, modifiers); 113 } 114 115 @Override 116 public String toString() { 117 return "releasing mouse button " + button + " with " + modifiers + " modifiers"; 118 } 119 }); 120 } 121 122 public void move() { 123 move(target.getClickPoint()); 124 } 125 126 public void move(final Point p) { 127 runAction(new Action() { 128 129 public void run(Object... parameters) { 130 robotDriver.moveMouse(getAbsolute(target, p)); 131 } 132 133 @Override 134 public String toString() { 135 return "moving mouse to " + p; 136 } 137 }); 138 } 139 140 public void click() { 141 this.click(1); 142 } 143 144 public void click(int count) { 145 this.click(count, null); 146 } 147 148 /** 149 * @param count todo document 150 * @param p Point to click, if null {@linkplain Wrap#getClickPoint() 151 * Wrap.getClickPoint()} method is invoked to get the point to click. 152 */ 153 public void click(int count, Point p) { 154 this.click(count, p, MouseButtons.BUTTON1); 155 } 156 157 /** 158 * 159 * @param count todo document 160 * @param p Point to click, if null {@linkplain Wrap#getClickPoint() 161 * Wrap.getClickPoint()} method is invoked to get the point to click. 162 * @param button todo document 163 */ 164 @Override 165 public void click(int count, Point p, MouseButton button) { 166 click(count, p, button, new Modifier[] {}); 167 } 168 169 /** 170 * 171 * @param count todo document 172 * @param p Point to click, if null {@linkplain Wrap#getClickPoint() 173 * Wrap.getClickPoint()} method is invoked to get the point to click. 174 * @param button todo document 175 * @param modifiers todo document 176 */ 177 @Override 178 public void click(final int count, final Point p, final MouseButton button, final Modifier... modifiers) { 179 runAction(new Action() { 180 181 public void run(Object... parameters) { 182 if (target.is(Showable.class)) { 183 target.as(Showable.class).shower().show(); 184 } 185 robotDriver.clickMouse(getAbsolute(target, 186 p == null ? target.getClickPoint() : p), 187 count, button, target.getEnvironment().getTimeout(CLICK), modifiers); 188 } 189 190 @Override 191 public String toString() { 192 return "clicking " + button + " mouse button " + count + " times at " + p + " with " + Arrays.toString(modifiers) + " modifiers"; 193 } 194 }); 195 } 196 197 static Point getAbsolute(Wrap<?> target, Point p) { 198 Rectangle screenBounds = target.getScreenBounds(); 199 return new Point(p.x + screenBounds.x, p.y + screenBounds.y); 200 } 201 202 private void turn(final Point p, final int amount, final Modifier... modifiers) { 203 runAction(new Action() { 204 205 public void run(Object... parameters) { 206 if (target.is(Showable.class)) { 207 target.as(Showable.class).shower().show(); 208 } 209 robotDriver.turnWheel(getAbsolute(target, 210 p == null ? target.getClickPoint() : p), 211 amount, modifiers); 212 } 213 214 @Override 215 public String toString() { 216 return "turning wheel to " + amount + " with " + Arrays.toString(modifiers) + " modifiers"; 217 } 218 }); 219 } 220 221 public void turnWheel(Point point, final int amount, Modifier... modifiers) { 222 turn(point, amount, modifiers); 223 } 224 225 public void turnWheel(Point point, final int amount) { 226 turn(point, amount, new Modifier[]{}); 227 } 228 229 public void turnWheel(final int amount) { 230 turn(null, amount, new Modifier[]{}); 231 } 232 }