Path: blob/master/src/java.desktop/share/classes/javax/accessibility/AccessibleBundle.java
41153 views
/*1* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.accessibility;2627import java.util.Locale;28import java.util.MissingResourceException;29import java.util.ResourceBundle;3031import sun.awt.AWTAccessor;3233/**34* Base class used to maintain a strongly typed enumeration. This is the35* superclass of {@link AccessibleState} and {@link AccessibleRole}.36* <p>37* The {@link #toDisplayString()} method allows you to obtain the localized38* string for a locale independent key from a predefined {@code ResourceBundle}39* for the keys defined in this class. This localized string is intended to be40* readable by humans.41*42* @author Willie Walker43* @author Peter Korn44* @author Lynn Monsanto45* @see AccessibleRole46* @see AccessibleState47*/48public abstract class AccessibleBundle {4950private final String defaultResourceBundleName51= "com.sun.accessibility.internal.resources.accessibility";5253static {54AWTAccessor.setAccessibleBundleAccessor(55new AWTAccessor.AccessibleBundleAccessor() {5657@Override58public String getKey(AccessibleBundle accessibleBundle) {59return accessibleBundle.key;60}61});62}6364/**65* Construct an {@code AccessibleBundle}.66*/67public AccessibleBundle() {68}6970/**71* The locale independent name of the state. This is a programmatic name72* that is not intended to be read by humans.73*74* @see #toDisplayString75*/76protected String key = null;7778/**79* Obtains the key as a localized string. If a localized string cannot be80* found for the key, the locale independent key stored in the role will be81* returned. This method is intended to be used only by subclasses so that82* they can specify their own resource bundles which contain localized83* strings for their keys.84*85* @param name the name of the resource bundle to use for lookup86* @param locale the locale for which to obtain a localized string87* @return a localized string for the key88*/89protected String toDisplayString(final String name, final Locale locale) {90try {91return ResourceBundle.getBundle(name, locale).getString(key);92} catch (ClassCastException | MissingResourceException ignored) {93return key; // return the non-localized key94}95}9697/**98* Obtains the key as a localized string. If a localized string cannot be99* found for the key, the locale independent key stored in the role will be100* returned.101*102* @param locale the locale for which to obtain a localized string103* @return a localized string for the key104*/105public String toDisplayString(Locale locale) {106return toDisplayString(defaultResourceBundleName, locale);107}108109/**110* Gets localized string describing the key using the default locale.111*112* @return a localized string describing the key using the default locale113*/114public String toDisplayString() {115return toDisplayString(Locale.getDefault());116}117118/**119* Gets localized string describing the key using the default locale.120*121* @return a localized string describing the key using the default locale122* @see #toDisplayString123*/124public String toString() {125return toDisplayString();126}127}128129130