-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUIResourceBundle.java
More file actions
265 lines (235 loc) · 10.1 KB
/
Copy pathGUIResourceBundle.java
File metadata and controls
265 lines (235 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import java.io.*;
import java.util.*;
import java.awt.*;
/**
* This class extends ResourceBundle and adds methods to retrieve types of
* resources commonly used in GUIs. Additionally, it adds extensibility
* by allowing ResourceParser objects to be registered to parse other
* resource types.
**/
public class GUIResourceBundle extends ResourceBundle {
// The root object. Required to parse certain resource types like Commands
Object root;
// The resource bundle that actually contains the textual resources
// This class is a wrapper around this bundle
ResourceBundle bundle;
/** Create a GUIResourceBundle wrapper around a specified bundle */
public GUIResourceBundle(Object root, ResourceBundle bundle) {
this.root = root;
this.bundle = bundle;
}
/**
* Load a named bundle and create a GUIResourceBundle around it. This
* constructor takes advantage of the internationalization features of
* the ResourceBundle.getBundle( ) method.
**/
public GUIResourceBundle(Object root, String bundleName)
throws MissingResourceException
{
this.root = root;
this.bundle = ResourceBundle.getBundle(bundleName);
}
/**
* Create a PropertyResourceBundle from the specified stream and then
* create a GUIResourceBundle wrapper for it
**/
public GUIResourceBundle(Object root, InputStream propertiesStream)
throws IOException
{
this.root = root;
this.bundle = new PropertyResourceBundle(propertiesStream);
}
/**
* Create a PropertyResourceBundle from the specified properties file and
* then create a GUIResourceBundle wrapper for it.
**/
public GUIResourceBundle(Object root, File propertiesFile)
throws IOException
{
this(root, new FileInputStream(propertiesFile));
}
/** This is one of the abstract methods of ResourceBundle */
public Enumeration getKeys( ) { return bundle.getKeys( ); }
/** This is the other abstract method of ResourceBundle */
protected Object handleGetObject(String key)
throws MissingResourceException
{
return bundle.getObject(key); // simply defer to the wrapped bundle
}
/** This is a property accessor method for our root object */
public Object getRoot( ) { return root; }
/**
* This method is like the inherited getString( ) method, except that
* when the named resource is not found, it returns the specified default
* instead of throwing an exception
**/
public String getString(String key, String defaultValue) {
try { return bundle.getString(key); }
catch(MissingResourceException e) { return defaultValue; }
}
/**
* Look up the named resource and parse it as a list of strings separated
* by spaces, tabs, or commas.
**/
public java.util.List getStringList(String key)
throws MissingResourceException
{
String s = getString(key);
StringTokenizer t = new StringTokenizer(s, ", \t", false);
ArrayList list = new ArrayList( );
while(t.hasMoreTokens( )) list.add(t.nextToken( ));
return list;
}
/** Like above, but return a default instead of throwing an exception */
public java.util.List getStringList(String key,
java.util.List defaultValue) {
try { return getStringList(key); }
catch(MissingResourceException e) { return defaultValue; }
}
/** Look up the named resource and try to interpret it as a boolean. */
public boolean getBoolean(String key) throws MissingResourceException, MalformedResourceException {
String s = bundle.getString(key);
s = s.toLowerCase( );
if (s.equals("true")) return true;
else if (s.equals("false")) return false;
else if (s.equals("yes")) return true;
else if (s.equals("no")) return false;
else if (s.equals("on")) return true;
else if (s.equals("off")) return false;
else {
throw new MalformedResourceException("boolean", key);
}
}
/** As above, but return the default instead of throwing an exception */
public boolean getBoolean(String key, boolean defaultValue) throws MalformedResourceException {
try { return getBoolean(key); }
catch(MissingResourceException e) {
if (e.equals(root))
System.err.println("WARNING: " + e.getMessage( ));
return defaultValue;
}
}
/** Like getBoolean( ), but for integers */
public int getInt(String key) throws MissingResourceException, MalformedResourceException {
String s = bundle.getString(key);
try {
// Use decode( ) instead of parseInt( ) so we support octal
// and hexadecimal numbers
return Integer.decode(s).intValue( );
} catch (NumberFormatException e) {
throw new MalformedResourceException("int", key);
}
}
/** As above, but with a default value */
public int getInt(String key, int defaultValue) throws MalformedResourceException {
try { return getInt(key); }
catch(MissingResourceException e) {
if (e.equals(root))
System.err.println("WARNING: " + e.getMessage( ));
return defaultValue;
}
}
/** Return a resource of type double */
public double getDouble(String key) throws MissingResourceException, MalformedResourceException {
String s = bundle.getString(key);
try {
return Double.parseDouble(s);
} catch (NumberFormatException e) {
throw new MalformedResourceException("double", key);
}
}
/** As above, but with a default value */
public double getDouble(String key, double defaultValue) throws MalformedResourceException {
try { return getDouble(key); }
catch(MissingResourceException e) {
if (e.equals(root))
System.err.println("WARNING: " + e.getMessage( ));
return defaultValue;
}
}
/** Look up the named resource and convert to a Font */
public Font getFont(String key) throws MissingResourceException {
// Font.decode( ) always returns a Font object, so we can't check
// whether the resource value was well-formed or not.
return Font.decode(bundle.getString(key));
}
/** As above, but with a default value */
public Font getFont(String key, Font defaultValue) {
try { return getFont(key); }
catch (MissingResourceException e) { return defaultValue; }
}
/** Look up the named resource, and convert to a Color */
public Color getColor(String key) throws MissingResourceException, MalformedResourceException {
try {
return Color.decode(bundle.getString(key));
}
catch (NumberFormatException e) {
// It would be useful to try to parse color names here as well
// as numeric color specifications
throw new MalformedResourceException("Color", key);
}
}
/** As above, but with a default value */
public Color getColor(String key, Color defaultValue) throws MalformedResourceException {
try { return getColor(key); }
catch(MissingResourceException e) {
if (e.equals(root))
System.err.println("WARNING: " + e.getMessage( ));
return defaultValue;
}
}
/** A hashtable for mapping resource types to resource parsers */
static HashMap parsers = new HashMap( );
/** An extension mechanism: register a parser for new resource types */
public static void registerResourceParser(ResourceParser parser) {
// Ask the ResourceParser what types it can parse
Class[ ] supportedTypes = parser.getResourceTypes( );
// Register it in the hashtable for each of those types
for(int i = 0; i < supportedTypes.length; i++)
parsers.put(supportedTypes[i], parser);
}
/** Look up a ResourceParser for the specified resource type */
public static ResourceParser getResourceParser(Class type) {
return (ResourceParser) parsers.get(type);
}
/**
* Look for a ResourceParser for the named type, and if one is found,
* ask it to parse and return the named resource
**/
public Object getResource(String key, Class type)
throws MissingResourceException, MalformedResourceException
{
// Get a parser for the specified type
ResourceParser parser = (ResourceParser)parsers.get(type);
if (parser == null)
throw new MissingResourceException(
"No ResourceParser registered for " +
type.getName( ) + " resources",
type.getName( ), key);
try { // Ask the parser to parse the resource
return parser.parse(this, key, type);
}
catch(MissingResourceException e) {
throw e; // Rethrow MissingResourceException exceptions
}
catch(Exception e) {
// If any other type of exception occurs, convert it to
// a MalformedResourceException
String msg = "Malformed " + type.getName( ) + " resource: " +
key + ": " + e.getMessage( );
throw new MalformedResourceException(msg, type.getName( ), key);
}
}
/**
* Like the 2-argument version of getResource, but return a default value
* instead of throwing a MissingResourceException
**/
public Object getResource(String key, Class type, Object defaultValue) throws MalformedResourceException {
try { return getResource(key, type); }
catch (MissingResourceException e) {
if (e.equals(root))
System.err.println("WARNING: " + e.getMessage( ));
return defaultValue;
}
}
}