< prev index next >

core/JemmyAWTInput/src/org/jemmy/input/RobotDriver.java

Print this page




  27 
  28 import org.jemmy.Point;
  29 import java.awt.event.InputEvent;
  30 import java.util.HashMap;
  31 import org.jemmy.Rectangle;
  32 import org.jemmy.env.Timeout;
  33 import org.jemmy.env.Environment;
  34 import org.jemmy.image.Image;
  35 import static org.jemmy.input.AWTRobotInputFactory.*;
  36 import org.jemmy.interfaces.Button;
  37 import org.jemmy.interfaces.Keyboard.KeyboardButton;
  38 import org.jemmy.interfaces.Keyboard.KeyboardButtons;
  39 import org.jemmy.interfaces.Keyboard.KeyboardModifiers;
  40 import org.jemmy.interfaces.Modifier;
  41 import org.jemmy.interfaces.Mouse.MouseButton;
  42 import org.jemmy.interfaces.Mouse.MouseButtons;
  43 import org.jemmy.interfaces.Mouse.MouseModifiers;
  44 
  45 
  46 /**
  47  * @author Alexandre Iline(alexandre.iline@sun.com), KAM <mrkam@mail.ru>


  48  */
  49 public class RobotDriver {
  50 
  51     private static boolean haveOldPos = false;
  52     private static int smoothness;
  53     private static double oldX;
  54     private static double oldY;
  55 
  56     static {
  57         Environment.getEnvironment().setTimeout(
  58                 new Timeout(ROBOT_DELAY_TIMEOUT_NAME, 10));
  59         Environment.getEnvironment().setPropertyIfNotSet(
  60                 AWTRobotInputFactory.ROBOT_MOUSE_SMOOTHNESS_PROPERTY,
  61                 new Integer(Integer.MAX_VALUE).toString());
  62         smoothness =  Integer.parseInt(
  63                 (String)Environment.getEnvironment().getProperty(
  64                 AWTRobotInputFactory.ROBOT_MOUSE_SMOOTHNESS_PROPERTY));
  65     }
  66 
  67     /**
  68      * Sets mouse smoothness
  69      * @param mouseSmoothness the maximum distance in pixels between
  70      * mouse positions during movement
  71      * @see #moveMouse(Point)
  72      */
  73     public static void setMouseSmoothness(int mouseSmoothness) {
  74         smoothness = mouseSmoothness;
  75     }
  76 
  77     /**
  78      * Gets mouse smoothness
  79      * @return the maximum distance in pixels between
  80      * mouse positions during movement
  81      * @see #setMouseSmoothness(int)


 191      * @param modifiers Combination of InputEvent.*_DOWN_MASK
 192      * @param mouseClick Timeout of the last click
 193      * @see java.awt.event.InputEvent
 194      * @see java.awt.event.MouseEvent
 195      */
 196     public void clickMouse(Point point, int clickCount, MouseButton mouseButton, Timeout mouseClick, Modifier... modifiers) {
 197         pressModifiers(modifiers);
 198         moveMouse(point);
 199         makeAnOperation("mousePress", new Object[]{mouseButton}, new Class[]{MouseButton.class});
 200         for (int i = 1; i < clickCount; i++) {
 201             makeAnOperation("mouseRelease", new Object[]{mouseButton}, new Class[]{MouseButton.class});
 202             makeAnOperation("mousePress", new Object[]{mouseButton}, new Class[]{MouseButton.class});
 203         }
 204         mouseClick.sleep();
 205         makeAnOperation("mouseRelease", new Object[]{mouseButton}, new Class[]{MouseButton.class});
 206         releaseModifiers(modifiers);
 207     }
 208 
 209     /**
 210      * @deprecated Implementation doesn't seem to be correct as it ignores mouseButton and modifiers
 211      * @param point
 212      * @param mouseButton One of MouseEvent.BUTTON*_MASK
 213      * @param modifiers
 214      */
 215     public void dragMouse(Point point, int mouseButton, int modifiers) {
 216         moveMouse(point);
 217     }
 218 
 219     /**
 220      * Performs drag and drop from startPoint to endPoint using specified
 221      * mouseButton and modifiers to perform it.
 222      * @param startPoint Screen coordinates of drag start point
 223      * @param endPoint Screen coordinates of drag end point
 224      * @param mouseButton One of MouseEvent.BUTTON*_MASK
 225      * @param modifiers Combination of InputEvent.*_DOWN_MASK
 226      * @param before Timeout between pressing mouse at the startPoint and
 227      * mouse move
 228      * @param after Timeout between mouse move to the endPoint and mouse
 229      * release
 230      */
 231     public void dragNDrop(Point startPoint, Point endPoint, MouseButton mouseButton, Modifier modifiers[], Timeout before, Timeout after) {
 232         moveMouse(startPoint);
 233         pressMouse(mouseButton, modifiers);


 246         pressModifiers(modifiers);
 247         makeAnOperation("keyPress",
 248                 new Object[]{kbdButton},
 249                 new Class[]{KeyboardButton.class});
 250     }
 251 
 252     /**
 253      * Releases a key.
 254      * @param kbdButton Key code (<code>KeyEventVK_*</code> field.
 255      * @param modifiers a combination of <code>InputEvent.*_MASK</code> fields.
 256      */
 257     public void releaseKey(KeyboardButton kbdButton, Modifier... modifiers) {
 258         makeAnOperation("keyRelease",
 259                 new Object[]{kbdButton},
 260                 new Class[]{KeyboardButton.class});
 261         releaseModifiers(modifiers);
 262     }
 263 
 264     /**
 265      * Turns the wheel.
 266      * @param p
 267      * @param amount Either positive or negative
 268      * @param modifiers
 269      */
 270     public void turnWheel(Point p, int amount, Modifier... modifiers) {
 271         pressModifiers(modifiers);
 272         moveMouse(p);
 273         java.awt.Robot r = null;
 274         makeAnOperation("mouseWheel",
 275                 new Object[]{amount},
 276                 new Class[]{Integer.TYPE});
 277         releaseModifiers(modifiers);
 278     }
 279 
 280     /**
 281      * Performs a single operation.
 282      * @param method a name of <code>java.awt.Robot</code> method.
 283      * @param params method parameters
 284      * @param paramClasses method parameters classes
 285      */
 286     public void makeAnOperation(final String method, final Object[] params, final Class[] paramClasses) {
 287         RobotExecutor.get().makeAnOperation(method, params, paramClasses);
 288     }




  27 
  28 import org.jemmy.Point;
  29 import java.awt.event.InputEvent;
  30 import java.util.HashMap;
  31 import org.jemmy.Rectangle;
  32 import org.jemmy.env.Timeout;
  33 import org.jemmy.env.Environment;
  34 import org.jemmy.image.Image;
  35 import static org.jemmy.input.AWTRobotInputFactory.*;
  36 import org.jemmy.interfaces.Button;
  37 import org.jemmy.interfaces.Keyboard.KeyboardButton;
  38 import org.jemmy.interfaces.Keyboard.KeyboardButtons;
  39 import org.jemmy.interfaces.Keyboard.KeyboardModifiers;
  40 import org.jemmy.interfaces.Modifier;
  41 import org.jemmy.interfaces.Mouse.MouseButton;
  42 import org.jemmy.interfaces.Mouse.MouseButtons;
  43 import org.jemmy.interfaces.Mouse.MouseModifiers;
  44 
  45 
  46 /**
  47  * @author shura
  48  * @author mrkam
  49  *
  50  */
  51 public class RobotDriver {
  52 
  53     private static boolean haveOldPos = false;
  54     private static int smoothness;
  55     private static double oldX;
  56     private static double oldY;
  57 
  58     static {
  59         Environment.getEnvironment().setTimeout(
  60                 new Timeout(ROBOT_DELAY_TIMEOUT_NAME, 10));
  61         Environment.getEnvironment().setPropertyIfNotSet(
  62                 AWTRobotInputFactory.ROBOT_MOUSE_SMOOTHNESS_PROPERTY,
  63                 Integer.toString(Integer.MAX_VALUE));
  64         smoothness =  Integer.parseInt(
  65                 (String)Environment.getEnvironment().getProperty(
  66                 AWTRobotInputFactory.ROBOT_MOUSE_SMOOTHNESS_PROPERTY));
  67     }
  68 
  69     /**
  70      * Sets mouse smoothness
  71      * @param mouseSmoothness the maximum distance in pixels between
  72      * mouse positions during movement
  73      * @see #moveMouse(Point)
  74      */
  75     public static void setMouseSmoothness(int mouseSmoothness) {
  76         smoothness = mouseSmoothness;
  77     }
  78 
  79     /**
  80      * Gets mouse smoothness
  81      * @return the maximum distance in pixels between
  82      * mouse positions during movement
  83      * @see #setMouseSmoothness(int)


 193      * @param modifiers Combination of InputEvent.*_DOWN_MASK
 194      * @param mouseClick Timeout of the last click
 195      * @see java.awt.event.InputEvent
 196      * @see java.awt.event.MouseEvent
 197      */
 198     public void clickMouse(Point point, int clickCount, MouseButton mouseButton, Timeout mouseClick, Modifier... modifiers) {
 199         pressModifiers(modifiers);
 200         moveMouse(point);
 201         makeAnOperation("mousePress", new Object[]{mouseButton}, new Class[]{MouseButton.class});
 202         for (int i = 1; i < clickCount; i++) {
 203             makeAnOperation("mouseRelease", new Object[]{mouseButton}, new Class[]{MouseButton.class});
 204             makeAnOperation("mousePress", new Object[]{mouseButton}, new Class[]{MouseButton.class});
 205         }
 206         mouseClick.sleep();
 207         makeAnOperation("mouseRelease", new Object[]{mouseButton}, new Class[]{MouseButton.class});
 208         releaseModifiers(modifiers);
 209     }
 210 
 211     /**
 212      * @deprecated Implementation doesn't seem to be correct as it ignores mouseButton and modifiers
 213      * @param point todo document
 214      * @param mouseButton One of MouseEvent.BUTTON*_MASK
 215      * @param modifiers todo document
 216      */
 217     public void dragMouse(Point point, int mouseButton, int modifiers) {
 218         moveMouse(point);
 219     }
 220 
 221     /**
 222      * Performs drag and drop from startPoint to endPoint using specified
 223      * mouseButton and modifiers to perform it.
 224      * @param startPoint Screen coordinates of drag start point
 225      * @param endPoint Screen coordinates of drag end point
 226      * @param mouseButton One of MouseEvent.BUTTON*_MASK
 227      * @param modifiers Combination of InputEvent.*_DOWN_MASK
 228      * @param before Timeout between pressing mouse at the startPoint and
 229      * mouse move
 230      * @param after Timeout between mouse move to the endPoint and mouse
 231      * release
 232      */
 233     public void dragNDrop(Point startPoint, Point endPoint, MouseButton mouseButton, Modifier modifiers[], Timeout before, Timeout after) {
 234         moveMouse(startPoint);
 235         pressMouse(mouseButton, modifiers);


 248         pressModifiers(modifiers);
 249         makeAnOperation("keyPress",
 250                 new Object[]{kbdButton},
 251                 new Class[]{KeyboardButton.class});
 252     }
 253 
 254     /**
 255      * Releases a key.
 256      * @param kbdButton Key code (<code>KeyEventVK_*</code> field.
 257      * @param modifiers a combination of <code>InputEvent.*_MASK</code> fields.
 258      */
 259     public void releaseKey(KeyboardButton kbdButton, Modifier... modifiers) {
 260         makeAnOperation("keyRelease",
 261                 new Object[]{kbdButton},
 262                 new Class[]{KeyboardButton.class});
 263         releaseModifiers(modifiers);
 264     }
 265 
 266     /**
 267      * Turns the wheel.
 268      * @param p todo document
 269      * @param amount Either positive or negative
 270      * @param modifiers a combination of <code>InputEvent.*_MASK</code> fields.
 271      */
 272     public void turnWheel(Point p, int amount, Modifier... modifiers) {
 273         pressModifiers(modifiers);
 274         moveMouse(p);
 275         java.awt.Robot r = null;
 276         makeAnOperation("mouseWheel",
 277                 new Object[]{amount},
 278                 new Class[]{Integer.TYPE});
 279         releaseModifiers(modifiers);
 280     }
 281 
 282     /**
 283      * Performs a single operation.
 284      * @param method a name of <code>java.awt.Robot</code> method.
 285      * @param params method parameters
 286      * @param paramClasses method parameters classes
 287      */
 288     public void makeAnOperation(final String method, final Object[] params, final Class[] paramClasses) {
 289         RobotExecutor.get().makeAnOperation(method, params, paramClasses);
 290     }


< prev index next >