jdk/src/share/classes/javax/accessibility/AccessibleBundle.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2002 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static Hashtable table = new Hashtable();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public AccessibleBundle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * The locale independent name of the state.  This is a programmatic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * name that is not intended to be read by humans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @see #toDisplayString
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    protected String key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Obtains the key as a localized string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * If a localized string cannot be found for the key, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * locale independent key stored in the role will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * This method is intended to be used only by subclasses so that they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * can specify their own resource bundles which contain localized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * strings for their keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param resourceBundleName the name of the resource bundle to use for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param locale the locale for which to obtain a localized string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @return a localized String for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    protected String toDisplayString(String resourceBundleName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                                     Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // loads the resource bundle if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        loadResourceBundle(resourceBundleName, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        // returns the localized string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        Object o = table.get(locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (o != null && o instanceof Hashtable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                Hashtable resourceTable = (Hashtable) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                o = resourceTable.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                if (o != null && o instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    return (String)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Obtains the key as a localized string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * If a localized string cannot be found for the key, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * locale independent key stored in the role will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param locale the locale for which to obtain a localized string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @return a localized String for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public String toDisplayString(Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return toDisplayString(defaultResourceBundleName, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Gets localized string describing the key using the default locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @return a localized String describing the key for the default locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public String toDisplayString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return toDisplayString(Locale.getDefault());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Gets localized string describing the key using the default locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @return a localized String describing the key using the default locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @see #toDisplayString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return toDisplayString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Loads the Accessibility resource bundle if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private void loadResourceBundle(String resourceBundleName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                    Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (! table.contains(locale)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                Hashtable resourceTable = new Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                ResourceBundle bundle = ResourceBundle.getBundle(resourceBundleName, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                Enumeration iter = bundle.getKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                while(iter.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    String key = (String)iter.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    resourceTable.put(key, bundle.getObject(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                table.put(locale, resourceTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                System.err.println("loadResourceBundle: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                // Just return so toDisplayString() returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                // non-localized key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
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
}