66 static KeyboardManager currentManager = new KeyboardManager(); |
66 static KeyboardManager currentManager = new KeyboardManager(); |
67 |
67 |
68 /** |
68 /** |
69 * maps top-level containers to a sub-hashtable full of keystrokes |
69 * maps top-level containers to a sub-hashtable full of keystrokes |
70 */ |
70 */ |
71 Hashtable<Container, Hashtable> containerMap = new Hashtable<Container, Hashtable>(); |
71 Hashtable<Container, Hashtable<Object, Object>> containerMap = new Hashtable<>(); |
72 |
72 |
73 /** |
73 /** |
74 * Maps component/keystroke pairs to a topLevel container |
74 * Maps component/keystroke pairs to a topLevel container |
75 * This is mainly used for fast unregister operations |
75 * This is mainly used for fast unregister operations |
76 */ |
76 */ |
77 Hashtable<ComponentKeyStrokePair, Container> componentKeyStrokeMap = new Hashtable<ComponentKeyStrokePair, Container>(); |
77 Hashtable<ComponentKeyStrokePair, Container> componentKeyStrokeMap = new Hashtable<>(); |
78 |
78 |
79 public static KeyboardManager getCurrentManager() { |
79 public static KeyboardManager getCurrentManager() { |
80 return currentManager; |
80 return currentManager; |
81 } |
81 } |
82 |
82 |
93 public void registerKeyStroke(KeyStroke k, JComponent c) { |
93 public void registerKeyStroke(KeyStroke k, JComponent c) { |
94 Container topContainer = getTopAncestor(c); |
94 Container topContainer = getTopAncestor(c); |
95 if (topContainer == null) { |
95 if (topContainer == null) { |
96 return; |
96 return; |
97 } |
97 } |
98 Hashtable keyMap = containerMap.get(topContainer); |
98 Hashtable<Object, Object> keyMap = containerMap.get(topContainer); |
99 |
99 |
100 if (keyMap == null) { // lazy evaluate one |
100 if (keyMap == null) { // lazy evaluate one |
101 keyMap = registerNewTopContainer(topContainer); |
101 keyMap = registerNewTopContainer(topContainer); |
102 } |
102 } |
103 |
103 |
104 Object tmp = keyMap.get(k); |
104 Object tmp = keyMap.get(k); |
105 if (tmp == null) { |
105 if (tmp == null) { |
106 keyMap.put(k,c); |
106 keyMap.put(k,c); |
107 } else if (tmp instanceof Vector) { // if there's a Vector there then add to it. |
107 } else if (tmp instanceof Vector) { // if there's a Vector there then add to it. |
108 Vector v = (Vector)tmp; |
108 @SuppressWarnings("unchecked") |
|
109 Vector<Object> v = (Vector)tmp; |
109 if (!v.contains(c)) { // only add if this keystroke isn't registered for this component |
110 if (!v.contains(c)) { // only add if this keystroke isn't registered for this component |
110 v.addElement(c); |
111 v.addElement(c); |
111 } |
112 } |
112 } else if (tmp instanceof JComponent) { |
113 } else if (tmp instanceof JComponent) { |
113 // if a JComponent is there then remove it and replace it with a vector |
114 // 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 add the old compoennt and the new compoent to the vector |
115 // then insert the vector in the table |
116 // 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 if (tmp != c) { // this means this is already registered for this component, no need to dup |
117 Vector<JComponent> v = new Vector<JComponent>(); |
118 Vector<JComponent> v = new Vector<>(); |
118 v.addElement((JComponent) tmp); |
119 v.addElement((JComponent) tmp); |
119 v.addElement(c); |
120 v.addElement(c); |
120 keyMap.put(k, v); |
121 keyMap.put(k, v); |
121 } |
122 } |
122 } else { |
123 } else { |
174 |
175 |
175 if (tmp instanceof JComponent && tmp == c) { |
176 if (tmp instanceof JComponent && tmp == c) { |
176 keyMap.remove(ks); // remove the KeyStroke from the Map |
177 keyMap.remove(ks); // remove the KeyStroke from the Map |
177 //System.out.println("removed a stroke" + ks); |
178 //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 } else if (tmp instanceof Vector ) { // this means there is more than one component reg for this key |
179 Vector v = (Vector)tmp; |
180 Vector<?> v = (Vector)tmp; |
180 v.removeElement(c); |
181 v.removeElement(c); |
181 if ( v.isEmpty() ) { |
182 if ( v.isEmpty() ) { |
182 keyMap.remove(ks); // remove the KeyStroke from the Map |
183 keyMap.remove(ks); // remove the KeyStroke from the Map |
183 //System.out.println("removed a ks vector"); |
184 //System.out.println("removed a ks vector"); |
184 } |
185 } |
225 ksE=KeyStroke.getKeyStroke(e.getExtendedKeyCode(), e.getModifiers(), !pressed); |
226 ksE=KeyStroke.getKeyStroke(e.getExtendedKeyCode(), e.getModifiers(), !pressed); |
226 } |
227 } |
227 ks=KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers(), !pressed); |
228 ks=KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers(), !pressed); |
228 } |
229 } |
229 |
230 |
230 Hashtable keyMap = containerMap.get(topAncestor); |
231 Hashtable<Object, Object> keyMap = containerMap.get(topAncestor); |
231 if (keyMap != null) { // this container isn't registered, so bail |
232 if (keyMap != null) { // this container isn't registered, so bail |
232 |
233 |
233 Object tmp = null; |
234 Object tmp = null; |
234 // extended code has priority |
235 // extended code has priority |
235 if( ksE != null ) { |
236 if( ksE != null ) { |
248 JComponent c = (JComponent)tmp; |
249 JComponent c = (JComponent)tmp; |
249 if ( c.isShowing() && c.isEnabled() ) { // only give it out if enabled and visible |
250 if ( c.isShowing() && c.isEnabled() ) { // only give it out if enabled and visible |
250 fireBinding(c, ks, e, pressed); |
251 fireBinding(c, ks, e, pressed); |
251 } |
252 } |
252 } else if ( tmp instanceof Vector) { //more than one comp registered for this |
253 } else if ( tmp instanceof Vector) { //more than one comp registered for this |
253 Vector v = (Vector)tmp; |
254 Vector<?> v = (Vector)tmp; |
254 // There is no well defined order for WHEN_IN_FOCUSED_WINDOW |
255 // There is no well defined order for WHEN_IN_FOCUSED_WINDOW |
255 // bindings, but we give precedence to those bindings just |
256 // bindings, but we give precedence to those bindings just |
256 // added. This is done so that JMenus WHEN_IN_FOCUSED_WINDOW |
257 // added. This is done so that JMenus WHEN_IN_FOCUSED_WINDOW |
257 // bindings are accessed before those of the JRootPane (they |
258 // bindings are accessed before those of the JRootPane (they |
258 // both have a WHEN_IN_FOCUSED_WINDOW binding for enter). |
259 // both have a WHEN_IN_FOCUSED_WINDOW binding for enter). |
277 } |
278 } |
278 // if no one else handled it, then give the menus a crack |
279 // 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 // The're handled differently. The key is to let any JMenuBars |
280 // process the event |
281 // process the event |
281 if ( keyMap != null) { |
282 if ( keyMap != null) { |
282 Vector v = (Vector)keyMap.get(JMenuBar.class); |
283 @SuppressWarnings("unchecked") |
|
284 Vector<JMenuBar> v = (Vector)keyMap.get(JMenuBar.class); |
283 if (v != null) { |
285 if (v != null) { |
284 Enumeration iter = v.elements(); |
286 Enumeration<JMenuBar> iter = v.elements(); |
285 while (iter.hasMoreElements()) { |
287 while (iter.hasMoreElements()) { |
286 JMenuBar mb = (JMenuBar)iter.nextElement(); |
288 JMenuBar mb = iter.nextElement(); |
287 if ( mb.isShowing() && mb.isEnabled() ) { // don't want to give these out |
289 if ( mb.isShowing() && mb.isEnabled() ) { // don't want to give these out |
288 boolean extended = (ksE != null) && !ksE.equals(ks); |
290 boolean extended = (ksE != null) && !ksE.equals(ks); |
289 if (extended) { |
291 if (extended) { |
290 fireBinding(mb, ksE, e, pressed); |
292 fireBinding(mb, ksE, e, pressed); |
291 } |
293 } |
313 public void registerMenuBar(JMenuBar mb) { |
315 public void registerMenuBar(JMenuBar mb) { |
314 Container top = getTopAncestor(mb); |
316 Container top = getTopAncestor(mb); |
315 if (top == null) { |
317 if (top == null) { |
316 return; |
318 return; |
317 } |
319 } |
318 Hashtable keyMap = containerMap.get(top); |
320 Hashtable<Object, Object> keyMap = containerMap.get(top); |
319 |
321 |
320 if (keyMap == null) { // lazy evaluate one |
322 if (keyMap == null) { // lazy evaluate one |
321 keyMap = registerNewTopContainer(top); |
323 keyMap = registerNewTopContainer(top); |
322 } |
324 } |
323 // use the menubar class as the key |
325 // use the menubar class as the key |
324 Vector menuBars = (Vector)keyMap.get(JMenuBar.class); |
326 @SuppressWarnings("unchecked") |
|
327 Vector<Object> menuBars = (Vector)keyMap.get(JMenuBar.class); |
325 |
328 |
326 if (menuBars == null) { // if we don't have a list of menubars, |
329 if (menuBars == null) { // if we don't have a list of menubars, |
327 // then make one. |
330 // then make one. |
328 menuBars = new Vector(); |
331 menuBars = new Vector<>(); |
329 keyMap.put(JMenuBar.class, menuBars); |
332 keyMap.put(JMenuBar.class, menuBars); |
330 } |
333 } |
331 |
334 |
332 if (!menuBars.contains(mb)) { |
335 if (!menuBars.contains(mb)) { |
333 menuBars.addElement(mb); |
336 menuBars.addElement(mb); |
338 public void unregisterMenuBar(JMenuBar mb) { |
341 public void unregisterMenuBar(JMenuBar mb) { |
339 Container topContainer = getTopAncestor(mb); |
342 Container topContainer = getTopAncestor(mb); |
340 if (topContainer == null) { |
343 if (topContainer == null) { |
341 return; |
344 return; |
342 } |
345 } |
343 Hashtable keyMap = containerMap.get(topContainer); |
346 Hashtable<Object, Object> keyMap = containerMap.get(topContainer); |
344 if (keyMap!=null) { |
347 if (keyMap!=null) { |
345 Vector v = (Vector)keyMap.get(JMenuBar.class); |
348 Vector<?> v = (Vector)keyMap.get(JMenuBar.class); |
346 if (v != null) { |
349 if (v != null) { |
347 v.removeElement(mb); |
350 v.removeElement(mb); |
348 if (v.isEmpty()) { |
351 if (v.isEmpty()) { |
349 keyMap.remove(JMenuBar.class); |
352 keyMap.remove(JMenuBar.class); |
350 if (keyMap.isEmpty()) { |
353 if (keyMap.isEmpty()) { |