| 1 | /* KeyboardFocusManager.java -- manage component focusing via the keyboard
|
|---|
| 2 | Copyright (C) 2002 Free Software Foundation
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | package java.awt;
|
|---|
| 40 |
|
|---|
| 41 | import java.awt.event.KeyEvent;
|
|---|
| 42 | import java.beans.PropertyChangeListener;
|
|---|
| 43 | import java.beans.PropertyChangeSupport;
|
|---|
| 44 | import java.beans.PropertyVetoException;
|
|---|
| 45 | import java.beans.VetoableChangeListener;
|
|---|
| 46 | import java.beans.VetoableChangeSupport;
|
|---|
| 47 | import java.util.ArrayList;
|
|---|
| 48 | import java.util.Collections;
|
|---|
| 49 | import java.util.HashSet;
|
|---|
| 50 | import java.util.Iterator;
|
|---|
| 51 | import java.util.List;
|
|---|
| 52 | import java.util.Set;
|
|---|
| 53 |
|
|---|
| 54 | /**
|
|---|
| 55 | *
|
|---|
| 56 | * @author Eric Blake <[email protected]>
|
|---|
| 57 | * @since 1.4
|
|---|
| 58 | * @status partially updated to 1.4, needs documentation.
|
|---|
| 59 | */
|
|---|
| 60 | public abstract class KeyboardFocusManager
|
|---|
| 61 | implements KeyEventDispatcher, KeyEventPostProcessor
|
|---|
| 62 | {
|
|---|
| 63 | public static final int FORWARD_TRAVERSAL_KEYS = 0;
|
|---|
| 64 | public static final int BACKWARD_TRAVERSAL_KEYS = 1;
|
|---|
| 65 | public static final int UP_CYCLE_TRAVERSAL_KEYS = 2;
|
|---|
| 66 | public static final int DOWN_CYCLE_TRAVERSAL_KEYS = 3;
|
|---|
| 67 |
|
|---|
| 68 | private static final Set DEFAULT_FORWARD_KEYS;
|
|---|
| 69 | private static final Set DEFAULT_BACKWARD_KEYS;
|
|---|
| 70 | static
|
|---|
| 71 | {
|
|---|
| 72 | Set s = new HashSet();
|
|---|
| 73 | s.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, 0));
|
|---|
| 74 | s.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB,
|
|---|
| 75 | KeyEvent.CTRL_DOWN_MASK));
|
|---|
| 76 | DEFAULT_FORWARD_KEYS = Collections.unmodifiableSet(s);
|
|---|
| 77 | s = new HashSet();
|
|---|
| 78 | s.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB,
|
|---|
| 79 | KeyEvent.SHIFT_DOWN_MASK));
|
|---|
| 80 | s.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB,
|
|---|
| 81 | KeyEvent.SHIFT_DOWN_MASK
|
|---|
| 82 | | KeyEvent.CTRL_DOWN_MASK));
|
|---|
| 83 | DEFAULT_BACKWARD_KEYS = Collections.unmodifiableSet(s);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | private static KeyboardFocusManager current
|
|---|
| 87 | = new DefaultKeyboardFocusManager();
|
|---|
| 88 |
|
|---|
| 89 | // XXX Not implemented correctly. I think a good implementation here may
|
|---|
| 90 | // be to have permanentFocusOwner be null, and fall back to focusOwner,
|
|---|
| 91 | // unless a temporary focus change is in effect.
|
|---|
| 92 | private static Component focusOwner;
|
|---|
| 93 | private static Component permanentFocusOwner;
|
|---|
| 94 |
|
|---|
| 95 | private static Window focusedWindow;
|
|---|
| 96 | private static Window activeWindow;
|
|---|
| 97 | private static Container focusCycleRoot;
|
|---|
| 98 |
|
|---|
| 99 | private FocusTraversalPolicy defaultPolicy;
|
|---|
| 100 | private Set[] defaultFocusKeys = new Set[] {
|
|---|
| 101 | DEFAULT_FORWARD_KEYS, DEFAULT_BACKWARD_KEYS,
|
|---|
| 102 | Collections.EMPTY_SET, Collections.EMPTY_SET
|
|---|
| 103 | };
|
|---|
| 104 |
|
|---|
| 105 | private final PropertyChangeSupport propertyChangeSupport
|
|---|
| 106 | = new PropertyChangeSupport(this);
|
|---|
| 107 | private final VetoableChangeSupport vetoableChangeSupport
|
|---|
| 108 | = new VetoableChangeSupport(this);
|
|---|
| 109 | private final ArrayList keyEventDispatchers = new ArrayList();
|
|---|
| 110 | private final ArrayList keyEventPostProcessors = new ArrayList();
|
|---|
| 111 |
|
|---|
| 112 | |
|---|
| 113 |
|
|---|
| 114 | public KeyboardFocusManager()
|
|---|
| 115 | {
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | public static KeyboardFocusManager getCurrentKeyboardFocusManager()
|
|---|
| 119 | {
|
|---|
| 120 | // XXX Need a way to divide this into contexts.
|
|---|
| 121 | return current;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | public static void setCurrentKeyboardFocusManager(KeyboardFocusManager m)
|
|---|
| 125 | {
|
|---|
| 126 | SecurityManager sm = System.getSecurityManager();
|
|---|
| 127 | if (sm != null)
|
|---|
| 128 | sm.checkPermission(new AWTPermission("replaceKeyboardFocusManager"));
|
|---|
| 129 | // XXX Need a way to divide this into contexts.
|
|---|
| 130 | current = m == null ? new DefaultKeyboardFocusManager() : m;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | public Component getFocusOwner()
|
|---|
| 134 | {
|
|---|
| 135 | // XXX Need an easy way to test if this thread is in the context of the
|
|---|
| 136 | // global focus owner, to avoid creating the exception in the first place.
|
|---|
| 137 | try
|
|---|
| 138 | {
|
|---|
| 139 | return getGlobalFocusOwner();
|
|---|
| 140 | }
|
|---|
| 141 | catch (SecurityException e)
|
|---|
| 142 | {
|
|---|
| 143 | return null;
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
|
|---|