1 19 20 package org.openide.explorer.propertysheet; 21 22 import java.awt.AWTEvent ; 23 import java.awt.BorderLayout ; 24 import java.awt.Component ; 25 import java.awt.Container ; 26 import java.awt.Dialog ; 27 import java.awt.EventQueue ; 28 import java.awt.Graphics ; 29 import java.awt.KeyboardFocusManager ; 30 import java.awt.Point ; 31 import java.awt.Rectangle ; 32 import java.awt.Toolkit ; 33 import java.awt.Window ; 34 import java.awt.event.ActionEvent ; 35 import java.awt.event.ActionListener ; 36 import java.awt.event.FocusEvent ; 37 import java.awt.event.FocusListener ; 38 import java.awt.event.KeyEvent ; 39 import java.awt.event.MouseEvent ; 40 import java.awt.event.WindowAdapter ; 41 import java.awt.event.WindowEvent ; 42 import java.beans.PropertyEditorManager ; 43 import java.lang.reflect.Method ; 44 import javax.swing.JButton ; 45 import javax.swing.JComponent ; 46 import javax.swing.JDialog ; 47 import javax.swing.JFrame ; 48 import javax.swing.JTable ; 49 import javax.swing.JTextField ; 50 import javax.swing.SwingUtilities ; 51 import org.netbeans.junit.NbTestCase; 52 import org.openide.nodes.Node; 53 54 66 public class ExtTestCase extends NbTestCase { 67 68 private static JDialog jd; 69 70 private static Boolean focusTestsSafe = null; 71 72 protected static JFrame jf = null; 73 74 private static int SLEEP_LENGTH = 100; 75 76 private static JTextField jtf = null; 77 78 static { 79 String [] syspesp = PropertyEditorManager.getEditorSearchPath(); 81 String [] nbpesp = new String [] { 82 "org.netbeans.beaninfo.editors", "org.openide.explorer.propertysheet.editors", }; 85 String [] allpesp = new String [syspesp.length + nbpesp.length]; 86 System.arraycopy(nbpesp, 0, allpesp, 0, nbpesp.length); 87 System.arraycopy(syspesp, 0, allpesp, nbpesp.length, syspesp.length); 88 PropertyEditorManager.setEditorSearchPath(allpesp); 89 } 90 91 92 public ExtTestCase(String name) { 93 super(name); 94 } 95 96 public static void main(String [] args) { 97 SwingUtilities.invokeLater(new Runnable () { 98 public void run() { 99 System.err.println("canSafelyRunFocusTests: " + canSafelyRunFocusTests()); 100 } 101 }); 102 } 103 104 protected static final void installCorePropertyEditors() { 105 String [] syspesp = PropertyEditorManager.getEditorSearchPath(); 106 String [] nbpesp = new String [] { 107 "org.netbeans.beaninfo.editors", "org.openide.explorer.propertysheet.editors", }; 110 String [] allpesp = new String [syspesp.length + nbpesp.length]; 111 System.arraycopy(nbpesp, 0, allpesp, 0, nbpesp.length); 112 System.arraycopy(syspesp, 0, allpesp, nbpesp.length, syspesp.length); 113 PropertyEditorManager.setEditorSearchPath(allpesp); 114 } 115 116 public void testNothing() { 117 } 119 120 128 public static boolean canSafelyRunFocusTests() { 129 if (focusTestsSafe != null) { 130 return focusTestsSafe.booleanValue(); 131 } 132 try { 133 jf = new JFrame (); 134 JButton jb = new JButton ("Show the dialog"); 135 jb.addActionListener(new ActionListener () { 136 public void actionPerformed(ActionEvent ae) { 137 try { 138 showDialog(jf); 139 } catch (Exception e) { 140 e.printStackTrace(); 141 } 142 } 143 }); 144 jf.getContentPane().setLayout(new BorderLayout ()); 145 jf.getContentPane().add(jb, BorderLayout.CENTER); 146 jf.setBounds(20,20, 100,100); 147 new WaitWindow(jf); 148 149 boolean frameGotFocus = checkFocusedContainer(jf); 150 151 if (!frameGotFocus) { 152 System.err.println("Newly shown JFrame did not get focus. Cannot reliably run focus-behavior-dependent tests on this machine"); 153 focusTestsSafe = Boolean.FALSE; 154 return false; 155 } 156 157 click(jb); 158 159 boolean frameLostFocus = checkNotFocusedContainer(jf); 160 if (!frameLostFocus) { 161 System.err.println("Newly shown child dialog of a frame did not remove focus from the frame. Cannot reliably run focus-behavior-dependent tests on this machine"); 162 focusTestsSafe = Boolean.FALSE; 163 if (jd != null) { 164 jd.hide(); 165 jd.dispose(); 166 } 167 return false; 168 } 169 170 boolean dlgGotFocus = checkFocusedContainer(jd); 171 if (!dlgGotFocus) { 172 System.err.println("Newly shown child dialog of a frame did not receive focus when it was shown. Cannot reliably run focus-behavior-dependent tests on this machine"); 173 focusTestsSafe = Boolean.FALSE; 174 return false; 175 } 176 177 boolean typingWorks = tryDispatchingKeystrokes(jtf); 178 if (!typingWorks) { 179 System.err.println("Typing into dialog did not produce expected result"); 180 focusTestsSafe = Boolean.FALSE; 181 return false; 182 } 183 184 jd.hide(); 185 187 sleep(); 188 189 boolean dlgReturnedFocus = checkFocusedContainer(jf); 190 if (!dlgReturnedFocus) { 191 System.err.println("Hiding child dialog did not return focus to parent. Cannot reliably run focus-behavior-dependent tests on this machine."); 192 focusTestsSafe = Boolean.FALSE; 193 return false; 194 } 195 196 focusTestsSafe = Boolean.TRUE; 197 System.err.println("Focus dependent tests can be safely run and will be included."); 198 return true; 199 } catch (Exception e) { 200 e.printStackTrace(); 201 focusTestsSafe = Boolean.FALSE; 202 return false; 203 } finally { 204 if (jf != null) { 205 jf.hide(); 206 jf.dispose(); 207 } 208 if (jd != null) { 209 jd.hide(); 210 jd.dispose(); 211 } 212 jf = null; 213 jd = null; 214 jtf = null; 215 } 216 } 217 218 private static boolean debug = false; 219 220 protected static boolean isDebug() { 221 return debug; 222 } 223 224 protected static void setCurrentNode(final Node node, final PropertySheet ps) throws Exception { 225 Runnable run = new Runnable () { 226 public void run() { 227 ps.setCurrentNode(node); 228 } 229 }; 230 invokeNow(run); 231 ensurePainted(ps); 232 sleep(); 233 } 234 235 protected static void ensurePainted(final JComponent ps) throws Exception { 236 if (SwingUtilities.isEventDispatchThread()) { 240 Graphics g = ps.getGraphics(); 241 ps.paintImmediately(0,0,ps.getWidth(), ps.getHeight()); 242 } else { 243 SwingUtilities.invokeAndWait(new Runnable () { 244 public void run() { 245 Graphics g = ps.getGraphics(); 246 ps.paintImmediately(0,0,ps.getWidth(), ps.getHeight()); 247 } 248 }); 249 } 250 } 251 252 254 protected static void setDebug(boolean val) { 255 debug = val; 256 } 257 258 private static boolean tryDispatchingKeystrokes(final JTextField j) throws Exception { 259 requestFocus(j); 260 j.selectAll(); 261 sleep(); 262 263 pressKey(j, KeyEvent.VK_SHIFT); 264 265 typeString("HELLO", j); 266 267 releaseKey(j, KeyEvent.VK_SHIFT); 268 269 System.err.println("Text area text is now " + j.getText()); 270 271 return "HELLO".equals(j.getText()); 272 } 273 274 protected static Component focusComp() throws Exception { 275 sleep(); 276 return KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner(); 277 } 278 279 281 protected static boolean checkFocusedContainer(Container c) throws Exception { 282 synchronized (c.getTreeLock()) { 283 c.getTreeLock().notifyAll(); 284 } 285 if (SwingUtilities.isEventDispatchThread()) { 286 drainEventQueue(); 289 } else { 290 SwingUtilities.invokeAndWait(new Runnable () { 291 public void run() { 292 try { 293 drainEventQueue(); 294 } catch (Exception e) { 295 throw new RuntimeException (e); 296 } 297 } 298 }); 299 } 300 Toolkit.getDefaultToolkit().sync(); 301 302 sleep(); 306 sleep(); 307 sleep(); 308 sleep(); 309 sleep(); 310 311 Window w = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow(); 312 Component currowner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 313 Component permowner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner(); 314 315 if (w == null) { 316 return false; 317 } 318 319 if (w.isAncestorOf(currowner)) { 320 if (c instanceof Window && w != c) { 322 System.err.println("Focused window is " + w); 323 return false; 324 } 325 } 326 if (c.isAncestorOf(currowner)) { 327 return true; 328 } 329 if (c.isAncestorOf(permowner)) { 330 return true; 331 } 332 return false; 333 } 334 335 337 protected static boolean checkNotFocusedContainer(Container c) throws Exception { 338 return !checkFocusedContainer(c); 339 } 340 341 private static void showDialog(JFrame parent) throws Exception { 342 jd = new JDialog (parent); 343 jtf = new JTextField ("What a day I'm having!"); 344 jd.getContentPane().setLayout(new BorderLayout ()); 345 jd.getContentPane().add(jtf, BorderLayout.CENTER); 346 jd.setBounds(400,400,100, 50); 347 new WaitWindow(jd); 348 sleep(); 349 } 350 351 352 protected static void click(final Component comp) throws Exception { 353 maybeInvokeLater(new Clicker(comp, MouseEvent.MOUSE_PRESSED)); 354 sleep(); 355 maybeInvokeLater(new Clicker(comp, MouseEvent.MOUSE_RELEASED)); 356 sleep(); 357 maybeInvokeLater(new Clicker(comp, MouseEvent.MOUSE_CLICKED)); 358 sleep(); 359 } 360 361 363 protected static void click(final Component comp, int x, int y) throws Exception { 364 maybeInvokeLater(new Clicker(comp, x, y, MouseEvent.MOUSE_PRESSED)); 365 sleep(); 366 maybeInvokeLater(new Clicker(comp, x, y, MouseEvent.MOUSE_RELEASED)); 367 sleep(); 368 maybeInvokeLater(new Clicker(comp, x, y, MouseEvent.MOUSE_CLICKED)); 369 sleep(); 370 } 371 372 373 protected static void press(final Component comp) throws Exception { 374 maybeInvokeLater(new Clicker(comp, MouseEvent.MOUSE_PRESSED)); 375 sleep(); 376 } 377 378 380 protected static void press(final Component comp, int x, int y) throws Exception { 381 maybeInvokeLater(new Clicker(comp, x, y, MouseEvent.MOUSE_PRESSED)); 382 sleep(); 383 } 384 385 386 protected static void release(final Component comp) throws Exception { 387 maybeInvokeLater(new Clicker(comp, MouseEvent.MOUSE_PRESSED)); 388 sleep(); 389 } 390 391 393 protected static void release(final Component comp, int x, int y) throws Exception { 394 maybeInvokeLater(new Clicker(comp, x, y, MouseEvent.MOUSE_PRESSED)); 395 sleep(); 396 } 397 398 399 protected static void requestFocus(final Component comp) throws Exception { 400 maybeInvokeLater(new FocusRequester(comp)); 401 sleep(); 402 } 403 404 405 protected static void pressCell(JTable tbl, int x, int y) throws Exception { 406 Rectangle r = tbl.getCellRect(x, y, false); 407 System.err.println("Pressing table at " + (r.x+5) + "," + r.y+5); 408 press(tbl, r.x+5, r.y+5); 409 } 410 411 private static class FocusRequester implements Runnable { 412 private Component comp; 413 public FocusRequester(Component comp) { 414 this.comp = comp; 415 } 416 public void run() { 417 comp.requestFocus(); 418 } 419 } 420 421 private static interface EventGenerator { 422 public AWTEvent getEvent(); 423 } 424 425 427 private static class Clicker implements Runnable , EventGenerator { 428 int x = -1; 429 int y = -1; 430 private Component target = null; 431 int type; 432 public Clicker(Component target, int type) { 433 this.target = target; 434 this.type = type; 435 } 436 437 public Clicker(Component target, int x, int y, int type) { 438 this.target = target; 439 this.x = x; 440 this.y = y; 441 this.type = type; 442 } 443 444 public AWTEvent getEvent() { 445 Point toClick; 446 if (x == -1 && y == -1) { 447 toClick = new Point (5,5); 448 } else { 449 toClick = new Point (x,y); 450 } 451 Component realtarget = target.getComponentAt(toClick); 452 453 MouseEvent result = new MouseEvent (realtarget, type, 454 System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, toClick.x, 455 toClick.y, 2, false); 456 457 return result; 458 } 459 460 public void run() { 461 try { 462 dispatchEvent(null, getEvent()); 463 } catch (Exception e) { 464 throw new RuntimeException (e); 465 } 466 } 467 } 468 469 475 protected static void maybeInvokeLater(Runnable run) throws Exception { 476 WrapperRunnable wrap = new WrapperRunnable(run); 477 if (!SwingUtilities.isEventDispatchThread()) { 478 SwingUtilities.invokeAndWait(wrap); 479 } else { 480 if (run instanceof EventGenerator) { 481 AWTEvent evt = ((EventGenerator)run).getEvent(); 482 ((Component ) evt.getSource()).dispatchEvent(evt); 483 } else { 484 wrap.run(); 485 } 486 } 487 wrap.throwAnyExceptions(); 488 sleep(); 489 } 490 491 protected static void invokeNow(Runnable run) throws Exception { 492 WrapperRunnable wrap = new WrapperRunnable(run); 493 if (!SwingUtilities.isEventDispatchThread()) { 494 SwingUtilities.invokeAndWait(wrap); 495 } else { 496 if (run instanceof EventGenerator) { 497 AWTEvent evt = ((EventGenerator)run).getEvent(); 498 ((Component ) evt.getSource()).dispatchEvent(evt); 499 } else { 500 wrap.run(); 501 } 502 } 503 wrap.throwAnyExceptions(); 504 sleep(); 505 } 506 507 509 public static class WrapperRunnable implements Runnable { 510 protected Exception exception = null; 511 private Runnable run; 512 public WrapperRunnable(Runnable run) { 513 this.run = run; 514 } 515 516 protected WrapperRunnable() { 517 this.run = null; 518 } 519 520 public void throwAnyExceptions() throws Exception { 521 if (exception != null) { 522 throw exception; 523 } 524 } 525 526 public void run() { 527 if (run == null) { 528 return; 530 } 531 try { 532 run.run(); 533 if (run instanceof WrapperRunnable) { 534 ((WrapperRunnable)run).throwAnyExceptions(); 535 } 536 } catch (Exception e) { 537 exception = e; 538 } 539 } 540 } 541 542 protected static void typeString(String s, Component comp) throws Exception { 543 char[] c = s.toCharArray(); 544 for (int i=0; i < c.length; i++) { 545 typeKey(comp, c[i]); 546 } 547 } 548 549 551 protected static void ctrlTypeKey(Component target, int key) throws Exception { 552 typeKey(target, key, KeyEvent.CTRL_MASK); 553 } 554 555 557 protected static void ctrlShiftTypeKey(Component target, int key) throws Exception { 558 typeKey(target, key, KeyEvent.SHIFT_MASK | KeyEvent.CTRL_MASK); 559 } 560 561 563 protected static void shiftTypeKey(Component target, int key) throws Exception { 564 typeKey(target, key, KeyEvent.SHIFT_MASK); 565 } 566 567 569 protected static void typeKey(Component target, int key) throws Exception { 570 pressKey(target, key); 571 typedKey(target, key); 572 releaseKey(target, key); 573 } 574 575 578 protected static void typeKey(Component target, int key, int mask) throws Exception { 579 pressKey(target, key, mask); 580 typedKey(target, key, mask); 581 releaseKey(target, key, mask); 582 } 583 584 585 protected static void pressKey(Component target, int key) throws Exception { 586 maybeInvokeLater(new Typist(target, key, KeyEvent.KEY_PRESSED, 0)); 587 sleep(); 588 } 589 590 591 protected static void pressKey(Component target, int key, int mask) throws Exception { 592 maybeInvokeLater(new Typist(target, key, KeyEvent.KEY_PRESSED, mask)); 593 sleep(); 594 } 595 596 597 protected static void releaseKey(Component target, int key) throws Exception { 598 maybeInvokeLater(new Typist(target, key, KeyEvent.KEY_RELEASED, 0)); 599 sleep(); 600 } 601 602 603 protected static void releaseKey(Component target, int key, int mask) throws Exception { 604 maybeInvokeLater(new Typist(target, key, KeyEvent.KEY_RELEASED, mask)); 605 sleep(); 606 } 607 608 609 protected static void typedKey(Component target, int key) throws Exception { 610 maybeInvokeLater(new Typist(target, key, 611 KeyEvent.KEY_TYPED, 0)); 612 sleep(); 613 } 614 615 616 protected static void typedKey(Component target, int key, int mask) throws Exception { 617 maybeInvokeLater(new Typist(target, key, 618 KeyEvent.KEY_TYPED, mask)); 619 sleep(); 620 } 621 622 public static boolean waitForAnythingToGetFocus() { 623 Component foc = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 624 int ct=0; 625 while (foc == null) { 626 try { 627 Thread.currentThread().sleep(100); 628 } catch (Exception e ){} 629 foc = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 630 ct++; 631 if (ct > 200) { 632 break; 633 } 634 } 635 return foc != null; 636 } 637 638 public static boolean waitForDialog() { 639 Container c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow(); 640 int ct = 0; 641 while (!(c instanceof Dialog )) { 642 try { 643 Thread.currentThread().sleep(50); 644 } catch (Exception e) {} 645 c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow(); 646 ct++; 647 if (ct > 100) { 648 return false; 649 } 650 } 651 return true; 652 } 653 654 public static boolean waitForFrame() { 655 Container c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow(); 656 int ct = 0; 657 while (!(c instanceof JFrame )) { 658 try { 659 Thread.currentThread().sleep(50); 660 } catch (Exception e) {} 661 c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow(); 662 ct++; 663 if (ct > 100) { 664 return false; 665 } 666 } 667 return true; 668 } 669 670 public static Component waitForComponentOrChildToGetFocus(Container c) { 671 Component foc = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 672 int ct=0; 673 while (foc == null || (foc != c && !c.isAncestorOf(foc))) { 674 try { 675 Thread.currentThread().sleep(100); 676 } catch (Exception e ) {} 677 foc = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 678 ct++; 679 if (ct > 200) { 680 break; 681 } 682 } 683 return foc; 684 } 685 686 687 private static class Typist implements Runnable , EventGenerator { 688 private Component target; 689 private int key; 690 private int type; 691 private int mask; 692 public Typist(Component target, int key, int type, int mask) { 693 this.target = target; 694 this.key = key; 695 this.type = type; 696 this.mask = mask; 697 } 698 699 public AWTEvent getEvent() { 700 return createKeyEvent(target, key, type, mask); 701 } 702 703 public void run() { 704 target.dispatchEvent(getEvent()); 705 } 706 } 707 708 710 private static KeyEvent createKeyEvent(Component target, int key, int type, int mask) { 711 KeyEvent result; 712 if (type != KeyEvent.KEY_TYPED) { 713 result = new KeyEvent (target, type, System.currentTimeMillis(), mask, 714 key, (char) key, KeyEvent.KEY_LOCATION_STANDARD); 715 } else { 716 result = new KeyEvent (target, type, System.currentTimeMillis(), mask, 717 KeyEvent.VK_UNDEFINED, (char) key); 718 } 719 return result; 720 } 721 722 public static class WaitFocus implements FocusListener { 723 724 public WaitFocus(Component c) throws Exception { 725 c.addFocusListener(this); 726 requestFocus(c); 727 if (!SwingUtilities.isEventDispatchThread()) { 728 synchronized (this) { 729 try { 730 wait(5000); 731 } catch (Exception e) { 732 e.printStackTrace(); 733 } 734 } 735 } 736 } 737 738 boolean gained = false; 739 public void focusGained(FocusEvent e) { 740 gained = true; 741 synchronized(this) { 742 notifyAll(); 743 } 744 } 745 746 public void focusLost(FocusEvent e) { 747 } 748 749 } 750 751 753 static class WaitWindow extends WindowAdapter { 754 boolean shown=false; 755 public WaitWindow(JFrame f) throws Exception { 756 f.addWindowListener(this); 757 f.show(); 758 f.toFront(); 759 f.requestFocus(); 760 761 if (f.isShowing()) { 762 return; 763 } 764 Runnable run = new Runnable () { 765 public void run() { 766 if (!shown) { 767 synchronized(WaitWindow.this) { 768 try { 769 wait(5000); 771 } catch (Exception e) {} 772 } 773 } 774 } 775 }; 776 maybeInvokeLater(run); 777 int ct = 0; 778 while (!f.isShowing()) { 779 ct++; 780 try { 781 Thread.currentThread().sleep(400); 782 } catch (Exception e) { 783 784 } 785 if (ct > 100) { 786 break; 787 } 788 } 789 ct=0; 790 Container c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow(); 791 while (c != f) { 792 try { 793 Thread.currentThread().sleep(400); 794 } catch (Exception e) { 795 796 } 797 c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow(); 798 ct++; 799 if (ct > 100) { 800 break; 801 } 802 } 803 } 804 805 public WaitWindow(JDialog f) throws Exception { 806 f.addWindowListener(this); 807 f.show(); 808 f.toFront(); 809 f.requestFocus(); 810 Runnable run = new Runnable () { 811 public void run() { 812 if (!shown) { 813 synchronized(this) { 814 try { 815 if (!SwingUtilities.isEventDispatchThread()) { 816 wait(5000); 817 } 818 } catch (Exception e) {} 819 } 820 } 821 } 822 }; 823 maybeInvokeLater(run); 824 if (!shown) { 825 synchronized(this) { 826 try { 827 wait(6000); 829 } catch (Exception e) {} 830 } 831 } 832 int ct = 0; 833 while (!f.isShowing()) { 834 ct++; 835 try { 836 Thread.currentThread().sleep(400); 837 } catch (Exception e) { 838 839 } 840 if (ct > 100) { 841 break; 842 } 843 } 844 ct=0; 845 Container c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow(); 846 while (c != f) { 847 try { 848 Thread.currentThread().sleep(400); 849 } catch (Exception e) { 850 851 } 852 c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow(); 853 ct++; 854 if (ct > 100) { 855 break; 856 } 857 } 858 } 859 860 public void windowOpened(WindowEvent e) { 861 shown = true; 862 synchronized(this) { 863 notifyAll(); 864 if (e.getSource() instanceof JFrame ) { 865 ((JFrame ) e.getSource()).removeWindowListener(this); 866 } else { 867 ((JDialog ) e.getSource()).removeWindowListener(this); 868 } 869 } 870 } 871 } 872 873 876 protected static void sleep() throws Exception { 877 if (isDebug()) { 878 try { 879 Thread.currentThread().sleep(400); 880 } catch (Exception e) { 881 e.printStackTrace(); 882 } 883 } 884 885 if (SwingUtilities.isEventDispatchThread()) { 886 try { 887 drainEventQueue(); 888 if (!isDebug()) { 889 Thread.currentThread().sleep(SLEEP_LENGTH); 890 } 891 } catch (InterruptedException ie) { 892 } 894 } else { 895 try { 896 SwingUtilities.invokeAndWait(new Runnable () { 897 public void run() { 898 System.currentTimeMillis(); 899 } 900 }); 901 SwingUtilities.invokeAndWait(new Runnable () { 902 public void run() { 903 System.currentTimeMillis(); 904 } 905 }); 906 } catch (Exception e) { 907 e.printStackTrace(); 908 } 909 } 910 } 911 912 private static void popEventQueue() throws Exception { 913 try { 914 Method m = EventQueue .class.getDeclaredMethod("pop", null); 915 m.setAccessible(true); 916 m.invoke(Toolkit.getDefaultToolkit().getSystemEventQueue(), null); 917 } catch (Exception e) { 918 919 } 920 } 921 922 private static void dispatchEvent(EventQueue queue, AWTEvent evt) throws Exception { 923 if (queue == null) { 924 queue = Toolkit.getDefaultToolkit().getSystemEventQueue(); 925 } 926 Method m = EventQueue .class.getDeclaredMethod("dispatchEvent", new Class [] {AWTEvent .class}); 927 m.setAccessible(true); 928 if (evt.getSource() instanceof JTextField ) { 929 foo = System.currentTimeMillis(); 930 } 931 m.invoke(queue, new Object [] {evt}); 932 } 933 static long foo = 0; 934 935 937 private static void drainEventQueue() throws Exception { 938 AWTEvent evt = EventQueue.getCurrentEvent(); 939 while (Toolkit.getDefaultToolkit().getSystemEventQueue().peekEvent() != null) { 942 EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue(); 944 evt = queue.getNextEvent(); 945 dispatchEvent(queue, evt); 946 } 947 } 948 949 } 950 | Popular Tags |