source: josm/trunk/src/org/openstreetmap/josm/gui/util/RedirectInputMap.java@ 12225

Last change on this file since 12225 was 8378, checked in by Don-vip, 9 years ago

fix copyright/license headers globally

  • Property svn:eol-style set to native
File size: 2.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.util;
3
4import javax.swing.ComponentInputMap;
5import javax.swing.InputMap;
6import javax.swing.JComponent;
7import javax.swing.KeyStroke;
8
9/**
10 * Make shortcuts from main window work in dialog windows.
11 *
12 * It's not possible to simply set component input map parent to be Main.contentPane.getInputMap
13 * because there is check in setParent that InputMap is for the same component.
14 * Yes, this is a hack.
15 * Another possibility would be simply copy InputMap, but that would require to
16 * keep copies synchronized when some shortcuts are changed later.
17 */
18public class RedirectInputMap extends ComponentInputMap {
19
20 private final InputMap target;
21
22 public RedirectInputMap(JComponent component, InputMap target) {
23 super(component);
24 this.target = target;
25 }
26
27 @Override
28 public Object get(KeyStroke keyStroke) {
29 return target.get(keyStroke);
30 }
31
32 @Override
33 public KeyStroke[] keys() {
34 return target.keys();
35 }
36
37 @Override
38 public int size() {
39 return target.size();
40 }
41
42 @Override
43 public KeyStroke[] allKeys() {
44 return target.allKeys();
45 }
46
47 @Override
48 public void put(KeyStroke keyStroke, Object actionMapKey) {
49 throw new UnsupportedOperationException();
50 }
51
52 @Override
53 public void remove(KeyStroke key) {
54 throw new UnsupportedOperationException();
55 }
56
57 @Override
58 public void clear() {
59 throw new UnsupportedOperationException();
60 }
61
62 public static void redirect(JComponent source, JComponent target) {
63 InputMap lastParent = source.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
64 while (lastParent.getParent() != null) {
65 lastParent = lastParent.getParent();
66 }
67 lastParent.setParent(new RedirectInputMap(source, target.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)));
68 source.getActionMap().setParent(target.getActionMap());
69 }
70}
Note: See TracBrowser for help on using the repository browser.