Java Reference
In-Depth Information
that does not exist in the child
Properties
object, the parent
Properties
object is
searched (and this continues recursively). The
ResourceBundle
class provides an
internationalization layer around properties files that allows properties to be cus-
tomized for use in different locales. Internationalization is an important considera-
tion for GUI-based applications, which makes the
ResourceBundle
class useful for
describing GUI resources.
Handling Basic GUI Resources
Because properties files are text-based, one limitation to working with
Resource-
Bundle
objects that are based on properties files is that they support only
String
resources. The
GUIResourceBundle
class, presented in Example 10-22, is a subclass
of
ResourceBundle
that adds additional methods for reading string resources and
converting them to objects of the types commonly used in GUI programming,
such as
Color
and
Font
.
The
GUIResourceBundle
code is straightforward. The
ResourceParser
interface
provides an extension mechanism; we'll look at that next. Note that the
MalformedResourceException
class used in this example is not a standard Java
class; it is a custom subclass of
MissingResourceException
that was developed for
this example. Because it is a trivial subclass, its code is not shown here, but you'll
find the code in the online example archive.
Example 10•22: GUIResourceBundle.java
package com.davidflanagan.examples.gui;
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)