source: trunk/gcc/libjava/java/awt/KeyboardFocusManager.java@ 3951

Last change on this file since 3951 was 1389, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 16.6 KB
Line 
1/* KeyboardFocusManager.java -- manage component focusing via the keyboard
2 Copyright (C) 2002 Free Software Foundation
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.awt;
40
41import java.awt.event.KeyEvent;
42import java.beans.PropertyChangeListener;
43import java.beans.PropertyChangeSupport;
44import java.beans.PropertyVetoException;
45import java.beans.VetoableChangeListener;
46import java.beans.VetoableChangeSupport;
47import java.util.ArrayList;
48import java.util.Collections;
49import java.util.HashSet;
50import java.util.Iterator;
51import java.util.List;
52import 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 */
60public 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