1 | // License: GPL. See LICENSE file for details.
|
---|
2 | package org.openstreetmap.josm.gui.util;
|
---|
3 |
|
---|
4 | import javax.swing.ComponentInputMap;
|
---|
5 | import javax.swing.InputMap;
|
---|
6 | import javax.swing.JComponent;
|
---|
7 | import 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 | */
|
---|
18 | public 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 | }
|
---|