Changeset 3470 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2010-08-25T20:01:11+02:00 (14 years ago)
Author:
jttt
Message:

Fix #3278 redirect shortcuts in the undocked windows to the main window

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/ToggleDialog.java

    r3421 r3470  
    2424import javax.swing.AbstractAction;
    2525import javax.swing.BorderFactory;
     26import javax.swing.ComponentInputMap;
    2627import javax.swing.ImageIcon;
     28import javax.swing.InputMap;
    2729import javax.swing.JButton;
    2830import javax.swing.JComponent;
     
    3133import javax.swing.JOptionPane;
    3234import javax.swing.JPanel;
     35import javax.swing.KeyStroke;
    3336
    3437import org.openstreetmap.josm.Main;
     
    4649 */
    4750public class ToggleDialog extends JPanel implements Helpful {
     51
     52    // It's not possible to simply set component input map parent to be Main.contentPane.getInputMap because
     53    // there is check in setParent that InputMap is for the same component
     54    // Yes, this is a hack
     55    // Another possibility would be simply copy InputMap, but that would require to keep copies synchronized when some shortcut is
     56    // later
     57    private static class RedirectInputMap extends ComponentInputMap {
     58
     59        private final InputMap target;
     60
     61        public RedirectInputMap(JComponent component, InputMap target) {
     62            super(component);
     63            this.target = target;
     64        }
     65
     66        @Override
     67        public Object get(KeyStroke keyStroke) {
     68            return target.get(keyStroke);
     69        }
     70
     71        @Override
     72        public KeyStroke[] keys() {
     73            return target.keys();
     74        }
     75
     76        @Override
     77        public int size() {
     78            return target.size();
     79        }
     80
     81        @Override
     82        public KeyStroke[] allKeys() {
     83            return target.allKeys();
     84        }
     85
     86        @Override
     87        public void put(KeyStroke keyStroke, Object actionMapKey) {
     88            throw new UnsupportedOperationException();
     89        }
     90
     91        @Override
     92        public void remove(KeyStroke key) {
     93            throw new UnsupportedOperationException();
     94        }
     95
     96        @Override
     97        public void clear() {
     98            throw new UnsupportedOperationException();
     99        }
     100
     101    }
     102
    48103    /** The action to toggle this dialog */
    49104    protected ToggleDialogAction toggleAction;
     
    97152     * @param defShow if the dialog should be shown by default, if there is no preference
    98153     */
     154    @SuppressWarnings("deprecation")
    99155    public ToggleDialog(String name, String iconName, String tooltip, Shortcut shortcut, int preferredHeight, boolean defShow) {
    100156        super(new BorderLayout());
     
    123179        isDocked = Main.pref.getBoolean(preferencePrefix+".docked", true);
    124180        isCollapsed = Main.pref.getBoolean(preferencePrefix+".minimized", false);
     181
     182        InputMap lastParent = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
     183        while (lastParent.getParent() != null) {
     184            lastParent = lastParent.getParent();
     185        }
     186        lastParent.setParent(new RedirectInputMap(this, Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)));
     187        getActionMap().setParent(Main.contentPane.getActionMap());
    125188    }
    126189
Note: See TracChangeset for help on using the changeset viewer.