| 1 7 package java.awt; 8 9 import java.awt.event.FocusEvent ; 10 import java.awt.event.InputEvent ; 11 import java.awt.event.KeyEvent ; 12 import java.awt.event.WindowEvent ; 13 14 import java.awt.peer.LightweightPeer; 15 import java.awt.peer.WindowPeer; 16 import java.beans.*; 17 import java.util.Set ; 18 import java.util.HashSet ; 19 import java.util.Collections ; 20 import java.util.Iterator ; 21 import java.util.LinkedList ; 22 import java.util.ListIterator ; 23 import java.util.StringTokenizer ; 24 import java.util.WeakHashMap ; 25 import java.lang.ref.WeakReference ; 26 import sun.awt.AppContext; 27 import sun.awt.DebugHelper; 28 import sun.awt.SunToolkit; 29 import sun.awt.HeadlessToolkit; 30 import java.util.logging.*; 31 import java.awt.peer.KeyboardFocusManagerPeer; 32 import java.lang.reflect.*; 33 import java.security.AccessController ; 34 import java.security.PrivilegedAction ; 35 36 80 public abstract class KeyboardFocusManager 81 implements KeyEventDispatcher , KeyEventPostProcessor 82 { 83 84 private static final Logger focusLog = Logger.getLogger("java.awt.focus.KeyboardFocusManager"); 86 87 static { 88 89 Toolkit.loadLibraries(); 90 if (!GraphicsEnvironment.isHeadless()) { 91 initIDs(); 92 } 93 } 94 95 transient KeyboardFocusManagerPeer peer; 96 97 100 private static native void initIDs(); 101 102 private static final DebugHelper dbg = 103 DebugHelper.create(KeyboardFocusManager .class); 104 105 113 public static final int FORWARD_TRAVERSAL_KEYS = 0; 114 115 123 public static final int BACKWARD_TRAVERSAL_KEYS = 1; 124 125 133 public static final int UP_CYCLE_TRAVERSAL_KEYS = 2; 134 135 143 public static final int DOWN_CYCLE_TRAVERSAL_KEYS = 3; 144 145 static final int TRAVERSAL_KEY_LENGTH = DOWN_CYCLE_TRAVERSAL_KEYS + 1; 146 147 private transient boolean inActivation; 148 149 153 synchronized boolean isInActivation() { 154 return inActivation; 155 } 156 157 synchronized void setInActivation(boolean inActivation) { 158 this.inActivation = inActivation; 159 } 160 161 168 public static KeyboardFocusManager getCurrentKeyboardFocusManager() { 169 return getCurrentKeyboardFocusManager(AppContext.getAppContext()); 170 } 171 172 synchronized static KeyboardFocusManager  173 getCurrentKeyboardFocusManager(AppContext appcontext) 174 { 175 KeyboardFocusManager manager = (KeyboardFocusManager ) 176 appcontext.get(KeyboardFocusManager .class); 177 if (manager == null) { 178 manager = new DefaultKeyboardFocusManager (); 179 appcontext.put(KeyboardFocusManager .class, manager); 180 } 181 return manager; 182 } 183 184 201 public static void setCurrentKeyboardFocusManager( 202 KeyboardFocusManager newManager) throws SecurityException  203 { 204 SecurityManager security = System.getSecurityManager(); 205 if (security != null) { 206 if (replaceKeyboardFocusManagerPermission == null) { 207 replaceKeyboardFocusManagerPermission = 208 new AWTPermission ("replaceKeyboardFocusManager"); 209 } 210 security. 211 checkPermission(replaceKeyboardFocusManagerPermission); 212 } 213 214 KeyboardFocusManager oldManager = null; 215 216 synchronized (KeyboardFocusManager .class) { 217 AppContext appcontext = AppContext.getAppContext(); 218 219 if (newManager != null) { 220 oldManager = getCurrentKeyboardFocusManager(appcontext); 221 222 appcontext.put(KeyboardFocusManager .class, newManager); 223 } else { 224 oldManager = getCurrentKeyboardFocusManager(appcontext); 225 appcontext.remove(KeyboardFocusManager .class); 226 } 227 } 228 229 if (oldManager != null) { 230 oldManager.firePropertyChange("managingFocus", 231 Boolean.TRUE, 232 Boolean.FALSE); 233 } 234 if (newManager != null) { 235 newManager.firePropertyChange("managingFocus", 236 Boolean.FALSE, 237 Boolean.TRUE); 238 } 239 } 240 241 245 private static Component focusOwner; 246 247 252 private static Component permanentFocusOwner; 253 254 257 private static Window focusedWindow; 258 259 266 private static Window activeWindow; 267 268 275 private FocusTraversalPolicy defaultPolicy = 276 new DefaultFocusTraversalPolicy (); 277 278 281 private static final String [] defaultFocusTraversalKeyPropertyNames = { 282 "forwardDefaultFocusTraversalKeys", 283 "backwardDefaultFocusTraversalKeys", 284 "upCycleDefaultFocusTraversalKeys", 285 "downCycleDefaultFocusTraversalKeys" 286 }; 287 288 291 private static final AWTKeyStroke [][] defaultFocusTraversalKeyStrokes = { 292 { 293 AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, 0, false), 294 AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK, false), 295 }, 296 { 297 AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK, false), 298 AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, 299 InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK | InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK, 300 false), 301 }, 302 {}, 303 {}, 304 }; 305 312 private Set [] defaultFocusTraversalKeys = new Set [4]; 313 314 321 private static Container currentFocusCycleRoot; 322 323 326 private VetoableChangeSupport vetoableSupport; 327 328 331 private PropertyChangeSupport changeSupport; 332 333 340 private java.util.LinkedList keyEventDispatchers; 341 342 349 private java.util.LinkedList keyEventPostProcessors; 350 351 354 private static java.util.Map mostRecentFocusOwners = new WeakHashMap (); 355 356 359 private static final String notPrivileged = "this KeyboardFocusManager is not installed in the current thread's context"; 360 361 365 private static AWTPermission replaceKeyboardFocusManagerPermission; 366 367 370 transient SequencedEvent currentSequencedEvent = null; 371 372 final void setCurrentSequencedEvent(SequencedEvent current) { 373 synchronized (SequencedEvent .class) { 374 assert(current == null || currentSequencedEvent == null); 375 currentSequencedEvent = current; 376 } 377 } 378 379 final SequencedEvent getCurrentSequencedEvent() { 380 synchronized (SequencedEvent .class) { 381 return currentSequencedEvent; 382 } 383 } 384 385 static Set initFocusTraversalKeysSet(String value, Set targetSet) { 386 StringTokenizer tokens = new StringTokenizer (value, ","); 387 while (tokens.hasMoreTokens()) { 388 targetSet.add(AWTKeyStroke.getAWTKeyStroke(tokens.nextToken())); 389 } 390 return (targetSet.isEmpty()) 391 ? Collections.EMPTY_SET 392 : Collections.unmodifiableSet(targetSet); 393 } 394 395 398 public KeyboardFocusManager() { 399 for (int i = 0; i < TRAVERSAL_KEY_LENGTH; i++) { 400 Set work_set = new HashSet (); 401 for (int j = 0; j < defaultFocusTraversalKeyStrokes[i].length; j++) { 402 work_set.add(defaultFocusTraversalKeyStrokes[i][j]); 403 } 404 defaultFocusTraversalKeys[i] = (work_set.isEmpty()) 405 ? Collections.EMPTY_SET 406 : Collections.unmodifiableSet(work_set); 407 } 408 initPeer(); 409 } 410 411 private void initPeer() { 412 if (Toolkit.getDefaultToolkit() instanceof HeadlessToolkit){ 413 peer = ((HeadlessToolkit)Toolkit.getDefaultToolkit()).createKeyboardFocusManagerPeer(this); 414 } 415 if (Toolkit.getDefaultToolkit() instanceof SunToolkit){ 416 peer = ((SunToolkit)Toolkit.getDefaultToolkit()).createKeyboardFocusManagerPeer(this); 417 } 418 } 419 420 434 public Component getFocusOwner() { 435 synchronized (KeyboardFocusManager .class) { 436 if (focusOwner == null) { 437 return null; 438 } 439 440 return (focusOwner.appContext == AppContext.getAppContext()) 441 ? focusOwner 442 : null; 443 } 444 } 445 446 465 protected Component getGlobalFocusOwner() throws SecurityException { 466 synchronized (KeyboardFocusManager .class) { 467 if (this == getCurrentKeyboardFocusManager()) { 468 return focusOwner; 469 } else { 470 if (focusLog.isLoggable(Level.FINE)) focusLog.fine("This manager is " + this + ", current is " + getCurrentKeyboardFocusManager()); 471 throw new SecurityException (notPrivileged); 472 } 473 } 474 } 475 476 500 protected void setGlobalFocusOwner(Component focusOwner) { 501 Component oldFocusOwner = null; 502 boolean shouldFire = false; 503 504 if (focusOwner == null || focusOwner.isFocusable()) { 505 synchronized (KeyboardFocusManager .class) { 506 oldFocusOwner = getFocusOwner(); 507 508 try { 509 fireVetoableChange("focusOwner", oldFocusOwner, 510 focusOwner); 511 } catch (PropertyVetoException e) { 512 return; 514 } 515 516 KeyboardFocusManager.focusOwner = focusOwner; 517 518 if (focusOwner != null && 519 (getCurrentFocusCycleRoot() == null || 520 !focusOwner.isFocusCycleRoot(getCurrentFocusCycleRoot()))) 521 { 522 Container rootAncestor = 523 focusOwner.getFocusCycleRootAncestor(); 524 if (rootAncestor == null && (focusOwner instanceof Window )) 525 { 526 rootAncestor = (Container )focusOwner; 527 } 528 if (rootAncestor != null) { 529 setGlobalCurrentFocusCycleRoot(rootAncestor); 530 } 531 } 532 533 shouldFire = true; 534 } 535 } 536 537 if (shouldFire) { 538 firePropertyChange("focusOwner", oldFocusOwner, focusOwner); 539 } 540 } 541 542 554 public void clearGlobalFocusOwner() { 555 if (!GraphicsEnvironment.isHeadless()) { 556 Toolkit.getDefaultToolkit(); 559 560 _clearGlobalFocusOwner(); 561 } 562 } 563 private void _clearGlobalFocusOwner() { 564 Window activeWindow = markClearGlobalFocusOwner(); 565 peer.clearGlobalFocusOwner(activeWindow); 566 } 567 568 Component getNativeFocusOwner() { 569 return peer.getCurrentFocusOwner(); 570 } 571 572 void setNativeFocusOwner(Component comp) { 573 focusLog.log(Level.FINEST, "Calling peer {0} setCurrentFocusOwner for {1}", 574 new Object [] {peer, comp}); 575 peer.setCurrentFocusOwner(comp); 576 } 577 578 Window getNativeFocusedWindow() { 579 return peer.getCurrentFocusedWindow(); 580 } 581 582 void setNativeFocusedWindow(Window win) { 583 peer.setCurrentFocusedWindow(win); 584 } 585 586 600 public Component getPermanentFocusOwner() { 601 synchronized (KeyboardFocusManager .class) { 602 if (permanentFocusOwner == null) { 603 return null; 604 } 605 606 return (permanentFocusOwner.appContext == 607 AppContext.getAppContext()) 608 ? permanentFocusOwner 609 : null; 610 } 611 } 612 613 632 protected Component getGlobalPermanentFocusOwner() 633 throws SecurityException  634 { 635 synchronized (KeyboardFocusManager .class) { 636 if (this == getCurrentKeyboardFocusManager()) { 637 return permanentFocusOwner; 638 } else { 639 if (focusLog.isLoggable(Level.FINE)) focusLog.fine("This manager is " + this + ", current is " + getCurrentKeyboardFocusManager()); 640 throw new SecurityException (notPrivileged); 641 } 642 } 643 } 644 645 670 protected void setGlobalPermanentFocusOwner(Component permanentFocusOwner) 671 { 672 Component oldPermanentFocusOwner = null; 673 boolean shouldFire = false; 674 675 if (permanentFocusOwner == null || permanentFocusOwner.isFocusable()) { 676 synchronized (KeyboardFocusManager .class) { 677 oldPermanentFocusOwner = getPermanentFocusOwner(); 678 679 try { 680 fireVetoableChange("permanentFocusOwner", 681 oldPermanentFocusOwner, 682 permanentFocusOwner); 683 } catch (PropertyVetoException e) { 684 return; 686 } 687 688 KeyboardFocusManager.permanentFocusOwner = permanentFocusOwner; 689 690 KeyboardFocusManager. 691 setMostRecentFocusOwner(permanentFocusOwner); 692 693 shouldFire = true; 694 } 695 } 696 697 if (shouldFire) { 698 firePropertyChange("permanentFocusOwner", oldPermanentFocusOwner, 699 permanentFocusOwner); 700 } 701 } 702 703 713 public Window getFocusedWindow() { 714 synchronized (KeyboardFocusManager .class) { 715 if (focusedWindow == null) { 716 return null; 717 } 718 719 return (focusedWindow.appContext == AppContext.getAppContext()) 720 ? focusedWindow 721 : null; 722 } 723 } 724 725 740 protected Window getGlobalFocusedWindow() throws SecurityException { 741 synchronized (KeyboardFocusManager .class) { 742 if (this == getCurrentKeyboardFocusManager()) { 743 return focusedWindow; 744 } else { 745 if (focusLog.isLoggable(Level.FINE)) focusLog.fine("This manager is " + this + ", current is " + getCurrentKeyboardFocusManager()); 746 throw new SecurityException (notPrivileged); 747 } 748 } 749 } 750 751 772 protected void setGlobalFocusedWindow(Window focusedWindow) { 773 Window oldFocusedWindow = null; 774 boolean shouldFire = false; 775 776 if (focusedWindow == null || focusedWindow.isFocusableWindow()) { 777 synchronized (KeyboardFocusManager .class) { 778 oldFocusedWindow = getFocusedWindow(); 779 780 try { 781 fireVetoableChange("focusedWindow", oldFocusedWindow, 782 focusedWindow); 783 } catch (PropertyVetoException e) { 784 return; 786 } 787 788 KeyboardFocusManager.focusedWindow = focusedWindow; 789 shouldFire = true; 790 } 791 } 792 793 if (shouldFire) { 794 firePropertyChange("focusedWindow", oldFocusedWindow, 795 focusedWindow); 796 } 797 } 798 799 812 public Window getActiveWindow() { 813 synchronized (KeyboardFocusManager .class) { 814 if (activeWindow == null) { 815 return null; 816 } 817 818 return (activeWindow.appContext == AppContext.getAppContext()) 819 ? activeWindow 820 : null; 821 } 822 } 823 824 842 protected Window getGlobalActiveWindow() throws SecurityException { 843 synchronized (KeyboardFocusManager .class) { 844 if (this == getCurrentKeyboardFocusManager()) { 845 return activeWindow; 846 } else { 847 if (focusLog.isLoggable(Level.FINE)) focusLog.fine("This manager is " + this + ", current is " + getCurrentKeyboardFocusManager()); 848 throw new SecurityException (notPrivileged); 849 } 850 } 851 } 852 853 875 protected void setGlobalActiveWindow(Window activeWindow) { 876 Window oldActiveWindow; 877 synchronized (KeyboardFocusManager .class) { 878 oldActiveWindow = getActiveWindow(); 879 if (focusLog.isLoggable(Level.FINER)) { 880 focusLog.finer("Setting global active window to " + activeWindow + ", old active " + oldActiveWindow); 881 } 882 883 try { 884 fireVetoableChange("activeWindow", oldActiveWindow, 885 activeWindow); 886 } catch (PropertyVetoException e) { 887 return; 889 } 890 891 KeyboardFocusManager.activeWindow = activeWindow; 892 } 893 894 firePropertyChange("activeWindow", oldActiveWindow, activeWindow); 895 } 896 897 |