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 }