< prev index next >

src/java.desktop/share/classes/javax/accessibility/AccessibleBundle.java

Print this page


   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     }


  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     }


 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.Enumeration;
  29 import java.util.Hashtable;
  30 import java.util.Locale;
  31 import java.util.MissingResourceException;
  32 import java.util.ResourceBundle;

  33 import sun.awt.AWTAccessor;
  34 
  35 /**
  36  * Base class used to maintain a strongly typed enumeration. This is the
  37  * superclass of {@link AccessibleState} and {@link AccessibleRole}.
  38  * <p>
  39  * The {@link #toDisplayString()} method allows you to obtain the localized
  40  * string for a locale independent key from a predefined {@code ResourceBundle}
  41  * for the keys defined in this class. This localized string is intended to be
  42  * readable by humans.
  43  *
  44  * @author Willie Walker
  45  * @author Peter Korn
  46  * @author Lynn Monsanto
  47  * @see AccessibleRole
  48  * @see AccessibleState
  49  */
  50 public abstract class AccessibleBundle {
  51 
  52     private static Hashtable<String, Hashtable<String, Object>> table = new Hashtable<>();
  53 
  54     private final String defaultResourceBundleName
  55         = "com.sun.accessibility.internal.resources.accessibility";
  56 
  57     static {
  58         AWTAccessor.setAccessibleBundleAccessor(
  59                 new AWTAccessor.AccessibleBundleAccessor() {
  60 
  61                     @Override
  62                     public String getKey(AccessibleBundle accessibleBundle) {
  63                         return accessibleBundle.key;
  64                     }
  65                 });
  66     }
  67 
  68     /**
  69      * Construct an {@code AccessibleBundle}.
  70      */
  71     public AccessibleBundle() {
  72     }


  76      * that is not intended to be read by humans.
  77      *
  78      * @see #toDisplayString
  79      */
  80     protected String key = null;
  81 
  82     /**
  83      * Obtains the key as a localized string. If a localized string cannot be
  84      * found for the key, the locale independent key stored in the role will be
  85      * returned. This method is intended to be used only by subclasses so that
  86      * they can specify their own resource bundles which contain localized
  87      * strings for their keys.
  88      *
  89      * @param  resourceBundleName the name of the resource bundle to use for
  90      *         lookup
  91      * @param  locale the locale for which to obtain a localized string
  92      * @return a localized string for the key
  93      */
  94     protected String toDisplayString(String resourceBundleName,
  95                                      Locale locale) {

  96         // loads the resource bundle if necessary
  97         loadResourceBundle(resourceBundleName, locale);
  98 
  99         // use the resource bundle name and locale based tag
 100         String hashCodeKey = resourceBundleName + locale.toLanguageTag();
 101 
 102         // returns the localized string
 103         Hashtable<String, Object> ht = table.get(hashCodeKey);
 104         if (ht != null) {
 105             Object o = ht.get(key);
 106             if (o != null && o instanceof String) {
 107                 return (String)o;
 108             }
 109         }
 110         return key;
 111     }
 112 
 113     /**
 114      * Obtains the key as a localized string. If a localized string cannot be
 115      * found for the key, the locale independent key stored in the role will be
 116      * returned.
 117      *
 118      * @param  locale the locale for which to obtain a localized string
 119      * @return a localized string for the key
 120      */
 121     public String toDisplayString(Locale locale) {
 122         return toDisplayString(defaultResourceBundleName, locale);
 123     }


 129      */
 130     public String toDisplayString() {
 131         return toDisplayString(Locale.getDefault());
 132     }
 133 
 134     /**
 135      * Gets localized string describing the key using the default locale.
 136      *
 137      * @return a localized string describing the key using the default locale
 138      * @see #toDisplayString
 139      */
 140     public String toString() {
 141         return toDisplayString();
 142     }
 143 
 144     /**
 145      * Loads the Accessibility resource bundle if necessary.
 146      */
 147     private void loadResourceBundle(String resourceBundleName,
 148                                     Locale locale) {
 149         // use the resource bundle name and locale based tag
 150         String hashCodeKey = resourceBundleName + locale.toLanguageTag();
 151 
 152         if (! table.containsKey(hashCodeKey)) {
 153             try {
 154                 Hashtable<String, Object> resourceTable = new Hashtable<>();
 155 
 156                 ResourceBundle bundle = ResourceBundle.getBundle(resourceBundleName, locale);
 157 
 158                 Enumeration<String> iter = bundle.getKeys();
 159                 while(iter.hasMoreElements()) {
 160                     String key = iter.nextElement();
 161                     resourceTable.put(key, bundle.getObject(key));
 162                 }
 163 
 164                 table.put(hashCodeKey, resourceTable);
 165             }
 166             catch (MissingResourceException e) {
 167                 System.err.println("loadResourceBundle: " + e);
 168                 // Just return so toDisplayString() returns the
 169                 // non-localized key.
 170                 return;
 171             }
 172         }
 173     }
 174 }
< prev index next >