jdk/src/share/classes/javax/swing/UIDefaults.java
changeset 12287 5b5175de639f
parent 7668 d4a77089c587
child 16073 da699ce2f8d2
--- a/jdk/src/share/classes/javax/swing/UIDefaults.java	Tue Apr 03 16:14:05 2012 +0400
+++ b/jdk/src/share/classes/javax/swing/UIDefaults.java	Wed Apr 04 16:27:29 2012 +0400
@@ -297,7 +297,7 @@
         Map<String, Object> values = resourceCache.get(l);
 
         if (values == null) {
-            values = new HashMap<String, Object>();
+            values = new TextAndMnemonicHashMap();
             for (int i=resourceBundles.size()-1; i >= 0; i--) {
                 String bundleName = resourceBundles.get(i);
                 try {
@@ -1215,4 +1215,120 @@
             return null;
         }
     }
+
+    /**
+     * <code>TextAndMnemonicHashMap</code> stores swing resource strings. Many of strings
+     * can have a mnemonic. For example:
+     *   FileChooser.saveButton.textAndMnemonic=&Save
+     * For this case method get returns "Save" for the key "FileChooser.saveButtonText" and
+     * mnemonic "S" for the key "FileChooser.saveButtonMnemonic"
+     *
+     * There are several patterns for the text and mnemonic suffixes which are checked by the
+     * <code>TextAndMnemonicHashMap</code> class.
+     * Patterns which are converted to the xxx.textAndMnemonic key:
+     * (xxxNameText, xxxNameMnemonic)
+     * (xxxNameText, xxxMnemonic)
+     * (xxx.nameText, xxx.mnemonic)
+     * (xxxText, xxxMnemonic)
+     *
+     * These patterns can have a mnemonic index in format
+     * (xxxDisplayedMnemonicIndex)
+     *
+     * Pattern which is converted to the xxx.titleAndMnemonic key:
+     * (xxxTitle, xxxMnemonic)
+     *
+     */
+    private static class TextAndMnemonicHashMap extends HashMap<String, Object> {
+
+        static final String AND_MNEMONIC = "AndMnemonic";
+        static final String TITLE_SUFFIX = ".titleAndMnemonic";
+        static final String TEXT_SUFFIX = ".textAndMnemonic";
+
+        @Override
+        public Object get(Object key) {
+
+            Object value = super.get(key);
+
+            if (value == null) {
+
+                boolean checkTitle = false;
+
+                String stringKey = key.toString();
+                String compositeKey = null;
+
+                if (stringKey.endsWith(AND_MNEMONIC)) {
+                    return null;
+                }
+
+                if (stringKey.endsWith(".mnemonic")) {
+                    compositeKey = composeKey(stringKey, 9, TEXT_SUFFIX);
+                } else if (stringKey.endsWith("NameMnemonic")) {
+                    compositeKey = composeKey(stringKey, 12, TEXT_SUFFIX);
+                } else if (stringKey.endsWith("Mnemonic")) {
+                    compositeKey = composeKey(stringKey, 8, TEXT_SUFFIX);
+                    checkTitle = true;
+                }
+
+                if (compositeKey != null) {
+                    value = super.get(compositeKey);
+                    if (value == null && checkTitle) {
+                        compositeKey = composeKey(stringKey, 8, TITLE_SUFFIX);
+                        value = super.get(compositeKey);
+                    }
+
+                    return value == null ? null : getMnemonicFromProperty(value.toString());
+                }
+
+                if (stringKey.endsWith("NameText")) {
+                    compositeKey = composeKey(stringKey, 8, TEXT_SUFFIX);
+                } else if (stringKey.endsWith(".nameText")) {
+                    compositeKey = composeKey(stringKey, 9, TEXT_SUFFIX);
+                } else if (stringKey.endsWith("Text")) {
+                    compositeKey = composeKey(stringKey, 4, TEXT_SUFFIX);
+                } else if (stringKey.endsWith("Title")) {
+                    compositeKey = composeKey(stringKey, 5, TITLE_SUFFIX);
+                }
+
+                if (compositeKey != null) {
+                    value = super.get(compositeKey);
+                    return value == null ? null : getTextFromProperty(value.toString());
+                }
+
+                if (stringKey.endsWith("DisplayedMnemonicIndex")) {
+                    compositeKey = composeKey(stringKey, 22, TEXT_SUFFIX);
+                    value = super.get(compositeKey);
+                    if (value == null) {
+                        compositeKey = composeKey(stringKey, 22, TITLE_SUFFIX);
+                        value = super.get(compositeKey);
+                    }
+                    return value == null ? null : getIndexFromProperty(value.toString());
+                }
+            }
+
+            return value;
+        }
+
+        String composeKey(String key, int reduce, String sufix) {
+            return key.substring(0, key.length() - reduce) + sufix;
+        }
+
+        String getTextFromProperty(String text) {
+            return text.replace("&", "");
+        }
+
+        String getMnemonicFromProperty(String text) {
+            int index = text.indexOf('&');
+            if (0 <= index && index < text.length() - 1) {
+                char c = text.charAt(index + 1);
+                return Integer.toString((int) Character.toUpperCase(c));
+            }
+            return null;
+        }
+
+        String getIndexFromProperty(String text) {
+            int index = text.indexOf('&');
+            return (index == -1) ? null : Integer.toString(index);
+        }
+    }
+
 }