1 /* 2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.accessibility; 27 28 import java.util.Enumeration; 29 import java.util.Hashtable; 30 import java.util.Locale; 31 import java.util.MissingResourceException; 32 import java.util.ResourceBundle; 33 34 import sun.awt.AWTAccessor; 35 36 /** 37 * Base class used to maintain a strongly typed enumeration. This is the 38 * superclass of {@link AccessibleState} and {@link AccessibleRole}. 39 * <p> 40 * The {@link #toDisplayString()} method allows you to obtain the localized 41 * string for a locale independent key from a predefined {@code ResourceBundle} 42 * for the keys defined in this class. This localized string is intended to be 43 * readable by humans. 44 * 45 * @author Willie Walker 46 * @author Peter Korn 47 * @author Lynn Monsanto 48 * @see AccessibleRole 49 * @see AccessibleState 50 */ 51 public abstract class AccessibleBundle { 52 53 private static Hashtable<Locale, Hashtable<String, Object>> table = new Hashtable<>(); 54 55 private final String defaultResourceBundleName 56 = "com.sun.accessibility.internal.resources.accessibility"; 57 58 static { 59 AWTAccessor.setAccessibleBundleAccessor( 60 new AWTAccessor.AccessibleBundleAccessor() { 61 62 @Override 63 public String getKey(AccessibleBundle accessibleBundle) { 64 return accessibleBundle.key; 65 } 66 }); 67 } 68 69 /** 70 * Construct an {@code AccessibleBundle}. 71 */ 72 public AccessibleBundle() { 73 } 74 75 /** 76 * The locale independent name of the state. This is a programmatic name 77 * that is not intended to be read by humans. 78 * 79 * @see #toDisplayString 80 */ 81 protected String key = null; 82 83 /** 84 * Obtains the key as a localized string. If a localized string cannot be 85 * found for the key, the locale independent key stored in the role will be 86 * returned. This method is intended to be used only by subclasses so that 87 * they can specify their own resource bundles which contain localized 88 * strings for their keys. 89 * 90 * @param resourceBundleName the name of the resource bundle to use for 91 * lookup 92 * @param locale the locale for which to obtain a localized string 93 * @return a localized string for the key 94 */ 95 protected String toDisplayString(String resourceBundleName, 96 Locale locale) { 97 98 // loads the resource bundle if necessary 99 loadResourceBundle(resourceBundleName, locale); 100 101 // returns the localized string 102 Hashtable<String, Object> ht = table.get(locale); 103 if (ht != null) { 104 Object o = ht.get(key); 105 if (o != null && o instanceof String) { 106 return (String)o; 107 } 108 } 109 return key; 110 } 111 112 /** 113 * Obtains the key as a localized string. If a localized string cannot be 114 * found for the key, the locale independent key stored in the role will be 115 * returned. 116 * 117 * @param locale the locale for which to obtain a localized string 118 * @return a localized string for the key 119 */ 120 public String toDisplayString(Locale locale) { 121 return toDisplayString(defaultResourceBundleName, locale); 122 } 123 124 /** 125 * Gets localized string describing the key using the default locale. 126 * 127 * @return a localized string describing the key using the default locale 128 */ 129 public String toDisplayString() { 130 return toDisplayString(Locale.getDefault()); 131 } 132 133 /** 134 * Gets localized string describing the key using the default locale. 135 * 136 * @return a localized string describing the key using the default locale 137 * @see #toDisplayString 138 */ 139 public String toString() { 140 return toDisplayString(); 141 } 142 143 /** 144 * Loads the Accessibility resource bundle if necessary. 145 */ 146 private void loadResourceBundle(String resourceBundleName, 147 Locale locale) { 148 if (! table.containsKey(locale)) { 149 150 try { 151 Hashtable<String, Object> resourceTable = new Hashtable<>(); 152 153 ResourceBundle bundle = ResourceBundle.getBundle(resourceBundleName, locale); 154 155 Enumeration<String> iter = bundle.getKeys(); 156 while(iter.hasMoreElements()) { 157 String key = iter.nextElement(); 158 resourceTable.put(key, bundle.getObject(key)); 159 } 160 161 table.put(locale, resourceTable); 162 } 163 catch (MissingResourceException e) { 164 System.err.println("loadResourceBundle: " + e); 165 // Just return so toDisplayString() returns the 166 // non-localized key. 167 return; 168 } 169 } 170 } 171 } | 1 /* 2 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.accessibility; 27 28 import java.util.Locale; 29 import java.util.MissingResourceException; 30 import java.util.ResourceBundle; 31 32 import sun.awt.AWTAccessor; 33 34 /** 35 * Base class used to maintain a strongly typed enumeration. This is the 36 * superclass of {@link AccessibleState} and {@link AccessibleRole}. 37 * <p> 38 * The {@link #toDisplayString()} method allows you to obtain the localized 39 * string for a locale independent key from a predefined {@code ResourceBundle} 40 * for the keys defined in this class. This localized string is intended to be 41 * readable by humans. 42 * 43 * @author Willie Walker 44 * @author Peter Korn 45 * @author Lynn Monsanto 46 * @see AccessibleRole 47 * @see AccessibleState 48 */ 49 public abstract class AccessibleBundle { 50 51 private final String defaultResourceBundleName 52 = "com.sun.accessibility.internal.resources.accessibility"; 53 54 static { 55 AWTAccessor.setAccessibleBundleAccessor( 56 new AWTAccessor.AccessibleBundleAccessor() { 57 58 @Override 59 public String getKey(AccessibleBundle accessibleBundle) { 60 return accessibleBundle.key; 61 } 62 }); 63 } 64 65 /** 66 * Construct an {@code AccessibleBundle}. 67 */ 68 public AccessibleBundle() { 69 } 70 71 /** 72 * The locale independent name of the state. This is a programmatic name 73 * that is not intended to be read by humans. 74 * 75 * @see #toDisplayString 76 */ 77 protected String key = null; 78 79 /** 80 * Obtains the key as a localized string. If a localized string cannot be 81 * found for the key, the locale independent key stored in the role will be 82 * returned. This method is intended to be used only by subclasses so that 83 * they can specify their own resource bundles which contain localized 84 * strings for their keys. 85 * 86 * @param name the name of the resource bundle to use for lookup 87 * @param locale the locale for which to obtain a localized string 88 * @return a localized string for the key 89 */ 90 protected String toDisplayString(final String name, final Locale locale) { 91 try { 92 return ResourceBundle.getBundle(name, locale).getString(key); 93 } catch (ClassCastException | MissingResourceException ignored) { 94 return key; // return the non-localized key 95 } 96 } 97 98 /** 99 * Obtains the key as a localized string. If a localized string cannot be 100 * found for the key, the locale independent key stored in the role will be 101 * returned. 102 * 103 * @param locale the locale for which to obtain a localized string 104 * @return a localized string for the key 105 */ 106 public String toDisplayString(Locale locale) { 107 return toDisplayString(defaultResourceBundleName, locale); 108 } 109 110 /** 111 * Gets localized string describing the key using the default locale. 112 * 113 * @return a localized string describing the key using the default locale 114 */ 115 public String toDisplayString() { 116 return toDisplayString(Locale.getDefault()); 117 } 118 119 /** 120 * Gets localized string describing the key using the default locale. 121 * 122 * @return a localized string describing the key using the default locale 123 * @see #toDisplayString 124 */ 125 public String toString() { 126 return toDisplayString(); 127 } 128 } |