@@ -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.
0 commit comments