Ignore:
Timestamp:
15.05.2010 18:59:10 (2 years ago)
Author:
jttt
Message:

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

File:
1 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) { 
Note: See TracChangeset for help on using the changeset viewer.