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