- Timestamp:
- 2021-10-18T22:05:17+02:00 (3 years ago)
- 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 3 3 4 4 import java.awt.event.FocusEvent; 5 import java.awt.event.InputEvent;6 import java.awt.event.KeyEvent;7 5 import java.util.ArrayList; 8 6 import java.util.HashSet; … … 11 9 12 10 import javax.swing.Action; 13 import javax.swing.JMenu;14 import javax.swing.JMenuItem;15 import javax.swing.KeyStroke;16 11 import javax.swing.text.Document; 17 12 18 13 import org.openstreetmap.josm.actions.JosmAction; 19 import org.openstreetmap.josm.gui.MainApplication;20 14 import org.openstreetmap.josm.tools.Pair; 21 15 import org.openstreetmap.josm.tools.Shortcut; … … 27 21 * @since 5696 28 22 */ 29 public class DisableShortcutsOnFocusGainedTextField extends JosmTextField {23 public class DisableShortcutsOnFocusGainedTextField extends JosmTextField implements DisableShortcutsOnFocusGainedComponent { 30 24 31 25 /** … … 98 92 public void focusGained(FocusEvent e) { 99 93 super.focusGained(e); 100 disableMenuActions(); 101 unregisterActionShortcuts(); 94 DisableShortcutsOnFocusGainedComponent.super.focusGained(e); 102 95 } 103 96 … … 105 98 public void focusLost(FocusEvent e) { 106 99 super.focusLost(e); 107 restoreActionShortcuts(); 108 restoreMenuActions(); 100 DisableShortcutsOnFocusGainedComponent.super.focusLost(e); 109 101 } 110 102 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; 138 106 } 139 107 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; 196 111 } 197 112 }
Note:
See TracChangeset
for help on using the changeset viewer.