< prev index next >

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

Print this page
rev 1527 : 6727662: Code improvement and warnings removing from swing packages
Summary: Removed unnecessary castings and other warnings
Reviewed-by: malenkov
rev 1566 : 6680988: KeyEvent is still missing VK values for many keyboards
Summary: 2 new methods and some fields added to KeyEvent, plus hash of constants introduced
Reviewed-by: art

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 1998, 2004, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2009, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -29,10 +29,11 @@
 import java.awt.*;
 import java.awt.event.*;
 import java.applet.*;
 import java.beans.*;
 import javax.swing.event.*;
+import sun.awt.AWTAccessor;
 import sun.awt.EmbeddedFrame;
 
 /**
   * The KeyboardManager class is used to help dispatch keyboard actions for the
   * WHEN_IN_FOCUSED_WINDOW style actions.  Actions with other conditions are handled

@@ -66,17 +67,17 @@
     static KeyboardManager currentManager = new KeyboardManager();
 
     /**
       * maps top-level containers to a sub-hashtable full of keystrokes
       */
-    Hashtable containerMap = new Hashtable();
+    Hashtable<Container, Hashtable> containerMap = new Hashtable<Container, Hashtable>();
 
     /**
       * Maps component/keystroke pairs to a topLevel container
       * This is mainly used for fast unregister operations
       */
-    Hashtable componentKeyStrokeMap = new Hashtable();
+    Hashtable<ComponentKeyStrokePair, Container> componentKeyStrokeMap = new Hashtable<ComponentKeyStrokePair, Container>();
 
     public static KeyboardManager getCurrentManager() {
         return currentManager;
     }
 

@@ -93,11 +94,11 @@
      public void registerKeyStroke(KeyStroke k, JComponent c) {
          Container topContainer = getTopAncestor(c);
          if (topContainer == null) {
              return;
          }
-         Hashtable keyMap = (Hashtable)containerMap.get(topContainer);
+         Hashtable keyMap = containerMap.get(topContainer);
 
          if (keyMap ==  null) {  // lazy evaluate one
              keyMap = registerNewTopContainer(topContainer);
          }
 

@@ -112,12 +113,12 @@
          } else if (tmp instanceof JComponent) {
            // if a JComponent is there then remove it and replace it with a vector
            // Then add the old compoennt and the new compoent to the vector
            // then insert the vector in the table
            if (tmp != c) {  // this means this is already registered for this component, no need to dup
-               Vector v = new Vector();
-               v.addElement(tmp);
+               Vector<JComponent> v = new Vector<JComponent>();
+               v.addElement((JComponent) tmp);
                v.addElement(c);
                keyMap.put(k, v);
            }
          } else {
              System.out.println("Unexpected condition in registerKeyStroke");

@@ -152,17 +153,17 @@
        // component may have already been removed from the hierarchy, we
        // need to look up the container using the componentKeyStrokeMap.
 
          ComponentKeyStrokePair ckp = new ComponentKeyStrokePair(c,ks);
 
-         Object topContainer = componentKeyStrokeMap.get(ckp);
+         Container topContainer = componentKeyStrokeMap.get(ckp);
 
          if (topContainer == null) {  // never heard of this pairing, so bail
              return;
          }
 
-         Hashtable keyMap = (Hashtable)containerMap.get(topContainer);
+         Hashtable keyMap = containerMap.get(topContainer);
          if  (keyMap == null) { // this should never happen, but I'm being safe
              Thread.dumpStack();
              return;
          }
 

@@ -210,23 +211,40 @@
          if (e.isConsumed()) {
               System.out.println("Aquired pre-used event!");
               Thread.dumpStack();
          }
 
+         // There may be two keystrokes associated with a low-level key event;
+         // in this case a keystroke made of an extended key code has a priority.
          KeyStroke ks;
+         KeyStroke ksE = null;
 
 
          if(e.getID() == KeyEvent.KEY_TYPED) {
                ks=KeyStroke.getKeyStroke(e.getKeyChar());
          } else {
+             int ekc = AWTAccessor.getKeyEventAccessor().getExtendedKeyCode(e);
+               if(e.getKeyCode() != ekc) {
+                   ksE=KeyStroke.getKeyStroke(ekc, e.getModifiers(), !pressed);
+               }
                ks=KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers(), !pressed);
          }
 
-         Hashtable keyMap = (Hashtable)containerMap.get(topAncestor);
+         Hashtable keyMap = containerMap.get(topAncestor);
          if (keyMap != null) { // this container isn't registered, so bail
 
-             Object tmp = keyMap.get(ks);
+             Object tmp = null;
+             // extended code has priority
+             if( ksE != null ) {
+                 tmp = keyMap.get(ksE);
+                 if( tmp != null ) {
+                     ks = ksE;
+                 }
+             }
+             if( tmp == null ) {
+                 tmp = keyMap.get(ks);
+             }
 
              if (tmp == null) {
                // don't do anything
              } else if ( tmp instanceof JComponent) {
                  JComponent c = (JComponent)tmp;

@@ -267,11 +285,16 @@
              if (v != null) {
                  Enumeration iter = v.elements();
                  while (iter.hasMoreElements()) {
                      JMenuBar mb = (JMenuBar)iter.nextElement();
                      if ( mb.isShowing() && mb.isEnabled() ) { // don't want to give these out
+                         if( !(ks.equals(ksE)) ) {
+                             fireBinding(mb, ksE, e, pressed);
+                         }
+                         if(ks.equals(ksE) || !e.isConsumed()) {
                          fireBinding(mb, ks, e, pressed);
+                         }
                          if (e.isConsumed()) {
                              return true;
                          }
                      }
                  }

@@ -291,11 +314,11 @@
     public void registerMenuBar(JMenuBar mb) {
         Container top = getTopAncestor(mb);
         if (top == null) {
             return;
         }
-        Hashtable keyMap = (Hashtable)containerMap.get(top);
+        Hashtable keyMap = containerMap.get(top);
 
         if (keyMap ==  null) {  // lazy evaluate one
              keyMap = registerNewTopContainer(top);
         }
         // use the menubar class as the key

@@ -312,15 +335,15 @@
         }
     }
 
 
     public void unregisterMenuBar(JMenuBar mb) {
-        Object topContainer = getTopAncestor(mb);
+        Container topContainer = getTopAncestor(mb);
         if (topContainer == null) {
             return;
         }
-        Hashtable keyMap = (Hashtable)containerMap.get(topContainer);
+        Hashtable keyMap = containerMap.get(topContainer);
         if (keyMap!=null) {
             Vector v = (Vector)keyMap.get(JMenuBar.class);
             if (v != null) {
                 v.removeElement(mb);
                 if (v.isEmpty()) {
< prev index next >