1 7 package java.awt; 8 9 import java.awt.event.FocusEvent ; 10 import java.awt.event.KeyEvent ; 11 import java.awt.event.WindowEvent ; 12 import java.awt.peer.ComponentPeer; 13 import java.awt.peer.LightweightPeer; 14 import java.beans.PropertyChangeListener ; 15 import java.util.LinkedList ; 16 import java.util.Iterator ; 17 import java.util.ListIterator ; 18 import java.util.Set ; 19 20 import java.util.logging.*; 21 22 import sun.awt.AppContext; 23 import sun.awt.SunToolkit; 24 25 45 public class DefaultKeyboardFocusManager extends KeyboardFocusManager { 46 private static final Logger focusLog = Logger.getLogger("java.awt.focus.DefaultKeyboardFocusManager"); 47 48 private Window realOppositeWindow; 49 private Component realOppositeComponent; 50 private int inSendMessage; 51 private LinkedList enqueuedKeyEvents = new LinkedList (), 52 typeAheadMarkers = new LinkedList (); 53 private boolean consumeNextKeyTyped; 54 55 private static class TypeAheadMarker { 56 long after; 57 Component untilFocused; 58 59 TypeAheadMarker(long after, Component untilFocused) { 60 this.after = after; 61 this.untilFocused = untilFocused; 62 } 63 66 public String toString() { 67 return ">>> Marker after " + after + " on " + untilFocused; 68 } 69 } 70 71 private Window getOwningFrameDialog(Window window) { 72 while (window != null && !(window instanceof Frame || 73 window instanceof Dialog )) { 74 window = (Window )window.getParent(); 75 } 76 return window; 77 } 78 79 84 private void restoreFocus(FocusEvent fe, Window newFocusedWindow) { 85 Component realOppositeComponent = this.realOppositeComponent; 86 Component vetoedComponent = fe.getComponent(); 87 if (newFocusedWindow != null && restoreFocus(newFocusedWindow, 88 vetoedComponent, false)) 89 { 90 } else if (realOppositeComponent != null && 91 restoreFocus(realOppositeComponent, false)) { 92 } else if (fe.getOppositeComponent() != null && 93 restoreFocus(fe.getOppositeComponent(), false)) { 94 } else { 95 clearGlobalFocusOwner(); 96 } 97 } 98 private void restoreFocus(WindowEvent we) { 99 Window realOppositeWindow = this.realOppositeWindow; 100 if (realOppositeWindow != null && restoreFocus(realOppositeWindow, 101 null, false)) { 102 } else if (we.getOppositeWindow() != null && 103 restoreFocus(we.getOppositeWindow(), null, false)) { 104 } else { 105 clearGlobalFocusOwner(); 106 } 107 } 108 private boolean restoreFocus(Window aWindow, Component vetoedComponent, 109 boolean clearOnFailure) { 110 Component toFocus = 111 KeyboardFocusManager.getMostRecentFocusOwner(aWindow); 112 if (toFocus != null && toFocus != vetoedComponent && restoreFocus(toFocus, false)) { 113 return true; 114 } else if (clearOnFailure) { 115 clearGlobalFocusOwner(); 116 return true; 117 } else { 118 return false; 119 } 120 } 121 private boolean restoreFocus(Component toFocus, boolean clearOnFailure) { 122 if (toFocus.isShowing() && toFocus.isFocusable() && 123 toFocus.requestFocus(false)) { 124 return true; 125 } else if (toFocus.nextFocusHelper()) { 126 return true; 127 } else if (clearOnFailure) { 128 clearGlobalFocusOwner(); 129 return true; 130 } else { 131 return false; 132 } 133 } 134 135 140 private static class DefaultKeyboardFocusManagerSentEvent 141 extends SentEvent 142 { 143 public DefaultKeyboardFocusManagerSentEvent(AWTEvent nested, 144 AppContext toNotify) { 145 super(nested, toNotify); 146 } 147 public final void dispatch() { 148 KeyboardFocusManager manager = 149 KeyboardFocusManager.getCurrentKeyboardFocusManager(); 150 DefaultKeyboardFocusManager defaultManager = 151 (manager instanceof DefaultKeyboardFocusManager ) 152 ? (DefaultKeyboardFocusManager )manager 153 : null; 154 155 if (defaultManager != null) { 156 synchronized (defaultManager) { 157 defaultManager.inSendMessage++; 158 } 159 } 160 161 super.dispatch(); 162 163 if (defaultManager != null) { 164 synchronized (defaultManager) { 165 defaultManager.inSendMessage--; 166 } 167 } 168 } 169 } 170 171 180 static boolean sendMessage(Component target, AWTEvent e) { 181 e.isPosted = true; 182 AppContext myAppContext = AppContext.getAppContext(); 183 final AppContext targetAppContext = target.appContext; 184 final SentEvent se = 185 new DefaultKeyboardFocusManagerSentEvent(e, myAppContext); 186 187 if (myAppContext == targetAppContext) { 188 se.dispatch(); 189 } else { 190 if (targetAppContext.isDisposed()) { 191 return false; 192 } 193 SunToolkit.postEvent(targetAppContext, se); 194 if (EventQueue.isDispatchThread()) { 195 EventDispatchThread edt = (EventDispatchThread ) 196 Thread.currentThread(); 197 edt.pumpEvents(SentEvent.ID, new Conditional () { 198 public boolean evaluate() { 199 return !se.dispatched && !targetAppContext.isDisposed(); 200 } 201 }); 202 } else { 203 synchronized (se) { 204 while (!se.dispatched && !targetAppContext.isDisposed()) { 205 try { 206 se.wait(1000); 207 } catch (InterruptedException ie) { 208 break; 209 } 210 } 211 } 212 } 213 } 214 return se.dispatched; 215 } 216 217 231 public boolean dispatchEvent(AWTEvent e) { 232 if (focusLog.isLoggable(Level.FINE) && (e instanceof WindowEvent || e instanceof FocusEvent )) focusLog.fine("" + e); 233 switch (e.getID()) { 234 case WindowEvent.WINDOW_GAINED_FOCUS: { 235 WindowEvent we = (WindowEvent )e; 236 Window oldFocusedWindow = getGlobalFocusedWindow(); 237 Window newFocusedWindow = we.getWindow(); 238 if (newFocusedWindow == oldFocusedWindow) { 239 break; 240 } 241 242 if (oldFocusedWindow != null) { 245 boolean isEventDispatched = 246 sendMessage(oldFocusedWindow, 247 new WindowEvent (oldFocusedWindow, 248 WindowEvent.WINDOW_LOST_FOCUS, 249 newFocusedWindow)); 250 if (!isEventDispatched) { 252 setGlobalFocusOwner(null); 253 setGlobalFocusedWindow(null); 254 } 255 } 256 257 Window newActiveWindow = 261 getOwningFrameDialog(newFocusedWindow); 262 Window currentActiveWindow = getGlobalActiveWindow(); 263 if (newActiveWindow != currentActiveWindow) { 264 sendMessage(newActiveWindow, 265 new WindowEvent (newActiveWindow, 266 WindowEvent.WINDOW_ACTIVATED, 267 currentActiveWindow)); 268 if (newActiveWindow != getGlobalActiveWindow()) { 269 restoreFocus(we); 272 break; 273 } 274 } 275 276 setGlobalFocusedWindow(newFocusedWindow); 277 278 if (newFocusedWindow != getGlobalFocusedWindow()) { 279 restoreFocus(we); 282 break; 283 } 284 285 setNativeFocusedWindow(newFocusedWindow); 286 if (inSendMessage == 0) { 294 306 307 Component toFocus = KeyboardFocusManager. 314 getMostRecentFocusOwner(newFocusedWindow); 315 if ((toFocus == null) && 316 newFocusedWindow.isFocusableWindow()) 317 { 318 toFocus = newFocusedWindow.getFocusTraversalPolicy(). 319 getInitialComponent(newFocusedWindow); 320 } 321 Component tempLost = null; 322 synchronized(KeyboardFocusManager .class) { 323 tempLost = newFocusedWindow.setTemporaryLostComponent(null); 324 } 325 326 if (focusLog.isLoggable(Level.FINER)) { 329 focusLog.log(Level.FINER, "tempLost {0}, toFocus {1}", 330 new Object []{tempLost, toFocus}); 331 } 332 setInActivation(true); 333 if (tempLost != null) { 334 tempLost.requestFocusInWindow(); 335 } 336 337 if (toFocus != null && toFocus != tempLost) { 338 toFocus.requestFocusInWindow(); 341 } 342 setInActivation(false); 343 } 344 345 Window realOppositeWindow = this.realOppositeWindow; 346 if (realOppositeWindow != we.getOppositeWindow()) { 347 we = new WindowEvent (newFocusedWindow, 348 WindowEvent.WINDOW_GAINED_FOCUS, 349 realOppositeWindow); 350 } 351 return typeAheadAssertions(newFocusedWindow, we); 352 } 353 354 case WindowEvent.WINDOW_ACTIVATED: { 355 WindowEvent we = (WindowEvent )e; 356 Window oldActiveWindow = getGlobalActiveWindow(); 357 Window newActiveWindow = we.getWindow(); 358 if (oldActiveWindow == newActiveWindow) { 359 break; 360 } 361 362 if (oldActiveWindow != null) { 365 boolean isEventDispatched = 366 sendMessage(oldActiveWindow, 367 new WindowEvent (oldActiveWindow, 368 WindowEvent.WINDOW_DEACTIVATED, 369 newActiveWindow)); 370 if (!isEventDispatched) { 372 setGlobalActiveWindow(null); 373 } 374 if (getGlobalActiveWindow() != null) { 375 break; 378 } 379 } 380 381 setGlobalActiveWindow(newActiveWindow); 382 383 if (newActiveWindow != getGlobalActiveWindow()) { 384 break; 387 } 388 389 return typeAheadAssertions(newActiveWindow, we); 390 } 391 392 case FocusEvent.FOCUS_GAINED: { 393 FocusEvent fe = (FocusEvent )e; 394 Component oldFocusOwner = getGlobalFocusOwner(); 395 Component newFocusOwner = fe.getComponent(); 396 if (oldFocusOwner == newFocusOwner) { 397 if (focusLog.isLoggable(Level.FINE)) focusLog.log(Level.FINE, "Skipping {0} because focus owner is the same", 398 new Object [] {e}); 399 dequeueKeyEvents(-1, newFocusOwner); 402 break; 403 } 404 405 if (oldFocusOwner != null) { 408 boolean isEventDispatched = 409 sendMessage(oldFocusOwner, 410 new FocusEvent (oldFocusOwner, 411 FocusEvent.FOCUS_LOST, 412 fe.isTemporary(), 413 newFocusOwner)); 414 if (!isEventDispatched) { 416 setGlobalFocusOwner(null); 417 if (!fe.isTemporary()) { 418 setGlobalPermanentFocusOwner(null); 419 } 420 } 421 } 422 423 Component newFocusedWindow = newFocusOwner; 429 while (newFocusedWindow != null && 430 !(newFocusedWindow instanceof Window )) { 431 newFocusedWindow = newFocusedWindow.parent; 432 } 433 Window currentFocusedWindow = getGlobalFocusedWindow(); 434 if (newFocusedWindow != null && 435 newFocusedWindow != currentFocusedWindow) 436 { 437 sendMessage(newFocusedWindow, 438 new WindowEvent ((Window )newFocusedWindow, 439 WindowEvent.WINDOW_GAINED_FOCUS, 440 currentFocusedWindow)); 441 if (newFocusedWindow != getGlobalFocusedWindow()) { 442 445 dequeueKeyEvents(-1, newFocusOwner); 449 break; 450 } 451 } 452 453 setGlobalFocusOwner(newFocusOwner); 454 455 if (newFocusOwner != getGlobalFocusOwner()) { 456 dequeueKeyEvents(-1, newFocusOwner); 459 if (! disableRestoreFocus ){ 460 restoreFocus(fe, (Window )newFocusedWindow); 461 } 462 break; 463 } 464 465 if (!fe.isTemporary()) { 466 setGlobalPermanentFocusOwner(newFocusOwner); 467 468 if (newFocusOwner != getGlobalPermanentFocusOwner()) { 469 dequeueKeyEvents(-1, newFocusOwner); 471 if (! disableRestoreFocus ){ 472 restoreFocus(fe, (Window )newFocusedWindow); 473 } 474 break; 475 } 476 } 477 478 setNativeFocusOwner(getHeavyweight(newFocusOwner)); 479 480 Component realOppositeComponent = this.realOppositeComponent; 481 if (realOppositeComponent != null && 482 realOppositeComponent != fe.getOppositeComponent()) { 483 fe = new FocusEvent (newFocusOwner, 484 FocusEvent.FOCUS_GAINED, 485 fe.isTemporary(), 486 realOppositeComponent); 487 ((AWTEvent ) fe).isPosted = true; 488 } 489 return typeAheadAssertions(newFocusOwner, fe); 490 } 491 492 case FocusEvent.FOCUS_LOST: { 493 FocusEvent fe = (FocusEvent )e; 494 Component currentFocusOwner = getGlobalFocusOwner(); 495 if (currentFocusOwner == null) { 496 if (focusLog.isLoggable(Level.FINE)) focusLog.log(Level.FINE, "Skipping {0} because focus owner is null", 497 new Object [] {e}); 498 break; 499 } 500 if (currentFocusOwner == fe.getOppositeComponent()) { 504 if (focusLog.isLoggable(Level.FINE)) focusLog.log(Level.FINE, "Skipping {0} because current focus owner is equal to opposite", 505 new Object [] {e}); 506 break; 507 } 508 509 setGlobalFocusOwner(null); 510 511 if (getGlobalFocusOwner() != null) { 512 restoreFocus(currentFocusOwner, true); 514 break; 515 } 516 517 if (!fe.isTemporary()) { 518 setGlobalPermanentFocusOwner(null); 519 520 if (getGlobalPermanentFocusOwner() != null) { 521 restoreFocus(currentFocusOwner, true); 523 break; 524 } 525 } else { 526 Window owningWindow = currentFocusOwner.getContainingWindow(); 527 if (owningWindow != null) { 528 owningWindow.setTemporaryLostComponent(currentFocusOwner); 529 } 530 } 531 532 setNativeFocusOwner(null); 533 534 fe.setSource(currentFocusOwner); 535 536 realOppositeComponent = (fe.getOppositeComponent() != null) 537 ? currentFocusOwner : null; 538 539 return typeAheadAssertions(currentFocusOwner, fe); 540 } 541 542 case WindowEvent.WINDOW_DEACTIVATED: { 543 WindowEvent we = (WindowEvent )e; 544 Window currentActiveWindow = getGlobalActiveWindow(); 545 if (currentActiveWindow == null) { 546 break; 547 } 548 549 if (currentActiveWindow != e.getSource()) { 550 break; 554 } 555 556 setGlobalActiveWindow(null); 557 if (getGlobalActiveWindow() != null) { 558 break; 560 } 561 562 we.setSource(currentActiveWindow); 563 return typeAheadAssertions(currentActiveWindow, we); 564 } 565 566 case WindowEvent.WINDOW_LOST_FOCUS: { 567 WindowEvent we = (WindowEvent )e; 568 Window currentFocusedWindow = getGlobalFocusedWindow(); 569 Window losingFocusWindow = we.getWindow(); 570 Window activeWindow = getGlobalActiveWindow(); 571 Window oppositeWindow = we.getOppositeWindow(); 572 if (focusLog.isLoggable(Level.FINE)) focusLog.log(Level.FINE, "Active {0}, Current focused {1}, losing focus {2} opposite {3}", 573 new Object [] {activeWindow, currentFocusedWindow, 574 losingFocusWindow, oppositeWindow}); 575 if (currentFocusedWindow == null) { 576 break; 577 } 578 579 if (inSendMessage == 0 && losingFocusWindow == activeWindow && 585 oppositeWindow == currentFocusedWindow) 586 { 587 break; 588 } 589 590 Component currentFocusOwner = getGlobalFocusOwner(); 591 if (currentFocusOwner != null) { 592 Component oppositeComp = null; 595 if (oppositeWindow != null) { 596 oppositeComp = oppositeWindow.getTemporaryLostComponent(); 597 if (oppositeComp == null) { 598 oppositeComp = oppositeWindow.getMostRecentFocusOwner(); 599 } 600 } 601 if (oppositeComp == null) { 602 oppositeComp = oppositeWindow; 603 } 604 sendMessage(currentFocusOwner, 605 new FocusEvent (currentFocusOwner, 606 FocusEvent.FOCUS_LOST, 607 true, 608 oppositeComp)); 609 } 610 611 setGlobalFocusedWindow(null); 612 if (getGlobalFocusedWindow() != null) { 613 restoreFocus(currentFocusedWindow, null, true); 615 break; 616 } 617 setNativeFocusedWindow(null); 618 619 we.setSource(currentFocusedWindow); 620 realOppositeWindow = (oppositeWindow != null) 621 ? currentFocusedWindow 622 : null; 623 typeAheadAssertions(currentFocusedWindow, we); 624 625 if (oppositeWindow == null) { 626 sendMessage(activeWindow, 630 new WindowEvent (activeWindow, 631 WindowEvent.WINDOW_DEACTIVATED, 632 null)); 633 if (getGlobalActiveWindow() != null) { 634 restoreFocus(currentFocusedWindow, null, true); 637 } 638 } 639 break; 640 } 641 642 case KeyEvent.KEY_TYPED: 643 case KeyEvent.KEY_PRESSED: 644 case KeyEvent.KEY_RELEASED: 645 return typeAheadAssertions(null, e); 646 647 default: 648 return false; 649 } 650 651 return true; 652 } 653 654 673 public boolean dispatchKeyEvent(KeyEvent e) { 674 Component focusOwner = (((AWTEvent )e).isPosted) ? getFocusOwner() : e.getComponent(); 675 676 if (focusOwner != null && focusOwner.isShowing() && 677 focusOwner.isFocusable() && focusOwner.isEnabled()) { 678 if (!e.isConsumed()) { 679 Component comp = e.getComponent(); 680 if (comp != null && comp.isEnabled()) { 681 redispatchEvent(comp, e); 682 } 683 } 684 } 685 boolean stopPostProcessing = false; 686 java.util.List processors = getKeyEventPostProcessors(); 687 if (processors != null) { 688 for (java.util.Iterator iter = processors.iterator(); 689 !stopPostProcessing && iter.hasNext(); ) 690 { 691 stopPostProcessing = (((KeyEventPostProcessor )(iter.next())). 692 postProcessKeyEvent(e)); 693 } 694 } 695 if (!stopPostProcessing) { 696 postProcessKeyEvent(e); 697 } 698 699 Component source = e.getComponent(); 701 ComponentPeer peer = source.getPeer(); 702 703 if (peer == null || peer instanceof LightweightPeer) { 704 Container target = source.getNativeContainer(); 707 if (target != null) { 708 peer = target.getPeer(); 709 } 710 } 711 if (peer != null) { 712 peer.handleEvent(e); 713 } 714 715 return true; 716 } 717 718 729 public boolean postProcessKeyEvent(KeyEvent e) { 730 if (!e.isConsumed()) { 731 Component target = e.getComponent(); 732 Container p = (Container ) 733 (target instanceof Container ? target : target.getParent()); 734 if (p != null) { 735 p.postProcessKeyEvent(e); 736 } 737 } 738 return true; 739 } 740 741 private void pumpApprovedKeyEvents() { 742 KeyEvent ke; 743 do { 744 ke = null; 745 synchronized (this) { 746 if (enqueuedKeyEvents.size() != 0) { 747 ke = (KeyEvent )enqueuedKeyEvents.getFirst(); 748 if (typeAheadMarkers.size() != 0) { 749 TypeAheadMarker marker = (TypeAheadMarker) 750 typeAheadMarkers.getFirst(); 751 if (ke.getWhen() > marker.after) { 752 ke = null; 753 } 754 } 755 if (ke != null) { 756 focusLog.log(Level.FINER, "Pumping approved event {0}", new Object [] {ke}); 757 enqueuedKeyEvents.removeFirst(); 758 } 759 } 760 } 761 if (ke != null) { 762 preDispatchKeyEvent(ke); 763 } 764 } while (ke != null); 765 } 766 767 770 void dumpMarkers() { 771 if (focusLog.isLoggable(Level.FINEST)) { 772 focusLog.log(Level.FINEST, ">>> Markers dump, time: {0}", System.currentTimeMillis()); 773 synchronized (this) { 774 if (typeAheadMarkers.size() != 0) { 775 Iterator iter = typeAheadMarkers.iterator(); 776 while (iter.hasNext()) { 777 TypeAheadMarker marker = (TypeAheadMarker)iter.next(); 778 focusLog.log(Level.FINEST, " {0}", marker); 779 } 780 } 781 } 782 } 783 } 784 785 private boolean typeAheadAssertions(Component target, AWTEvent e) { 786 787 pumpApprovedKeyEvents(); 791 792 switch (e.getID()) { 793 case KeyEvent.KEY_TYPED: 794 case KeyEvent.KEY_PRESSED: 795 case KeyEvent.KEY_RELEASED: { 796 KeyEvent ke = (KeyEvent )e; 797 synchronized (this) { 798 if (e.isPosted && typeAheadMarkers.size() != 0) { 799 TypeAheadMarker marker = (TypeAheadMarker) 800 typeAheadMarkers.getFirst(); 801 if (ke.getWhen() > marker.after) { 802 focusLog.log(Level.FINER, "Storing event {0} because of marker {1}", new Object [] {ke, marker}); 803 enqueuedKeyEvents.addLast(ke); 804 return true; 805 } 806 } 807 } 808 809 return preDispatchKeyEvent(ke); 811 } 812 813 case FocusEvent.FOCUS_GAINED: 814 focusLog.log(Level.FINEST, "Markers before FOCUS_GAINED on {0}", new Object [] {target}); 815 dumpMarkers(); 816 synchronized (this) { 827 boolean found = false; 828 if (hasMarker(target)) { 829 for (Iterator iter = typeAheadMarkers.iterator(); 830 iter.hasNext(); ) 831 { 832 if (((TypeAheadMarker)iter.next()).untilFocused == 833 target) 834 { 835 found = true; 836 } else if (found) { 837 break; 838 } 839 iter.remove(); 840 } 841 } else { 842 focusLog.log(Level.FINER, "Event without marker {0}", e); 844 } 845 } 846 focusLog.log(Level.FINEST, "Markers after FOCUS_GAINED"); 847 dumpMarkers(); 848 849 redispatchEvent(target, e); 850 851 pumpApprovedKeyEvents(); 855 return true; 856 857 default: 858 redispatchEvent(target, e); 859 return true; 860 } 861 } 862 863 868 private boolean hasMarker(Component comp) { 869 for (Iterator iter = typeAheadMarkers.iterator(); iter.hasNext(); ) { 870 if (((TypeAheadMarker)iter.next()).untilFocused == comp) { 871 return true; 872 } 873 } 874 return false; 875 } 876 877 881 void clearMarkers() { 882 synchronized(this) { 883 typeAheadMarkers.clear(); 884 } 885 } 886 887 private boolean preDispatchKeyEvent(KeyEvent ke) { 888 if (((AWTEvent ) ke).isPosted) { 889 Component focusOwner = getFocusOwner(); 890 ke.setSource(((focusOwner != null) ? focusOwner : getFocusedWindow())); 891 } 892 if (ke.getSource() == null) { 893 return true; 894 } 895 896 EventQueue.setCurrentEventAndMostRecentTime(ke); 902 903 910 if (KeyboardFocusManager.isProxyActive(ke)) { 911 Component source = (Component )ke.getSource(); 912 Container target = source.getNativeContainer(); 913 if (target != null) { 914 ComponentPeer peer = target.getPeer(); 915 if (peer != null) { 916 peer.handleEvent(ke); 917 920 ke.consume(); 921 } 922 } 923 return true; 924 } 925 926 java.util.List dispatchers = getKeyEventDispatchers(); 927 if (dispatchers != null) { 928 for (java.util.Iterator iter = dispatchers.iterator(); 929 iter.hasNext(); ) 930 { 931 if (((KeyEventDispatcher )(iter.next())). 932 dispatchKeyEvent(ke)) 933 { 934 return true; 935 } 936 } 937 } 938 return dispatchKeyEvent(ke); 939 } 940 941 private void consumeTraversalKey(KeyEvent e) { 942 e.consume(); 943 consumeNextKeyTyped = (e.getID() == KeyEvent.KEY_PRESSED) && 944 !e.isActionKey(); 945 } 946 947 950 private boolean consumeProcessedKeyEvent(KeyEvent e) { 951 if ((e.getID() == KeyEvent.KEY_TYPED) && consumeNextKeyTyped) { 952 e.consume(); 953 consumeNextKeyTyped = false; 954 return true; 955 } 956 return false; 957 } 958 959 972 public void processKeyEvent(Component focusedComponent, KeyEvent e) { 973 if (consumeProcessedKeyEvent(e)) { 975 return; 976 } 977 978 if (e.getID() == KeyEvent.KEY_TYPED) { 980 return; 981 } 982 983 if (focusedComponent.getFocusTraversalKeysEnabled() && 984 !e.isConsumed()) 985 { 986 AWTKeyStroke stroke = AWTKeyStroke.getAWTKeyStrokeForEvent(e), 987 oppStroke = AWTKeyStroke.getAWTKeyStroke(stroke.getKeyCode(), 988 stroke.getModifiers(), 989 !stroke.isOnKeyRelease()); 990 Set toTest; 991 boolean contains, containsOpp; 992 993 toTest = focusedComponent.getFocusTraversalKeys( 994 KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS); 995 contains = toTest.contains(stroke); 996 containsOpp = toTest.contains(oppStroke); 997 998 if (contains || containsOpp) { 999 consumeTraversalKey(e); 1000 if (contains) { 1001 focusNextComponent(focusedComponent); 1002 } 1003 return; 1004 } 1005 1006 toTest = focusedComponent.getFocusTraversalKeys( 1007 KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS); 1008 contains = toTest.contains(stroke); 1009 containsOpp = toTest.contains(oppStroke); 1010 1011 if (contains || containsOpp) { 1012 consumeTraversalKey(e); 1013 if (contains) { 1014 focusPreviousComponent(focusedComponent); 1015 } 1016 return; 1017 } 1018 1019 toTest = focusedComponent.getFocusTraversalKeys( 1020 KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS); 1021 contains = toTest.contains(stroke); 1022 containsOpp = toTest.contains(oppStroke); 1023 1024 if (contains || containsOpp) { 1025 consumeTraversalKey(e); 1026 if (contains) { 1027 upFocusCycle(focusedComponent); 1028 } 1029 return; 1030 } 1031 1032 if (!((focusedComponent instanceof Container ) && 1033 ((Container )focusedComponent).isFocusCycleRoot())) { 1034 return; 1035 } 1036 1037 toTest = focusedComponent.getFocusTraversalKeys( 1038 KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS); 1039 contains = toTest.contains(stroke); 1040 containsOpp = toTest.contains(oppStroke); 1041 1042 if (contains || containsOpp) { 1043 consumeTraversalKey(e); 1044 if (contains) { 1045 downFocusCycle((Container )focusedComponent); 1046 } 1047 } 1048 } 1049 } 1050 1051 1066 protected synchronized void enqueueKeyEvents(long after, 1067 Component untilFocused) { 1068 if (untilFocused == null) { 1069 return; 1070 } 1071 1072 focusLog.log(Level.FINER, "Enqueue at {0} for {1}", 1073 new Object [] {after, untilFocused}); 1074 1075 int insertionIndex = 0, 1076 i = typeAheadMarkers.size(); 1077 ListIterator iter = typeAheadMarkers.listIterator(i); 1078 1079 for (; i > 0; i--) { 1080 TypeAheadMarker marker = (TypeAheadMarker)iter.previous(); 1081 if (marker.after <= after) { 1082 insertionIndex = i; 1083 break; 1084 } 1085 } 1086 1087 typeAheadMarkers.add(insertionIndex, 1088 new TypeAheadMarker(after, untilFocused)); 1089 } 1090 1091 1106 protected synchronized void dequeueKeyEvents(long after, 1107 Component untilFocused) { 1108 if (untilFocused == null) { 1109 return; 1110 } 1111 1112 focusLog.log(Level.FINER, "Dequeue at {0} for {1}", 1113 new Object [] {after, untilFocused}); 1114 1115 TypeAheadMarker marker; 1116 ListIterator iter = typeAheadMarkers.listIterator 1117 ((after >= 0) ? typeAheadMarkers.size() : 0); 1118 1119 if (after < 0) { 1120 while (iter.hasNext()) { 1121 marker = (TypeAheadMarker)iter.next(); 1122 if (marker.untilFocused == untilFocused) 1123 { 1124 iter.remove(); 1125 return; 1126 } 1127 } 1128 } else { 1129 while (iter.hasPrevious()) { 1130 marker = (TypeAheadMarker)iter.previous(); 1131 if (marker.untilFocused == untilFocused && 1132 marker.after == after) 1133 { 1134 iter.remove(); 1135 return; 1136 } 1137 } 1138 } 1139 } 1140 1141 1151 protected synchronized void discardKeyEvents(Component comp) { 1152 if (comp == null) { 1153 return; 1154 } 1155 1156 long start = -1; 1157 1158 for (Iterator iter = typeAheadMarkers.iterator(); iter.hasNext(); ) { 1159 TypeAheadMarker marker = (TypeAheadMarker)iter.next(); 1160 Component toTest = marker.untilFocused; 1161 boolean match = (toTest == comp); 1162 while (!match && toTest != null && !(toTest instanceof Window )) { 1163 toTest = toTest.getParent(); 1164 match = (toTest == comp); 1165 } 1166 if (match) { 1167 if (start < 0) { 1168 start = marker.after; 1169 } 1170 iter.remove(); 1171 } else if (start >= 0) { 1172 purgeStampedEvents(start, marker.after); 1173 start = -1; 1174 } 1175 } 1176 1177 purgeStampedEvents(start, -1); 1178 } 1179 1180 private void purgeStampedEvents(long start, long end) { 1186 if (start < 0) { 1187 return; 1188 } 1189 1190 for (Iterator iter = enqueuedKeyEvents.iterator(); iter.hasNext(); ) { 1191 KeyEvent ke = (KeyEvent )iter.next(); 1192 long time = ke.getWhen(); 1193 1194 if (start < time && (end < 0 || time <= end)) { 1195 iter.remove(); 1196 } 1197 1198 if (end >= 0 && time > end) { 1199 break; 1200 } 1201 } 1202 } 1203 1204 1213 public void focusPreviousComponent(Component aComponent) { 1214 if (aComponent != null) { 1215 aComponent.transferFocusBackward(); 1216 } 1217 } 1218 1219 1228 public void focusNextComponent(Component aComponent) { 1229 if (aComponent != null) { 1230 aComponent.transferFocus(); 1231 } 1232 } 1233 1234 1246 public void upFocusCycle(Component aComponent) { 1247 if (aComponent != null) { 1248 aComponent.transferFocusUpCycle(); 1249 } 1250 } 1251 1252 1263 public void downFocusCycle(Container aContainer) { 1264 if (aContainer != null && aContainer.isFocusCycleRoot()) { 1265 aContainer.transferFocusDownCycle(); 1266 } 1267 } 1268} 1269 | Popular Tags |