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.awt;
  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 org.jemmy.interfaces.Showable;
  37 
  38 /**
  39  *
  40  * @author shura
  41  */
  42 public class MouseImpl implements Mouse {
  43 
  44     private Wrap<?> target;
  45     private RobotDriver robotDriver;
  46     private boolean detached = false;
  47 
  48     static {
  49         if (Environment.getEnvironment().getTimeout(CLICK) == null) {
  50             Environment.getEnvironment().setTimeout(MouseImpl.CLICK);
  51         }
  52     }
  53 
  54     public MouseImpl(Wrap<?> target) {
  55         this.target = target;
  56         this.robotDriver = new RobotDriver(new Timeout("", 10));
  57     }
  58 
  59     public Mouse detached() {
  60         this.detached = true;
  61         return this;
  62     }
  63 
  64     private void runAction(Action action) {
  65         if (detached) {
  66             target.getEnvironment().getExecutor().executeDetached(target.getEnvironment(), false, action);
  67         } else {
  68             target.getEnvironment().getExecutor().execute(target.getEnvironment(), false, action);
  69         }
  70     }
  71 
  72     @Override
  73     public void press() {
  74         press(MouseButtons.BUTTON1);
  75     }
  76 
  77     @Override
  78     public void press(MouseButton button) {
  79         press(button, new Modifier[]{});
  80     }
  81 
  82     @Override
  83     public void press(final MouseButton button, final Modifier... modifiers) {
  84         runAction(new Action() {
  85 
  86             public void run(Object... parameters) {
  87                 robotDriver.pressMouse(button, modifiers);
  88             }
  89 
  90             @Override
  91             public String toString() {
  92                 return "pressing mouse button " + button + " with " + modifiers + " modifiers";
  93             }
  94         });
  95     }
  96 
  97     public void release() {
  98         release(MouseButtons.BUTTON1);
  99     }
 100 
 101     @Override
 102     public void release(MouseButton button) {
 103         release(button, new Modifier[]{});
 104     }
 105 
 106     @Override
 107     public void release(final MouseButton button, final Modifier... modifiers) {
 108         runAction(new Action() {
 109 
 110             public void run(Object... parameters) {
 111                 robotDriver.releaseMouse(button, modifiers);
 112             }
 113 
 114             @Override
 115             public String toString() {
 116                 return "releasing mouse button " + button + " with " + modifiers + " modifiers";
 117             }
 118         });
 119     }
 120 
 121     public void move() {
 122         move(target.getClickPoint());
 123     }
 124 
 125     public void move(final Point p) {
 126         runAction(new Action() {
 127 
 128             public void run(Object... parameters) {
 129                 robotDriver.moveMouse(getAbsolute(target, p));
 130             }
 131 
 132             @Override
 133             public String toString() {
 134                 return "moving mouse to " + p;
 135             }
 136         });
 137     }
 138 
 139     public void click() {
 140         this.click(1);
 141     }
 142 
 143     public void click(int count) {
 144         this.click(count, null);
 145     }
 146 
 147     /**
 148      * @param count todo document
 149      * @param p Point to click, if null {@linkplain Wrap#getClickPoint()
 150      * Wrap.getClickPoint()} method is invoked to get the point to click.
 151      */
 152     public void click(int count, Point p) {
 153         this.click(count, p, MouseButtons.BUTTON1);
 154     }
 155 
 156     /**
 157      *
 158      * @param count todo document
 159      * @param p Point to click, if null {@linkplain Wrap#getClickPoint()
 160      * Wrap.getClickPoint()} method is invoked to get the point to click.
 161      * @param button todo document
 162      */
 163     @Override
 164     public void click(int count, Point p, MouseButton button) {
 165         click(count, p, button, new Modifier[] {});
 166     }
 167 
 168     /**
 169      *
 170      * @param count todo document
 171      * @param p Point to click, if null {@linkplain Wrap#getClickPoint()
 172      * Wrap.getClickPoint()} method is invoked to get the point to click.
 173      * @param button todo document
 174      * @param modifiers todo document
 175      */
 176     @Override
 177     public void click(final int count, final Point p, final MouseButton button, final Modifier... modifiers) {
 178         runAction(new Action() {
 179 
 180             public void run(Object... parameters) {
 181                 if (target.is(Showable.class)) {
 182                     target.as(Showable.class).shower().show();
 183                 }
 184                 robotDriver.clickMouse(getAbsolute(target,
 185                         p == null ? target.getClickPoint() : p),
 186                         count, button, target.getEnvironment().getTimeout(CLICK), modifiers);
 187             }
 188 
 189             @Override
 190             public String toString() {
 191                 return "clicking " + button + " mouse button " + count + " times at " + p + " with " + Arrays.toString(modifiers) + " modifiers";
 192             }
 193         });
 194     }
 195 
 196     static Point getAbsolute(Wrap<?> target, Point p) {
 197         Rectangle screenBounds = target.getScreenBounds();
 198         return new Point(p.x + screenBounds.x, p.y + screenBounds.y);
 199     }
 200 
 201     private void turn(final Point p, final int amount, final Modifier... modifiers) {
 202         runAction(new Action() {
 203 
 204             public void run(Object... parameters) {
 205                 if (target.is(Showable.class)) {
 206                     target.as(Showable.class).shower().show();
 207                 }
 208                 robotDriver.turnWheel(getAbsolute(target,
 209                         p == null ? target.getClickPoint() : p),
 210                         amount, modifiers);
 211             }
 212 
 213             @Override
 214             public String toString() {
 215                 return "turning wheel to " + amount + " with " + Arrays.toString(modifiers) + " modifiers";
 216             }
 217         });
 218     }
 219 
 220     public void turnWheel(Point point, final int amount, Modifier... modifiers) {
 221         turn(point, amount, modifiers);
 222     }
 223 
 224     public void turnWheel(Point point, final int amount) {
 225         turn(point, amount, new Modifier[]{});
 226     }
 227 
 228     public void turnWheel(final int amount) {
 229         turn(null, amount, new Modifier[]{});
 230     }
 231 }