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 907 public synchronized FocusTraversalPolicy getDefaultFocusTraversalPolicy() { 908 return defaultPolicy; 909 } 910 911 927 public void setDefaultFocusTraversalPolicy(FocusTraversalPolicy 928 defaultPolicy) { 929 if (defaultPolicy == null) { 930 throw new IllegalArgumentException ("default focus traversal policy cannot be null"); 931 } 932 933 FocusTraversalPolicy oldPolicy; 934 935 synchronized (this) { 936 oldPolicy = this.defaultPolicy; 937 this.defaultPolicy = defaultPolicy; 938 } 939 940 firePropertyChange("defaultFocusTraversalPolicy", oldPolicy, 941 defaultPolicy); 942 } 943 944 1031 public void 1032 setDefaultFocusTraversalKeys(int id, 1033 Set <? extends AWTKeyStroke > keystrokes) 1034 { 1035 if (id < 0 || id >= TRAVERSAL_KEY_LENGTH) { 1036 throw new IllegalArgumentException ("invalid focus traversal key identifier"); 1037 } 1038 if (keystrokes == null) { 1039 throw new IllegalArgumentException ("cannot set null Set of default focus traversal keys"); 1040 } 1041 1042 Set oldKeys; 1043 1044 synchronized (this) { 1045 for (Iterator iter = keystrokes.iterator(); iter.hasNext(); ) { 1046 Object obj = iter.next(); 1047 1048 if (obj == null) { 1049 throw new IllegalArgumentException ("cannot set null focus traversal key"); 1050 } 1051 1052 AWTKeyStroke keystroke = (AWTKeyStroke )obj; 1055 1056 if (keystroke.getKeyChar() != KeyEvent.CHAR_UNDEFINED) { 1057 throw new IllegalArgumentException ("focus traversal keys cannot map to KEY_TYPED events"); 1058 } 1059 1060 for (int i = 0; i < TRAVERSAL_KEY_LENGTH; i++) { 1063 if (i == id) { 1064 continue; 1065 } 1066 1067 if (defaultFocusTraversalKeys[i].contains(keystroke)) { 1068 throw new IllegalArgumentException ("focus traversal keys must be unique for a Component"); 1069 } 1070 } 1071 } 1072 1073 oldKeys = defaultFocusTraversalKeys[id]; 1074 defaultFocusTraversalKeys[id] = 1075 Collections.unmodifiableSet(new HashSet (keystrokes)); 1076 } 1077 1078 firePropertyChange(defaultFocusTraversalKeyPropertyNames[id], 1079 oldKeys, keystrokes); 1080 } 1081 1082 1108 public Set <AWTKeyStroke > getDefaultFocusTraversalKeys(int id) { 1109 if (id < 0 || id >= TRAVERSAL_KEY_LENGTH) { 1110 throw new IllegalArgumentException ("invalid focus traversal key identifier"); 1111 } 1112 1113 return defaultFocusTraversalKeys[id]; 1115 } 1116 1117 1133 public Container getCurrentFocusCycleRoot() { 1134 synchronized (KeyboardFocusManager .class) { 1135 if (currentFocusCycleRoot == null) { 1136 return null; 1137 } 1138 1139 return (currentFocusCycleRoot.appContext == 1140 AppContext.getAppContext()) 1141 ? currentFocusCycleRoot 1142 : null; 1143 } 1144 } 1145 1146 1165 protected Container getGlobalCurrentFocusCycleRoot() 1166 throws SecurityException 1167 { 1168 synchronized (KeyboardFocusManager .class) { 1169 if (this == getCurrentKeyboardFocusManager()) { 1170 return currentFocusCycleRoot; 1171 } else { 1172 if (focusLog.isLoggable(Level.FINE)) focusLog.fine("This manager is " + this + ", current is " + getCurrentKeyboardFocusManager()); 1173 throw new SecurityException (notPrivileged); 1174 } 1175 } 1176 } 1177 1178 1194 public void setGlobalCurrentFocusCycleRoot(Container newFocusCycleRoot) { 1195 Container oldFocusCycleRoot; 1196 1197 synchronized (KeyboardFocusManager .class) { 1198 oldFocusCycleRoot = getCurrentFocusCycleRoot(); 1199 currentFocusCycleRoot = newFocusCycleRoot; 1200 } 1201 1202 firePropertyChange("currentFocusCycleRoot", oldFocusCycleRoot, 1203 newFocusCycleRoot); 1204 } 1205 1206 1237 public void addPropertyChangeListener(PropertyChangeListener listener) { 1238 if (listener != null) { 1239 synchronized (this) { 1240 if (changeSupport == null) { 1241 changeSupport = new PropertyChangeSupport(this); 1242 } 1243 changeSupport.addPropertyChangeListener(listener); 1244 } 1245 } 1246 } 1247 1248 1260 public void removePropertyChangeListener(PropertyChangeListener listener) { 1261 if (listener != null) { 1262 synchronized (this) { 1263 if (changeSupport != null) { 1264 changeSupport.removePropertyChangeListener(listener); 1265 } 1266 } 1267 } 1268 } 1269 1270 1284 public synchronized PropertyChangeListener[] getPropertyChangeListeners() { 1285 if (changeSupport == null) { 1286 changeSupport = new PropertyChangeSupport(this); 1287 } 1288 return changeSupport.getPropertyChangeListeners(); 1289 } 1290 1291 1323 public void addPropertyChangeListener(String propertyName, 1324 PropertyChangeListener listener) { 1325 if (listener != null) { 1326 synchronized (this) { 1327 if (changeSupport == null) { 1328 changeSupport = new PropertyChangeSupport(this); 1329 } 1330 changeSupport.addPropertyChangeListener(propertyName, 1331 listener); 1332 } 1333 } 1334 } 1335 1336 1349 public void removePropertyChangeListener(String propertyName, 1350 PropertyChangeListener listener) { 1351 if (listener != null) { 1352 synchronized (this) { 1353 if (changeSupport != null) { 1354 changeSupport.removePropertyChangeListener(propertyName, 1355 listener); 1356 } 1357 } 1358 } 1359 } 1360 1361 1373 public synchronized PropertyChangeListener[] getPropertyChangeListeners(String propertyName) { 1374 if (changeSupport == null) { 1375 changeSupport = new PropertyChangeSupport(this); 1376 } 1377 return changeSupport.getPropertyChangeListeners(propertyName); 1378 } 1379 1380 1389 protected void firePropertyChange(String propertyName, Object oldValue, 1390 Object newValue) { 1391 PropertyChangeSupport changeSupport = this.changeSupport; 1392 if (changeSupport != null) { 1393 changeSupport.firePropertyChange(propertyName, oldValue, newValue); 1394 } 1395 } 1396 1397 1414 public void addVetoableChangeListener(VetoableChangeListener listener) { 1415 if (listener != null) { 1416 synchronized (this) { 1417 if (vetoableSupport == null) { 1418 vetoableSupport = 1419 new VetoableChangeSupport(this); 1420 } 1421 vetoableSupport.addVetoableChangeListener(listener); 1422 } 1423 } 1424 } 1425 1426 1438 public void removeVetoableChangeListener(VetoableChangeListener listener) { 1439 if (listener != null) { 1440 synchronized (this) { 1441 if (vetoableSupport != null) { 1442 vetoableSupport.removeVetoableChangeListener(listener); 1443 } 1444 } 1445 } 1446 } 1447 1448 1462 public synchronized VetoableChangeListener[] getVetoableChangeListeners() { 1463 if (vetoableSupport == null) { 1464 vetoableSupport = new VetoableChangeSupport(this); 1465 } 1466 return vetoableSupport.getVetoableChangeListeners(); 1467 } 1468 1469 1487 public void addVetoableChangeListener(String propertyName, 1488 VetoableChangeListener listener) { 1489 if (listener != null) { 1490 synchronized (this) { 1491 if (vetoableSupport == null) { 1492 vetoableSupport = 1493 new VetoableChangeSupport(this); 1494 } 1495 vetoableSupport.addVetoableChangeListener(propertyName, 1496 listener); 1497 } 1498 } 1499 } 1500 1501 1514 public void removeVetoableChangeListener(String propertyName, 1515 VetoableChangeListener listener) { 1516 if (listener != null) { 1517 synchronized (this) { 1518 if (vetoableSupport != null) { 1519 vetoableSupport.removeVetoableChangeListener(propertyName, 1520 listener); 1521 } 1522 } 1523 } 1524 } 1525 1526 1539 public synchronized VetoableChangeListener[] getVetoableChangeListeners(String propertyName) { 1540 if (vetoableSupport == null) { 1541 vetoableSupport = new VetoableChangeSupport(this); 1542 } 1543 return vetoableSupport.getVetoableChangeListeners(propertyName); 1544 } 1545 1546 1562 protected void fireVetoableChange(String propertyName, Object oldValue, 1563 Object newValue) 1564 throws PropertyVetoException 1565 { 1566 VetoableChangeSupport vetoableSupport = 1567 this.vetoableSupport; 1568 if (vetoableSupport != null) { 1569 vetoableSupport.fireVetoableChange(propertyName, oldValue, 1570 newValue); 1571 } 1572 } 1573 1574 1591 public void addKeyEventDispatcher(KeyEventDispatcher dispatcher) { 1592 if (dispatcher != null) { 1593 synchronized (this) { 1594 if (keyEventDispatchers == null) { 1595 keyEventDispatchers = new java.util.LinkedList (); 1596 } 1597 keyEventDispatchers.add(dispatcher); 1598 } 1599 } 1600 } 1601 1602 1617 public void removeKeyEventDispatcher(KeyEventDispatcher dispatcher) { 1618 if (dispatcher != null) { 1619 synchronized (this) { 1620 if (keyEventDispatchers != null) { 1621 keyEventDispatchers.remove(dispatcher); 1622 } 1623 } 1624 } 1625 } 1626 1627 1640 protected synchronized java.util.List <KeyEventDispatcher > 1641 getKeyEventDispatchers() 1642 { 1643 return (keyEventDispatchers != null) 1644 ? (java.util.List )keyEventDispatchers.clone() 1645 : null; 1646 } 1647 1648 1669 public void addKeyEventPostProcessor(KeyEventPostProcessor processor) { 1670 if (processor != null) { 1671 synchronized (this) { 1672 if (keyEventPostProcessors == null) { 1673 keyEventPostProcessors = new java.util.LinkedList (); 1674 } 1675 keyEventPostProcessors.add(processor); 1676 } 1677 } 1678 } 1679 1680 1681 1697 public void removeKeyEventPostProcessor(KeyEventPostProcessor processor) { 1698 if (processor != null) { 1699 synchronized (this) { 1700 if (keyEventPostProcessors != null) { 1701 keyEventPostProcessors.remove(processor); 1702 } 1703 } 1704 } 1705 } 1706 1707 1708 1721 protected java.util.List <KeyEventPostProcessor > 1722 getKeyEventPostProcessors() 1723 { 1724 return (keyEventPostProcessors != null) 1725 ? (java.util.List )keyEventPostProcessors.clone() 1726 : null; 1727 } 1728 1729 1730 1731 static void setMostRecentFocusOwner(Component component) { 1732 Component window = component; 1733 while (window != null && !(window instanceof Window )) { 1734 window = window.parent; 1735 } 1736 if (window != null) { 1737 setMostRecentFocusOwner((Window )window, component); 1738 } 1739 } 1740 static synchronized void setMostRecentFocusOwner(Window window, 1741 Component component) { 1742 WeakReference weakValue = null; 1747 if (component != null) { 1748 weakValue = new WeakReference (component); 1749 } 1750 mostRecentFocusOwners.put(window, weakValue); 1751 } 1752 static void clearMostRecentFocusOwner(Component comp) { 1753 Container window; 1754 1755 if (comp == null) { 1756 return; 1757 } 1758 1759 synchronized (comp.getTreeLock()) { 1760 window = comp.getParent(); 1761 while (window != null && !(window instanceof Window )) { 1762 window = window.getParent(); 1763 } 1764 } 1765 1766 synchronized (KeyboardFocusManager .class) { 1767 if ((window != null) 1768 && (getMostRecentFocusOwner((Window )window) == comp)) 1769 { 1770 setMostRecentFocusOwner((Window )window, null); 1771 } 1772 if (window != null) { 1774 Window realWindow = (Window )window; 1775 if (realWindow.getTemporaryLostComponent() == comp) { 1776 realWindow.setTemporaryLostComponent(null); 1777 } 1778 } 1779 } 1780 } 1781 static synchronized Component getMostRecentFocusOwner(Window window) { 1782 WeakReference weakValue = 1783 (WeakReference )mostRecentFocusOwners.get(window); 1784 return weakValue == null ? null : (Component )weakValue.get(); 1785 } 1786 1787 1806 public abstract boolean dispatchEvent(AWTEvent e); 1807 1808 1824 public final void redispatchEvent(Component target, AWTEvent e) { 1825 e.focusManagerIsDispatching = true; 1826 target.dispatchEvent(e); 1827 e.focusManagerIsDispatching = false; 1828 } 1829 1830 1845 public abstract boolean dispatchKeyEvent(KeyEvent e); 1846 1847 1859 public abstract boolean postProcessKeyEvent(KeyEvent e); 1860 1861 1874 public abstract void processKeyEvent(Component focusedComponent, 1875 KeyEvent e); 1876 1877 1897 protected abstract void enqueueKeyEvents(long after, 1898 Component untilFocused); 1899 1900 1916 protected abstract void dequeueKeyEvents(long after, 1917 Component untilFocused); 1918 1919 1930 protected abstract void discardKeyEvents(Component comp); 1931 1932 1940 public abstract void focusNextComponent(Component aComponent); 1941 1942 1950 public abstract void focusPreviousComponent(Component aComponent); 1951 1952 1963 public abstract void upFocusCycle(Component aComponent); 1964 1965 1975 public abstract void downFocusCycle(Container aContainer); 1976 1977 1980 public final void focusNextComponent() { 1981 Component focusOwner = getFocusOwner(); 1982 if (focusOwner != null) { 1983 focusNextComponent(focusOwner); 1984 } 1985 } 1986 1987 1990 public final void focusPreviousComponent() { 1991 Component focusOwner = getFocusOwner(); 1992 if (focusOwner != null) { 1993 focusPreviousComponent(focusOwner); 1994 } 1995 } 1996 1997 2006 public final void upFocusCycle() { 2007 Component focusOwner = getFocusOwner(); 2008 if (focusOwner != null) { 2009 upFocusCycle(focusOwner); 2010 } 2011 } 2012 2013 2022 public final void downFocusCycle() { 2023 Component focusOwner = getFocusOwner(); 2024 if (focusOwner instanceof Container ) { 2025 downFocusCycle((Container )focusOwner); 2026 } 2027 } 2028 2029 2032 void dumpRequests() { 2033 System.err.println(">>> Requests dump, time: " + System.currentTimeMillis()); 2034 synchronized (heavyweightRequests) { 2035 Iterator iter = heavyweightRequests.iterator(); 2036 while (iter.hasNext()) { 2037 HeavyweightFocusRequest req = (HeavyweightFocusRequest)iter.next(); 2038 System.err.println(">>> Req: " + req); 2039 } 2040 } 2041 System.err.println(""); 2042 } 2043 2044 private static final class LightweightFocusRequest { 2045 final Component component; 2046 final boolean temporary; 2047 2048 LightweightFocusRequest(Component component, boolean temporary) { 2049 this.component = component; 2050 this.temporary = temporary; 2051 } 2052 public String toString() { 2053 return "LightweightFocusRequest[component=" + component + 2054 ",temporary=" + temporary + "]"; 2055 } 2056 } 2057 2058 private static final class HeavyweightFocusRequest { 2059 final Component heavyweight; 2060 final LinkedList lightweightRequests; 2061 2062 static final HeavyweightFocusRequest CLEAR_GLOBAL_FOCUS_OWNER = 2063 new HeavyweightFocusRequest(); 2064 2065 private HeavyweightFocusRequest() { 2066 heavyweight = null; 2067 lightweightRequests = null; 2068 } 2069 2070 HeavyweightFocusRequest(Component heavyweight, Component descendant, 2071 boolean temporary) { 2072 if (dbg.on) { 2073 dbg.assertion(heavyweight != null); 2074 } 2075 2076 this.heavyweight = heavyweight; 2077 this.lightweightRequests = new LinkedList (); 2078 addLightweightRequest(descendant, temporary); 2079 } 2080 boolean addLightweightRequest(Component descendant, 2081 boolean temporary) { 2082 if (dbg.on) { 2083 dbg.assertion(this != HeavyweightFocusRequest. 2084 CLEAR_GLOBAL_FOCUS_OWNER); 2085 dbg.assertion(descendant != null); 2086 } 2087 2088 Component lastDescendant = ((lightweightRequests.size() > 0) 2089 ? ((LightweightFocusRequest)lightweightRequests.getLast()). 2090 component 2091 : null); 2092 2093 if (descendant != lastDescendant) { 2094 lightweightRequests.add 2096 (new LightweightFocusRequest(descendant, temporary)); 2097 return true; 2098 } else { 2099 return false; 2100 } 2101 } 2102 2103 LightweightFocusRequest getFirstLightweightRequest() { 2104 if (this == CLEAR_GLOBAL_FOCUS_OWNER) { 2105 return null; 2106 } 2107 return (LightweightFocusRequest)lightweightRequests.getFirst(); 2108 } 2109 public String toString() { 2110 boolean first = true; 2111 String str = "HeavyweightFocusRequest[heavweight=" + heavyweight + 2112 ",lightweightRequests="; 2113 if (lightweightRequests == null) { 2114 str += null; 2115 } else { 2116 str += "["; 2117 for (Iterator iter = lightweightRequests.iterator(); 2118 iter.hasNext(); ) 2119 { 2120 if (first) { 2121 first = false; 2122 } else { 2123 str += ","; 2124 } 2125 str += iter.next(); 2126 } 2127 str += "]"; 2128 } 2129 str += "]"; 2130 return str; 2131 } 2132 } 2133 2134 2139 private static LinkedList heavyweightRequests = new LinkedList (); 2140 private static LinkedList currentLightweightRequests; 2141 private static boolean clearingCurrentLightweightRequests; 2142 private static boolean allowSyncFocusRequests = true; 2143 private static Component newFocusOwner = null; 2144 2145 static volatile boolean disableRestoreFocus; 2146 2147 static final int SNFH_FAILURE = 0; 2148 static final int SNFH_SUCCESS_HANDLED = 1; 2149 static final int SNFH_SUCCESS_PROCEED = 2; 2150 2151 static boolean processSynchronousLightweightTransfer(Component heavyweight, Component descendant, 2152 boolean temporary, boolean focusedWindowChangeAllowed, 2153 long time) { 2154 Window parentWindow = Component.getContainingWindow(heavyweight); 2155 if (parentWindow == null || !parentWindow.syncLWRequests) { 2156 return false; 2157 } 2158 if (descendant == null) { 2159 descendant = heavyweight; 2163 } 2164 2165 KeyboardFocusManager manager = getCurrentKeyboardFocusManager(SunToolkit.targetToAppContext(descendant)); 2166 2167 FocusEvent currentFocusOwnerEvent = null; 2168 FocusEvent newFocusOwnerEvent = null; 2169 Component currentFocusOwner = manager.getGlobalFocusOwner(); 2170 2171 synchronized (heavyweightRequests) { 2172 HeavyweightFocusRequest hwFocusRequest = (HeavyweightFocusRequest) 2173 ((heavyweightRequests.size() > 0) 2174 ? heavyweightRequests.getLast() : null); 2175 if (hwFocusRequest == null && 2176 heavyweight == manager.getNativeFocusOwner() && 2177 allowSyncFocusRequests) 2178 { 2179 if (descendant == currentFocusOwner) { 2180 return true; 2182 } 2183 2184 manager.enqueueKeyEvents(time, descendant); 2189 2190 hwFocusRequest = 2191 new HeavyweightFocusRequest(heavyweight, descendant, 2192 temporary); 2193 heavyweightRequests.add(hwFocusRequest); 2194 2195 if (currentFocusOwner != null) { 2196 currentFocusOwnerEvent = 2197 new FocusEvent (currentFocusOwner, 2198 FocusEvent.FOCUS_LOST, 2199 temporary, descendant); 2200 } 2201 newFocusOwnerEvent = 2202 new FocusEvent (descendant, FocusEvent.FOCUS_GAINED, 2203 temporary, currentFocusOwner); 2204 } 2205 } 2206 boolean result = false; 2207 final boolean clearing = clearingCurrentLightweightRequests; 2208 2209 Throwable caughtEx = null; 2210 try { 2211 clearingCurrentLightweightRequests = false; 2212 synchronized(Component.LOCK) { 2213 if (currentFocusOwnerEvent != null && currentFocusOwner != null) { 2214 ((AWTEvent ) currentFocusOwnerEvent).isPosted = true; 2215 caughtEx=dispatchAndCatchException(caughtEx, currentFocusOwner, currentFocusOwnerEvent); 2216 result = true; 2217 } 2218 if (newFocusOwnerEvent != null && descendant != null) { 2219 ((AWTEvent ) newFocusOwnerEvent).isPosted = true; 2220 caughtEx=dispatchAndCatchException(caughtEx, descendant, newFocusOwnerEvent); 2221 result = true; 2222 } 2223 } 2224 } finally { 2225 clearingCurrentLightweightRequests = clearing; 2226 } 2227 2228 if (caughtEx instanceof RuntimeException ){ 2229 throw (RuntimeException ) caughtEx; 2230 } else if (caughtEx instanceof Error ) { 2231 throw (Error ) caughtEx; 2232 } 2233 2234 return result; 2235 } 2236 2237 2255 static int shouldNativelyFocusHeavyweight 2256 (Component heavyweight, Component descendant, boolean temporary, 2257 boolean focusedWindowChangeAllowed, long time) 2258 { 2259 if (dbg.on) { 2260 dbg.assertion(heavyweight != null); 2261 dbg.assertion(time != 0); 2262 } 2263 2264 if (descendant == null) { 2265 descendant = heavyweight; 2269 } 2270 2271 KeyboardFocusManager manager = 2272 getCurrentKeyboardFocusManager(SunToolkit.targetToAppContext(descendant)); 2273 KeyboardFocusManager thisManager = getCurrentKeyboardFocusManager(); 2274 Component currentFocusOwner = thisManager.getGlobalFocusOwner(); 2275 Component nativeFocusOwner = thisManager.getNativeFocusOwner(); 2276 Window nativeFocusedWindow = thisManager.getNativeFocusedWindow(); 2277 if (focusLog.isLoggable(Level.FINER)) { 2278 focusLog.log(Level.FINER, "SNFH for {0} in {1}", 2279 new Object [] {descendant, heavyweight}); 2280 } 2281 if (focusLog.isLoggable(Level.FINEST)) { 2282 focusLog.log(Level.FINEST, "0. Current focus owner {0}", 2283 currentFocusOwner); 2284 focusLog.log(Level.FINEST, "0. Native focus owner {0}", 2285 nativeFocusOwner); 2286 focusLog.log(Level.FINEST, "0. Native focused window {0}", 2287 nativeFocusedWindow); 2288 } 2289 synchronized (heavyweightRequests) { 2290 HeavyweightFocusRequest hwFocusRequest = (HeavyweightFocusRequest) 2291 ((heavyweightRequests.size() > 0) 2292 ? heavyweightRequests.getLast() : null); 2293 if (focusLog.isLoggable(Level.FINEST)) { 2294 focusLog.log(Level.FINEST, "Request {0}", hwFocusRequest); 2295 } 2296 if (hwFocusRequest == null && 2297 heavyweight == nativeFocusOwner) 2298 { 2299 if (descendant == currentFocusOwner) { 2300 if (focusLog.isLoggable(Level.FINEST)) { 2302 focusLog.log(Level.FINEST, "1. SNFH_FAILURE for {0}", 2303 descendant); 2304 } 2305 return SNFH_FAILURE; 2306 } 2307 2308 manager.enqueueKeyEvents(time, descendant); 2313 2314 hwFocusRequest = 2315 new HeavyweightFocusRequest(heavyweight, descendant, 2316 temporary); 2317 heavyweightRequests.add(hwFocusRequest); 2318 2319 if (currentFocusOwner != null) { 2320 FocusEvent currentFocusOwnerEvent = 2321 new FocusEvent (currentFocusOwner, 2322 FocusEvent.FOCUS_LOST, 2323 temporary, descendant); 2324 SunToolkit.postEvent(currentFocusOwner.appContext, 2325 currentFocusOwnerEvent); 2326 } 2327 FocusEvent newFocusOwnerEvent = 2328 new FocusEvent (descendant, FocusEvent.FOCUS_GAINED, 2329 temporary, currentFocusOwner); 2330 SunToolkit.postEvent(descendant.appContext, 2331 newFocusOwnerEvent); 2332 2333 if (focusLog.isLoggable(Level.FINEST)) { 2334 focusLog.log(Level.FINEST, "2. SNFH_HANDLED for {0}", descendant); 2335 } 2336 return SNFH_SUCCESS_HANDLED; 2337 } else if (hwFocusRequest != null && 2338 hwFocusRequest.heavyweight == heavyweight) { 2339 if (hwFocusRequest.addLightweightRequest(descendant, 2344 temporary)) { 2345 manager.enqueueKeyEvents(time, descendant); 2346 } 2347 2348 if (focusLog.isLoggable(Level.FINEST)) { 2349 focusLog.finest("3. SNFH_HANDLED for lightweight" + 2350 descendant + " in " + heavyweight); 2351 } 2352 return SNFH_SUCCESS_HANDLED; 2353 } else { 2354 if (!focusedWindowChangeAllowed) { 2355 if (hwFocusRequest == 2361 HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER) 2362 { 2363 int size = heavyweightRequests.size(); 2364 hwFocusRequest = (HeavyweightFocusRequest)((size >= 2) 2365 ? heavyweightRequests.get(size - 2) 2366 : null); 2367 } 2368 if (focusedWindowChanged(heavyweight, 2369 (hwFocusRequest != null) 2370 ? hwFocusRequest.heavyweight 2371 : nativeFocusedWindow)) { 2372 if (focusLog.isLoggable(Level.FINEST)) { 2373 focusLog.finest("4. SNFH_FAILURE for " + descendant); 2374 } 2375 return SNFH_FAILURE; 2376 } 2377 } 2378 2379 manager.enqueueKeyEvents(time, descendant); 2380 heavyweightRequests.add 2381 (new HeavyweightFocusRequest(heavyweight, descendant, 2382 temporary)); 2383 if (focusLog.isLoggable(Level.FINEST)) { 2384 focusLog.finest("5. SNFH_PROCEED for " + descendant); 2385 } 2386 return SNFH_SUCCESS_PROCEED; 2387 } 2388 } 2389 } 2390 static void heavyweightButtonDown(Component heavyweight, long time) { 2391 heavyweightButtonDown(heavyweight, time, false); 2392 } 2393 static void heavyweightButtonDown(Component heavyweight, long time, boolean acceptDuplicates) { 2394 if (dbg.on) { 2395 dbg.assertion(heavyweight != null); 2396 dbg.assertion(time != 0); 2397 } 2398 KeyboardFocusManager manager = getCurrentKeyboardFocusManager(SunToolkit.targetToAppContext(heavyweight)); 2399 2400 synchronized (heavyweightRequests) { 2401 HeavyweightFocusRequest hwFocusRequest = (HeavyweightFocusRequest) 2402 ((heavyweightRequests.size() > 0) 2403 ? heavyweightRequests.getLast() : null); 2404 Component currentNativeFocusOwner = (hwFocusRequest == null) 2405 ? manager.getNativeFocusOwner() 2406 : hwFocusRequest.heavyweight; 2407 2408 2451 if (acceptDuplicates || heavyweight != currentNativeFocusOwner) { 2452 getCurrentKeyboardFocusManager 2453 (SunToolkit.targetToAppContext(heavyweight)). 2454 enqueueKeyEvents(time, heavyweight); 2455 heavyweightRequests.add 2456 (new HeavyweightFocusRequest(heavyweight, heavyweight, 2457 false)); 2458 } 2459 } 2460 } 2461 2468 static Window markClearGlobalFocusOwner() { 2469 synchronized (heavyweightRequests) { 2470 HeavyweightFocusRequest hwFocusRequest = (HeavyweightFocusRequest) 2471 ((heavyweightRequests.size() > 0) 2472 ? heavyweightRequests.getLast() : null); 2473 if (hwFocusRequest == 2474 HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER) 2475 { 2476 return null; 2478 } 2479 2480 KeyboardFocusManager manager = getCurrentKeyboardFocusManager(); 2481 heavyweightRequests.add 2482 (HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER); 2483 2484 Component activeWindow = ((hwFocusRequest != null) 2485 ? Component.getContainingWindow(hwFocusRequest.heavyweight) 2486 : manager.getNativeFocusedWindow()); 2487 while (activeWindow != null && 2488 !((activeWindow instanceof Frame ) || 2489 (activeWindow instanceof Dialog ))) 2490 { 2491 activeWindow = activeWindow.getParent(); 2492 } 2493 2494 return (Window )activeWindow; 2495 } 2496 } 2497 Component getCurrentWaitingRequest(Component parent) { 2498 synchronized (heavyweightRequests) { 2499 HeavyweightFocusRequest hwFocusRequest = (HeavyweightFocusRequest) 2500 ((heavyweightRequests.size() > 0) 2501 ? heavyweightRequests.getFirst() : null); 2502 if (hwFocusRequest != null) { 2503 if (hwFocusRequest.heavyweight == parent) { 2504 LightweightFocusRequest lwFocusRequest = 2505 (LightweightFocusRequest)hwFocusRequest. 2506 lightweightRequests.getFirst(); 2507 if (lwFocusRequest != null) { 2508 return lwFocusRequest.component; 2509 } 2510 } 2511 } 2512 } 2513 return null; 2514 } 2515 2516 2522 static private Throwable dispatchAndCatchException(Throwable ex, Component comp, FocusEvent event) { 2523 Throwable retEx = null; 2524 try { 2525 comp.dispatchEvent(event); 2526 } catch (RuntimeException re) { 2527 retEx = re; 2528 } catch (Error er) { 2529 retEx = er; 2530 } 2531 if (retEx != null) { 2532 if (ex != null) { 2533 handleException(ex); 2534 } 2535 return retEx; 2536 } 2537 return ex; 2538 } 2539 2540 static private void handleException(Throwable ex) { 2541 ex.printStackTrace(); 2542 } 2543 2544 static boolean hasFocusRequests() { 2545 synchronized (heavyweightRequests) { 2546 return heavyweightRequests.size() > 0; 2547 } 2548 } 2549 2550 static void processCurrentLightweightRequests() { 2551 KeyboardFocusManager manager = getCurrentKeyboardFocusManager(); 2552 LinkedList localLightweightRequests = null; 2553 2554 Component globalFocusOwner = manager.getGlobalFocusOwner(); 2555 if ((globalFocusOwner != null) && 2556 (globalFocusOwner.appContext != AppContext.getAppContext())) 2557 { 2558 return; 2562 } 2563 2564 synchronized(heavyweightRequests) { 2565 if (currentLightweightRequests != null) { 2566 clearingCurrentLightweightRequests = true; 2567 disableRestoreFocus=true; 2568 localLightweightRequests = currentLightweightRequests; 2569 allowSyncFocusRequests = (localLightweightRequests.size() < 2); 2570 currentLightweightRequests = null; 2571 } else { 2572 return; 2574 } 2575 } 2576 2577 Throwable caughtEx=null; 2578 try { 2579 if (localLightweightRequests != null) { 2580 Component lastFocusOwner = null; 2581 Component currentFocusOwner = null; 2582 2583 for (Iterator iter = localLightweightRequests.iterator(); iter.hasNext(); ) 2584 { 2585 currentFocusOwner = manager.getGlobalFocusOwner(); 2586 LightweightFocusRequest lwFocusRequest = (LightweightFocusRequest)iter.next(); 2587 2588 2597 if (!iter.hasNext()) { 2598 disableRestoreFocus = false; 2599 } 2600 2601 FocusEvent currentFocusOwnerEvent = null; 2602 if ( currentFocusOwner != null ){ 2603 currentFocusOwnerEvent = new FocusEvent (currentFocusOwner, 2604 FocusEvent.FOCUS_LOST, 2605 lwFocusRequest.temporary, lwFocusRequest.component); 2606 } 2607 2608 FocusEvent newFocusOwnerEvent = new FocusEvent (lwFocusRequest.component, 2609 FocusEvent.FOCUS_GAINED, 2610 lwFocusRequest.temporary, 2611 currentFocusOwner == null? lastFocusOwner : currentFocusOwner); 2612 2613 if (currentFocusOwner != null){ 2614 ((AWTEvent ) currentFocusOwnerEvent).isPosted = true; 2615 caughtEx=dispatchAndCatchException(caughtEx,currentFocusOwner,currentFocusOwnerEvent); 2616 } 2617 2618 ((AWTEvent ) newFocusOwnerEvent).isPosted = true; 2619 caughtEx=dispatchAndCatchException(caughtEx, lwFocusRequest.component, newFocusOwnerEvent); 2620 2621 if (manager.getGlobalFocusOwner() == lwFocusRequest.component){ 2622 lastFocusOwner = lwFocusRequest.component; 2623 } 2624 } 2625 } 2626 } finally { 2627 clearingCurrentLightweightRequests = false; 2628 disableRestoreFocus=false; 2629 localLightweightRequests = null; 2630 allowSyncFocusRequests = true; 2631 } 2632 2633 if (caughtEx instanceof RuntimeException ) { 2634 throw (RuntimeException )caughtEx; 2635 } else if (caughtEx instanceof Error ) { 2636 throw (Error )caughtEx; 2637 } 2638 2639 } 2640 2641 static FocusEvent retargetUnexpectedFocusEvent(FocusEvent fe) { 2642 synchronized (heavyweightRequests) { 2643 2647 if (removeFirstRequest()) { 2648 return (FocusEvent )retargetFocusEvent(fe); 2649 } 2650 2651 Component source = fe.getComponent(); 2652 Component opposite = fe.getOppositeComponent(); 2653 boolean temporary = false; 2654 if (fe.getID() == FocusEvent.FOCUS_LOST && 2655 (opposite == null || isTemporary(opposite, source))) 2656 { 2657 temporary = true; 2658 } 2659 return new FocusEvent (source, fe.getID(), temporary, opposite); 2660 } 2661 } 2662 2663 static FocusEvent retargetFocusGained(FocusEvent fe) { 2664 assert (fe.getID() == FocusEvent.FOCUS_GAINED); 2665 2666 Component currentFocusOwner = getCurrentKeyboardFocusManager(). 2667 getGlobalFocusOwner(); 2668 Component source = fe.getComponent(); 2669 Component opposite = fe.getOppositeComponent(); 2670 Component nativeSource = getHeavyweight(source); 2671 2672 synchronized (heavyweightRequests) { 2673 HeavyweightFocusRequest hwFocusRequest = (HeavyweightFocusRequest) 2674 ((heavyweightRequests.size() > 0) 2675 ? heavyweightRequests.getFirst() : null); 2676 2677 if (hwFocusRequest == HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER) 2678 { 2679 return retargetUnexpectedFocusEvent(fe); 2680 } 2681 2682 if (source != null && nativeSource == null && hwFocusRequest != null) { 2683 if (source == hwFocusRequest.getFirstLightweightRequest().component) 2687 { 2688 source = hwFocusRequest.heavyweight; 2689 nativeSource = source; } 2691 } 2692 if (hwFocusRequest != null && 2693 nativeSource == hwFocusRequest.heavyweight) 2694 { 2695 2698 heavyweightRequests.removeFirst(); 2699 2700 LightweightFocusRequest lwFocusRequest = 2701 (LightweightFocusRequest)hwFocusRequest. 2702 lightweightRequests.removeFirst(); 2703 2704 Component newSource = lwFocusRequest.component; 2705 if (currentFocusOwner != null) { 2706 2717 newFocusOwner = newSource; 2718 } 2719 2720 boolean temporary = (opposite == null || 2721 isTemporary(newSource, opposite)) 2722 ? false 2723 : lwFocusRequest.temporary; 2724 2725 if (hwFocusRequest.lightweightRequests.size() > 0) { 2726 currentLightweightRequests = 2727 hwFocusRequest.lightweightRequests; 2728 EventQueue.invokeLater(new Runnable () { 2729 public void run() { 2730 processCurrentLightweightRequests(); 2731 } 2732 }); 2733 } 2734 2735 return new FocusEvent (newSource, 2738 FocusEvent.FOCUS_GAINED, temporary, 2739 opposite); 2740 } 2741 2742 if (currentFocusOwner != null 2743 && currentFocusOwner.getContainingWindow() == source 2744 && (hwFocusRequest == null || source != hwFocusRequest.heavyweight)) 2745 { 2746 return new FocusEvent (currentFocusOwner, FocusEvent.FOCUS_GAINED, false, null); 2751 } 2752 2753 return retargetUnexpectedFocusEvent(fe); 2754 } } 2756 2757 static FocusEvent retargetFocusLost(FocusEvent fe) { 2758 assert (fe.getID() == FocusEvent.FOCUS_LOST); 2759 2760 Component currentFocusOwner = getCurrentKeyboardFocusManager(). 2761 getGlobalFocusOwner(); 2762 Component opposite = fe.getOppositeComponent(); 2763 Component nativeOpposite = getHeavyweight(opposite); 2764 2765 synchronized (heavyweightRequests) { 2766 HeavyweightFocusRequest hwFocusRequest = (HeavyweightFocusRequest) 2767 ((heavyweightRequests.size() > 0) 2768 ? heavyweightRequests.getFirst() : null); 2769 2770 if (hwFocusRequest == HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER) 2771 { 2772 if (currentFocusOwner != null) { 2773 heavyweightRequests.removeFirst(); 2775 return new FocusEvent (currentFocusOwner, 2776 FocusEvent.FOCUS_LOST, false, null); 2777 } 2778 2779 2781 } else if (opposite == null) 2782 { 2783 if (currentFocusOwner != null) { 2785 return new FocusEvent (currentFocusOwner, 2786 FocusEvent.FOCUS_LOST, 2787 true, null); 2788 } else { 2789 return fe; 2790 } 2791 } else if (hwFocusRequest != null && 2792 (nativeOpposite == hwFocusRequest.heavyweight || 2793 nativeOpposite == null && 2794 opposite == hwFocusRequest.getFirstLightweightRequest().component)) 2795 { 2796 if (currentFocusOwner == null) { 2797 return fe; 2798 } 2799 2802 2807 LightweightFocusRequest lwFocusRequest = 2808 (LightweightFocusRequest)hwFocusRequest. 2809 lightweightRequests.getFirst(); 2810 2811 boolean temporary = isTemporary(opposite, 2812 currentFocusOwner) 2813 ? true 2814 : lwFocusRequest.temporary; 2815 2816 return new FocusEvent (currentFocusOwner, FocusEvent.FOCUS_LOST, 2817 temporary, lwFocusRequest.component); 2818 } else if (focusedWindowChanged(opposite, currentFocusOwner)) { 2819 if (!fe.isTemporary() && currentFocusOwner != null) { 2822 fe = new FocusEvent (currentFocusOwner, FocusEvent.FOCUS_LOST, 2824 true, opposite); 2825 } 2826 return fe; 2827 } 2828 2829 return retargetUnexpectedFocusEvent(fe); 2830 } } 2832 2833 static AWTEvent retargetFocusEvent(AWTEvent event) { 2834 if (clearingCurrentLightweightRequests) { 2835 return event; 2836 } 2837 2838 KeyboardFocusManager manager = getCurrentKeyboardFocusManager(); 2839 if (focusLog.isLoggable(Level.FINE)) { 2840 if (event instanceof FocusEvent || event instanceof WindowEvent ) { 2841 focusLog.log(Level.FINE, ">>> {0}", new Object [] {event}); 2842 } 2843 if (focusLog.isLoggable(Level.FINER) && event instanceof KeyEvent ) { 2844 focusLog.log(Level.FINER, " focus owner is {0}", new Object [] {manager.getGlobalFocusOwner()}); 2845 focusLog.log(Level.FINER, ">>> {0}", new Object [] {event}); 2846 } 2847 } 2848 2849 synchronized(heavyweightRequests) { 2850 2860 if (newFocusOwner != null && 2861 event.getID() == FocusEvent.FOCUS_LOST) 2862 { 2863 FocusEvent fe = (FocusEvent )event; 2864 2865 if (manager.getGlobalFocusOwner() == fe.getComponent() && 2866 fe.getOppositeComponent() == newFocusOwner) 2867 { 2868 newFocusOwner = null; 2869 return event; 2870 } 2871 } 2872 } 2873 2874 processCurrentLightweightRequests(); 2875 2876 switch (event.getID()) { 2877 case FocusEvent.FOCUS_GAINED: { 2878 event = retargetFocusGained((FocusEvent )event); 2879 break; 2880 } 2881 case FocusEvent.FOCUS_LOST: { 2882 event = retargetFocusLost((FocusEvent )event); 2883 break; 2884 } 2885 default: 2886 2887 } 2888 return event; 2889 } 2890 2891 2897 void clearMarkers() { 2898 } 2899 2900 static boolean removeFirstRequest() { 2901 KeyboardFocusManager manager = 2902 KeyboardFocusManager.getCurrentKeyboardFocusManager(); 2903 2904 synchronized(heavyweightRequests) { 2905 HeavyweightFocusRequest hwFocusRequest = (HeavyweightFocusRequest) 2906 2907 ((heavyweightRequests.size() > 0) 2908 ? heavyweightRequests.getFirst() : null); 2909 if (hwFocusRequest != null) { 2910 heavyweightRequests.removeFirst(); 2911 if (hwFocusRequest.lightweightRequests != null) { 2912 for (Iterator lwIter = hwFocusRequest.lightweightRequests. 2913 iterator(); 2914 lwIter.hasNext(); ) 2915 { 2916 manager.dequeueKeyEvents 2917 (-1, ((LightweightFocusRequest)lwIter.next()). 2918 component); 2919 } 2920 } 2921 } 2922 if (heavyweightRequests.size() == 0) { 2925 manager.clearMarkers(); 2926 } 2927 return (heavyweightRequests.size() > 0); 2928 } 2929 } 2930 static void removeLastFocusRequest(Component heavyweight) { 2931 if (dbg.on) { 2932 dbg.assertion(heavyweight != null); 2933 } 2934 2935 KeyboardFocusManager manager = 2936 KeyboardFocusManager.getCurrentKeyboardFocusManager(); 2937 synchronized(heavyweightRequests) { 2938 HeavyweightFocusRequest hwFocusRequest = (HeavyweightFocusRequest) 2939 ((heavyweightRequests.size() > 0) 2940 ? heavyweightRequests.getLast() : null); 2941 if (hwFocusRequest != null && 2942 hwFocusRequest.heavyweight == heavyweight) { 2943 heavyweightRequests.removeLast(); 2944 } 2945 if (heavyweightRequests.size() == 0) { 2948 manager.clearMarkers(); 2949 } 2950 } 2951 } 2952 2953 private static boolean focusedWindowChanged(Component to, Component from) { 2954 Window wto = Component.getContainingWindow(to); 2955 Window wfrom = Component.getContainingWindow(from); 2956 if (wto == null && wfrom == null) { 2957 return true; 2958 } 2959 if (wto == null) { 2960 return true; 2961 } 2962 if (wfrom == null) { 2963 return true; 2964 } 2965 return (wto != wfrom); 2966 } 2967 2968 private static boolean isTemporary(Component to, Component from) { 2969 Window wto = Component.getContainingWindow(to); 2970 Window wfrom = Component.getContainingWindow(from); 2971 if (wto == null && wfrom == null) { 2972 return false; 2973 } 2974 if (wto == null) { 2975 return true; 2976 } 2977 if (wfrom == null) { 2978 return false; 2979 } 2980 return (wto != wfrom); 2981 } 2982 2983 static Component getHeavyweight(Component comp) { 2984 if (comp == null || comp.getPeer() == null) { 2985 return null; 2986 } else if (comp.getPeer() instanceof LightweightPeer) { 2987 return comp.getNativeContainer(); 2988 } else { 2989 return comp; 2990 } 2991 } 2992 2993 static Field proxyActive; 2994 private static boolean isProxyActiveImpl(KeyEvent e) { 2996 if (proxyActive == null) { 2997 proxyActive = (Field) AccessController.doPrivileged(new PrivilegedAction () { 2998 public Object run() { 2999 Field field = null; 3000 try { 3001 field = KeyEvent .class.getDeclaredField("isProxyActive"); 3002 if (field != null) { 3003 field.setAccessible(true); 3004 } 3005 } catch (NoSuchFieldException nsf) { 3006 assert(false); 3007 } 3008 return field; 3009 } 3010 }); 3011 } 3012 3013 try { 3014 return proxyActive.getBoolean(e); 3015 } catch (IllegalAccessException iae) { 3016 assert(false); 3017 } 3018 return false; 3019 } 3020 3021 static boolean isProxyActive(KeyEvent e) { 3023 if (!GraphicsEnvironment.isHeadless()) { 3024 return isProxyActiveImpl(e); 3025 } else { 3026 return false; 3027 } 3028 } 3029} 3030 | Popular Tags |