[8378] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[3502] | 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 |
|
---|
[12304] | 22 | /**
|
---|
| 23 | * Create a new {@link RedirectInputMap}
|
---|
| 24 | * @param component The component the input map will be added to
|
---|
| 25 | * @param target The target input map that should be mirrored.
|
---|
| 26 | */
|
---|
[3502] | 27 | public RedirectInputMap(JComponent component, InputMap target) {
|
---|
| 28 | super(component);
|
---|
| 29 | this.target = target;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | @Override
|
---|
| 33 | public Object get(KeyStroke keyStroke) {
|
---|
| 34 | return target.get(keyStroke);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | @Override
|
---|
| 38 | public KeyStroke[] keys() {
|
---|
| 39 | return target.keys();
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | @Override
|
---|
| 43 | public int size() {
|
---|
| 44 | return target.size();
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | @Override
|
---|
| 48 | public KeyStroke[] allKeys() {
|
---|
| 49 | return target.allKeys();
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | @Override
|
---|
| 53 | public void put(KeyStroke keyStroke, Object actionMapKey) {
|
---|
| 54 | throw new UnsupportedOperationException();
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | @Override
|
---|
| 58 | public void remove(KeyStroke key) {
|
---|
| 59 | throw new UnsupportedOperationException();
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | @Override
|
---|
| 63 | public void clear() {
|
---|
| 64 | throw new UnsupportedOperationException();
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[12304] | 67 | /**
|
---|
| 68 | * Redirects the key inputs from one component to an other component
|
---|
| 69 | * @param source The source component
|
---|
| 70 | * @param target The target component to send the keystrokes to.
|
---|
| 71 | */
|
---|
[3502] | 72 | public static void redirect(JComponent source, JComponent target) {
|
---|
| 73 | InputMap lastParent = source.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
|
---|
| 74 | while (lastParent.getParent() != null) {
|
---|
| 75 | lastParent = lastParent.getParent();
|
---|
| 76 | }
|
---|
| 77 | lastParent.setParent(new RedirectInputMap(source, target.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)));
|
---|
| 78 | source.getActionMap().setParent(target.getActionMap());
|
---|
| 79 | }
|
---|
| 80 | }
|
---|