Changeset 18285 in josm for trunk/src/org


Ignore:
Timestamp:
2021-10-18T22:05:17+02:00 (3 years ago)
Author:
Don-vip
Message:

see #21443 - Extract shortcut disablement to interface, with default implementations for focusGained/focusLost (patch by taylor.smock)

Location:
trunk/src/org/openstreetmap/josm/gui/widgets
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/widgets/DisableShortcutsOnFocusGainedTextField.java

    r17235 r18285  
    33
    44import java.awt.event.FocusEvent;
    5 import java.awt.event.InputEvent;
    6 import java.awt.event.KeyEvent;
    75import java.util.ArrayList;
    86import java.util.HashSet;
     
    119
    1210import javax.swing.Action;
    13 import javax.swing.JMenu;
    14 import javax.swing.JMenuItem;
    15 import javax.swing.KeyStroke;
    1611import javax.swing.text.Document;
    1712
    1813import org.openstreetmap.josm.actions.JosmAction;
    19 import org.openstreetmap.josm.gui.MainApplication;
    2014import org.openstreetmap.josm.tools.Pair;
    2115import org.openstreetmap.josm.tools.Shortcut;
     
    2721 * @since 5696
    2822 */
    29 public class DisableShortcutsOnFocusGainedTextField extends JosmTextField {
     23public class DisableShortcutsOnFocusGainedTextField extends JosmTextField implements DisableShortcutsOnFocusGainedComponent {
    3024
    3125    /**
     
    9892    public void focusGained(FocusEvent e) {
    9993        super.focusGained(e);
    100         disableMenuActions();
    101         unregisterActionShortcuts();
     94        DisableShortcutsOnFocusGainedComponent.super.focusGained(e);
    10295    }
    10396
     
    10598    public void focusLost(FocusEvent e) {
    10699        super.focusLost(e);
    107         restoreActionShortcuts();
    108         restoreMenuActions();
     100        DisableShortcutsOnFocusGainedComponent.super.focusLost(e);
    109101    }
    110102
    111     /**
    112      * Disables all relevant menu actions.
    113      * @see #hasToBeDisabled
    114      */
    115     protected void disableMenuActions() {
    116         disabledMenuActions.clear();
    117         for (int i = 0; i < MainApplication.getMenu().getMenuCount(); i++) {
    118             JMenu menu = MainApplication.getMenu().getMenu(i);
    119             if (menu != null) {
    120                 for (int j = 0; j < menu.getItemCount(); j++) {
    121                     JMenuItem item = menu.getItem(j);
    122                     if (item != null) {
    123                         Action action = item.getAction();
    124                         if (action instanceof JosmAction && action.isEnabled()) {
    125                             Shortcut shortcut = ((JosmAction) action).getShortcut();
    126                             if (shortcut != null) {
    127                                 KeyStroke ks = shortcut.getKeyStroke();
    128                                 if (hasToBeDisabled(ks)) {
    129                                     action.setEnabled(false);
    130                                     disabledMenuActions.add((JosmAction) action);
    131                                 }
    132                             }
    133                         }
    134                     }
    135                 }
    136             }
    137         }
     103    @Override
     104    public List<Pair<Action, Shortcut>> getUnregisteredActionShortcuts() {
     105        return this.unregisteredActionShortcuts;
    138106    }
    139107
    140     /**
    141      * Unregisters all relevant action shortcuts.
    142      * @see #hasToBeDisabled
    143      */
    144     protected void unregisterActionShortcuts() {
    145         unregisteredActionShortcuts.clear();
    146         // Unregister all actions with Shift modifier or without modifiers to avoid them to be triggered by typing in this text field
    147         for (Shortcut shortcut : Shortcut.listAll()) {
    148             KeyStroke ks = shortcut.getKeyStroke();
    149             if (hasToBeDisabled(ks)) {
    150                 Action action = MainApplication.getRegisteredActionShortcut(shortcut);
    151                 if (action != null) {
    152                     MainApplication.unregisterActionShortcut(action, shortcut);
    153                     unregisteredActionShortcuts.add(new Pair<>(action, shortcut));
    154                 }
    155             }
    156         }
    157     }
    158 
    159     /**
    160      * Returns true if the given shortcut has Shift modifier or no modifier and is not an actions key.
    161      * @param ks key stroke
    162      * @return {@code true} if the given shortcut has to be disabled
    163      * @see KeyEvent#isActionKey()
    164      */
    165     protected boolean hasToBeDisabled(KeyStroke ks) {
    166         return ks != null && (ks.getModifiers() == 0 || isOnlyShift(ks.getModifiers())) && !new KeyEvent(
    167                 this, KeyEvent.KEY_PRESSED, 0, ks.getModifiers(), ks.getKeyCode(), ks.getKeyChar()).isActionKey();
    168     }
    169 
    170     private static boolean isOnlyShift(int modifiers) {
    171         return (modifiers & InputEvent.SHIFT_DOWN_MASK) != 0
    172                 && (modifiers & InputEvent.CTRL_DOWN_MASK) == 0
    173                 && (modifiers & InputEvent.ALT_DOWN_MASK) == 0
    174                 && (modifiers & InputEvent.ALT_GRAPH_DOWN_MASK) == 0
    175                 && (modifiers & InputEvent.META_DOWN_MASK) == 0;
    176     }
    177 
    178     /**
    179      * Restore all actions previously disabled
    180      */
    181     protected void restoreMenuActions() {
    182         for (JosmAction a : disabledMenuActions) {
    183             a.setEnabled(true);
    184         }
    185         disabledMenuActions.clear();
    186     }
    187 
    188     /**
    189      * Restore all action shortcuts previously unregistered
    190      */
    191     protected void restoreActionShortcuts() {
    192         for (Pair<Action, Shortcut> p : unregisteredActionShortcuts) {
    193             MainApplication.registerActionShortcut(p.a, p.b);
    194         }
    195         unregisteredActionShortcuts.clear();
     108    @Override
     109    public Set<JosmAction> getDisabledMenuActions() {
     110        return this.disabledMenuActions;
    196111    }
    197112}
Note: See TracChangeset for help on using the changeset viewer.