< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1999, 2014, 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 
  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      */

 596     public synchronized String toString() {
 597         String params = "autoDelay = "+getAutoDelay()+", "+"autoWaitForIdle = "+isAutoWaitForIdle();
 598         return getClass().getName() + "[ " + params + " ]";
 599     }
 600 }
   1 /*
   2  * Copyright (c) 1999, 2015, 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 
  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 
  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         SunToolkit.flushPendingEvents();










 559         ((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
 560     }








 561 
 562     private void checkNotDispatchThread() {
 563         if (EventQueue.isDispatchThread()) {
 564             throw new IllegalThreadStateException("Cannot call method from the event dispatcher thread");
 565         }
 566     }
 567 
 568     /**
 569      * Returns a string representation of this Robot.
 570      *
 571      * @return  the string representation.
 572      */
 573     @Override
 574     public synchronized String toString() {
 575         String params = "autoDelay = "+getAutoDelay()+", "+"autoWaitForIdle = "+isAutoWaitForIdle();
 576         return getClass().getName() + "[ " + params + " ]";
 577     }
 578 }
< prev index next >