src/java.desktop/share/classes/java/awt/Robot.java

Print this page




  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 
  26 package java.awt;
  27 
  28 import java.awt.event.InputEvent;
  29 import java.awt.event.KeyEvent;
  30 import java.awt.image.BufferedImage;
  31 import java.awt.image.DataBufferInt;
  32 import java.awt.image.DirectColorModel;
  33 import java.awt.image.Raster;
  34 import java.awt.image.WritableRaster;
  35 import java.awt.peer.RobotPeer;
  36 import java.lang.reflect.InvocationTargetException;


  37 import sun.awt.AWTPermissions;
  38 import sun.awt.ComponentFactory;
  39 import sun.awt.SunToolkit;

  40 import sun.awt.image.SunWritableRaster;
  41 
  42 /**
  43  * This class is used to generate native system input events
  44  * for the purposes of test automation, self-running demos, and
  45  * other applications where control of the mouse and keyboard
  46  * is needed. The primary purpose of Robot is to facilitate
  47  * automated testing of Java platform implementations.
  48  * <p>
  49  * Using the class to generate input events differs from posting
  50  * events to the AWT event queue or AWT components in that the
  51  * events are generated in the platform's native input
  52  * queue. For example, <code>Robot.mouseMove</code> will actually move
  53  * the mouse cursor instead of just generating mouse move events.
  54  * <p>
  55  * Note that some platforms require special privileges or extensions
  56  * to access low-level input control. If the current platform configuration
  57  * does not allow input control, an <code>AWTException</code> will be thrown
  58  * when trying to construct Robot objects. For example, X-Window systems
  59  * will throw the exception if the XTEST 2.2 standard extension is not supported


 538         checkDelayArgument(ms);
 539         try {
 540             Thread.sleep(ms);
 541         } catch(InterruptedException ite) {
 542             ite.printStackTrace();
 543         }
 544     }
 545 
 546     private void checkDelayArgument(int ms) {
 547         if (ms < 0 || ms > MAX_DELAY) {
 548             throw new IllegalArgumentException("Delay must be to 0 to 60,000ms");
 549         }
 550     }
 551 
 552     /**
 553      * Waits until all events currently on the event queue have been processed.
 554      * @throws  IllegalThreadStateException if called on the AWT event dispatching thread
 555      */
 556     public synchronized void waitForIdle() {
 557         checkNotDispatchThread();
 558         // post a dummy event to the queue so we know when
 559         // all the events before it have been processed
 560         try {
 561             SunToolkit.flushPendingEvents();




 562             EventQueue.invokeAndWait( new Runnable() {
 563                                             public void run() {
 564                                                 // dummy implementation
 565                                             }
 566                                         } );



 567         } catch(InterruptedException ite) {
 568             System.err.println("Robot.waitForIdle, non-fatal exception caught:");
 569             ite.printStackTrace();
 570         } catch(InvocationTargetException ine) {
 571             System.err.println("Robot.waitForIdle, non-fatal exception caught:");
 572             ine.printStackTrace();
 573         }
 574     }
 575 
 576     private void checkNotDispatchThread() {
 577         if (EventQueue.isDispatchThread()) {
 578             throw new IllegalThreadStateException("Cannot call method from the event dispatcher thread");
 579         }
 580     }
 581 
 582     /**
 583      * Returns a string representation of this Robot.
 584      *
 585      * @return  the string representation.
 586      */


  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 
  26 package java.awt;
  27 
  28 import java.awt.event.InputEvent;
  29 import java.awt.event.KeyEvent;
  30 import java.awt.image.BufferedImage;
  31 import java.awt.image.DataBufferInt;
  32 import java.awt.image.DirectColorModel;
  33 import java.awt.image.Raster;
  34 import java.awt.image.WritableRaster;
  35 import java.awt.peer.RobotPeer;
  36 import java.lang.reflect.InvocationTargetException;
  37 import java.security.AccessController;
  38 
  39 import sun.awt.AWTPermissions;
  40 import sun.awt.ComponentFactory;
  41 import sun.awt.SunToolkit;
  42 import sun.awt.OSInfo;
  43 import sun.awt.image.SunWritableRaster;
  44 
  45 /**
  46  * This class is used to generate native system input events
  47  * for the purposes of test automation, self-running demos, and
  48  * other applications where control of the mouse and keyboard
  49  * is needed. The primary purpose of Robot is to facilitate
  50  * automated testing of Java platform implementations.
  51  * <p>
  52  * Using the class to generate input events differs from posting
  53  * events to the AWT event queue or AWT components in that the
  54  * events are generated in the platform's native input
  55  * queue. For example, <code>Robot.mouseMove</code> will actually move
  56  * the mouse cursor instead of just generating mouse move events.
  57  * <p>
  58  * Note that some platforms require special privileges or extensions
  59  * to access low-level input control. If the current platform configuration
  60  * does not allow input control, an <code>AWTException</code> will be thrown
  61  * when trying to construct Robot objects. For example, X-Window systems
  62  * will throw the exception if the XTEST 2.2 standard extension is not supported


 541         checkDelayArgument(ms);
 542         try {
 543             Thread.sleep(ms);
 544         } catch(InterruptedException ite) {
 545             ite.printStackTrace();
 546         }
 547     }
 548 
 549     private void checkDelayArgument(int ms) {
 550         if (ms < 0 || ms > MAX_DELAY) {
 551             throw new IllegalArgumentException("Delay must be to 0 to 60,000ms");
 552         }
 553     }
 554 
 555     /**
 556      * Waits until all events currently on the event queue have been processed.
 557      * @throws  IllegalThreadStateException if called on the AWT event dispatching thread
 558      */
 559     public synchronized void waitForIdle() {
 560         checkNotDispatchThread();
 561 

 562         try {
 563             SunToolkit.flushPendingEvents();
 564             // 7185258: realSync() call blocks all DnD tests on OS X
 565             if (AccessController.doPrivileged(OSInfo.getOSTypeAction()) = OSInfo.OSType.MACOSX) {
 566                 // post a dummy event to the queue so we know when
 567                 // all the events before it have been processed
 568                 EventQueue.invokeAndWait( new Runnable() {
 569                                                 public void run() {
 570                                                     // dummy implementation
 571                                                 }
 572                                             } );
 573             } else {
 574                 ((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
 575             }
 576         } catch(InterruptedException ite) {
 577             System.err.println("Robot.waitForIdle, non-fatal exception caught:");
 578             ite.printStackTrace();
 579         } catch(InvocationTargetException ine) {
 580             System.err.println("Robot.waitForIdle, non-fatal exception caught:");
 581             ine.printStackTrace();
 582         }
 583     }
 584 
 585     private void checkNotDispatchThread() {
 586         if (EventQueue.isDispatchThread()) {
 587             throw new IllegalThreadStateException("Cannot call method from the event dispatcher thread");
 588         }
 589     }
 590 
 591     /**
 592      * Returns a string representation of this Robot.
 593      *
 594      * @return  the string representation.
 595      */