1 | //License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.KeyEvent;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.LinkedHashMap;
|
---|
12 | import java.util.LinkedList;
|
---|
13 | import java.util.List;
|
---|
14 | import java.util.Map;
|
---|
15 |
|
---|
16 | import javax.swing.AbstractAction;
|
---|
17 | import javax.swing.AbstractButton;
|
---|
18 | import javax.swing.JMenu;
|
---|
19 | import javax.swing.JOptionPane;
|
---|
20 | import javax.swing.KeyStroke;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.Main;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Global shortcut class.
|
---|
26 | *
|
---|
27 | * Note: This class represents a single shortcut, contains the factory to obtain
|
---|
28 | * shortcut objects from, manages shortcuts and shortcut collisions, and
|
---|
29 | * finally manages loading and saving shortcuts to/from the preferences.
|
---|
30 | *
|
---|
31 | * Action authors: You only need the {@see #registerShortcut} factory. Ignore everything
|
---|
32 | * else.
|
---|
33 | *
|
---|
34 | * All: Use only public methods that are also marked to be used. The others are
|
---|
35 | * public so the shortcut preferences can use them.
|
---|
36 | *
|
---|
37 | */
|
---|
38 | public class Shortcut {
|
---|
39 | private String shortText; // the unique ID of the shortcut
|
---|
40 | private String longText; // a human readable description that will be shown in the preferences
|
---|
41 | private int requestedKey; // the key, the caller requested
|
---|
42 | private int requestedGroup; // the group, the caller requested
|
---|
43 | private int assignedKey; // the key that actually is used
|
---|
44 | private int assignedModifier; // the modifiers that are used
|
---|
45 | private boolean assignedDefault; // true if it got assigned what was requested. (Note: modifiers will be ignored in favour of group when loading it from the preferences then.)
|
---|
46 | private boolean assignedUser; // true if the user changed this shortcut
|
---|
47 | private boolean automatic; // true if the user cannot change this shortcut (Note: it also will not be saved into the preferences)
|
---|
48 | private boolean reset; // true if the user requested this shortcut to be set to its default value (will happen on next restart, as this shortcut will not be saved to the preferences)
|
---|
49 |
|
---|
50 | // simple constructor
|
---|
51 | private Shortcut(String shortText, String longText, int requestedKey, int requestedGroup, int assignedKey, int assignedModifier, boolean assignedDefault, boolean assignedUser) {
|
---|
52 | this.shortText = shortText;
|
---|
53 | this.longText = longText;
|
---|
54 | this.requestedKey = requestedKey;
|
---|
55 | this.requestedGroup = requestedGroup;
|
---|
56 | this.assignedKey = assignedKey;
|
---|
57 | this.assignedModifier = assignedModifier;
|
---|
58 | this.assignedDefault = assignedDefault;
|
---|
59 | this.assignedUser = assignedUser;
|
---|
60 | this.automatic = false;
|
---|
61 | this.reset = false;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public String getShortText() {
|
---|
65 | return shortText;
|
---|
66 | }
|
---|
67 |
|
---|
68 | public String getLongText() {
|
---|
69 | return longText;
|
---|
70 | }
|
---|
71 |
|
---|
72 | // a shortcut will be renamed when it is handed out again, because the original name
|
---|
73 | // may be a dummy
|
---|
74 | private void setLongText(String longText) {
|
---|
75 | this.longText = longText;
|
---|
76 | }
|
---|
77 |
|
---|
78 | private int getRequestedKey() {
|
---|
79 | return requestedKey;
|
---|
80 | }
|
---|
81 |
|
---|
82 | public int getRequestedGroup() {
|
---|
83 | return requestedGroup;
|
---|
84 | }
|
---|
85 |
|
---|
86 | public int getAssignedKey() {
|
---|
87 | return assignedKey;
|
---|
88 | }
|
---|
89 |
|
---|
90 | public int getAssignedModifier() {
|
---|
91 | return assignedModifier;
|
---|
92 | }
|
---|
93 |
|
---|
94 | public boolean getAssignedDefault() {
|
---|
95 | return assignedDefault;
|
---|
96 | }
|
---|
97 |
|
---|
98 | public boolean getAssignedUser() {
|
---|
99 | return assignedUser;
|
---|
100 | }
|
---|
101 |
|
---|
102 | public boolean getAutomatic() {
|
---|
103 | return automatic;
|
---|
104 | }
|
---|
105 |
|
---|
106 | public boolean isChangeable() {
|
---|
107 | return !automatic && !shortText.equals("core:none");
|
---|
108 | }
|
---|
109 |
|
---|
110 | private boolean getReset() {
|
---|
111 | return reset;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * FOR PREF PANE ONLY
|
---|
116 | */
|
---|
117 | public void setAutomatic() {
|
---|
118 | automatic = true;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * FOR PREF PANE ONLY
|
---|
123 | */
|
---|
124 | public void setAssignedModifier(int assignedModifier) {
|
---|
125 | this.assignedModifier = assignedModifier;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * FOR PREF PANE ONLY
|
---|
130 | */
|
---|
131 | public void setAssignedKey(int assignedKey) {
|
---|
132 | this.assignedKey = assignedKey;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * FOR PREF PANE ONLY
|
---|
137 | */
|
---|
138 | public void setAssignedUser(boolean assignedUser) {
|
---|
139 | this.reset = (this.assignedUser || reset) && !assignedUser;
|
---|
140 | if (assignedUser) {
|
---|
141 | assignedDefault = false;
|
---|
142 | } else if (reset) {
|
---|
143 | assignedKey = requestedKey;
|
---|
144 | assignedModifier = findModifier(requestedGroup, null);
|
---|
145 | }
|
---|
146 | this.assignedUser = assignedUser;
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Use this to register the shortcut with Swing
|
---|
151 | */
|
---|
152 | public KeyStroke getKeyStroke() {
|
---|
153 | if (assignedModifier != -1)
|
---|
154 | return KeyStroke.getKeyStroke(assignedKey, assignedModifier);
|
---|
155 | return null;
|
---|
156 | }
|
---|
157 |
|
---|
158 | private boolean isSame(int isKey, int isModifier) {
|
---|
159 | // -1 --- an unassigned shortcut is different from any other shortcut
|
---|
160 | return( isKey == assignedKey && isModifier == assignedModifier && assignedModifier != getGroupModifier(NONE));
|
---|
161 | }
|
---|
162 |
|
---|
163 | // create a shortcut object from an string as saved in the preferences
|
---|
164 | private Shortcut(String prefString) {
|
---|
165 | ArrayList<String> s = (new ArrayList<String>(Main.pref.getCollection(prefString)));
|
---|
166 | this.shortText = prefString.substring(15);
|
---|
167 | this.longText = s.get(0);
|
---|
168 | this.requestedKey = Integer.parseInt(s.get(1));
|
---|
169 | this.requestedGroup = Integer.parseInt(s.get(2));
|
---|
170 | this.assignedKey = Integer.parseInt(s.get(3));
|
---|
171 | this.assignedModifier = Integer.parseInt(s.get(4));
|
---|
172 | this.assignedDefault = Boolean.parseBoolean(s.get(5));
|
---|
173 | this.assignedUser = Boolean.parseBoolean(s.get(6));
|
---|
174 | }
|
---|
175 |
|
---|
176 | private void saveDefault() {
|
---|
177 | Main.pref.getCollection("shortcut.entry."+shortText, Arrays.asList(new String[]{longText,
|
---|
178 | String.valueOf(requestedKey), String.valueOf(requestedGroup), String.valueOf(requestedKey),
|
---|
179 | String.valueOf(getGroupModifier(requestedGroup)), String.valueOf(true), String.valueOf(false)}));
|
---|
180 | }
|
---|
181 |
|
---|
182 | // get a string that can be put into the preferences
|
---|
183 | private boolean save() {
|
---|
184 | if (getAutomatic() || getReset() || !getAssignedUser()) {
|
---|
185 | return Main.pref.putCollection("shortcut.entry."+shortText, null);
|
---|
186 | } else {
|
---|
187 | return Main.pref.putCollection("shortcut.entry."+shortText, Arrays.asList(new String[]{longText,
|
---|
188 | String.valueOf(requestedKey), String.valueOf(requestedGroup), String.valueOf(assignedKey),
|
---|
189 | String.valueOf(assignedModifier), String.valueOf(assignedDefault), String.valueOf(assignedUser)}));
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | private boolean isSame(Shortcut other) {
|
---|
194 | return assignedKey == other.assignedKey && assignedModifier == other.assignedModifier;
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * use this to set a menu's mnemonic
|
---|
199 | */
|
---|
200 | public void setMnemonic(JMenu menu) {
|
---|
201 | if (requestedGroup == GROUP_MNEMONIC && assignedModifier == getGroupModifier(requestedGroup + GROUPS_DEFAULT) && getKeyStroke() != null && KeyEvent.getKeyText(assignedKey).length() == 1) {
|
---|
202 | menu.setMnemonic(KeyEvent.getKeyText(assignedKey).charAt(0)); //getKeyStroke().getKeyChar() seems not to work here
|
---|
203 | }
|
---|
204 | }
|
---|
205 | /**
|
---|
206 | * use this to set a buttons's mnemonic
|
---|
207 | */
|
---|
208 | public void setMnemonic(AbstractButton button) {
|
---|
209 | if (requestedGroup == GROUP_MNEMONIC && assignedModifier == getGroupModifier(requestedGroup + GROUPS_DEFAULT) && getKeyStroke() != null && KeyEvent.getKeyText(assignedKey).length() == 1) {
|
---|
210 | button.setMnemonic(KeyEvent.getKeyText(assignedKey).charAt(0)); //getKeyStroke().getKeyChar() seems not to work here
|
---|
211 | }
|
---|
212 | }
|
---|
213 | /**
|
---|
214 | * use this to set a actions's accelerator
|
---|
215 | */
|
---|
216 | public void setAccelerator(AbstractAction action) {
|
---|
217 | if (getKeyStroke() != null) {
|
---|
218 | action.putValue(AbstractAction.ACCELERATOR_KEY, getKeyStroke());
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * use this to get a human readable text for your shortcut
|
---|
224 | */
|
---|
225 | public String getKeyText() {
|
---|
226 | KeyStroke keyStroke = getKeyStroke();
|
---|
227 | if (keyStroke == null) return "";
|
---|
228 | String modifText = KeyEvent.getKeyModifiersText(keyStroke.getModifiers());
|
---|
229 | if ("".equals (modifText)) return KeyEvent.getKeyText (keyStroke.getKeyCode ());
|
---|
230 | return modifText + "+" + KeyEvent.getKeyText(keyStroke.getKeyCode ());
|
---|
231 | }
|
---|
232 |
|
---|
233 | @Override
|
---|
234 | public String toString() {
|
---|
235 | return getKeyText();
|
---|
236 | }
|
---|
237 |
|
---|
238 | ///////////////////////////////
|
---|
239 | // everything's static below //
|
---|
240 | ///////////////////////////////
|
---|
241 |
|
---|
242 | // here we store our shortcuts
|
---|
243 | private static Map<String, Shortcut> shortcuts = new LinkedHashMap<String, Shortcut>();
|
---|
244 |
|
---|
245 | // and here our modifier groups
|
---|
246 | private static Map<Integer, Integer> groups= new HashMap<Integer, Integer>();
|
---|
247 |
|
---|
248 | // check if something collides with an existing shortcut
|
---|
249 | private static Shortcut findShortcut(int requestedKey, int modifier) {
|
---|
250 | if (modifier == getGroupModifier(GROUP_NONE))
|
---|
251 | return null;
|
---|
252 | for (Shortcut sc : shortcuts.values()) {
|
---|
253 | if (sc.isSame(requestedKey, modifier))
|
---|
254 | return sc;
|
---|
255 | }
|
---|
256 | return null;
|
---|
257 | }
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * FOR PREF PANE ONLY
|
---|
261 | */
|
---|
262 | public static List<Shortcut> listAll() {
|
---|
263 | List<Shortcut> l = new ArrayList<Shortcut>();
|
---|
264 | for(Shortcut c : shortcuts.values())
|
---|
265 | {
|
---|
266 | if(!c.shortText.equals("core:none")) {
|
---|
267 | l.add(c);
|
---|
268 | }
|
---|
269 | }
|
---|
270 | return l;
|
---|
271 | }
|
---|
272 |
|
---|
273 | public static final int NONE = 5000;
|
---|
274 | public static final int MNEMONIC = 5001;
|
---|
275 | public static final int RESERVED = 5002;
|
---|
276 | public static final int DIRECT = 5003;
|
---|
277 | public static final int ALT = 5004;
|
---|
278 | public static final int SHIFT = 5005;
|
---|
279 | public static final int CTRL = 5006;
|
---|
280 | public static final int ALT_SHIFT = 5007;
|
---|
281 | public static final int ALT_CTRL = 5008;
|
---|
282 | public static final int CTRL_SHIFT = 5009;
|
---|
283 | public static final int ALT_CTRL_SHIFT = 5010;
|
---|
284 |
|
---|
285 | /* old */
|
---|
286 | @Deprecated public static final int GROUP_NONE = 0;
|
---|
287 | @Deprecated public static final int GROUP_HOTKEY = 1;
|
---|
288 | @Deprecated public static final int GROUP_MENU = 2;
|
---|
289 | @Deprecated public static final int GROUP_EDIT = 3;
|
---|
290 | @Deprecated public static final int GROUP_LAYER = 4;
|
---|
291 | @Deprecated public static final int GROUP_DIRECT = 5;
|
---|
292 | @Deprecated public static final int GROUP_MNEMONIC = 6;
|
---|
293 | @Deprecated public static final int GROUP_DIRECT2 = 7;
|
---|
294 | @Deprecated public static final int GROUP_DIRECT3 = 8;
|
---|
295 | @Deprecated public static final int GROUPS_DEFAULT = 0;
|
---|
296 | @Deprecated public static final int GROUPS_ALT1 = 100;
|
---|
297 | @Deprecated public static final int GROUPS_ALT2 = 200;
|
---|
298 | @Deprecated public static final int SHIFT_DEFAULT = 1;
|
---|
299 |
|
---|
300 | // bootstrap
|
---|
301 | private static boolean initdone = false;
|
---|
302 | private static void doInit() {
|
---|
303 | if (initdone) return;
|
---|
304 | initdone = true;
|
---|
305 | groups.put(NONE, -1);
|
---|
306 | groups.put(MNEMONIC, KeyEvent.ALT_DOWN_MASK);
|
---|
307 | groups.put(DIRECT, 0);
|
---|
308 | groups.put(ALT, KeyEvent.ALT_DOWN_MASK);
|
---|
309 | groups.put(SHIFT, KeyEvent.SHIFT_DOWN_MASK);
|
---|
310 | groups.put(CTRL, KeyEvent.CTRL_DOWN_MASK);
|
---|
311 | groups.put(ALT_SHIFT, KeyEvent.ALT_DOWN_MASK|KeyEvent.SHIFT_DOWN_MASK);
|
---|
312 | groups.put(ALT_CTRL, KeyEvent.ALT_DOWN_MASK|KeyEvent.CTRL_DOWN_MASK);
|
---|
313 | groups.put(CTRL_SHIFT, KeyEvent.CTRL_DOWN_MASK|KeyEvent.SHIFT_DOWN_MASK);
|
---|
314 | groups.put(ALT_CTRL_SHIFT, KeyEvent.ALT_DOWN_MASK|KeyEvent.CTRL_DOWN_MASK|KeyEvent.SHIFT_DOWN_MASK);
|
---|
315 |
|
---|
316 | /* old */
|
---|
317 | groups.put(GROUPS_DEFAULT+GROUP_NONE, -1);
|
---|
318 | groups.put(GROUPS_DEFAULT+GROUP_HOTKEY, KeyEvent.CTRL_DOWN_MASK);
|
---|
319 | groups.put(GROUPS_DEFAULT+GROUP_MENU, KeyEvent.CTRL_DOWN_MASK);
|
---|
320 | groups.put(GROUPS_DEFAULT+GROUP_EDIT, 0);
|
---|
321 | groups.put(GROUPS_DEFAULT+GROUP_LAYER, KeyEvent.ALT_DOWN_MASK);
|
---|
322 | groups.put(GROUPS_DEFAULT+GROUP_DIRECT, 0);
|
---|
323 | groups.put(GROUPS_DEFAULT+GROUP_MNEMONIC,KeyEvent.ALT_DOWN_MASK);
|
---|
324 | groups.put(GROUPS_DEFAULT+GROUP_DIRECT2, KeyEvent.ALT_DOWN_MASK);
|
---|
325 | groups.put(GROUPS_DEFAULT+GROUP_DIRECT3, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK);
|
---|
326 |
|
---|
327 | groups.put(GROUPS_ALT1+GROUP_NONE, -1);
|
---|
328 | groups.put(GROUPS_ALT1+GROUP_HOTKEY, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK);
|
---|
329 | groups.put(GROUPS_ALT1+GROUP_MENU, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK);
|
---|
330 | groups.put(GROUPS_ALT1+GROUP_EDIT, KeyEvent.SHIFT_DOWN_MASK);
|
---|
331 | groups.put(GROUPS_ALT1+GROUP_LAYER, KeyEvent.ALT_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK);
|
---|
332 | groups.put(GROUPS_ALT1+GROUP_DIRECT, KeyEvent.SHIFT_DOWN_MASK);
|
---|
333 | groups.put(GROUPS_ALT1+GROUP_MNEMONIC, KeyEvent.ALT_DOWN_MASK);
|
---|
334 | groups.put(GROUPS_ALT1+GROUP_DIRECT2, KeyEvent.ALT_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK);
|
---|
335 | groups.put(GROUPS_ALT1+GROUP_DIRECT3, KeyEvent.ALT_DOWN_MASK | KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK);
|
---|
336 |
|
---|
337 | groups.put(GROUPS_ALT2+GROUP_NONE, -1);
|
---|
338 | groups.put(GROUPS_ALT2+GROUP_HOTKEY, KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK);
|
---|
339 | groups.put(GROUPS_ALT2+GROUP_MENU, KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK);
|
---|
340 | groups.put(GROUPS_ALT2+GROUP_EDIT, KeyEvent.ALT_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK);
|
---|
341 | groups.put(GROUPS_ALT2+GROUP_LAYER, KeyEvent.SHIFT_DOWN_MASK);
|
---|
342 | groups.put(GROUPS_ALT2+GROUP_DIRECT, KeyEvent.CTRL_DOWN_MASK);
|
---|
343 | groups.put(GROUPS_ALT2+GROUP_MNEMONIC, KeyEvent.ALT_DOWN_MASK);
|
---|
344 | groups.put(GROUPS_ALT2+GROUP_DIRECT2, KeyEvent.ALT_DOWN_MASK | KeyEvent.CTRL_DOWN_MASK);
|
---|
345 | groups.put(GROUPS_ALT2+GROUP_DIRECT3, KeyEvent.META_DOWN_MASK | KeyEvent.CTRL_DOWN_MASK);
|
---|
346 |
|
---|
347 | // (1) System reserved shortcuts
|
---|
348 | Main.platform.initSystemShortcuts();
|
---|
349 | // (2) User defined shortcuts
|
---|
350 | LinkedList<Shortcut> shortcuts = new LinkedList<Shortcut>();
|
---|
351 | for(String s : Main.pref.getAllPrefixCollectionKeys("shortcut.entry.")) {
|
---|
352 | shortcuts.add(new Shortcut(s));
|
---|
353 | }
|
---|
354 | for(Shortcut sc : shortcuts) {
|
---|
355 | if (sc.getAssignedUser()) {
|
---|
356 | registerShortcut(sc);
|
---|
357 | }
|
---|
358 | }
|
---|
359 | // Shortcuts at their default values
|
---|
360 | for(Shortcut sc : shortcuts) {
|
---|
361 | if (!sc.getAssignedUser() && sc.getAssignedDefault()) {
|
---|
362 | registerShortcut(sc);
|
---|
363 | }
|
---|
364 | }
|
---|
365 | // Shortcuts that were automatically moved
|
---|
366 | for(Shortcut sc : shortcuts) {
|
---|
367 | if (!sc.getAssignedUser() && !sc.getAssignedDefault()) {
|
---|
368 | registerShortcut(sc);
|
---|
369 | }
|
---|
370 | }
|
---|
371 | }
|
---|
372 |
|
---|
373 | private static int getGroupModifier(int group) {
|
---|
374 | Integer m = groups.get(group);
|
---|
375 | if(m == null)
|
---|
376 | m = -1;
|
---|
377 | return m;
|
---|
378 | }
|
---|
379 |
|
---|
380 | // shutdown handling
|
---|
381 | public static boolean savePrefs() {
|
---|
382 | boolean changed = false;
|
---|
383 | for (Shortcut sc : shortcuts.values()) {
|
---|
384 | changed = changed | sc.save();
|
---|
385 | }
|
---|
386 | return changed;
|
---|
387 | }
|
---|
388 |
|
---|
389 | // this is used to register a shortcut that was read from the preferences
|
---|
390 | private static void registerShortcut(Shortcut sc) {
|
---|
391 | // put a user configured shortcut in as-is -- unless there's a conflict
|
---|
392 | if(sc.getAssignedUser() && findShortcut(sc.getAssignedKey(),
|
---|
393 | sc.getAssignedModifier()) == null) {
|
---|
394 | shortcuts.put(sc.getShortText(), sc);
|
---|
395 | } else {
|
---|
396 | registerShortcut(sc.getShortText(), sc.getLongText(), sc.getRequestedKey(),
|
---|
397 | sc.getRequestedGroup(), sc.getAssignedModifier(), sc);
|
---|
398 | }
|
---|
399 | }
|
---|
400 |
|
---|
401 | /**
|
---|
402 | * FOR PLATFORMHOOK USE ONLY
|
---|
403 | *
|
---|
404 | * This registers a system shortcut. See PlatformHook for details.
|
---|
405 | */
|
---|
406 | public static Shortcut registerSystemShortcut(String shortText, String longText, int key, int modifier) {
|
---|
407 | if (shortcuts.containsKey(shortText))
|
---|
408 | return shortcuts.get(shortText);
|
---|
409 | Shortcut potentialShortcut = findShortcut(key, modifier);
|
---|
410 | if (potentialShortcut != null) {
|
---|
411 | // this always is a logic error in the hook
|
---|
412 | System.err.println("CONFLICT WITH SYSTEM KEY "+shortText);
|
---|
413 | return null;
|
---|
414 | }
|
---|
415 | potentialShortcut = new Shortcut(shortText, longText, key, RESERVED, key, modifier, true, false);
|
---|
416 | shortcuts.put(shortText, potentialShortcut);
|
---|
417 | return potentialShortcut;
|
---|
418 | }
|
---|
419 |
|
---|
420 | /**
|
---|
421 | * Register a shortcut.
|
---|
422 | *
|
---|
423 | * Here you get your shortcuts from. The parameters are:
|
---|
424 | *
|
---|
425 | * @param shortText an ID. re-use a {@code "system:*"} ID if possible, else use something unique.
|
---|
426 | * {@code "menu:*"} is reserved for menu mnemonics, {@code "core:*"} is reserved for
|
---|
427 | * actions that are part of JOSM's core. Use something like
|
---|
428 | * {@code <pluginname>+":"+<actionname>}.
|
---|
429 | * @param longText this will be displayed in the shortcut preferences dialog. Better
|
---|
430 | * use something the user will recognize...
|
---|
431 | * @param requestedKey the key you'd prefer. Use a {@link KeyEvent KeyEvent.VK_*} constant here.
|
---|
432 | * @param requestedGroup the group this shortcut fits best. This will determine the
|
---|
433 | * modifiers your shortcut will get assigned. Use the {@code GROUP_*}
|
---|
434 | * constants defined above.
|
---|
435 | * @param modifier to register a {@code ctrl+shift} command, use {@see #SHIFT_DEFAULT}.
|
---|
436 | */
|
---|
437 | @Deprecated
|
---|
438 | public static Shortcut registerShortcut(String shortText, String longText, int requestedKey, int requestedGroup, int modifier) {
|
---|
439 | return registerShortcut(shortText, longText, requestedKey, requestedGroup, modifier, null);
|
---|
440 | }
|
---|
441 |
|
---|
442 | /**
|
---|
443 | * Register a shortcut.
|
---|
444 | *
|
---|
445 | * Here you get your shortcuts from. The parameters are:
|
---|
446 | *
|
---|
447 | * @param shortText an ID. re-use a {@code "system:*"} ID if possible, else use something unique.
|
---|
448 | * {@code "menu:*"} is reserved for menu mnemonics, {@code "core:*"} is reserved for
|
---|
449 | * actions that are part of JOSM's core. Use something like
|
---|
450 | * {@code <pluginname>+":"+<actionname>}.
|
---|
451 | * @param longText this will be displayed in the shortcut preferences dialog. Better
|
---|
452 | * use something the user will recognize...
|
---|
453 | * @param requestedKey the key you'd prefer. Use a {@link KeyEvent KeyEvent.VK_*} constant here.
|
---|
454 | * @param requestedGroup the group this shortcut fits best. This will determine the
|
---|
455 | * modifiers your shortcut will get assigned. Use the {@code GROUP_*}
|
---|
456 | * constants defined above.
|
---|
457 | */
|
---|
458 | public static Shortcut registerShortcut(String shortText, String longText, int requestedKey, int requestedGroup) {
|
---|
459 | return registerShortcut(shortText, longText, requestedKey, requestedGroup, null, null);
|
---|
460 | }
|
---|
461 |
|
---|
462 | private static int findModifier(int group, Integer modifier) {
|
---|
463 | Integer defaultModifier = getGroupModifier(group);
|
---|
464 | if(modifier != null) {
|
---|
465 | if(modifier == SHIFT_DEFAULT) {
|
---|
466 | defaultModifier |= KeyEvent.SHIFT_DOWN_MASK;
|
---|
467 | } else {
|
---|
468 | defaultModifier = modifier;
|
---|
469 | }
|
---|
470 | }
|
---|
471 | else if (defaultModifier == null) { // garbage in, no shortcut out
|
---|
472 | defaultModifier = getGroupModifier(NONE);
|
---|
473 | }
|
---|
474 | return defaultModifier;
|
---|
475 | }
|
---|
476 |
|
---|
477 | private static int[] mods = {ALT_CTRL, ALT_SHIFT, CTRL_SHIFT, ALT_CTRL_SHIFT};
|
---|
478 | private static int[] keys = {KeyEvent.VK_F1, KeyEvent.VK_F2, KeyEvent.VK_F3, KeyEvent.VK_F4,
|
---|
479 | KeyEvent.VK_F5, KeyEvent.VK_F6, KeyEvent.VK_F7, KeyEvent.VK_F8,
|
---|
480 | KeyEvent.VK_F9, KeyEvent.VK_F10, KeyEvent.VK_F11, KeyEvent.VK_F12};
|
---|
481 | // and now the workhorse. same parameters as above, just one more: if originalShortcut is not null and
|
---|
482 | // is different from the shortcut that will be assigned, a popup warning will be displayed to the user.
|
---|
483 | // This is used when registering shortcuts that have been visible to the user before (read: have been
|
---|
484 | // read from the preferences file). New shortcuts will never warn, even when they land on some funny
|
---|
485 | // random fallback key like Ctrl+Alt+Shift+Z for "File Open..." <g>
|
---|
486 | private static Shortcut registerShortcut(String shortText, String longText, int requestedKey, int requestedGroup, Integer modifier,
|
---|
487 | Shortcut originalShortcut) {
|
---|
488 | doInit();
|
---|
489 | Integer defaultModifier = findModifier(requestedGroup, modifier);
|
---|
490 | if (shortcuts.containsKey(shortText)) { // a re-register? maybe a sc already read from the preferences?
|
---|
491 | Shortcut sc = shortcuts.get(shortText);
|
---|
492 | sc.setLongText(longText); // or set by the platformHook, in this case the original longText doesn't match the real action
|
---|
493 | sc.saveDefault();
|
---|
494 | return sc;
|
---|
495 | }
|
---|
496 | Shortcut conflict = findShortcut(requestedKey, defaultModifier);
|
---|
497 | if (conflict != null) {
|
---|
498 | for (int m : mods) {
|
---|
499 | for (int k : keys) {
|
---|
500 | int newmodifier = getGroupModifier(m);
|
---|
501 | if ( findShortcut(k, m) == null ) {
|
---|
502 | Shortcut newsc = new Shortcut(shortText, longText, requestedKey, m, k, newmodifier, false, false);
|
---|
503 | System.out.println(tr("Silent shortcut conflict: ''{0}'' moved by ''{1}'' to ''{2}''.",
|
---|
504 | shortText, conflict.getShortText(), newsc.getKeyText()));
|
---|
505 | newsc.saveDefault();
|
---|
506 | shortcuts.put(shortText, newsc);
|
---|
507 | return newsc;
|
---|
508 | }
|
---|
509 | }
|
---|
510 | }
|
---|
511 | } else {
|
---|
512 | Shortcut newsc = new Shortcut(shortText, longText, requestedKey, requestedGroup, requestedKey, defaultModifier, true, false);
|
---|
513 | newsc.saveDefault();
|
---|
514 | shortcuts.put(shortText, newsc);
|
---|
515 | return newsc;
|
---|
516 | }
|
---|
517 |
|
---|
518 | return null;
|
---|
519 | }
|
---|
520 |
|
---|
521 | /**
|
---|
522 | * Replies the platform specific key stroke for the 'Copy' command, i.e.
|
---|
523 | * 'Ctrl-C' on windows or 'Meta-C' on a Mac. null, if the platform specific
|
---|
524 | * copy command isn't known.
|
---|
525 | *
|
---|
526 | * @return the platform specific key stroke for the 'Copy' command
|
---|
527 | */
|
---|
528 | static public KeyStroke getCopyKeyStroke() {
|
---|
529 | Shortcut sc = shortcuts.get("system:copy");
|
---|
530 | if (sc == null) return null;
|
---|
531 | return sc.getKeyStroke();
|
---|
532 | }
|
---|
533 |
|
---|
534 | /**
|
---|
535 | * Replies the platform specific key stroke for the 'Paste' command, i.e.
|
---|
536 | * 'Ctrl-V' on windows or 'Meta-V' on a Mac. null, if the platform specific
|
---|
537 | * paste command isn't known.
|
---|
538 | *
|
---|
539 | * @return the platform specific key stroke for the 'Paste' command
|
---|
540 | */
|
---|
541 | static public KeyStroke getPasteKeyStroke() {
|
---|
542 | Shortcut sc = shortcuts.get("system:paste");
|
---|
543 | if (sc == null) return null;
|
---|
544 | return sc.getKeyStroke();
|
---|
545 | }
|
---|
546 |
|
---|
547 | /**
|
---|
548 | * Replies the platform specific key stroke for the 'Cut' command, i.e.
|
---|
549 | * 'Ctrl-X' on windows or 'Meta-X' on a Mac. null, if the platform specific
|
---|
550 | * 'Cut' command isn't known.
|
---|
551 | *
|
---|
552 | * @return the platform specific key stroke for the 'Cut' command
|
---|
553 | */
|
---|
554 | static public KeyStroke getCutKeyStroke() {
|
---|
555 | Shortcut sc = shortcuts.get("system:cut");
|
---|
556 | if (sc == null) return null;
|
---|
557 | return sc.getKeyStroke();
|
---|
558 | }
|
---|
559 |
|
---|
560 | public boolean isEvent(KeyEvent e) {
|
---|
561 | return getKeyStroke() != null && getKeyStroke().equals(
|
---|
562 | KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers()));
|
---|
563 | }
|
---|
564 | }
|
---|