1 /* 2 * Copyright (c) 1997, 2002, 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.Vector; 31 import java.util.Locale; 32 import java.util.MissingResourceException; 33 import java.util.ResourceBundle; 34 35 /** 36 * <p>Base class used to maintain a strongly typed enumeration. This is 37 * the superclass of {@link AccessibleState} and {@link AccessibleRole}. 38 * <p>The toDisplayString method allows you to obtain the localized string 39 * for a locale independent key from a predefined ResourceBundle for the 40 * keys defined in this class. This localized string is intended to be 41 * readable by humans. 42 * 43 * @see AccessibleRole 44 * @see AccessibleState 45 * 46 * @author Willie Walker 47 * @author Peter Korn 48 * @author Lynn Monsanto 49 */ 50 public abstract class AccessibleBundle { 51 52 private static Hashtable table = new Hashtable(); 53 private final String defaultResourceBundleName 54 = "com.sun.accessibility.internal.resources.accessibility"; 55 56 public AccessibleBundle() { 57 } 58 59 /** 60 * The locale independent name of the state. This is a programmatic 61 * name that is not intended to be read by humans. 62 * @see #toDisplayString 63 */ 64 protected String key = null; 65 66 /** 67 * Obtains the key as a localized string. 68 * If a localized string cannot be found for the key, the 69 * locale independent key stored in the role will be returned. 70 * This method is intended to be used only by subclasses so that they 71 * can specify their own resource bundles which contain localized 72 * strings for their keys. 73 * @param resourceBundleName the name of the resource bundle to use for 74 * lookup 75 * @param locale the locale for which to obtain a localized string 76 * @return a localized String for the key. 77 */ 78 protected String toDisplayString(String resourceBundleName, 79 Locale locale) { 80 81 // loads the resource bundle if necessary 82 loadResourceBundle(resourceBundleName, locale); 83 84 // returns the localized string 85 Object o = table.get(locale); 86 if (o != null && o instanceof Hashtable) { 87 Hashtable resourceTable = (Hashtable) o; 88 o = resourceTable.get(key); 89 90 if (o != null && o instanceof String) { 91 return (String)o; 92 } 93 } 94 return key; 95 } 96 97 /** 98 * Obtains the key as a localized string. 99 * If a localized string cannot be found for the key, the 100 * locale independent key stored in the role will be returned. 101 * 102 * @param locale the locale for which to obtain a localized string 103 * @return a localized String for the key. 104 */ 105 public String toDisplayString(Locale locale) { 106 return toDisplayString(defaultResourceBundleName, locale); 107 } 108 109 /** 110 * Gets localized string describing the key using the default locale. 111 * @return a localized String describing the key for the default locale 112 */ 113 public String toDisplayString() { 114 return toDisplayString(Locale.getDefault()); 115 } 116 117 /** 118 * Gets localized string describing the key using the default locale. 119 * @return a localized String describing the key using the default locale 120 * @see #toDisplayString 121 */ 122 public String toString() { 123 return toDisplayString(); 124 } 125 126 /* 127 * Loads the Accessibility resource bundle if necessary. 128 */ 129 private void loadResourceBundle(String resourceBundleName, 130 Locale locale) { 131 if (! table.contains(locale)) { 132 133 try { 134 Hashtable resourceTable = new Hashtable(); 135 136 ResourceBundle bundle = ResourceBundle.getBundle(resourceBundleName, locale); 137 138 Enumeration iter = bundle.getKeys(); 139 while(iter.hasMoreElements()) { 140 String key = (String)iter.nextElement(); 141 resourceTable.put(key, bundle.getObject(key)); 142 } 143 144 table.put(locale, resourceTable); 145 } 146 catch (MissingResourceException e) { 147 System.err.println("loadResourceBundle: " + e); 148 // Just return so toDisplayString() returns the 149 // non-localized key. 150 return; 151 } 152 } 153 } 154 155 }