jdk/src/share/classes/javax/swing/KeyboardManager.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1301 15e81207e1f2
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2004 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.applet.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.awt.EmbeddedFrame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
  * The KeyboardManager class is used to help dispatch keyboard actions for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
  * WHEN_IN_FOCUSED_WINDOW style actions.  Actions with other conditions are handled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
  * directly in JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
  * Here's a description of the symantics of how keyboard dispatching should work
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
  * atleast as I understand it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
  * KeyEvents are dispatched to the focused component.  The focus manager gets first
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
  * crack at processing this event.  If the focus manager doesn't want it, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
  * the JComponent calls super.processKeyEvent() this allows listeners a chance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
  * to process the event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
  * If none of the listeners "consumes" the event then the keybindings get a shot.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
  * This is where things start to get interesting.  First, KeyStokes defined with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
  * WHEN_FOCUSED condition get a chance.  If none of these want the event, then the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
  * walks though it's parents looked for actions of type WHEN_ANCESTOR_OF_FOCUSED_COMPONENT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  * If no one has taken it yet, then it winds up here.  We then look for components registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  * for WHEN_IN_FOCUSED_WINDOW events and fire to them.  Note that if none of those are found
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
  * then we pass the event to the menubars and let them have a crack at it.  They're handled differently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
  * Lastly, we check if we're looking at an internal frame.  If we are and no one wanted the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
  * then we move up to the InternalFrame's creator and see if anyone wants the event (and so on and so on).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
  * @see InputMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
class KeyboardManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static KeyboardManager currentManager = new KeyboardManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
      * maps top-level containers to a sub-hashtable full of keystrokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    Hashtable containerMap = new Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
      * Maps component/keystroke pairs to a topLevel container
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
      * This is mainly used for fast unregister operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    Hashtable componentKeyStrokeMap = new Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static KeyboardManager getCurrentManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return currentManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public static void setCurrentManager(KeyboardManager km) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        currentManager = km;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
      * register keystrokes here which are for the WHEN_IN_FOCUSED_WINDOW
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
      * case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
      * Other types of keystrokes will be handled by walking the hierarchy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
      * That simplifies some potentially hairy stuff.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     public void registerKeyStroke(KeyStroke k, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
         Container topContainer = getTopAncestor(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
         if (topContainer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
             return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
         Hashtable keyMap = (Hashtable)containerMap.get(topContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
         if (keyMap ==  null) {  // lazy evaluate one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
             keyMap = registerNewTopContainer(topContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
         Object tmp = keyMap.get(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
         if (tmp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
             keyMap.put(k,c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
         } else if (tmp instanceof Vector) {  // if there's a Vector there then add to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
             Vector v = (Vector)tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
             if (!v.contains(c)) {  // only add if this keystroke isn't registered for this component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                 v.addElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
         } else if (tmp instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
           // if a JComponent is there then remove it and replace it with a vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
           // Then add the old compoennt and the new compoent to the vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
           // then insert the vector in the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
           if (tmp != c) {  // this means this is already registered for this component, no need to dup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
               Vector v = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
               v.addElement(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
               v.addElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
               keyMap.put(k, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
         } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
             System.out.println("Unexpected condition in registerKeyStroke");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
             Thread.dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
         componentKeyStrokeMap.put(new ComponentKeyStrokePair(c,k), topContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
         // Check for EmbeddedFrame case, they know how to process accelerators even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
         // when focus is not in Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
         if (topContainer instanceof EmbeddedFrame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
             ((EmbeddedFrame)topContainer).registerAccelerator(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
       * Find the top focusable Window, Applet, or InternalFrame
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     private static Container getTopAncestor(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        for(Container p = c.getParent(); p != null; p = p.getParent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if (p instanceof Window && ((Window)p).isFocusableWindow() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                p instanceof Applet || p instanceof JInternalFrame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     public void unregisterKeyStroke(KeyStroke ks, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
       // component may have already been removed from the hierarchy, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
       // need to look up the container using the componentKeyStrokeMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
         ComponentKeyStrokePair ckp = new ComponentKeyStrokePair(c,ks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
         Object topContainer = componentKeyStrokeMap.get(ckp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         if (topContainer == null) {  // never heard of this pairing, so bail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
             return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
         Hashtable keyMap = (Hashtable)containerMap.get(topContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
         if  (keyMap == null) { // this should never happen, but I'm being safe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
             Thread.dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
             return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
         Object tmp = keyMap.get(ks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
         if (tmp == null) {  // this should never happen, but I'm being safe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
             Thread.dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
             return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         if (tmp instanceof JComponent && tmp == c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
             keyMap.remove(ks);  // remove the KeyStroke from the Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
             //System.out.println("removed a stroke" + ks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         } else if (tmp instanceof Vector ) {  // this means there is more than one component reg for this key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
             Vector v = (Vector)tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
             v.removeElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
             if ( v.isEmpty() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                 keyMap.remove(ks);  // remove the KeyStroke from the Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                 //System.out.println("removed a ks vector");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
         if ( keyMap.isEmpty() ) {  // if no more bindings in this table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
             containerMap.remove(topContainer);  // remove table to enable GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
             //System.out.println("removed a container");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
         componentKeyStrokeMap.remove(ckp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
         // Check for EmbeddedFrame case, they know how to process accelerators even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
         // when focus is not in Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
         if (topContainer instanceof EmbeddedFrame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
             ((EmbeddedFrame)topContainer).unregisterAccelerator(ks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
      * This method is called when the focused component (and none of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
      * its ancestors) want the key event.  This will look up the keystroke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
      * to see if any chidren (or subchildren) of the specified container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
      * want a crack at the event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
      * If one of them wants it, then it will "DO-THE-RIGHT-THING"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public boolean fireKeyboardAction(KeyEvent e, boolean pressed, Container topAncestor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
         if (e.isConsumed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
              System.out.println("Aquired pre-used event!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
              Thread.dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         KeyStroke ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
         if(e.getID() == KeyEvent.KEY_TYPED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
               ks=KeyStroke.getKeyStroke(e.getKeyChar());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
               ks=KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers(), !pressed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         Hashtable keyMap = (Hashtable)containerMap.get(topAncestor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         if (keyMap != null) { // this container isn't registered, so bail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
             Object tmp = keyMap.get(ks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
             if (tmp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
               // don't do anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
             } else if ( tmp instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                 JComponent c = (JComponent)tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                 if ( c.isShowing() && c.isEnabled() ) { // only give it out if enabled and visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                     fireBinding(c, ks, e, pressed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
             } else if ( tmp instanceof Vector) { //more than one comp registered for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                 Vector v = (Vector)tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                 // There is no well defined order for WHEN_IN_FOCUSED_WINDOW
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                 // bindings, but we give precedence to those bindings just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                 // added. This is done so that JMenus WHEN_IN_FOCUSED_WINDOW
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                 // bindings are accessed before those of the JRootPane (they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                 // both have a WHEN_IN_FOCUSED_WINDOW binding for enter).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                 for (int counter = v.size() - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                     JComponent c = (JComponent)v.elementAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                     //System.out.println("Trying collision: " + c + " vector = "+ v.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                     if ( c.isShowing() && c.isEnabled() ) { // don't want to give these out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                         fireBinding(c, ks, e, pressed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                         if (e.isConsumed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                             return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
             } else  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                 System.out.println( "Unexpected condition in fireKeyboardAction " + tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                 // This means that tmp wasn't null, a JComponent, or a Vector.  What is it?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                 Thread.dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
         if (e.isConsumed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
             return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
         // if no one else handled it, then give the menus a crack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
         // The're handled differently.  The key is to let any JMenuBars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
         // process the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
         if ( keyMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
             Vector v = (Vector)keyMap.get(JMenuBar.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
             if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                 Enumeration iter = v.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                 while (iter.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                     JMenuBar mb = (JMenuBar)iter.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                     if ( mb.isShowing() && mb.isEnabled() ) { // don't want to give these out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                         fireBinding(mb, ks, e, pressed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                         if (e.isConsumed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                             return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
         return e.isConsumed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    void fireBinding(JComponent c, KeyStroke ks, KeyEvent e, boolean pressed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (c.processKeyBinding(ks, e, JComponent.WHEN_IN_FOCUSED_WINDOW,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                pressed)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public void registerMenuBar(JMenuBar mb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        Container top = getTopAncestor(mb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (top == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        Hashtable keyMap = (Hashtable)containerMap.get(top);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (keyMap ==  null) {  // lazy evaluate one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
             keyMap = registerNewTopContainer(top);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        // use the menubar class as the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        Vector menuBars = (Vector)keyMap.get(JMenuBar.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (menuBars == null) {  // if we don't have a list of menubars,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                 // then make one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            menuBars = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            keyMap.put(JMenuBar.class, menuBars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (!menuBars.contains(mb)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            menuBars.addElement(mb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public void unregisterMenuBar(JMenuBar mb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        Object topContainer = getTopAncestor(mb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (topContainer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        Hashtable keyMap = (Hashtable)containerMap.get(topContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (keyMap!=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            Vector v = (Vector)keyMap.get(JMenuBar.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                v.removeElement(mb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                if (v.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    keyMap.remove(JMenuBar.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    if (keyMap.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                        // remove table to enable GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                        containerMap.remove(topContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    protected Hashtable registerNewTopContainer(Container topContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
             Hashtable keyMap = new Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
             containerMap.put(topContainer, keyMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
             return keyMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
      * This class is used to create keys for a hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
      * which looks up topContainers based on component, keystroke pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
      * This is used to make unregistering KeyStrokes fast
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    class ComponentKeyStrokePair {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        Object component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        Object keyStroke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        public ComponentKeyStrokePair(Object comp, Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            component = comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            keyStroke = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            if ( !(o instanceof ComponentKeyStrokePair)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            ComponentKeyStrokePair ckp = (ComponentKeyStrokePair)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            return ((component.equals(ckp.component)) && (keyStroke.equals(ckp.keyStroke)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            return component.hashCode() * keyStroke.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
} // end KeyboardManager