src/share/classes/java/awt/KeyboardFocusManager.java

Print this page




 331         {
 332             AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, 0, false),
 333             AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK, false),
 334         },
 335         {
 336             AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK, false),
 337             AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB,
 338                                          InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK | InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK,
 339                                          false),
 340         },
 341         {},
 342         {},
 343       };
 344     /**
 345      * The default focus traversal keys. Each array of traversal keys will be
 346      * in effect on all Windows that have no such array of their own explicitly
 347      * set. Each array will also be inherited, recursively, by any child
 348      * Component of those Windows that has no such array of its own explicitly
 349      * set.
 350      */

 351     private Set<AWTKeyStroke>[] defaultFocusTraversalKeys = new Set[4];
 352 
 353     /**
 354      * The current focus cycle root. If the focus owner is itself a focus cycle
 355      * root, then it may be ambiguous as to which Components represent the next
 356      * and previous Components to focus during normal focus traversal. In that
 357      * case, the current focus cycle root is used to differentiate among the
 358      * possibilities.
 359      */
 360     private static Container currentFocusCycleRoot;
 361 
 362     /**
 363      * A description of any VetoableChangeListeners which have been registered.
 364      */
 365     private VetoableChangeSupport vetoableSupport;
 366 
 367     /**
 368      * A description of any PropertyChangeListeners which have been registered.
 369      */
 370     private PropertyChangeSupport changeSupport;


 405 
 406     final void setCurrentSequencedEvent(SequencedEvent current) {
 407         synchronized (SequencedEvent.class) {
 408             assert(current == null || currentSequencedEvent == null);
 409             currentSequencedEvent = current;
 410         }
 411     }
 412 
 413     final SequencedEvent getCurrentSequencedEvent() {
 414         synchronized (SequencedEvent.class) {
 415             return currentSequencedEvent;
 416         }
 417     }
 418 
 419     static Set<AWTKeyStroke> initFocusTraversalKeysSet(String value, Set<AWTKeyStroke> targetSet) {
 420         StringTokenizer tokens = new StringTokenizer(value, ",");
 421         while (tokens.hasMoreTokens()) {
 422             targetSet.add(AWTKeyStroke.getAWTKeyStroke(tokens.nextToken()));
 423         }
 424         return (targetSet.isEmpty())
 425             ? Collections.EMPTY_SET
 426             : Collections.unmodifiableSet(targetSet);
 427     }
 428 
 429     /**
 430      * Initializes a KeyboardFocusManager.
 431      */
 432     public KeyboardFocusManager() {
 433         for (int i = 0; i < TRAVERSAL_KEY_LENGTH; i++) {
 434             Set<AWTKeyStroke> work_set = new HashSet<>();
 435             for (int j = 0; j < defaultFocusTraversalKeyStrokes[i].length; j++) {
 436                 work_set.add(defaultFocusTraversalKeyStrokes[i][j]);
 437             }
 438             defaultFocusTraversalKeys[i] = (work_set.isEmpty())
 439                 ? Collections.EMPTY_SET
 440                 : Collections.unmodifiableSet(work_set);
 441         }
 442         initPeer();
 443     }
 444 
 445     private void initPeer() {
 446         Toolkit tk = Toolkit.getDefaultToolkit();
 447         KeyboardFocusManagerPeerProvider peerProvider = (KeyboardFocusManagerPeerProvider)tk;
 448         peer = peerProvider.getKeyboardFocusManagerPeer();
 449     }
 450 
 451     /**
 452      * Returns the focus owner, if the focus owner is in the same context as
 453      * the calling thread. The focus owner is defined as the Component in an
 454      * application that will typically receive all KeyEvents generated by the
 455      * user. KeyEvents which map to the focus owner's focus traversal keys will
 456      * not be delivered if focus traversal keys are enabled for the focus
 457      * owner. In addition, KeyEventDispatchers may retarget or consume
 458      * KeyEvents before they reach the focus owner.
 459      *


1733                 if (keyEventDispatchers != null) {
1734                     keyEventDispatchers.remove(dispatcher);
1735                 }
1736             }
1737         }
1738     }
1739 
1740     /**
1741      * Returns this KeyboardFocusManager's KeyEventDispatcher chain as a List.
1742      * The List will not include this KeyboardFocusManager unless it was
1743      * explicitly re-registered via a call to
1744      * <code>addKeyEventDispatcher</code>. If no other KeyEventDispatchers are
1745      * registered, implementations are free to return null or a List of length
1746      * 0. Client code should not assume one behavior over another, nor should
1747      * it assume that the behavior, once established, will not change.
1748      *
1749      * @return a possibly null or empty List of KeyEventDispatchers
1750      * @see #addKeyEventDispatcher
1751      * @see #removeKeyEventDispatcher
1752      */

1753     protected synchronized java.util.List<KeyEventDispatcher>
1754         getKeyEventDispatchers()
1755     {
1756         return (keyEventDispatchers != null)
1757             ? (java.util.List)keyEventDispatchers.clone()
1758             : null;
1759     }
1760 
1761     /**
1762      * Adds a KeyEventPostProcessor to this KeyboardFocusManager's post-
1763      * processor chain. After a KeyEvent has been dispatched to and handled by
1764      * its target, KeyboardFocusManager will request that each
1765      * KeyEventPostProcessor perform any necessary post-processing as part
1766      * of the KeyEvent's final resolution. KeyEventPostProcessors
1767      * will be notified in the order in which they were added; the current
1768      * KeyboardFocusManager will be notified last. Notifications will halt
1769      * as soon as one KeyEventPostProcessor returns <code>true</code> from its
1770      * <code>postProcessKeyEvent</code> method. There is no limit to the the
1771      * total number of KeyEventPostProcessors that can be added, nor to the
1772      * number of times that a particular KeyEventPostProcessor instance can be
1773      * added.
1774      * <p>
1775      * If a null post-processor is specified, no action is taken and no
1776      * exception is thrown.
1777      * <p>


1824                     keyEventPostProcessors.remove(processor);
1825                 }
1826             }
1827         }
1828     }
1829 
1830 
1831     /**
1832      * Returns this KeyboardFocusManager's KeyEventPostProcessor chain as a
1833      * List. The List will not include this KeyboardFocusManager unless it was
1834      * explicitly added via a call to <code>addKeyEventPostProcessor</code>. If
1835      * no KeyEventPostProcessors are registered, implementations are free to
1836      * return null or a List of length 0. Client code should not assume one
1837      * behavior over another, nor should it assume that the behavior, once
1838      * established, will not change.
1839      *
1840      * @return a possibly null or empty List of KeyEventPostProcessors
1841      * @see #addKeyEventPostProcessor
1842      * @see #removeKeyEventPostProcessor
1843      */

1844     protected java.util.List<KeyEventPostProcessor>
1845         getKeyEventPostProcessors()
1846     {
1847         return (keyEventPostProcessors != null)
1848             ? (java.util.List)keyEventPostProcessors.clone()
1849             : null;
1850     }
1851 
1852 
1853 
1854     static void setMostRecentFocusOwner(Component component) {
1855         Component window = component;
1856         while (window != null && !(window instanceof Window)) {
1857             window = window.parent;
1858         }
1859         if (window != null) {
1860             setMostRecentFocusOwner((Window)window, component);
1861         }
1862     }
1863     static synchronized void setMostRecentFocusOwner(Window window,
1864                                                      Component component) {
1865         // ATTN: component has a strong reference to window via chain
1866         // of Component.parent fields.  Since WeakHasMap refers to its
1867         // values strongly, we need to break the strong link from the
1868         // value (component) back to its key (window).


1890             if ((window != null)
1891                 && (getMostRecentFocusOwner((Window)window) == comp))
1892             {
1893                 setMostRecentFocusOwner((Window)window, null);
1894             }
1895             // Also clear temporary lost component stored in Window
1896             if (window != null) {
1897                 Window realWindow = (Window)window;
1898                 if (realWindow.getTemporaryLostComponent() == comp) {
1899                     realWindow.setTemporaryLostComponent(null);
1900                 }
1901             }
1902         }
1903     }
1904 
1905     /*
1906      * Please be careful changing this method! It is called from
1907      * javax.swing.JComponent.runInputVerifier() using reflection.
1908      */
1909     static synchronized Component getMostRecentFocusOwner(Window window) {
1910         WeakReference<Component> weakValue =
1911             (WeakReference)mostRecentFocusOwners.get(window);
1912         return weakValue == null ? null : weakValue.get();
1913     }
1914 
1915     /**
1916      * This method is called by the AWT event dispatcher requesting that the
1917      * current KeyboardFocusManager dispatch the specified event on its behalf.
1918      * It is expected that all KeyboardFocusManagers will dispatch all
1919      * FocusEvents, all WindowEvents related to focus, and all KeyEvents.
1920      * These events should be dispatched based on the KeyboardFocusManager's
1921      * notion of the focus owner and the focused and active Windows, sometimes
1922      * overriding the source of the specified AWTEvent. Dispatching must be
1923      * done using <code>redispatchEvent</code> to prevent the AWT event
1924      * dispatcher from recursively requesting that the KeyboardFocusManager
1925      * dispatch the event again. If this method returns <code>false</code>,
1926      * then the AWT event dispatcher will attempt to dispatch the event itself.
1927      *
1928      * @param e the AWTEvent to be dispatched
1929      * @return <code>true</code> if this method dispatched the event;
1930      *         <code>false</code> otherwise
1931      * @see #redispatchEvent




 331         {
 332             AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, 0, false),
 333             AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK, false),
 334         },
 335         {
 336             AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK, false),
 337             AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB,
 338                                          InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK | InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK,
 339                                          false),
 340         },
 341         {},
 342         {},
 343       };
 344     /**
 345      * The default focus traversal keys. Each array of traversal keys will be
 346      * in effect on all Windows that have no such array of their own explicitly
 347      * set. Each array will also be inherited, recursively, by any child
 348      * Component of those Windows that has no such array of its own explicitly
 349      * set.
 350      */
 351     @SuppressWarnings({"unchecked", "rawtypes"})
 352     private Set<AWTKeyStroke>[] defaultFocusTraversalKeys = new Set[4];
 353 
 354     /**
 355      * The current focus cycle root. If the focus owner is itself a focus cycle
 356      * root, then it may be ambiguous as to which Components represent the next
 357      * and previous Components to focus during normal focus traversal. In that
 358      * case, the current focus cycle root is used to differentiate among the
 359      * possibilities.
 360      */
 361     private static Container currentFocusCycleRoot;
 362 
 363     /**
 364      * A description of any VetoableChangeListeners which have been registered.
 365      */
 366     private VetoableChangeSupport vetoableSupport;
 367 
 368     /**
 369      * A description of any PropertyChangeListeners which have been registered.
 370      */
 371     private PropertyChangeSupport changeSupport;


 406 
 407     final void setCurrentSequencedEvent(SequencedEvent current) {
 408         synchronized (SequencedEvent.class) {
 409             assert(current == null || currentSequencedEvent == null);
 410             currentSequencedEvent = current;
 411         }
 412     }
 413 
 414     final SequencedEvent getCurrentSequencedEvent() {
 415         synchronized (SequencedEvent.class) {
 416             return currentSequencedEvent;
 417         }
 418     }
 419 
 420     static Set<AWTKeyStroke> initFocusTraversalKeysSet(String value, Set<AWTKeyStroke> targetSet) {
 421         StringTokenizer tokens = new StringTokenizer(value, ",");
 422         while (tokens.hasMoreTokens()) {
 423             targetSet.add(AWTKeyStroke.getAWTKeyStroke(tokens.nextToken()));
 424         }
 425         return (targetSet.isEmpty())
 426             ? Collections.emptySet()
 427             : Collections.unmodifiableSet(targetSet);
 428     }
 429 
 430     /**
 431      * Initializes a KeyboardFocusManager.
 432      */
 433     public KeyboardFocusManager() {
 434         for (int i = 0; i < TRAVERSAL_KEY_LENGTH; i++) {
 435             Set<AWTKeyStroke> work_set = new HashSet<>();
 436             for (int j = 0; j < defaultFocusTraversalKeyStrokes[i].length; j++) {
 437                 work_set.add(defaultFocusTraversalKeyStrokes[i][j]);
 438             }
 439             defaultFocusTraversalKeys[i] = (work_set.isEmpty())
 440                 ? Collections.emptySet()
 441                 : Collections.unmodifiableSet(work_set);
 442         }
 443         initPeer();
 444     }
 445 
 446     private void initPeer() {
 447         Toolkit tk = Toolkit.getDefaultToolkit();
 448         KeyboardFocusManagerPeerProvider peerProvider = (KeyboardFocusManagerPeerProvider)tk;
 449         peer = peerProvider.getKeyboardFocusManagerPeer();
 450     }
 451 
 452     /**
 453      * Returns the focus owner, if the focus owner is in the same context as
 454      * the calling thread. The focus owner is defined as the Component in an
 455      * application that will typically receive all KeyEvents generated by the
 456      * user. KeyEvents which map to the focus owner's focus traversal keys will
 457      * not be delivered if focus traversal keys are enabled for the focus
 458      * owner. In addition, KeyEventDispatchers may retarget or consume
 459      * KeyEvents before they reach the focus owner.
 460      *


1734                 if (keyEventDispatchers != null) {
1735                     keyEventDispatchers.remove(dispatcher);
1736                 }
1737             }
1738         }
1739     }
1740 
1741     /**
1742      * Returns this KeyboardFocusManager's KeyEventDispatcher chain as a List.
1743      * The List will not include this KeyboardFocusManager unless it was
1744      * explicitly re-registered via a call to
1745      * <code>addKeyEventDispatcher</code>. If no other KeyEventDispatchers are
1746      * registered, implementations are free to return null or a List of length
1747      * 0. Client code should not assume one behavior over another, nor should
1748      * it assume that the behavior, once established, will not change.
1749      *
1750      * @return a possibly null or empty List of KeyEventDispatchers
1751      * @see #addKeyEventDispatcher
1752      * @see #removeKeyEventDispatcher
1753      */
1754     @SuppressWarnings("unchecked") // Cast of result of clone
1755     protected synchronized java.util.List<KeyEventDispatcher>
1756         getKeyEventDispatchers()
1757     {
1758         return (keyEventDispatchers != null)
1759             ? (java.util.List<KeyEventDispatcher>)keyEventDispatchers.clone()
1760             : null;
1761     }
1762 
1763     /**
1764      * Adds a KeyEventPostProcessor to this KeyboardFocusManager's post-
1765      * processor chain. After a KeyEvent has been dispatched to and handled by
1766      * its target, KeyboardFocusManager will request that each
1767      * KeyEventPostProcessor perform any necessary post-processing as part
1768      * of the KeyEvent's final resolution. KeyEventPostProcessors
1769      * will be notified in the order in which they were added; the current
1770      * KeyboardFocusManager will be notified last. Notifications will halt
1771      * as soon as one KeyEventPostProcessor returns <code>true</code> from its
1772      * <code>postProcessKeyEvent</code> method. There is no limit to the the
1773      * total number of KeyEventPostProcessors that can be added, nor to the
1774      * number of times that a particular KeyEventPostProcessor instance can be
1775      * added.
1776      * <p>
1777      * If a null post-processor is specified, no action is taken and no
1778      * exception is thrown.
1779      * <p>


1826                     keyEventPostProcessors.remove(processor);
1827                 }
1828             }
1829         }
1830     }
1831 
1832 
1833     /**
1834      * Returns this KeyboardFocusManager's KeyEventPostProcessor chain as a
1835      * List. The List will not include this KeyboardFocusManager unless it was
1836      * explicitly added via a call to <code>addKeyEventPostProcessor</code>. If
1837      * no KeyEventPostProcessors are registered, implementations are free to
1838      * return null or a List of length 0. Client code should not assume one
1839      * behavior over another, nor should it assume that the behavior, once
1840      * established, will not change.
1841      *
1842      * @return a possibly null or empty List of KeyEventPostProcessors
1843      * @see #addKeyEventPostProcessor
1844      * @see #removeKeyEventPostProcessor
1845      */
1846     @SuppressWarnings("unchecked") // Cast of result of clone
1847     protected java.util.List<KeyEventPostProcessor>
1848         getKeyEventPostProcessors()
1849     {
1850         return (keyEventPostProcessors != null)
1851             ? (java.util.List<KeyEventPostProcessor>)keyEventPostProcessors.clone()
1852             : null;
1853     }
1854 
1855 
1856 
1857     static void setMostRecentFocusOwner(Component component) {
1858         Component window = component;
1859         while (window != null && !(window instanceof Window)) {
1860             window = window.parent;
1861         }
1862         if (window != null) {
1863             setMostRecentFocusOwner((Window)window, component);
1864         }
1865     }
1866     static synchronized void setMostRecentFocusOwner(Window window,
1867                                                      Component component) {
1868         // ATTN: component has a strong reference to window via chain
1869         // of Component.parent fields.  Since WeakHasMap refers to its
1870         // values strongly, we need to break the strong link from the
1871         // value (component) back to its key (window).


1893             if ((window != null)
1894                 && (getMostRecentFocusOwner((Window)window) == comp))
1895             {
1896                 setMostRecentFocusOwner((Window)window, null);
1897             }
1898             // Also clear temporary lost component stored in Window
1899             if (window != null) {
1900                 Window realWindow = (Window)window;
1901                 if (realWindow.getTemporaryLostComponent() == comp) {
1902                     realWindow.setTemporaryLostComponent(null);
1903                 }
1904             }
1905         }
1906     }
1907 
1908     /*
1909      * Please be careful changing this method! It is called from
1910      * javax.swing.JComponent.runInputVerifier() using reflection.
1911      */
1912     static synchronized Component getMostRecentFocusOwner(Window window) {
1913         WeakReference<Component> weakValue = mostRecentFocusOwners.get(window);

1914         return weakValue == null ? null : weakValue.get();
1915     }
1916 
1917     /**
1918      * This method is called by the AWT event dispatcher requesting that the
1919      * current KeyboardFocusManager dispatch the specified event on its behalf.
1920      * It is expected that all KeyboardFocusManagers will dispatch all
1921      * FocusEvents, all WindowEvents related to focus, and all KeyEvents.
1922      * These events should be dispatched based on the KeyboardFocusManager's
1923      * notion of the focus owner and the focused and active Windows, sometimes
1924      * overriding the source of the specified AWTEvent. Dispatching must be
1925      * done using <code>redispatchEvent</code> to prevent the AWT event
1926      * dispatcher from recursively requesting that the KeyboardFocusManager
1927      * dispatch the event again. If this method returns <code>false</code>,
1928      * then the AWT event dispatcher will attempt to dispatch the event itself.
1929      *
1930      * @param e the AWTEvent to be dispatched
1931      * @return <code>true</code> if this method dispatched the event;
1932      *         <code>false</code> otherwise
1933      * @see #redispatchEvent