author | ptbrunet |
Thu, 26 Jun 2014 12:05:57 +0400 | |
changeset 25553 | 1082bb71e6c5 |
parent 19215 | 42891cf030cb |
permissions | -rw-r--r-- |
2 | 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 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package javax.accessibility; |
|
27 |
||
28 |
import java.util.Enumeration; |
|
29 |
import java.util.Hashtable; |
|
30 |
import java.util.Vector; |
|
31 |
import java.util.Locale; |
|
32 |
import java.util.MissingResourceException; |
|
33 |
import java.util.ResourceBundle; |
|
34 |
||
35 |
/** |
|
36 |
* <p>Base class used to maintain a strongly typed enumeration. This is |
|
37 |
* the superclass of {@link AccessibleState} and {@link AccessibleRole}. |
|
38 |
* <p>The toDisplayString method allows you to obtain the localized string |
|
39 |
* for a locale independent key from a predefined ResourceBundle for the |
|
40 |
* keys defined in this class. This localized string is intended to be |
|
41 |
* readable by humans. |
|
42 |
* |
|
43 |
* @see AccessibleRole |
|
44 |
* @see AccessibleState |
|
45 |
* |
|
46 |
* @author Willie Walker |
|
47 |
* @author Peter Korn |
|
48 |
* @author Lynn Monsanto |
|
49 |
*/ |
|
50 |
public abstract class AccessibleBundle { |
|
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 | 53 |
private final String defaultResourceBundleName |
54 |
= "com.sun.accessibility.internal.resources.accessibility"; |
|
55 |
||
19215 | 56 |
/** |
57 |
* Construct an {@code AccessibleBundle}. |
|
58 |
*/ |
|
2 | 59 |
public AccessibleBundle() { |
60 |
} |
|
61 |
||
62 |
/** |
|
63 |
* The locale independent name of the state. This is a programmatic |
|
64 |
* name that is not intended to be read by humans. |
|
65 |
* @see #toDisplayString |
|
66 |
*/ |
|
67 |
protected String key = null; |
|
68 |
||
69 |
/** |
|
70 |
* Obtains the key as a localized string. |
|
71 |
* If a localized string cannot be found for the key, the |
|
72 |
* locale independent key stored in the role will be returned. |
|
73 |
* This method is intended to be used only by subclasses so that they |
|
74 |
* can specify their own resource bundles which contain localized |
|
75 |
* strings for their keys. |
|
76 |
* @param resourceBundleName the name of the resource bundle to use for |
|
77 |
* lookup |
|
78 |
* @param locale the locale for which to obtain a localized string |
|
79 |
* @return a localized String for the key. |
|
80 |
*/ |
|
81 |
protected String toDisplayString(String resourceBundleName, |
|
82 |
Locale locale) { |
|
83 |
||
84 |
// loads the resource bundle if necessary |
|
85 |
loadResourceBundle(resourceBundleName, locale); |
|
86 |
||
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 | 94 |
} |
95 |
return key; |
|
96 |
} |
|
97 |
||
98 |
/** |
|
99 |
* Obtains the key as a localized string. |
|
100 |
* If a localized string cannot be found for the key, the |
|
101 |
* locale independent key stored in the role will be 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 |
* @return a localized String describing the key for the default locale |
|
113 |
*/ |
|
114 |
public String toDisplayString() { |
|
115 |
return toDisplayString(Locale.getDefault()); |
|
116 |
} |
|
117 |
||
118 |
/** |
|
119 |
* Gets localized string describing the key using the default locale. |
|
120 |
* @return a localized String describing the key using the default locale |
|
121 |
* @see #toDisplayString |
|
122 |
*/ |
|
123 |
public String toString() { |
|
124 |
return toDisplayString(); |
|
125 |
} |
|
126 |
||
127 |
/* |
|
128 |
* Loads the Accessibility resource bundle if necessary. |
|
129 |
*/ |
|
130 |
private void loadResourceBundle(String resourceBundleName, |
|
131 |
Locale locale) { |
|
132 |
if (! table.contains(locale)) { |
|
133 |
||
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 | 136 |
|
137 |
ResourceBundle bundle = ResourceBundle.getBundle(resourceBundleName, locale); |
|
138 |
||
25553
1082bb71e6c5
8048022: Fix raw and unchecked warnings in javax.accessibility
ptbrunet
parents:
19215
diff
changeset
|
139 |
Enumeration<String> iter = bundle.getKeys(); |
2 | 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 | 142 |
resourceTable.put(key, bundle.getObject(key)); |
143 |
} |
|
144 |
||
145 |
table.put(locale, resourceTable); |
|
146 |
} |
|
147 |
catch (MissingResourceException e) { |
|
148 |
System.err.println("loadResourceBundle: " + e); |
|
149 |
// Just return so toDisplayString() returns the |
|
150 |
// non-localized key. |
|
151 |
return; |
|
152 |
} |
|
153 |
} |
|
154 |
} |
|
155 |
||
156 |
} |