533 * @throws IllegalArgumentException if {@code ms} 534 * is not between 0 and 60,000 milliseconds inclusive 535 * @see java.lang.Thread#sleep 536 */ 537 public synchronized void delay(int ms) { 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 */ 587 public synchronized String toString() { 588 String params = "autoDelay = "+getAutoDelay()+", "+"autoWaitForIdle = "+isAutoWaitForIdle(); 589 return getClass().getName() + "[ " + params + " ]"; 590 } 591 } | 533 * @throws IllegalArgumentException if {@code ms} 534 * is not between 0 and 60,000 milliseconds inclusive 535 * @see java.lang.Thread#sleep 536 */ 537 public synchronized void delay(int ms) { 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 and tries 554 * to synchronize with the native windowing sub-system. 555 * @throws IllegalThreadStateException if called on the AWT event dispatching thread 556 */ 557 public synchronized void waitForIdle() { 558 checkNotDispatchThread(); 559 SunToolkit.flushPendingEvents(); 560 ((SunToolkit) Toolkit.getDefaultToolkit()).realSync(); 561 } 562 563 private void checkNotDispatchThread() { 564 if (EventQueue.isDispatchThread()) { 565 throw new IllegalThreadStateException("Cannot call method from the event dispatcher thread"); 566 } 567 } 568 569 /** 570 * Returns a string representation of this Robot. 571 * 572 * @return the string representation. 573 */ 574 public synchronized String toString() { 575 String params = "autoDelay = "+getAutoDelay()+", "+"autoWaitForIdle = "+isAutoWaitForIdle(); 576 return getClass().getName() + "[ " + params + " ]"; 577 } 578 } |