Changeset 1088


Ignore:
Timestamp:
Jun 22, 2011, 9:21:44 PM (6 years ago)
Author:
stefan
Message:

automatically update editor shortcuts after changed in global.properties

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/jlatexeditor/JLatexEditorJFrame.java

    r1071 r1088  
    517517  /**
    518518   * Creates a keyboard shortcut without a menu item.
     519   *
     520   * @param listener action listener to be informed about keystrokes
     521   * @param receiver receiver
     522   * @param command action command
     523   * @param withAndWithoutShift register the shortcut also in combination with shift?
    519524   */
    520   private void createShortcut(ActionListener listener, String receiver, String command, boolean withAndWithoutShift) {
    521     String shorcutString = GProperties.getString("shortcut." + command);
    522     if (shorcutString != null && !shorcutString.equals("")) {
    523       KeyStroke keyStroke = KeyStroke.getKeyStroke(shorcutString);
    524       menuBar.registerKeyboardAction(listener, receiver + command, keyStroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
    525 
    526       if(withAndWithoutShift) {
    527         keyStroke = KeyStroke.getKeyStroke("shift " + shorcutString);
    528         menuBar.registerKeyboardAction(listener, receiver + command, keyStroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
    529       }
    530     }
     525  private void createShortcut(final ActionListener listener, final String receiver, final String command, final boolean withAndWithoutShift) {
     526    // register a property change listener to update the shortcut in the menu if it has been changed in global.properties
     527    PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
     528      private ArrayList<KeyStroke> oldKeyStrokes = new ArrayList<KeyStroke>();
     529
     530      public void propertyChange(PropertyChangeEvent evt) {
     531        // unregister old keystrokes
     532        for (KeyStroke oldKeyStroke : oldKeyStrokes) {
     533          menuBar.unregisterKeyboardAction(oldKeyStroke);
     534        }
     535
     536        // register new keystroke
     537        String shortcutString = GProperties.getString("shortcut." + command);
     538        if (shortcutString != null && !shortcutString.equals("")) {
     539          KeyStroke keyStroke = KeyStroke.getKeyStroke(shortcutString);
     540          menuBar.registerKeyboardAction(listener, receiver + command, keyStroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
     541          oldKeyStrokes.add(keyStroke);
     542
     543          if(withAndWithoutShift) {
     544            keyStroke = KeyStroke.getKeyStroke("shift " + shortcutString);
     545            menuBar.registerKeyboardAction(listener, receiver + command, keyStroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
     546            oldKeyStrokes.add(keyStroke);
     547          }
     548        }
     549      }
     550    };
     551    GProperties.addPropertyChangeListener("shortcut." + command, propertyChangeListener);
     552    // fire an initial change to load the shortcut
     553    propertyChangeListener.propertyChange(null);
    531554  }
    532555
Note: See TracChangeset for help on using the changeset viewer.