src/share/classes/javax/swing/KeyboardManager.java

Print this page




  51   * WHEN_FOCUSED condition get a chance.  If none of these want the event, then the component
  52   * walks though it's parents looked for actions of type WHEN_ANCESTOR_OF_FOCUSED_COMPONENT.
  53   *
  54   * If no one has taken it yet, then it winds up here.  We then look for components registered
  55   * for WHEN_IN_FOCUSED_WINDOW events and fire to them.  Note that if none of those are found
  56   * then we pass the event to the menubars and let them have a crack at it.  They're handled differently.
  57   *
  58   * Lastly, we check if we're looking at an internal frame.  If we are and no one wanted the event
  59   * then we move up to the InternalFrame's creator and see if anyone wants the event (and so on and so on).
  60   *
  61   *
  62   * @see InputMap
  63   */
  64 class KeyboardManager {
  65 
  66     static KeyboardManager currentManager = new KeyboardManager();
  67 
  68     /**
  69       * maps top-level containers to a sub-hashtable full of keystrokes
  70       */
  71     Hashtable<Container, Hashtable> containerMap = new Hashtable<Container, Hashtable>();
  72 
  73     /**
  74       * Maps component/keystroke pairs to a topLevel container
  75       * This is mainly used for fast unregister operations
  76       */
  77     Hashtable<ComponentKeyStrokePair, Container> componentKeyStrokeMap = new Hashtable<ComponentKeyStrokePair, Container>();
  78 
  79     public static KeyboardManager getCurrentManager() {
  80         return currentManager;
  81     }
  82 
  83     public static void setCurrentManager(KeyboardManager km) {
  84         currentManager = km;
  85     }
  86 
  87     /**
  88       * register keystrokes here which are for the WHEN_IN_FOCUSED_WINDOW
  89       * case.
  90       * Other types of keystrokes will be handled by walking the hierarchy
  91       * That simplifies some potentially hairy stuff.
  92       */
  93      public void registerKeyStroke(KeyStroke k, JComponent c) {
  94          Container topContainer = getTopAncestor(c);
  95          if (topContainer == null) {
  96              return;
  97          }
  98          Hashtable keyMap = containerMap.get(topContainer);
  99 
 100          if (keyMap ==  null) {  // lazy evaluate one
 101              keyMap = registerNewTopContainer(topContainer);
 102          }
 103 
 104          Object tmp = keyMap.get(k);
 105          if (tmp == null) {
 106              keyMap.put(k,c);
 107          } else if (tmp instanceof Vector) {  // if there's a Vector there then add to it.
 108              Vector v = (Vector)tmp;

 109              if (!v.contains(c)) {  // only add if this keystroke isn't registered for this component
 110                  v.addElement(c);
 111              }
 112          } else if (tmp instanceof JComponent) {
 113            // if a JComponent is there then remove it and replace it with a vector
 114            // Then add the old compoennt and the new compoent to the vector
 115            // then insert the vector in the table
 116            if (tmp != c) {  // this means this is already registered for this component, no need to dup
 117                Vector<JComponent> v = new Vector<JComponent>();
 118                v.addElement((JComponent) tmp);
 119                v.addElement(c);
 120                keyMap.put(k, v);
 121            }
 122          } else {
 123              System.out.println("Unexpected condition in registerKeyStroke");
 124              Thread.dumpStack();
 125          }
 126 
 127          componentKeyStrokeMap.put(new ComponentKeyStrokePair(c,k), topContainer);
 128 
 129          // Check for EmbeddedFrame case, they know how to process accelerators even
 130          // when focus is not in Java
 131          if (topContainer instanceof EmbeddedFrame) {
 132              ((EmbeddedFrame)topContainer).registerAccelerator(k);
 133          }
 134      }
 135 
 136      /**
 137        * Find the top focusable Window, Applet, or InternalFrame


 143 
 144                 return p;
 145             }
 146         }
 147         return null;
 148      }
 149 
 150      public void unregisterKeyStroke(KeyStroke ks, JComponent c) {
 151 
 152        // component may have already been removed from the hierarchy, we
 153        // need to look up the container using the componentKeyStrokeMap.
 154 
 155          ComponentKeyStrokePair ckp = new ComponentKeyStrokePair(c,ks);
 156 
 157          Container topContainer = componentKeyStrokeMap.get(ckp);
 158 
 159          if (topContainer == null) {  // never heard of this pairing, so bail
 160              return;
 161          }
 162 
 163          Hashtable keyMap = containerMap.get(topContainer);
 164          if  (keyMap == null) { // this should never happen, but I'm being safe
 165              Thread.dumpStack();
 166              return;
 167          }
 168 
 169          Object tmp = keyMap.get(ks);
 170          if (tmp == null) {  // this should never happen, but I'm being safe
 171              Thread.dumpStack();
 172              return;
 173          }
 174 
 175          if (tmp instanceof JComponent && tmp == c) {
 176              keyMap.remove(ks);  // remove the KeyStroke from the Map
 177              //System.out.println("removed a stroke" + ks);
 178          } else if (tmp instanceof Vector ) {  // this means there is more than one component reg for this key
 179              Vector v = (Vector)tmp;
 180              v.removeElement(c);
 181              if ( v.isEmpty() ) {
 182                  keyMap.remove(ks);  // remove the KeyStroke from the Map
 183                  //System.out.println("removed a ks vector");
 184              }
 185          }
 186 
 187          if ( keyMap.isEmpty() ) {  // if no more bindings in this table
 188              containerMap.remove(topContainer);  // remove table to enable GC
 189              //System.out.println("removed a container");
 190          }
 191 
 192          componentKeyStrokeMap.remove(ckp);
 193 
 194          // Check for EmbeddedFrame case, they know how to process accelerators even
 195          // when focus is not in Java
 196          if (topContainer instanceof EmbeddedFrame) {
 197              ((EmbeddedFrame)topContainer).unregisterAccelerator(ks);
 198          }
 199      }


 210          if (e.isConsumed()) {
 211               System.out.println("Acquired pre-used event!");
 212               Thread.dumpStack();
 213          }
 214 
 215          // There may be two keystrokes associated with a low-level key event;
 216          // in this case a keystroke made of an extended key code has a priority.
 217          KeyStroke ks;
 218          KeyStroke ksE = null;
 219 
 220 
 221          if(e.getID() == KeyEvent.KEY_TYPED) {
 222                ks=KeyStroke.getKeyStroke(e.getKeyChar());
 223          } else {
 224                if(e.getKeyCode() != e.getExtendedKeyCode()) {
 225                    ksE=KeyStroke.getKeyStroke(e.getExtendedKeyCode(), e.getModifiers(), !pressed);
 226                }
 227                ks=KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers(), !pressed);
 228          }
 229 
 230          Hashtable keyMap = containerMap.get(topAncestor);
 231          if (keyMap != null) { // this container isn't registered, so bail
 232 
 233              Object tmp = null;
 234              // extended code has priority
 235              if( ksE != null ) {
 236                  tmp = keyMap.get(ksE);
 237                  if( tmp != null ) {
 238                      ks = ksE;
 239                  }
 240              }
 241              if( tmp == null ) {
 242                  tmp = keyMap.get(ks);
 243              }
 244 
 245              if (tmp == null) {
 246                // don't do anything
 247              } else if ( tmp instanceof JComponent) {
 248                  JComponent c = (JComponent)tmp;
 249                  if ( c.isShowing() && c.isEnabled() ) { // only give it out if enabled and visible
 250                      fireBinding(c, ks, e, pressed);
 251                  }
 252              } else if ( tmp instanceof Vector) { //more than one comp registered for this
 253                  Vector v = (Vector)tmp;
 254                  // There is no well defined order for WHEN_IN_FOCUSED_WINDOW
 255                  // bindings, but we give precedence to those bindings just
 256                  // added. This is done so that JMenus WHEN_IN_FOCUSED_WINDOW
 257                  // bindings are accessed before those of the JRootPane (they
 258                  // both have a WHEN_IN_FOCUSED_WINDOW binding for enter).
 259                  for (int counter = v.size() - 1; counter >= 0; counter--) {
 260                      JComponent c = (JComponent)v.elementAt(counter);
 261                      //System.out.println("Trying collision: " + c + " vector = "+ v.size());
 262                      if ( c.isShowing() && c.isEnabled() ) { // don't want to give these out
 263                          fireBinding(c, ks, e, pressed);
 264                          if (e.isConsumed())
 265                              return true;
 266                      }
 267                  }
 268              } else  {
 269                  System.out.println( "Unexpected condition in fireKeyboardAction " + tmp);
 270                  // This means that tmp wasn't null, a JComponent, or a Vector.  What is it?
 271                  Thread.dumpStack();
 272              }
 273          }
 274 
 275          if (e.isConsumed()) {
 276              return true;
 277          }
 278          // if no one else handled it, then give the menus a crack
 279          // The're handled differently.  The key is to let any JMenuBars
 280          // process the event
 281          if ( keyMap != null) {
 282              Vector v = (Vector)keyMap.get(JMenuBar.class);

 283              if (v != null) {
 284                  Enumeration iter = v.elements();
 285                  while (iter.hasMoreElements()) {
 286                      JMenuBar mb = (JMenuBar)iter.nextElement();
 287                      if ( mb.isShowing() && mb.isEnabled() ) { // don't want to give these out
 288                          boolean extended = (ksE != null) && !ksE.equals(ks);
 289                          if (extended) {
 290                              fireBinding(mb, ksE, e, pressed);
 291                          }
 292                          if (!extended || !e.isConsumed()) {
 293                              fireBinding(mb, ks, e, pressed);
 294                          }
 295                          if (e.isConsumed()) {
 296                              return true;
 297                          }
 298                      }
 299                  }
 300              }
 301          }
 302 
 303          return e.isConsumed();
 304     }
 305 
 306     void fireBinding(JComponent c, KeyStroke ks, KeyEvent e, boolean pressed) {
 307         if (c.processKeyBinding(ks, e, JComponent.WHEN_IN_FOCUSED_WINDOW,
 308                                 pressed)) {
 309             e.consume();
 310         }
 311     }
 312 
 313     public void registerMenuBar(JMenuBar mb) {
 314         Container top = getTopAncestor(mb);
 315         if (top == null) {
 316             return;
 317         }
 318         Hashtable keyMap = containerMap.get(top);
 319 
 320         if (keyMap ==  null) {  // lazy evaluate one
 321              keyMap = registerNewTopContainer(top);
 322         }
 323         // use the menubar class as the key
 324         Vector menuBars = (Vector)keyMap.get(JMenuBar.class);

 325 
 326         if (menuBars == null) {  // if we don't have a list of menubars,
 327                                  // then make one.
 328             menuBars = new Vector();
 329             keyMap.put(JMenuBar.class, menuBars);
 330         }
 331 
 332         if (!menuBars.contains(mb)) {
 333             menuBars.addElement(mb);
 334         }
 335     }
 336 
 337 
 338     public void unregisterMenuBar(JMenuBar mb) {
 339         Container topContainer = getTopAncestor(mb);
 340         if (topContainer == null) {
 341             return;
 342         }
 343         Hashtable keyMap = containerMap.get(topContainer);
 344         if (keyMap!=null) {
 345             Vector v = (Vector)keyMap.get(JMenuBar.class);
 346             if (v != null) {
 347                 v.removeElement(mb);
 348                 if (v.isEmpty()) {
 349                     keyMap.remove(JMenuBar.class);
 350                     if (keyMap.isEmpty()) {
 351                         // remove table to enable GC
 352                         containerMap.remove(topContainer);
 353                     }
 354                 }
 355             }
 356         }
 357     }
 358     protected Hashtable registerNewTopContainer(Container topContainer) {
 359              Hashtable keyMap = new Hashtable();
 360              containerMap.put(topContainer, keyMap);
 361              return keyMap;
 362     }
 363 
 364     /**
 365       * This class is used to create keys for a hashtable
 366       * which looks up topContainers based on component, keystroke pairs
 367       * This is used to make unregistering KeyStrokes fast
 368       */
 369     class ComponentKeyStrokePair {
 370         Object component;
 371         Object keyStroke;
 372 
 373         public ComponentKeyStrokePair(Object comp, Object key) {
 374             component = comp;
 375             keyStroke = key;
 376         }
 377 
 378         public boolean equals(Object o) {
 379             if ( !(o instanceof ComponentKeyStrokePair)) {


  51   * WHEN_FOCUSED condition get a chance.  If none of these want the event, then the component
  52   * walks though it's parents looked for actions of type WHEN_ANCESTOR_OF_FOCUSED_COMPONENT.
  53   *
  54   * If no one has taken it yet, then it winds up here.  We then look for components registered
  55   * for WHEN_IN_FOCUSED_WINDOW events and fire to them.  Note that if none of those are found
  56   * then we pass the event to the menubars and let them have a crack at it.  They're handled differently.
  57   *
  58   * Lastly, we check if we're looking at an internal frame.  If we are and no one wanted the event
  59   * then we move up to the InternalFrame's creator and see if anyone wants the event (and so on and so on).
  60   *
  61   *
  62   * @see InputMap
  63   */
  64 class KeyboardManager {
  65 
  66     static KeyboardManager currentManager = new KeyboardManager();
  67 
  68     /**
  69       * maps top-level containers to a sub-hashtable full of keystrokes
  70       */
  71     Hashtable<Container, Hashtable<Object, Object>> containerMap = new Hashtable<>();
  72 
  73     /**
  74       * Maps component/keystroke pairs to a topLevel container
  75       * This is mainly used for fast unregister operations
  76       */
  77     Hashtable<ComponentKeyStrokePair, Container> componentKeyStrokeMap = new Hashtable<>();
  78 
  79     public static KeyboardManager getCurrentManager() {
  80         return currentManager;
  81     }
  82 
  83     public static void setCurrentManager(KeyboardManager km) {
  84         currentManager = km;
  85     }
  86 
  87     /**
  88       * register keystrokes here which are for the WHEN_IN_FOCUSED_WINDOW
  89       * case.
  90       * Other types of keystrokes will be handled by walking the hierarchy
  91       * That simplifies some potentially hairy stuff.
  92       */
  93      public void registerKeyStroke(KeyStroke k, JComponent c) {
  94          Container topContainer = getTopAncestor(c);
  95          if (topContainer == null) {
  96              return;
  97          }
  98          Hashtable<Object, Object> keyMap = containerMap.get(topContainer);
  99 
 100          if (keyMap ==  null) {  // lazy evaluate one
 101              keyMap = registerNewTopContainer(topContainer);
 102          }
 103 
 104          Object tmp = keyMap.get(k);
 105          if (tmp == null) {
 106              keyMap.put(k,c);
 107          } else if (tmp instanceof Vector) {  // if there's a Vector there then add to it.
 108              @SuppressWarnings("unchecked")
 109              Vector<Object> v = (Vector)tmp;
 110              if (!v.contains(c)) {  // only add if this keystroke isn't registered for this component
 111                  v.addElement(c);
 112              }
 113          } else if (tmp instanceof JComponent) {
 114            // if a JComponent is there then remove it and replace it with a vector
 115            // Then add the old compoennt and the new compoent to the vector
 116            // then insert the vector in the table
 117            if (tmp != c) {  // this means this is already registered for this component, no need to dup
 118                Vector<JComponent> v = new Vector<>();
 119                v.addElement((JComponent) tmp);
 120                v.addElement(c);
 121                keyMap.put(k, v);
 122            }
 123          } else {
 124              System.out.println("Unexpected condition in registerKeyStroke");
 125              Thread.dumpStack();
 126          }
 127 
 128          componentKeyStrokeMap.put(new ComponentKeyStrokePair(c,k), topContainer);
 129 
 130          // Check for EmbeddedFrame case, they know how to process accelerators even
 131          // when focus is not in Java
 132          if (topContainer instanceof EmbeddedFrame) {
 133              ((EmbeddedFrame)topContainer).registerAccelerator(k);
 134          }
 135      }
 136 
 137      /**
 138        * Find the top focusable Window, Applet, or InternalFrame


 144 
 145                 return p;
 146             }
 147         }
 148         return null;
 149      }
 150 
 151      public void unregisterKeyStroke(KeyStroke ks, JComponent c) {
 152 
 153        // component may have already been removed from the hierarchy, we
 154        // need to look up the container using the componentKeyStrokeMap.
 155 
 156          ComponentKeyStrokePair ckp = new ComponentKeyStrokePair(c,ks);
 157 
 158          Container topContainer = componentKeyStrokeMap.get(ckp);
 159 
 160          if (topContainer == null) {  // never heard of this pairing, so bail
 161              return;
 162          }
 163 
 164          Hashtable<Object, Object> keyMap = containerMap.get(topContainer);
 165          if  (keyMap == null) { // this should never happen, but I'm being safe
 166              Thread.dumpStack();
 167              return;
 168          }
 169 
 170          Object tmp = keyMap.get(ks);
 171          if (tmp == null) {  // this should never happen, but I'm being safe
 172              Thread.dumpStack();
 173              return;
 174          }
 175 
 176          if (tmp instanceof JComponent && tmp == c) {
 177              keyMap.remove(ks);  // remove the KeyStroke from the Map
 178              //System.out.println("removed a stroke" + ks);
 179          } else if (tmp instanceof Vector ) {  // this means there is more than one component reg for this key
 180              Vector<?> v = (Vector)tmp;
 181              v.removeElement(c);
 182              if ( v.isEmpty() ) {
 183                  keyMap.remove(ks);  // remove the KeyStroke from the Map
 184                  //System.out.println("removed a ks vector");
 185              }
 186          }
 187 
 188          if ( keyMap.isEmpty() ) {  // if no more bindings in this table
 189              containerMap.remove(topContainer);  // remove table to enable GC
 190              //System.out.println("removed a container");
 191          }
 192 
 193          componentKeyStrokeMap.remove(ckp);
 194 
 195          // Check for EmbeddedFrame case, they know how to process accelerators even
 196          // when focus is not in Java
 197          if (topContainer instanceof EmbeddedFrame) {
 198              ((EmbeddedFrame)topContainer).unregisterAccelerator(ks);
 199          }
 200      }


 211          if (e.isConsumed()) {
 212               System.out.println("Acquired pre-used event!");
 213               Thread.dumpStack();
 214          }
 215 
 216          // There may be two keystrokes associated with a low-level key event;
 217          // in this case a keystroke made of an extended key code has a priority.
 218          KeyStroke ks;
 219          KeyStroke ksE = null;
 220 
 221 
 222          if(e.getID() == KeyEvent.KEY_TYPED) {
 223                ks=KeyStroke.getKeyStroke(e.getKeyChar());
 224          } else {
 225                if(e.getKeyCode() != e.getExtendedKeyCode()) {
 226                    ksE=KeyStroke.getKeyStroke(e.getExtendedKeyCode(), e.getModifiers(), !pressed);
 227                }
 228                ks=KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers(), !pressed);
 229          }
 230 
 231          Hashtable<Object, Object> keyMap = containerMap.get(topAncestor);
 232          if (keyMap != null) { // this container isn't registered, so bail
 233 
 234              Object tmp = null;
 235              // extended code has priority
 236              if( ksE != null ) {
 237                  tmp = keyMap.get(ksE);
 238                  if( tmp != null ) {
 239                      ks = ksE;
 240                  }
 241              }
 242              if( tmp == null ) {
 243                  tmp = keyMap.get(ks);
 244              }
 245 
 246              if (tmp == null) {
 247                // don't do anything
 248              } else if ( tmp instanceof JComponent) {
 249                  JComponent c = (JComponent)tmp;
 250                  if ( c.isShowing() && c.isEnabled() ) { // only give it out if enabled and visible
 251                      fireBinding(c, ks, e, pressed);
 252                  }
 253              } else if ( tmp instanceof Vector) { //more than one comp registered for this
 254                  Vector<?> v = (Vector)tmp;
 255                  // There is no well defined order for WHEN_IN_FOCUSED_WINDOW
 256                  // bindings, but we give precedence to those bindings just
 257                  // added. This is done so that JMenus WHEN_IN_FOCUSED_WINDOW
 258                  // bindings are accessed before those of the JRootPane (they
 259                  // both have a WHEN_IN_FOCUSED_WINDOW binding for enter).
 260                  for (int counter = v.size() - 1; counter >= 0; counter--) {
 261                      JComponent c = (JComponent)v.elementAt(counter);
 262                      //System.out.println("Trying collision: " + c + " vector = "+ v.size());
 263                      if ( c.isShowing() && c.isEnabled() ) { // don't want to give these out
 264                          fireBinding(c, ks, e, pressed);
 265                          if (e.isConsumed())
 266                              return true;
 267                      }
 268                  }
 269              } else  {
 270                  System.out.println( "Unexpected condition in fireKeyboardAction " + tmp);
 271                  // This means that tmp wasn't null, a JComponent, or a Vector.  What is it?
 272                  Thread.dumpStack();
 273              }
 274          }
 275 
 276          if (e.isConsumed()) {
 277              return true;
 278          }
 279          // if no one else handled it, then give the menus a crack
 280          // The're handled differently.  The key is to let any JMenuBars
 281          // process the event
 282          if ( keyMap != null) {
 283              @SuppressWarnings("unchecked")
 284              Vector<JMenuBar> v = (Vector)keyMap.get(JMenuBar.class);
 285              if (v != null) {
 286                  Enumeration<JMenuBar> iter = v.elements();
 287                  while (iter.hasMoreElements()) {
 288                      JMenuBar mb = iter.nextElement();
 289                      if ( mb.isShowing() && mb.isEnabled() ) { // don't want to give these out
 290                          boolean extended = (ksE != null) && !ksE.equals(ks);
 291                          if (extended) {
 292                              fireBinding(mb, ksE, e, pressed);
 293                          }
 294                          if (!extended || !e.isConsumed()) {
 295                              fireBinding(mb, ks, e, pressed);
 296                          }
 297                          if (e.isConsumed()) {
 298                              return true;
 299                          }
 300                      }
 301                  }
 302              }
 303          }
 304 
 305          return e.isConsumed();
 306     }
 307 
 308     void fireBinding(JComponent c, KeyStroke ks, KeyEvent e, boolean pressed) {
 309         if (c.processKeyBinding(ks, e, JComponent.WHEN_IN_FOCUSED_WINDOW,
 310                                 pressed)) {
 311             e.consume();
 312         }
 313     }
 314 
 315     public void registerMenuBar(JMenuBar mb) {
 316         Container top = getTopAncestor(mb);
 317         if (top == null) {
 318             return;
 319         }
 320         Hashtable<Object, Object> keyMap = containerMap.get(top);
 321 
 322         if (keyMap ==  null) {  // lazy evaluate one
 323              keyMap = registerNewTopContainer(top);
 324         }
 325         // use the menubar class as the key
 326         @SuppressWarnings("unchecked")
 327         Vector<Object> menuBars = (Vector)keyMap.get(JMenuBar.class);
 328 
 329         if (menuBars == null) {  // if we don't have a list of menubars,
 330                                  // then make one.
 331             menuBars = new Vector<>();
 332             keyMap.put(JMenuBar.class, menuBars);
 333         }
 334 
 335         if (!menuBars.contains(mb)) {
 336             menuBars.addElement(mb);
 337         }
 338     }
 339 
 340 
 341     public void unregisterMenuBar(JMenuBar mb) {
 342         Container topContainer = getTopAncestor(mb);
 343         if (topContainer == null) {
 344             return;
 345         }
 346         Hashtable<Object, Object> keyMap = containerMap.get(topContainer);
 347         if (keyMap!=null) {
 348             Vector<?> v = (Vector)keyMap.get(JMenuBar.class);
 349             if (v != null) {
 350                 v.removeElement(mb);
 351                 if (v.isEmpty()) {
 352                     keyMap.remove(JMenuBar.class);
 353                     if (keyMap.isEmpty()) {
 354                         // remove table to enable GC
 355                         containerMap.remove(topContainer);
 356                     }
 357                 }
 358             }
 359         }
 360     }
 361     protected Hashtable<Object, Object> registerNewTopContainer(Container topContainer) {
 362              Hashtable<Object, Object> keyMap = new Hashtable<>();
 363              containerMap.put(topContainer, keyMap);
 364              return keyMap;
 365     }
 366 
 367     /**
 368       * This class is used to create keys for a hashtable
 369       * which looks up topContainers based on component, keystroke pairs
 370       * This is used to make unregistering KeyStrokes fast
 371       */
 372     class ComponentKeyStrokePair {
 373         Object component;
 374         Object keyStroke;
 375 
 376         public ComponentKeyStrokePair(Object comp, Object key) {
 377             component = comp;
 378             keyStroke = key;
 379         }
 380 
 381         public boolean equals(Object o) {
 382             if ( !(o instanceof ComponentKeyStrokePair)) {