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 | import org.openstreetmap.josm.Main;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Make shortcuts from main window work in dialog windows.
|
---|
13 | *
|
---|
14 | * It's not possible to simply set component input map parent to be Main.contentPane.getInputMap
|
---|
15 | * because there is check in setParent that InputMap is for the same component.
|
---|
16 | * Yes, this is a hack.
|
---|
17 | * Another possibility would be simply copy InputMap, but that would require to
|
---|
18 | * keep copies synchronized when some shortcuts are changed later.
|
---|
19 | */
|
---|
20 | public class RedirectInputMap extends ComponentInputMap {
|
---|
21 |
|
---|
22 | private final InputMap target;
|
---|
23 |
|
---|
24 | public RedirectInputMap(JComponent component, InputMap target) {
|
---|
25 | super(component);
|
---|
26 | this.target = target;
|
---|
27 | }
|
---|
28 |
|
---|
29 | @Override
|
---|
30 | public Object get(KeyStroke keyStroke) {
|
---|
31 | return target.get(keyStroke);
|
---|
32 | }
|
---|
33 |
|
---|
34 | @Override
|
---|
35 | public KeyStroke[] keys() {
|
---|
36 | return target.keys();
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public int size() {
|
---|
41 | return target.size();
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | public KeyStroke[] allKeys() {
|
---|
46 | return target.allKeys();
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public void put(KeyStroke keyStroke, Object actionMapKey) {
|
---|
51 | throw new UnsupportedOperationException();
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public void remove(KeyStroke key) {
|
---|
56 | throw new UnsupportedOperationException();
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | public void clear() {
|
---|
61 | throw new UnsupportedOperationException();
|
---|
62 | }
|
---|
63 |
|
---|
64 | public static void redirectToMainContentPane(JComponent source) {
|
---|
65 | @SuppressWarnings("deprecation") JComponent target = Main.contentPane;
|
---|
66 | redirect(source, target);
|
---|
67 | }
|
---|
68 |
|
---|
69 | public static void redirect(JComponent source, JComponent target) {
|
---|
70 | InputMap lastParent = source.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
|
---|
71 | while (lastParent.getParent() != null) {
|
---|
72 | lastParent = lastParent.getParent();
|
---|
73 | }
|
---|
74 | lastParent.setParent(new RedirectInputMap(source, target.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)));
|
---|
75 | source.getActionMap().setParent(target.getActionMap());
|
---|
76 | }
|
---|
77 | }
|
---|