X Tutup
Skip to content

Commit bdd8fbc

Browse files
committed
rewrite key stroke handling; now multi-platform, overridable by prefs
1 parent 4923432 commit bdd8fbc

File tree

6 files changed

+66
-66
lines changed

6 files changed

+66
-66
lines changed

app/src/processing/app/ui/Editor.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -881,13 +881,8 @@ protected JMenu buildEditMenu() {
881881
undoItem = Toolkit.newJMenuItem(undoAction = new UndoAction(), 'Z');
882882
menu.add(undoItem);
883883

884-
// Gotta follow them interface guidelines
885-
// http://code.google.com/p/processing/issues/detail?id=363
886-
if (Platform.isWindows()) {
887-
redoItem = Toolkit.newJMenuItem(redoAction = new RedoAction(), 'Y');
888-
} else { // Linux and OS X
889-
redoItem = Toolkit.newJMenuItemShift(redoAction = new RedoAction(), 'Z');
890-
}
884+
redoItem = new JMenuItem(redoAction = new RedoAction());
885+
redoItem.setAccelerator(Toolkit.getKeyStrokeExt("menu.edit.redo"));
891886
menu.add(redoItem);
892887

893888
menu.addSeparator();
@@ -956,27 +951,23 @@ public void actionPerformed(ActionEvent e) {
956951
});
957952
menu.add(item);
958953

959-
item = Toolkit.newJMenuItem(Language.text("menu.edit.comment_uncomment"),
960-
Language.text("menu.edit.comment_uncomment.keystroke"));
954+
item = Toolkit.newJMenuItemExt("menu.edit.comment_uncomment");
961955
item.addActionListener(new ActionListener() {
962956
public void actionPerformed(ActionEvent e) {
963957
handleCommentUncomment();
964958
}
965959
});
966960
menu.add(item);
967961

968-
item = Toolkit.newJMenuItem("\u2192 " + Language.text("menu.edit.increase_indent"),
969-
Language.text("menu.edit.increase_indent.keystroke"));
970-
962+
item = Toolkit.newJMenuItemExt("menu.edit.increase_indent");
971963
item.addActionListener(new ActionListener() {
972964
public void actionPerformed(ActionEvent e) {
973965
handleIndentOutdent(true);
974966
}
975967
});
976968
menu.add(item);
977969

978-
item = Toolkit.newJMenuItem("\u2190 " + Language.text("menu.edit.decrease_indent"),
979-
Language.text("menu.edit.decrease_indent.keystroke"));
970+
item = Toolkit.newJMenuItemExt("menu.edit.decrease_indent");
980971
item.addActionListener(new ActionListener() {
981972
public void actionPerformed(ActionEvent e) {
982973
handleIndentOutdent(false);

app/src/processing/app/ui/EditorHeader.java

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -494,51 +494,29 @@ public void actionPerformed(ActionEvent e) {
494494

495495
// KeyEvent.VK_LEFT and VK_RIGHT will make Windows beep
496496

497-
final String prevTab = Language.text("editor.header.previous_tab");
498-
if (Platform.isLinux()) {
499-
item = Toolkit.newJMenuItem(prevTab, KeyEvent.VK_PAGE_UP);
500-
} else {
501-
//item = Toolkit.newJMenuItemAlt(prevTab, KeyEvent.VK_LEFT);
502-
item = Toolkit.newJMenuItem(prevTab, Language.text("editor.header.previous_tab.keystroke"));
503-
}
497+
mapKey = "editor.header.previous_tab";
498+
item = Toolkit.newJMenuItemExt(mapKey);
504499
action = new AbstractAction() {
505500
@Override
506501
public void actionPerformed(ActionEvent e) {
507502
editor.getSketch().handlePrevCode();
508503
}
509504
};
510-
mapKey = "editor.header.previous_tab";
511-
if (Platform.isLinux()) {
512-
keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, Toolkit.SHORTCUT_KEY_MASK);
513-
} else {
514-
//keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, Toolkit.SHORTCUT_ALT_KEY_MASK);
515-
keyStroke = KeyStroke.getKeyStroke(Language.text("editor.header.previous_tab.keystroke"));
516-
}
505+
keyStroke = item.getAccelerator();
517506
inputMap.put(keyStroke, mapKey);
518507
actionMap.put(mapKey, action);
519508
item.addActionListener(action);
520509
menu.add(item);
521510

522-
final String nextTab = Language.text("editor.header.next_tab");
523-
if (Platform.isLinux()) {
524-
item = Toolkit.newJMenuItem(nextTab, KeyEvent.VK_PAGE_DOWN);
525-
} else {
526-
//item = Toolkit.newJMenuItemAlt(nextTab, KeyEvent.VK_RIGHT);
527-
item = Toolkit.newJMenuItem(nextTab, Language.text("editor.header.next_tab.keystroke"));
528-
}
511+
mapKey = "editor.header.next_tab";
512+
item = Toolkit.newJMenuItemExt(mapKey);
529513
action = new AbstractAction() {
530514
@Override
531515
public void actionPerformed(ActionEvent e) {
532516
editor.getSketch().handleNextCode();
533517
}
534518
};
535-
mapKey = "editor.header.next_tab";
536-
if (Platform.isLinux()) {
537-
keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, Toolkit.SHORTCUT_KEY_MASK);
538-
} else {
539-
//keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, Toolkit.SHORTCUT_ALT_KEY_MASK);
540-
keyStroke = KeyStroke.getKeyStroke(Language.text("editor.header.next_tab.keystroke"));
541-
}
519+
keyStroke = item.getAccelerator();
542520
inputMap.put(keyStroke, mapKey);
543521
actionMap.put(mapKey, action);
544522
item.addActionListener(action);

app/src/processing/app/ui/Toolkit.java

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ public class Toolkit {
103103
static public final KeyStroke WINDOW_CLOSE_KEYSTROKE =
104104
KeyStroke.getKeyStroke('W', SHORTCUT_KEY_MASK);
105105

106+
static final String BAD_KEYSTROKE =
107+
"'%s' is not understood, please re-read the Java reference for KeyStroke";
106108

107109
/**
108110
* Standardized width for buttons. Mac OS X 10.3 wants 70 as its default,
@@ -118,16 +120,36 @@ static public int getButtonWidth() {
118120

119121

120122
/**
121-
* A software engineer, somewhere, needs to have their abstraction
122-
* taken away. Who crafts the sort of API that would require a
123-
* five-line helper function just to set the shortcut key for a
124-
* menu item?
123+
* Return the correct KeyStroke per locale and platform.
124+
* Also checks for any additional overrides in preferences.txt.
125+
* @param base the localization key for the menu item
126+
* (.keystroke and .platform will be added to the end)
127+
* @return KeyStroke for base + .keystroke + .platform
128+
* (or the value from preferences) or null if none found
125129
*/
126-
static public JMenuItem newJMenuItem(String title, int what) {
127-
JMenuItem menuItem = new JMenuItem(title);
128-
int modifiers = awtToolkit.getMenuShortcutKeyMask();
129-
menuItem.setAccelerator(KeyStroke.getKeyStroke(what, modifiers));
130-
return menuItem;
130+
static public KeyStroke getKeyStrokeExt(String base) {
131+
String key = base + ".keystroke";
132+
133+
// see if there's an override in preferences.txt
134+
String sequence = Preferences.get(key);
135+
if (sequence != null) {
136+
KeyStroke ks = KeyStroke.getKeyStroke(sequence);
137+
if (ks != null) {
138+
return ks; // user did good, we're all set
139+
140+
} else {
141+
System.err.format(BAD_KEYSTROKE, sequence);
142+
}
143+
}
144+
145+
sequence = Language.text(key + "." + Platform.getName());
146+
KeyStroke ks = KeyStroke.getKeyStroke(sequence);
147+
if (ks == null) {
148+
// this can only happen if user has screwed up their language files
149+
System.err.format(BAD_KEYSTROKE, sequence);
150+
//return KeyStroke.getKeyStroke(0, 0); // badness
151+
}
152+
return ks;
131153
}
132154

133155

@@ -138,22 +160,30 @@ static public JMenuItem newJMenuItem(String title, int what) {
138160
* @param sequence the name, as outlined by the KeyStroke API
139161
* @param fallback what to use if getKeyStroke() comes back null
140162
*/
141-
static public JMenuItem newJMenuItem(String title,
142-
String sequence) {
143-
JMenuItem menuItem = new JMenuItem(title);
144-
KeyStroke ks = KeyStroke.getKeyStroke(sequence);
163+
static public JMenuItem newJMenuItemExt(String base) {
164+
JMenuItem menuItem = new JMenuItem(Language.text(base));
165+
KeyStroke ks = getKeyStrokeExt(base); // will print error if necessary
145166
if (ks != null) {
146167
menuItem.setAccelerator(ks);
147-
148-
} else {
149-
System.err.println("'" + sequence + "' is not understood, " +
150-
"pleae re-read the Java reference for KeyStroke");
151-
//ks = KeyStroke.getKeyStroke(fallback);
152168
}
153169
return menuItem;
154170
}
155171

156172

173+
/**
174+
* A software engineer, somewhere, needs to have their abstraction
175+
* taken away. Who crafts the sort of API that would require a
176+
* five-line helper function just to set the shortcut key for a
177+
* menu item?
178+
*/
179+
static public JMenuItem newJMenuItem(String title, int what) {
180+
JMenuItem menuItem = new JMenuItem(title);
181+
int modifiers = awtToolkit.getMenuShortcutKeyMask();
182+
menuItem.setAccelerator(KeyStroke.getKeyStroke(what, modifiers));
183+
return menuItem;
184+
}
185+
186+
157187
/**
158188
* @param action: use an Action, which sets the title, reaction
159189
* and enabled-ness all by itself.

build/shared/lib/languages/PDE.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ menu.file.quit = Quit
3131
menu.edit = Edit
3232
menu.edit.undo = Undo
3333
menu.edit.redo = Redo
34+
menu.edit.redo.keystroke.macosx = shift meta pressed Z
35+
menu.edit.redo.keystroke.windows = meta pressed Y
36+
menu.edit.redo.keystroke.linux = shift ctrl pressed Z
3437
menu.edit.action.addition = addition
3538
menu.edit.action.deletion = deletion
3639
menu.edit.cut = Cut

java/src/processing/mode/java/JavaEditor.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,8 +1384,7 @@ public void actionPerformed(ActionEvent e) {
13841384
// });
13851385
// debugMenu.add(item);
13861386

1387-
item = Toolkit.newJMenuItem(Language.text("menu.debug.step"),
1388-
Language.text("menu.debug.step.keystroke"));
1387+
item = Toolkit.newJMenuItemExt("menu.debug.step");
13891388
item.addActionListener(new ActionListener() {
13901389
public void actionPerformed(ActionEvent e) {
13911390
handleStep(0);
@@ -1394,8 +1393,7 @@ public void actionPerformed(ActionEvent e) {
13941393
debugMenu.add(item);
13951394
item.setEnabled(false);
13961395

1397-
item = Toolkit.newJMenuItem(Language.text("menu.debug.step_into"),
1398-
Language.text("menu.debug.step_into.keystroke"));
1396+
item = Toolkit.newJMenuItemExt("menu.debug.step_into");
13991397
item.addActionListener(new ActionListener() {
14001398
public void actionPerformed(ActionEvent e) {
14011399
handleStep(ActionEvent.SHIFT_MASK);
@@ -1404,8 +1402,7 @@ public void actionPerformed(ActionEvent e) {
14041402
debugMenu.add(item);
14051403
item.setEnabled(false);
14061404

1407-
item = Toolkit.newJMenuItem(Language.text("menu.debug.step_out"),
1408-
Language.text("menu.debug.step_out.keystroke"));
1405+
item = Toolkit.newJMenuItemExt("menu.debug.step_out");
14091406
item.addActionListener(new ActionListener() {
14101407
public void actionPerformed(ActionEvent e) {
14111408
handleStep(ActionEvent.ALT_MASK);

todo.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
0268 (3.5.2)
22
_ shortcuts not working on Windows/Linux (using meta)
33
_ https://github.com/processing/processing/issues/5763
4+
_ update https://github.com/processing/processing/wiki/Localization#shortcuts-and-key-bindings
45

56

67
_ Find in Reference disabled for various keywords (draw, for, if, catch, while)

0 commit comments

Comments
 (0)
X Tutup