src/share/classes/javax/swing/KeyboardManager.java
Print this page
*** 66,82 ****
static KeyboardManager currentManager = new KeyboardManager();
/**
* maps top-level containers to a sub-hashtable full of keystrokes
*/
! 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<ComponentKeyStrokePair, Container> componentKeyStrokeMap = new Hashtable<ComponentKeyStrokePair, Container>();
public static KeyboardManager getCurrentManager() {
return currentManager;
}
--- 66,82 ----
static KeyboardManager currentManager = new KeyboardManager();
/**
* maps top-level containers to a sub-hashtable full of keystrokes
*/
! Hashtable<Container, Hashtable<Object, Object>> containerMap = new Hashtable<>();
/**
* Maps component/keystroke pairs to a topLevel container
* This is mainly used for fast unregister operations
*/
! Hashtable<ComponentKeyStrokePair, Container> componentKeyStrokeMap = new Hashtable<>();
public static KeyboardManager getCurrentManager() {
return currentManager;
}
*** 93,122 ****
public void registerKeyStroke(KeyStroke k, JComponent c) {
Container topContainer = getTopAncestor(c);
if (topContainer == null) {
return;
}
! Hashtable keyMap = containerMap.get(topContainer);
if (keyMap == null) { // lazy evaluate one
keyMap = registerNewTopContainer(topContainer);
}
Object tmp = keyMap.get(k);
if (tmp == null) {
keyMap.put(k,c);
} else if (tmp instanceof Vector) { // if there's a Vector there then add to it.
! Vector v = (Vector)tmp;
if (!v.contains(c)) { // only add if this keystroke isn't registered for this component
v.addElement(c);
}
} 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<JComponent> v = new Vector<JComponent>();
v.addElement((JComponent) tmp);
v.addElement(c);
keyMap.put(k, v);
}
} else {
--- 93,123 ----
public void registerKeyStroke(KeyStroke k, JComponent c) {
Container topContainer = getTopAncestor(c);
if (topContainer == null) {
return;
}
! Hashtable<Object, Object> keyMap = containerMap.get(topContainer);
if (keyMap == null) { // lazy evaluate one
keyMap = registerNewTopContainer(topContainer);
}
Object tmp = keyMap.get(k);
if (tmp == null) {
keyMap.put(k,c);
} else if (tmp instanceof Vector) { // if there's a Vector there then add to it.
! @SuppressWarnings("unchecked")
! Vector<Object> v = (Vector)tmp;
if (!v.contains(c)) { // only add if this keystroke isn't registered for this component
v.addElement(c);
}
} 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<JComponent> v = new Vector<>();
v.addElement((JComponent) tmp);
v.addElement(c);
keyMap.put(k, v);
}
} else {
*** 158,168 ****
if (topContainer == null) { // never heard of this pairing, so bail
return;
}
! Hashtable keyMap = containerMap.get(topContainer);
if (keyMap == null) { // this should never happen, but I'm being safe
Thread.dumpStack();
return;
}
--- 159,169 ----
if (topContainer == null) { // never heard of this pairing, so bail
return;
}
! Hashtable<Object, Object> keyMap = containerMap.get(topContainer);
if (keyMap == null) { // this should never happen, but I'm being safe
Thread.dumpStack();
return;
}
*** 174,184 ****
if (tmp instanceof JComponent && tmp == c) {
keyMap.remove(ks); // remove the KeyStroke from the Map
//System.out.println("removed a stroke" + ks);
} else if (tmp instanceof Vector ) { // this means there is more than one component reg for this key
! Vector v = (Vector)tmp;
v.removeElement(c);
if ( v.isEmpty() ) {
keyMap.remove(ks); // remove the KeyStroke from the Map
//System.out.println("removed a ks vector");
}
--- 175,185 ----
if (tmp instanceof JComponent && tmp == c) {
keyMap.remove(ks); // remove the KeyStroke from the Map
//System.out.println("removed a stroke" + ks);
} else if (tmp instanceof Vector ) { // this means there is more than one component reg for this key
! Vector<?> v = (Vector)tmp;
v.removeElement(c);
if ( v.isEmpty() ) {
keyMap.remove(ks); // remove the KeyStroke from the Map
//System.out.println("removed a ks vector");
}
*** 225,235 ****
ksE=KeyStroke.getKeyStroke(e.getExtendedKeyCode(), e.getModifiers(), !pressed);
}
ks=KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers(), !pressed);
}
! Hashtable keyMap = containerMap.get(topAncestor);
if (keyMap != null) { // this container isn't registered, so bail
Object tmp = null;
// extended code has priority
if( ksE != null ) {
--- 226,236 ----
ksE=KeyStroke.getKeyStroke(e.getExtendedKeyCode(), e.getModifiers(), !pressed);
}
ks=KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers(), !pressed);
}
! Hashtable<Object, Object> keyMap = containerMap.get(topAncestor);
if (keyMap != null) { // this container isn't registered, so bail
Object tmp = null;
// extended code has priority
if( ksE != null ) {
*** 248,258 ****
JComponent c = (JComponent)tmp;
if ( c.isShowing() && c.isEnabled() ) { // only give it out if enabled and visible
fireBinding(c, ks, e, pressed);
}
} else if ( tmp instanceof Vector) { //more than one comp registered for this
! Vector v = (Vector)tmp;
// There is no well defined order for WHEN_IN_FOCUSED_WINDOW
// bindings, but we give precedence to those bindings just
// added. This is done so that JMenus WHEN_IN_FOCUSED_WINDOW
// bindings are accessed before those of the JRootPane (they
// both have a WHEN_IN_FOCUSED_WINDOW binding for enter).
--- 249,259 ----
JComponent c = (JComponent)tmp;
if ( c.isShowing() && c.isEnabled() ) { // only give it out if enabled and visible
fireBinding(c, ks, e, pressed);
}
} else if ( tmp instanceof Vector) { //more than one comp registered for this
! Vector<?> v = (Vector)tmp;
// There is no well defined order for WHEN_IN_FOCUSED_WINDOW
// bindings, but we give precedence to those bindings just
// added. This is done so that JMenus WHEN_IN_FOCUSED_WINDOW
// bindings are accessed before those of the JRootPane (they
// both have a WHEN_IN_FOCUSED_WINDOW binding for enter).
*** 277,291 ****
}
// if no one else handled it, then give the menus a crack
// The're handled differently. The key is to let any JMenuBars
// process the event
if ( keyMap != null) {
! Vector v = (Vector)keyMap.get(JMenuBar.class);
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
boolean extended = (ksE != null) && !ksE.equals(ks);
if (extended) {
fireBinding(mb, ksE, e, pressed);
}
--- 278,293 ----
}
// if no one else handled it, then give the menus a crack
// The're handled differently. The key is to let any JMenuBars
// process the event
if ( keyMap != null) {
! @SuppressWarnings("unchecked")
! Vector<JMenuBar> v = (Vector)keyMap.get(JMenuBar.class);
if (v != null) {
! Enumeration<JMenuBar> iter = v.elements();
while (iter.hasMoreElements()) {
! JMenuBar mb = iter.nextElement();
if ( mb.isShowing() && mb.isEnabled() ) { // don't want to give these out
boolean extended = (ksE != null) && !ksE.equals(ks);
if (extended) {
fireBinding(mb, ksE, e, pressed);
}
*** 313,333 ****
public void registerMenuBar(JMenuBar mb) {
Container top = getTopAncestor(mb);
if (top == null) {
return;
}
! Hashtable keyMap = containerMap.get(top);
if (keyMap == null) { // lazy evaluate one
keyMap = registerNewTopContainer(top);
}
// use the menubar class as the key
! Vector menuBars = (Vector)keyMap.get(JMenuBar.class);
if (menuBars == null) { // if we don't have a list of menubars,
// then make one.
! menuBars = new Vector();
keyMap.put(JMenuBar.class, menuBars);
}
if (!menuBars.contains(mb)) {
menuBars.addElement(mb);
--- 315,336 ----
public void registerMenuBar(JMenuBar mb) {
Container top = getTopAncestor(mb);
if (top == null) {
return;
}
! Hashtable<Object, Object> keyMap = containerMap.get(top);
if (keyMap == null) { // lazy evaluate one
keyMap = registerNewTopContainer(top);
}
// use the menubar class as the key
! @SuppressWarnings("unchecked")
! Vector<Object> menuBars = (Vector)keyMap.get(JMenuBar.class);
if (menuBars == null) { // if we don't have a list of menubars,
// then make one.
! menuBars = new Vector<>();
keyMap.put(JMenuBar.class, menuBars);
}
if (!menuBars.contains(mb)) {
menuBars.addElement(mb);
*** 338,350 ****
public void unregisterMenuBar(JMenuBar mb) {
Container topContainer = getTopAncestor(mb);
if (topContainer == null) {
return;
}
! Hashtable keyMap = containerMap.get(topContainer);
if (keyMap!=null) {
! Vector v = (Vector)keyMap.get(JMenuBar.class);
if (v != null) {
v.removeElement(mb);
if (v.isEmpty()) {
keyMap.remove(JMenuBar.class);
if (keyMap.isEmpty()) {
--- 341,353 ----
public void unregisterMenuBar(JMenuBar mb) {
Container topContainer = getTopAncestor(mb);
if (topContainer == null) {
return;
}
! Hashtable<Object, Object> keyMap = containerMap.get(topContainer);
if (keyMap!=null) {
! Vector<?> v = (Vector)keyMap.get(JMenuBar.class);
if (v != null) {
v.removeElement(mb);
if (v.isEmpty()) {
keyMap.remove(JMenuBar.class);
if (keyMap.isEmpty()) {
*** 353,364 ****
}
}
}
}
}
! protected Hashtable registerNewTopContainer(Container topContainer) {
! Hashtable keyMap = new Hashtable();
containerMap.put(topContainer, keyMap);
return keyMap;
}
/**
--- 356,367 ----
}
}
}
}
}
! protected Hashtable<Object, Object> registerNewTopContainer(Container topContainer) {
! Hashtable<Object, Object> keyMap = new Hashtable<>();
containerMap.put(topContainer, keyMap);
return keyMap;
}
/**