jdk/test/lib/testlibrary/ExtendedRobot.java
author yan
Wed, 09 Apr 2014 17:30:42 +0400
changeset 24171 9a263c3c482b
child 25108 cbf21a81a738
permissions -rw-r--r--
8038631: Create wrapper for awt.Robot with additional functionality Reviewed-by: pchelko, alexsch Contributed-by: Dmitriy Ermashov <dmitriy.ermashov@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24171
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
     1
/*
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
     4
 *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    10
 *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    15
 * accompanied this code).
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    16
 *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    20
 *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    23
 * questions.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    24
 */
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    25
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    26
import sun.awt.ExtendedKeyCodes;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    27
import sun.awt.SunToolkit;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    28
import sun.security.action.GetIntegerAction;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    29
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    30
import java.awt.AWTException;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    31
import java.awt.Robot;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    32
import java.awt.GraphicsDevice;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    33
import java.awt.Toolkit;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    34
import java.awt.Point;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    35
import java.awt.MouseInfo;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    36
import java.awt.event.InputEvent;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    37
import java.security.AccessController;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    38
import java.security.PrivilegedAction;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    39
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    40
/**
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    41
 * ExtendedRobot is a subclass of {@link java.awt.Robot}. It provides some convenience methods that are
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    42
 * ought to be moved to {@link java.awt.Robot} class.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    43
 * <p>
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    44
 * ExtendedRobot uses delay {@link #getSyncDelay()} to make syncing threads with {@link #waitForIdle()}
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    45
 * more stable. This delay can be set once on creating object and could not be changed throughout object
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    46
 * lifecycle. Constructor reads vm integer property {@code java.awt.robotdelay} and sets the delay value
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    47
 * equal to the property value. If the property was not set 500 milliseconds default value is used.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    48
 * <p>
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    49
 * When using jtreg you would include this class via something like:
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    50
 * <pre>
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    51
 * {@literal @}library ../../../../lib/testlibrary
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    52
 * {@literal @}build ExtendedRobot
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    53
 * </pre>
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    54
 *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    55
 * @author      Dmitriy Ermashov
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    56
 * @since       1.9
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    57
 */
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    58
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    59
public class ExtendedRobot extends Robot {
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    60
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    61
    private static int DEFAULT_SPEED = 20;       // Speed for mouse glide and click
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    62
    private static int DEFAULT_SYNC_DELAY = 500; // Default Additional delay for waitForIdle()
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    63
    private static int DEFAULT_STEP_LENGTH = 2;  // Step length (in pixels) for mouse glide
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    64
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    65
    private final int syncDelay = DEFAULT_SYNC_DELAY;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    66
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    67
    //TODO: uncomment three lines below after moving functionality to java.awt.Robot
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    68
    //{
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    69
    //    syncDelay = AccessController.doPrivileged(new GetIntegerAction("java.awt.robotdelay", DEFAULT_SYNC_DELAY));
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    70
    //}
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    71
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    72
    /**
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    73
     * Constructs an ExtendedRobot object in the coordinate system of the primary screen.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    74
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    75
     * @throws  AWTException if the platform configuration does not allow low-level input
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    76
     *          control. This exception is always thrown when
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    77
     *          GraphicsEnvironment.isHeadless() returns true
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    78
     * @throws  SecurityException if {@code createRobot} permission is not granted
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    79
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    80
     * @see     java.awt.GraphicsEnvironment#isHeadless
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    81
     * @see     SecurityManager#checkPermission
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    82
     * @see     java.awt.AWTPermission
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    83
     */
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    84
    public ExtendedRobot() throws AWTException {
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    85
        super();
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    86
    }
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    87
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    88
    /**
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    89
     * Creates an ExtendedRobot for the given screen device. Coordinates passed
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    90
     * to ExtendedRobot method calls like mouseMove and createScreenCapture will
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    91
     * be interpreted as being in the same coordinate system as the specified screen.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    92
     * Note that depending on the platform configuration, multiple screens may either:
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    93
     * <ul>
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    94
     * <li>share the same coordinate system to form a combined virtual screen</li>
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    95
     * <li>use different coordinate systems to act as independent screens</li>
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    96
     * </ul>
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    97
     * This constructor is meant for the latter case.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    98
     * <p>
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
    99
     * If screen devices are reconfigured such that the coordinate system is
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   100
     * affected, the behavior of existing ExtendedRobot objects is undefined.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   101
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   102
     * @param   screen  A screen GraphicsDevice indicating the coordinate
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   103
     *                  system the Robot will operate in.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   104
     * @throws  AWTException if the platform configuration does not allow low-level input
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   105
     *          control. This exception is always thrown when
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   106
     *          GraphicsEnvironment.isHeadless() returns true.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   107
     * @throws  IllegalArgumentException if {@code screen} is not a screen
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   108
     *          GraphicsDevice.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   109
     * @throws  SecurityException if {@code createRobot} permission is not granted
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   110
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   111
     * @see     java.awt.GraphicsEnvironment#isHeadless
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   112
     * @see     GraphicsDevice
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   113
     * @see     SecurityManager#checkPermission
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   114
     * @see     java.awt.AWTPermission
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   115
     */
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   116
    public ExtendedRobot(GraphicsDevice screen) throws AWTException {
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   117
        super(screen);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   118
    }
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   119
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   120
    /**
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   121
     * Returns delay length for {@link #waitForIdle()} method
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   122
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   123
     * @return  Current delay value
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   124
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   125
     * @see     #waitForIdle()
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   126
     */
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   127
    public int getSyncDelay(){ return this.syncDelay; }
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   128
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   129
    /**
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   130
     * Clicks mouse button(s) by calling {@link java.awt.Robot#mousePress(int)} and
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   131
     * {@link java.awt.Robot#mouseRelease(int)} methods
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   132
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   133
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   134
     * @param   buttons The button mask; a combination of one or more mouse button masks.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   135
     * @throws  IllegalArgumentException if the {@code buttons} mask contains the mask for
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   136
     *          extra mouse button and support for extended mouse buttons is
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   137
     *          {@link Toolkit#areExtraMouseButtonsEnabled() disabled} by Java
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   138
     * @throws  IllegalArgumentException if the {@code buttons} mask contains the mask for
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   139
     *          extra mouse button that does not exist on the mouse and support for extended
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   140
     *          mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled() enabled}
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   141
     *          by Java
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   142
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   143
     * @see     #mousePress(int)
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   144
     * @see     #mouseRelease(int)
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   145
     * @see     InputEvent#getMaskForButton(int)
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   146
     * @see     Toolkit#areExtraMouseButtonsEnabled()
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   147
     * @see     java.awt.event.MouseEvent
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   148
     */
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   149
    public void click(int buttons) {
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   150
        mousePress(buttons);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   151
        waitForIdle(DEFAULT_SPEED);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   152
        mouseRelease(buttons);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   153
        waitForIdle();
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   154
    }
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   155
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   156
    /**
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   157
     * Clicks mouse button 1
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   158
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   159
     * @throws  IllegalArgumentException if the {@code buttons} mask contains the mask for
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   160
     *          extra mouse button and support for extended mouse buttons is
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   161
     *          {@link Toolkit#areExtraMouseButtonsEnabled() disabled} by Java
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   162
     * @throws  IllegalArgumentException if the {@code buttons} mask contains the mask for
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   163
     *          extra mouse button that does not exist on the mouse and support for extended
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   164
     *          mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled() enabled}
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   165
     *          by Java
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   166
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   167
     * @see     #click(int)
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   168
     */
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   169
    public void click() {
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   170
        click(InputEvent.BUTTON1_DOWN_MASK);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   171
    }
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   172
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   173
    /**
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   174
     * Waits until all events currently on the event queue have been processed with given
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   175
     * delay after syncing threads. It uses more advanced method of synchronizing threads
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   176
     * unlike {@link java.awt.Robot#waitForIdle()}
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   177
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   178
     * @param   delayValue  Additional delay length in milliseconds to wait until thread
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   179
     *                      sync been completed
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   180
     * @throws  sun.awt.SunToolkit.IllegalThreadException if called on the AWT event
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   181
     *          dispatching thread
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   182
     */
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   183
    public synchronized void waitForIdle(int delayValue) {
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   184
        SunToolkit.flushPendingEvents();
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   185
        ((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   186
        delay(delayValue);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   187
    }
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   188
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   189
    /**
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   190
     * Waits until all events currently on the event queue have been processed with delay
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   191
     * {@link #getSyncDelay()} after syncing threads. It uses more advanced method of
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   192
     * synchronizing threads unlike {@link java.awt.Robot#waitForIdle()}
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   193
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   194
     * @throws  sun.awt.SunToolkit.IllegalThreadException if called on the AWT event
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   195
     *          dispatching thread
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   196
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   197
     * @see     #waitForIdle(int)
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   198
     */
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   199
    @Override
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   200
    public synchronized void waitForIdle() {
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   201
        waitForIdle(syncDelay);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   202
    }
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   203
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   204
    /**
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   205
     * Move the mouse in multiple steps from where it is
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   206
     * now to the destination coordinates.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   207
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   208
     * @param   x   Destination point x coordinate
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   209
     * @param   y   Destination point y coordinate
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   210
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   211
     * @see     #glide(int, int, int, int)
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   212
     */
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   213
    public void glide(int x, int y) {
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   214
        Point p = MouseInfo.getPointerInfo().getLocation();
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   215
        glide(p.x, p.y, x, y);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   216
    }
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   217
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   218
    /**
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   219
     * Move the mouse in multiple steps from where it is
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   220
     * now to the destination point.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   221
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   222
     * @param   dest    Destination point
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   223
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   224
     * @see     #glide(int, int)
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   225
     */
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   226
    public void glide(Point dest) {
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   227
        glide(dest.x, dest.y);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   228
    }
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   229
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   230
    /**
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   231
     * Move the mouse in multiple steps from source coordinates
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   232
     * to the destination coordinates.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   233
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   234
     * @param   fromX   Source point x coordinate
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   235
     * @param   fromY   Source point y coordinate
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   236
     * @param   toX     Destination point x coordinate
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   237
     * @param   toY     Destination point y coordinate
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   238
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   239
     * @see     #glide(int, int, int, int, int, int)
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   240
     */
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   241
    public void glide(int fromX, int fromY, int toX, int toY) {
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   242
        glide(fromX, fromY, toX, toY, DEFAULT_STEP_LENGTH, DEFAULT_SPEED);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   243
    }
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   244
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   245
    /**
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   246
     * Move the mouse in multiple steps from source point to the
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   247
     * destination point with default speed and step length.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   248
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   249
     * @param   src     Source point
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   250
     * @param   dest    Destination point
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   251
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   252
     * @see     #glide(int, int, int, int, int, int)
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   253
     */
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   254
    public void glide(Point src, Point dest) {
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   255
        glide(src.x, src.y, dest.x, dest.y, DEFAULT_STEP_LENGTH, DEFAULT_SPEED);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   256
    }
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   257
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   258
    /**
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   259
     * Move the mouse in multiple steps from source point to the
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   260
     * destination point with given speed and step length.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   261
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   262
     * @param   srcX        Source point x cordinate
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   263
     * @param   srcY        Source point y cordinate
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   264
     * @param   destX       Destination point x cordinate
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   265
     * @param   destY       Destination point y cordinate
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   266
     * @param   stepLength  Approximate length of one step
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   267
     * @param   speed       Delay between steps.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   268
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   269
     * @see     #mouseMove(int, int)
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   270
     * @see     #delay(int)
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   271
     */
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   272
     public void glide(int srcX, int srcY, int destX, int destY, int stepLength, int speed) {
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   273
        int stepNum;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   274
        double tDx, tDy;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   275
        double dx, dy, ds;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   276
        double x, y;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   277
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   278
        dx = (destX - srcX);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   279
        dy = (destY - srcY);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   280
        ds = Math.sqrt(dx*dx + dy*dy);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   281
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   282
        tDx = dx / ds * stepLength;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   283
        tDy = dy / ds * stepLength;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   284
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   285
        int stepsCount = (int) ds / stepLength;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   286
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   287
        // Walk the mouse to the destination one step at a time
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   288
        mouseMove(srcX, srcY);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   289
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   290
        for (x = srcX, y = srcY, stepNum = 0;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   291
             stepNum < stepsCount;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   292
             stepNum++) {
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   293
            x += tDx;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   294
            y += tDy;
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   295
            mouseMove((int)x, (int)y);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   296
            delay(speed);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   297
        }
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   298
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   299
        // Ensure the mouse moves to the right destination.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   300
        // The steps may have led the mouse to a slightly wrong place.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   301
        mouseMove(destX, destY);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   302
    }
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   303
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   304
    /**
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   305
     * Moves mouse pointer to given screen coordinates.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   306
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   307
     * @param   position    Target position
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   308
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   309
     * @see     java.awt.Robot#mouseMove(int, int)
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   310
     */
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   311
    public synchronized void mouseMove(Point position) {
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   312
        mouseMove(position.x, position.y);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   313
    }
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   314
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   315
    /**
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   316
     * Successively presses and releases a given key.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   317
     * <p>
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   318
     * Key codes that have more than one physical key associated with them
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   319
     * (e.g. {@code KeyEvent.VK_SHIFT} could mean either the
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   320
     * left or right shift key) will map to the left key.
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   321
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   322
     * @param   keycode Key to press (e.g. {@code KeyEvent.VK_A})
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   323
     * @throws  IllegalArgumentException if {@code keycode} is not
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   324
     *          a valid key
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   325
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   326
     * @see     java.awt.Robot#keyPress(int)
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   327
     * @see     java.awt.Robot#keyRelease(int)
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   328
     * @see     java.awt.event.KeyEvent
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   329
     */
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   330
    public void type(int keycode) {
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   331
        keyPress(keycode);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   332
        waitForIdle(DEFAULT_SPEED);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   333
        keyRelease(keycode);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   334
        waitForIdle(DEFAULT_SPEED);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   335
    }
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   336
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   337
    /**
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   338
     * Types given character
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   339
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   340
     * @param   c   Character to be typed (e.g. {@code 'a'})
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   341
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   342
     * @see     #type(int)
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   343
     * @see     java.awt.event.KeyEvent
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   344
     */
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   345
    public void type(char c) {
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   346
        type(ExtendedKeyCodes.getExtendedKeyCodeForChar(c));
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   347
    }
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   348
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   349
    /**
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   350
     * Types given array of characters one by one
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   351
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   352
     * @param   symbols Array of characters to be typed
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   353
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   354
     * @see     #type(char)
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   355
     */
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   356
    public void type(char[] symbols) {
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   357
        for (int i = 0; i < symbols.length; i++) {
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   358
            type(symbols[i]);
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   359
        }
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   360
    }
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   361
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   362
    /**
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   363
     * Types given string
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   364
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   365
     * @param   s   String to be typed
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   366
     *
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   367
     * @see     #type(char[])
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   368
     */
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   369
    public void type(String s) {
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   370
        type(s.toCharArray());
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   371
    }
9a263c3c482b 8038631: Create wrapper for awt.Robot with additional functionality
yan
parents:
diff changeset
   372
}