OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libjava/] [classpath/] [javax/] [swing/] [KeyboardManager.java] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/* KeyboardManager.java --
2
   Copyright (C) 2005 Free Software Foundation, Inc.
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., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 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 javax.swing;
40
 
41
import java.applet.Applet;
42
import java.awt.Component;
43
import java.awt.Container;
44
import java.awt.Window;
45
import java.awt.event.KeyEvent;
46
import java.util.Enumeration;
47
import java.util.Hashtable;
48
import java.util.Vector;
49
 
50
/**
51
 * This class maintains a mapping from top-level containers to a
52
 * Hashtable.  The Hashtable maps KeyStrokes to Components to be used when
53
 * Components register keyboard actions with the condition
54
 * JComponent.WHEN_IN_FOCUSED_WINDOW.
55
 *
56
 * @author Anthony Balkissoon abalkiss at redhat dot com
57
 *
58
 */
59
class KeyboardManager
60
{
61
  /** Shared instance of KeyboardManager **/
62
  static KeyboardManager manager = new KeyboardManager();
63
 
64
  /**
65
   * A mapping between top level containers and Hashtables that
66
   * map KeyStrokes to Components.
67
   */
68
  Hashtable topLevelLookup = new Hashtable();
69
 
70
  /**
71
   * A mapping between top level containers and Vectors of JMenuBars
72
   * used to allow all the JMenuBars within a top level container
73
   * a chance to consume key events.
74
   */
75
  Hashtable menuBarLookup = new Hashtable();
76
  /**
77
   * Returns the shared instance of KeyboardManager.
78
   * @return the shared instance of KeybaordManager.
79
   */
80
  public static KeyboardManager getManager()
81
  {
82
    return manager;
83
  }
84
  /**
85
   * Returns the top-level ancestor for the given JComponent.
86
   * @param c the JComponent whose top-level ancestor we want
87
   * @return the top-level ancestor for the given JComponent.
88
   */
89
  static Container findTopLevel (Component c)
90
  {
91
    Container topLevel = (c instanceof Container) ? (Container) c
92
                                                 : c.getParent();
93
    while (topLevel != null &&
94
           !(topLevel instanceof Window) &&
95
           !(topLevel instanceof Applet) &&
96
           !(topLevel instanceof JInternalFrame))
97
      topLevel = topLevel.getParent();
98
    return topLevel;
99
  }
100
 
101
  /**
102
   * Returns the Hashtable that maps KeyStrokes to Components, for
103
   * the specified top-level container c.  If no Hashtable exists
104
   * we create and register it here and return the newly created
105
   * Hashtable.
106
   *
107
   * @param c the top-level container whose Hashtable we want
108
   * @return the Hashtable mapping KeyStrokes to Components for the
109
   * specified top-level container
110
   */
111
  Hashtable getHashtableForTopLevel (Container c)
112
  {
113
    Hashtable keyToComponent = (Hashtable)topLevelLookup.get(c);
114
    if (keyToComponent == null)
115
      {
116
        keyToComponent = new Hashtable();
117
        topLevelLookup.put(c, keyToComponent);
118
      }
119
    return keyToComponent;
120
  }
121
 
122
  /**
123
   * Registers a KeyStroke with a Component.  This does not register
124
   * the KeyStroke to a specific Action.  When searching for a
125
   * WHEN_IN_FOCUSED_WINDOW binding we will first go up to the focused
126
   * top-level Container, then get the Hashtable that maps KeyStrokes
127
   * to components for that particular top-level Container, then
128
   * call processKeyBindings on that component with the condition
129
   * JComponent.WHEN_IN_FOCUSED_WINDOW.
130
   * @param comp the JComponent associated with the KeyStroke
131
   * @param key the KeyStroke
132
   */
133
  public void registerBinding(JComponent comp, KeyStroke key)
134
  {
135
    // This method associates a KeyStroke with a particular JComponent
136
    // When the KeyStroke occurs, if this component's top-level ancestor
137
    // has focus (one of its children is the focused Component) then 
138
    // comp.processKeyBindings will be called with condition
139
    // JComponent.WHEN_IN_FOCUSED_WINDOW.
140
 
141
    // Look for the JComponent's top-level parent and return if it is null
142
    Container topLevel = findTopLevel(comp);
143
    if (topLevel == null)
144
      return;
145
 
146
    // Now get the Hashtable for this top-level container
147
    Hashtable keyToComponent = getHashtableForTopLevel(topLevel);
148
 
149
    // And add the new binding to this Hashtable
150
    // FIXME: should allow more than one JComponent to be associated
151
    // with a KeyStroke, in case one of them is disabled
152
    keyToComponent.put(key, comp);
153
  }
154
 
155
  public void clearBindingsForComp(JComponent comp)
156
  {
157
    // This method clears all the WHEN_IN_FOCUSED_WINDOW bindings associated
158
    // with <code>comp</code>.  This is used for a terribly ineffcient
159
    // strategy in which JComponent.updateComponentInputMap simply clears
160
    // all bindings associated with its component and then reloads all the
161
    // bindings from the updated ComponentInputMap.  This is only a preliminary
162
    // strategy and should be improved upon once the WHEN_IN_FOCUSED_WINDOW
163
    // bindings work.
164
 
165
    // Find the top-level ancestor
166
 
167
    Container topLevel = findTopLevel(comp);
168
    if (topLevel == null)
169
      return;
170
    // And now get its Hashtable
171
    Hashtable keyToComponent = getHashtableForTopLevel(topLevel);
172
 
173
    Enumeration keys = keyToComponent.keys();
174
    Object temp;
175
 
176
    // Iterate through the keys and remove any key whose value is comp
177
    while (keys.hasMoreElements())
178
      {
179
        temp = keys.nextElement();
180
        if (comp == (JComponent)keyToComponent.get(temp))
181
          keyToComponent.remove(temp);
182
      }
183
  }
184
 
185
  /**
186
   * This method registers all the bindings in the given ComponentInputMap.
187
   * Rather than call registerBinding on all the keys, we do the work here
188
   * so that we don't duplicate finding the top-level container and
189
   * getting its Hashtable.
190
   *
191
   * @param map the ComponentInputMap whose bindings we want to register
192
   */
193
  public void registerEntireMap (ComponentInputMap map)
194
  {
195
    if (map == null)
196
      return;
197
    JComponent comp = map.getComponent();
198
    KeyStroke[] keys = map.keys();
199
    if (keys == null)
200
      return;
201
    // Find the top-level container associated with this ComponentInputMap
202
    Container topLevel = findTopLevel(comp);
203
    if (topLevel == null)
204
      return;
205
 
206
    // Register the KeyStrokes in the top-level container's Hashtable
207
    Hashtable keyToComponent = getHashtableForTopLevel(topLevel);
208
    for (int i = 0; i < keys.length; i++)
209
      keyToComponent.put(keys[i], comp);
210
  }
211
 
212
  public boolean processKeyStroke (Component comp, KeyStroke key, KeyEvent e)
213
  {
214
    boolean pressed = e.getID() == KeyEvent.KEY_PRESSED;
215
 
216
    // Look for the top-level ancestor
217
    Container topLevel = findTopLevel(comp);
218
    if (topLevel == null)
219
      return false;
220
    // Now get the Hashtable for that top-level container
221
    Hashtable keyToComponent = getHashtableForTopLevel(topLevel);
222
    Enumeration keys = keyToComponent.keys();
223
    JComponent target = (JComponent)keyToComponent.get(key);
224
    if (target != null && target.processKeyBinding
225
        (key, e, JComponent.WHEN_IN_FOCUSED_WINDOW, pressed))
226
      return true;
227
 
228
    // Have to give all the JMenuBars a chance to consume the event
229
    Vector menuBars = getVectorForTopLevel(topLevel);
230
    for (int i = 0; i < menuBars.size(); i++)
231
      if (((JMenuBar)menuBars.elementAt(i)).processKeyBinding(key, e, JComponent.WHEN_IN_FOCUSED_WINDOW, pressed))
232
        return true;
233
    return false;
234
  }
235
 
236
  /**
237
   * Returns the Vector of JMenuBars associated with the top-level
238
   * @param c the top-level container whose JMenuBar Vector we want
239
   * @return the Vector of JMenuBars for this top level container
240
   */
241
  Vector getVectorForTopLevel(Container c)
242
  {
243
    Vector result = (Vector) menuBarLookup.get(c);
244
    if (result == null)
245
      {
246
        result = new Vector();
247
        menuBarLookup.put (c, result);
248
      }
249
    return result;
250
  }
251
 
252
  /**
253
   * In processKeyStroke, KeyManager must give all JMenuBars in the
254
   * focused top-level container a chance to process the event.  So,
255
   * JMenuBars must be registered in KeyManager and associated with a
256
   * top-level container.  That's what this method is for.
257
   * @param menuBar the JMenuBar to register
258
   */
259
  public void registerJMenuBar (JMenuBar menuBar)
260
  {
261
    Container topLevel = findTopLevel(menuBar);
262
    Vector menuBars = getVectorForTopLevel(topLevel);
263
    if (!menuBars.contains(menuBar))
264
      menuBars.add(menuBar);
265
  }
266
 
267
  /**
268
   * Unregisters a JMenuBar from its top-level container.  This is
269
   * called before the JMenuBar is actually removed from the container
270
   * so findTopLevel will still find us the correct top-level container.
271
   * @param menuBar the JMenuBar to unregister.
272
   */
273
  public void unregisterJMenuBar (JMenuBar menuBar)
274
  {
275
    Container topLevel = findTopLevel(menuBar);
276
    Vector menuBars = getVectorForTopLevel(topLevel);
277
    if (menuBars.contains(menuBar))
278
      menuBars.remove(menuBar);
279
  }
280
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.