Changeset 3252 in josm


Ignore:
Timestamp:
2010-05-15T18:59:10+02:00 (15 years ago)
Author:
jttt
Message:

Fix #2234: Translation can cause JosmActions to illegally handle shortcuts

Location:
trunk/src/org/openstreetmap/josm
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/Main.java

    r3071 r3252  
    2727import java.util.regex.Pattern;
    2828
     29import javax.swing.Action;
     30import javax.swing.InputMap;
    2931import javax.swing.JComponent;
    3032import javax.swing.JFrame;
    3133import javax.swing.JOptionPane;
    3234import javax.swing.JPanel;
     35import javax.swing.KeyStroke;
    3336import javax.swing.UIManager;
    3437
     
    198201        isOpenjdk = System.getProperty("java.vm.name").toUpperCase().indexOf("OPENJDK") != -1;
    199202        platform.startupHook();
    200         contentPane.add(panel, BorderLayout.CENTER);
     203        contentPanePrivate.add(panel, BorderLayout.CENTER);
    201204        panel.add(gettingStarted, BorderLayout.CENTER);
    202205        menu = new MainMenu();
     
    205208
    206209        // creating toolbar
    207         contentPane.add(toolbar.control, BorderLayout.NORTH);
    208 
    209         contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
    210         .put(Shortcut.registerShortcut("system:help", tr("Help"),
    211                 KeyEvent.VK_F1, Shortcut.GROUP_DIRECT).getKeyStroke(), "Help");
    212         contentPane.getActionMap().put("Help", menu.help);
     210        contentPanePrivate.add(toolbar.control, BorderLayout.NORTH);
     211
     212        registerActionShortcut(menu.help, Shortcut.registerShortcut("system:help", tr("Help"),
     213                KeyEvent.VK_F1, Shortcut.GROUP_DIRECT));
    213214
    214215        TaggingPresetPreference.initialize();
     
    218219
    219220        toolbar.control.updateUI();
    220         contentPane.updateUI();
     221        contentPanePrivate.updateUI();
    221222    }
    222223
     
    226227    public final void addLayer(final Layer layer) {
    227228        if (map == null) {
    228             final MapFrame mapFrame = new MapFrame();
     229            final MapFrame mapFrame = new MapFrame(contentPanePrivate);
    229230            setMapFrame(mapFrame);
    230231            mapFrame.selectMapMode((MapMode)mapFrame.getDefaultButtonAction());
     
    281282    }
    282283
    283     /**
    284      * Use this to register shortcuts to
    285      */
    286     public static final JPanel contentPane = new JPanel(new BorderLayout());
     284    protected static JPanel contentPanePrivate = new JPanel(new BorderLayout());
     285
     286    /**
     287     * @deprecated If you just need to register shortcut for action, use registerActionShortcut instead of accessing InputMap directly
     288     */
     289    @Deprecated
     290    public static final JPanel contentPane = contentPanePrivate;
     291
     292    public static void registerActionShortcut(Action action, Shortcut shortcut) {
     293        registerActionShortcut(action, shortcut.getKeyStroke());
     294    }
     295
     296    public static void registerActionShortcut(Action action, KeyStroke keyStroke) {
     297        if (keyStroke == null)
     298            return;
     299
     300        InputMap inputMap = contentPanePrivate.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
     301        Object existing = inputMap.get(keyStroke);
     302        if (existing != null && !existing.equals(action)) {
     303            System.out.println(String.format("Keystroke is already assigned to %s, will be overridden by %s", existing, action));
     304        }
     305        inputMap.put(keyStroke, action);
     306
     307        contentPanePrivate.getActionMap().put(action, action);
     308    }
     309
     310    public static void unregisterActionShortcut(Shortcut shortcut) {
     311        contentPanePrivate.getInputMap().remove(shortcut.getKeyStroke());
     312    }
    287313
    288314    ///////////////////////////////////////////////////////////////////////////
     
    324350            }
    325351            toolbar = new ToolbarPreferences();
    326             contentPane.updateUI();
     352            contentPanePrivate.updateUI();
    327353            panel.updateUI();
    328354        } catch (final Exception e) {
  • trunk/src/org/openstreetmap/josm/actions/JosmAction.java

    r3244 r3252  
    77
    88import javax.swing.AbstractAction;
    9 import javax.swing.JComponent;
    109
    1110import org.openstreetmap.josm.Main;
     
    7271        sc = shortcut;
    7372        if (sc != null) {
    74             Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), name);
    75             Main.contentPane.getActionMap().put(name, this);
     73            Main.registerActionShortcut(this, sc);
    7674        }
    7775        putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tooltip, sc));
     
    9088    public void destroy() {
    9189        if (sc != null) {
    92             Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).remove(sc.getKeyStroke());
    93             Main.contentPane.getActionMap().remove(sc.getKeyStroke());
     90            Main.unregisterActionShortcut(sc);
    9491        }
    9592        MapView.removeLayerChangeListener(layerChangeAdapter);
  • trunk/src/org/openstreetmap/josm/actions/UnselectAllAction.java

    r2512 r3252  
    22package org.openstreetmap.josm.actions;
    33
     4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
    45import static org.openstreetmap.josm.tools.I18n.tr;
    5 import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
    66
    77import java.awt.event.ActionEvent;
    88import java.awt.event.KeyEvent;
    9 
    10 import javax.swing.JComponent;
    119
    1210import org.openstreetmap.josm.Main;
     
    2220
    2321        // Add extra shortcut C-S-a
    24         Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
    25                 Shortcut.registerShortcut("edit:unselectallfocus", tr("Edit: {0}", tr("Unselect All (Focus)")),
    26                         KeyEvent.VK_A, Shortcut.GROUP_MENU, Shortcut.SHIFT_DEFAULT).getKeyStroke(),
    27                         tr("Unselect All"));
     22        Main.registerActionShortcut(this, Shortcut.registerShortcut("edit:unselectallfocus", tr("Edit: {0}", tr("Unselect All (Focus)")),
     23                KeyEvent.VK_A, Shortcut.GROUP_MENU, Shortcut.SHIFT_DEFAULT));
     24
     25
    2826
    2927        // Add extra shortcut ESCAPE
     
    3331         * for now this is a reasonable approximation.
    3432         */
    35         Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
    36                 Shortcut.registerShortcut("edit:unselectallescape", tr("Edit: {0}", tr("Unselect All (Escape)")),
    37                         KeyEvent.VK_ESCAPE, Shortcut.GROUP_DIRECT).getKeyStroke(),
    38                         tr("Unselect All"));
     33        Main.registerActionShortcut(this, Shortcut.registerShortcut("edit:unselectallescape", tr("Edit: {0}", tr("Unselect All (Escape)")),
     34                KeyEvent.VK_ESCAPE, Shortcut.GROUP_DIRECT));
    3935
    4036        putValue("help", ht("/Action/UnselectAll"));
  • trunk/src/org/openstreetmap/josm/actions/ZoomInAction.java

    r3083 r3252  
    88import java.awt.event.KeyEvent;
    99
    10 import javax.swing.JComponent;
    1110import javax.swing.KeyStroke;
    1211
     
    2524        );
    2625        putValue("help", ht("/Action/ZoomIn"));
    27         // make numpad + behave like + (action is already registred)
    28         Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ADD,0), tr("Zoom In"));
     26        // make numpad + behave like +
     27        Main.registerActionShortcut(this, KeyStroke.getKeyStroke(KeyEvent.VK_ADD, 0));
    2928    }
    3029
    3130    public void actionPerformed(ActionEvent e) {
    32         Object name = Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).get(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, 0));
    33         Main.contentPane.getActionMap().put(name, this);
    34 
    3531        if (!Main.isDisplayingMapView()) return;
    3632        Main.map.mapView.zoomToFactor(0.9);
  • trunk/src/org/openstreetmap/josm/actions/ZoomOutAction.java

    r3083 r3252  
    88import java.awt.event.KeyEvent;
    99
    10 import javax.swing.JComponent;
    1110import javax.swing.KeyStroke;
    1211
     
    2120        putValue("help", ht("/Action/ZoomOut"));
    2221        // make numpad - behave like -
    23         Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT,0), tr("Zoom Out"));
     22        Main.registerActionShortcut(this, KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT,0));
    2423    }
    2524
  • trunk/src/org/openstreetmap/josm/actions/mapmode/DrawAction.java

    r3177 r3252  
    3030import java.util.Set;
    3131
    32 import javax.swing.JComponent;
    3332import javax.swing.JOptionPane;
    3433
     
    9594
    9695        // Add extra shortcut N
    97         Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
    98                 Shortcut.registerShortcut("mapmode:drawfocus", tr("Mode: Draw Focus"), KeyEvent.VK_N, Shortcut.GROUP_EDIT).getKeyStroke(), tr("Draw"));
     96        Main.registerActionShortcut(this, Shortcut.registerShortcut("mapmode:drawfocus", tr("Mode: Draw Focus"), KeyEvent.VK_N, Shortcut.GROUP_EDIT));
    9997
    10098        cursorCrosshair = getCursor();
  • trunk/src/org/openstreetmap/josm/gui/MainApplet.java

    r3248 r3252  
    4747    private final class MainCaller extends Main {
    4848        private MainCaller() {
    49             setContentPane(contentPane);
     49            setContentPane(contentPanePrivate);
    5050            setJMenuBar(menu);
    5151            setBounds(bounds);
  • trunk/src/org/openstreetmap/josm/gui/MainApplication.java

    r3232 r3252  
    5353    public MainApplication(JFrame mainFrame) {
    5454        super();
    55         mainFrame.setContentPane(contentPane);
     55        mainFrame.setContentPane(contentPanePrivate);
    5656        mainFrame.setJMenuBar(menu);
    5757        mainFrame.setBounds(bounds);
  • trunk/src/org/openstreetmap/josm/gui/MapFrame.java

    r3189 r3252  
    9191    public final int DEF_TOGGLE_DLG_WIDTH = 330;
    9292
    93     public MapFrame() {
     93    public MapFrame(JPanel contentPane) {
    9494        setSize(400,400);
    9595        setLayout(new BorderLayout());
    9696
    97         mapView = new MapView();
     97        mapView = new MapView(contentPane);
    9898
    9999        new FileDrop(mapView);
  • trunk/src/org/openstreetmap/josm/gui/MapView.java

    r3229 r3252  
    2929
    3030import javax.swing.AbstractButton;
    31 import javax.swing.JComponent;
    3231import javax.swing.JOptionPane;
     32import javax.swing.JPanel;
    3333
    3434import org.openstreetmap.josm.Main;
    3535import org.openstreetmap.josm.actions.AutoScaleAction;
    36 import org.openstreetmap.josm.actions.JosmAction;
    3736import org.openstreetmap.josm.actions.MoveAction;
    3837import org.openstreetmap.josm.actions.mapmode.MapMode;
     
    189188    private Rectangle lastClipBounds = new Rectangle();
    190189
    191     public MapView() {
     190    public MapView(final JPanel contentPane) {
    192191        Main.pref.addPreferenceChangeListener(this);
    193192        addComponentListener(new ComponentAdapter(){
     
    213212                }
    214213
    215                 new MapMover(MapView.this, Main.contentPane);
    216                 JosmAction mv;
    217                 mv = new MoveAction(MoveAction.Direction.UP);
    218                 if (mv.getShortcut() != null) {
    219                     Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(mv.getShortcut().getKeyStroke(), "UP");
    220                     Main.contentPane.getActionMap().put("UP", mv);
    221                 }
    222                 mv = new MoveAction(MoveAction.Direction.DOWN);
    223                 if (mv.getShortcut() != null) {
    224                     Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(mv.getShortcut().getKeyStroke(), "DOWN");
    225                     Main.contentPane.getActionMap().put("DOWN", mv);
    226                 }
    227                 mv = new MoveAction(MoveAction.Direction.LEFT);
    228                 if (mv.getShortcut() != null) {
    229                     Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(mv.getShortcut().getKeyStroke(), "LEFT");
    230                     Main.contentPane.getActionMap().put("LEFT", mv);
    231                 }
    232                 mv = new MoveAction(MoveAction.Direction.RIGHT);
    233                 if (mv.getShortcut() != null) {
    234                     Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(mv.getShortcut().getKeyStroke(), "RIGHT");
    235                     Main.contentPane.getActionMap().put("RIGHT", mv);
    236                 }
     214                new MapMover(MapView.this, contentPane);
     215                new MoveAction(MoveAction.Direction.UP);
     216                new MoveAction(MoveAction.Direction.DOWN);
     217                new MoveAction(MoveAction.Direction.LEFT);
     218                new MoveAction(MoveAction.Direction.RIGHT);
    237219            }
    238220        });
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java

    r3219 r3252  
    610610        btnAdd.getActionMap().put("onEnter", addAction);
    611611
    612         Shortcut sc = Shortcut.registerShortcut("properties:add", tr("Add Properties"), KeyEvent.VK_B,
    613                 Shortcut.GROUP_MNEMONIC);
    614         Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), "properties:add");
    615         Main.contentPane.getActionMap().put("properties:add", addAction);
     612        Main.registerActionShortcut(addAction, Shortcut.registerShortcut("properties:add", tr("Add Properties"), KeyEvent.VK_B,
     613                Shortcut.GROUP_MNEMONIC));
    616614
    617615        // -- edit action
  • trunk/src/org/openstreetmap/josm/gui/layer/geoimage/ImageViewerDialog.java

    r2969 r3252  
    1717import java.awt.event.WindowEvent;
    1818
     19import javax.swing.AbstractAction;
    1920import javax.swing.Box;
    20 import javax.swing.AbstractAction;
    2121import javax.swing.ImageIcon;
    2222import javax.swing.JButton;
     
    5454
    5555    public static ImageViewerDialog getInstance() {
    56         if (dialog == null) {
     56        if (dialog == null)
    5757            throw new AssertionError(); // a new instance needs to be created first
    58         }
    5958        return dialog;
    6059    }
     
    8382        btnPrevious.setPreferredSize(buttonDim);
    8483        Shortcut scPrev = Shortcut.registerShortcut(
    85             "geoimage:previous", tr("Geoimage: {0}", tr("Show previous Image")), KeyEvent.VK_PAGE_UP, Shortcut.GROUP_DIRECT);
     84                "geoimage:previous", tr("Geoimage: {0}", tr("Show previous Image")), KeyEvent.VK_PAGE_UP, Shortcut.GROUP_DIRECT);
    8685        final String APREVIOUS = "Previous Image";
    87         Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scPrev.getKeyStroke(), APREVIOUS);
    88         Main.contentPane.getActionMap().put(APREVIOUS, prevAction);
     86        Main.registerActionShortcut(prevAction, scPrev);
    8987        btnPrevious.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scPrev.getKeyStroke(), APREVIOUS);
    9088        btnPrevious.getActionMap().put(APREVIOUS, prevAction);
     
    9593        btnDelete.setPreferredSize(buttonDim);
    9694        Shortcut scDelete = Shortcut.registerShortcut(
    97             "geoimage:deleteimagefromlayer", tr("Geoimage: {0}", DELETE_TEXT), KeyEvent.VK_DELETE, Shortcut.GROUP_DIRECT, Shortcut.SHIFT_DEFAULT);
    98         Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scDelete.getKeyStroke(), DELETE_TEXT);
    99         Main.contentPane.getActionMap().put(DELETE_TEXT, delAction);
     95                "geoimage:deleteimagefromlayer", tr("Geoimage: {0}", DELETE_TEXT), KeyEvent.VK_DELETE, Shortcut.GROUP_DIRECT, Shortcut.SHIFT_DEFAULT);
     96        Main.registerActionShortcut(delAction, scDelete);
    10097        btnDelete.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scDelete.getKeyStroke(), DELETE_TEXT);
    10198        btnDelete.getActionMap().put(DELETE_TEXT, delAction);
     
    105102        btnDeleteFromDisk.setPreferredSize(buttonDim);
    106103        Shortcut scDeleteFromDisk = Shortcut.registerShortcut(
    107             "geoimage:deletefilefromdisk", tr("Geoimage: {0}", tr("Delete File from disk")), KeyEvent.VK_DELETE, Shortcut.GROUP_DIRECT, Shortcut.GROUP_MENU + Shortcut.SHIFT_DEFAULT);
     104                "geoimage:deletefilefromdisk", tr("Geoimage: {0}", tr("Delete File from disk")), KeyEvent.VK_DELETE, Shortcut.GROUP_DIRECT, Shortcut.GROUP_MENU + Shortcut.SHIFT_DEFAULT);
    108105        final String ADELFROMDISK = "Delete image file from disk";
    109         Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scDeleteFromDisk.getKeyStroke(), ADELFROMDISK);
    110         Main.contentPane.getActionMap().put(ADELFROMDISK, delFromDiskAction);
     106        Main.registerActionShortcut(delFromDiskAction, scDeleteFromDisk);
    111107        btnDeleteFromDisk.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scDeleteFromDisk.getKeyStroke(), ADELFROMDISK);
    112108        btnDeleteFromDisk.getActionMap().put(ADELFROMDISK, delFromDiskAction);
     
    116112        btnNext.setPreferredSize(buttonDim);
    117113        Shortcut scNext = Shortcut.registerShortcut(
    118             "geoimage:next", tr("Geoimage: {0}", tr("Show next Image")), KeyEvent.VK_PAGE_DOWN, Shortcut.GROUP_DIRECT);
     114                "geoimage:next", tr("Geoimage: {0}", tr("Show next Image")), KeyEvent.VK_PAGE_DOWN, Shortcut.GROUP_DIRECT);
    119115        final String ANEXT = "Next Image";
    120         Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scNext.getKeyStroke(), ANEXT);
    121         Main.contentPane.getActionMap().put(ANEXT, nextAction);
     116        Main.registerActionShortcut(nextAction, scNext);
    122117        btnNext.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scNext.getKeyStroke(), ANEXT);
    123118        btnNext.getActionMap().put(ANEXT, nextAction);
     
    192187            } else if (COMMAND_REMOVE.equals(action)) {
    193188                if (currentLayer != null) {
    194                    currentLayer.removeCurrentPhoto();
     189                    currentLayer.removeCurrentPhoto();
    195190                }
    196191            } else if (COMMAND_REMOVE_FROM_DISK.equals(action)) {
     
    221216    public void displayImage(GeoImageLayer layer, ImageEntry entry) {
    222217        synchronized(this) {
    223 //            if (currentLayer == layer && currentEntry == entry) {
    224 //                repaint();
    225 //                return;
    226 //            }                     TODO: pop up image dialog but don't load image again
     218            //            if (currentLayer == layer && currentEntry == entry) {
     219            //                repaint();
     220            //                return;
     221            //            }                     TODO: pop up image dialog but don't load image again
    227222
    228223            if (centerView && Main.map != null && entry != null && entry.getPos() != null) {
Note: See TracChangeset for help on using the changeset viewer.