KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > accessibility > AccessibleBundle


1 /*
2  * @(#)AccessibleBundle.java 1.21 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.accessibility;
9
10 import java.util.Enumeration JavaDoc;
11 import java.util.Hashtable JavaDoc;
12 import java.util.Vector JavaDoc;
13 import java.util.Locale JavaDoc;
14 import java.util.MissingResourceException JavaDoc;
15 import java.util.ResourceBundle JavaDoc;
16
17 /**
18  * <p>Base class used to maintain a strongly typed enumeration. This is
19  * the superclass of {@link AccessibleState} and {@link AccessibleRole}.
20  * <p>The toDisplayString method allows you to obtain the localized string
21  * for a locale independent key from a predefined ResourceBundle for the
22  * keys defined in this class. This localized string is intended to be
23  * readable by humans.
24  *
25  * @see AccessibleRole
26  * @see AccessibleState
27  *
28  * @version 1.12 10/05/99
29  * @author Willie Walker
30  * @author Peter Korn
31  * @author Lynn Monsanto
32  */

33 public abstract class AccessibleBundle {
34
35     private static Hashtable JavaDoc table = new Hashtable JavaDoc();
36     private final String JavaDoc defaultResourceBundleName
37     = "com.sun.accessibility.internal.resources.accessibility";
38
39     public AccessibleBundle() {
40     }
41     
42     /**
43      * The locale independent name of the state. This is a programmatic
44      * name that is not intended to be read by humans.
45      * @see #toDisplayString
46      */

47     protected String JavaDoc key = null;
48
49     /**
50      * Obtains the key as a localized string.
51      * If a localized string cannot be found for the key, the
52      * locale independent key stored in the role will be returned.
53      * This method is intended to be used only by subclasses so that they
54      * can specify their own resource bundles which contain localized
55      * strings for their keys.
56      * @param resourceBundleName the name of the resource bundle to use for
57      * lookup
58      * @param locale the locale for which to obtain a localized string
59      * @return a localized String for the key.
60      */

61     protected String JavaDoc toDisplayString(String JavaDoc resourceBundleName,
62                          Locale JavaDoc locale) {
63
64     // loads the resource bundle if necessary
65
loadResourceBundle(resourceBundleName, locale);
66
67     // returns the localized string
68
Object JavaDoc o = table.get(locale);
69     if (o != null && o instanceof Hashtable JavaDoc) {
70                 Hashtable JavaDoc resourceTable = (Hashtable JavaDoc) o;
71                 o = resourceTable.get(key);
72                 
73                 if (o != null && o instanceof String JavaDoc) {
74                     return (String JavaDoc)o;
75                 }
76     }
77     return key;
78     }
79
80     /**
81      * Obtains the key as a localized string.
82      * If a localized string cannot be found for the key, the
83      * locale independent key stored in the role will be returned.
84      *
85      * @param locale the locale for which to obtain a localized string
86      * @return a localized String for the key.
87      */

88     public String JavaDoc toDisplayString(Locale JavaDoc locale) {
89         return toDisplayString(defaultResourceBundleName, locale);
90     }
91
92     /**
93      * Gets localized string describing the key using the default locale.
94      * @return a localized String describing the key for the default locale
95      */

96     public String JavaDoc toDisplayString() {
97         return toDisplayString(Locale.getDefault());
98     }
99
100     /**
101      * Gets localized string describing the key using the default locale.
102      * @return a localized String describing the key using the default locale
103      * @see #toDisplayString
104      */

105     public String JavaDoc toString() {
106         return toDisplayString();
107     }
108
109     /*
110      * Loads the Accessibility resource bundle if necessary.
111      */

112     private void loadResourceBundle(String JavaDoc resourceBundleName,
113                     Locale JavaDoc locale) {
114     if (! table.contains(locale)) {
115                 
116         try {
117                 Hashtable JavaDoc resourceTable = new Hashtable JavaDoc();
118         
119                 ResourceBundle JavaDoc bundle = ResourceBundle.getBundle(resourceBundleName, locale);
120         
121                 Enumeration JavaDoc iter = bundle.getKeys();
122         while(iter.hasMoreElements()) {
123             String JavaDoc key = (String JavaDoc)iter.nextElement();
124             resourceTable.put(key, bundle.getObject(key));
125         }
126
127                 table.put(locale, resourceTable);
128         }
129             catch (MissingResourceException JavaDoc e) {
130                 System.err.println("loadResourceBundle: " + e);
131         // Just return so toDisplayString() returns the
132
// non-localized key.
133
return;
134         }
135     }
136     }
137
138 }
139
Popular Tags