Java Reference
In-Depth Information
Example 10•22: GUIResourceBundle.java (continued)
}
}
An Extension Mechanism for Complex Resources
As we just saw, Example 10-22 uses the ResourceParser interface to provide an
extension mechanism that allows it to handle more complex resource types. Exam-
ple 10-23 is a listing of this simple interface. We'll see some interesting implemen-
tations of the interface in the sections that follow.
Example 10•23: ResourceParser.java
package com.davidflanagan.examples.gui;
/**
* This interface defines an extension mechanism that allows GUIResourceBundle
* to parse arbitrary resource types
**/
public interface ResourceParser {
/**
* Return an array of classes that specify what kind of resources
* this parser can handle
**/
public Class[] getResourceTypes();
/**
* Read the property named by key from the specified bundle, convert
* it to the specified type, and return it. For complex resources,
* the parser may need to read more than one property from the bundle;
* typically it may a number of properties whose names begin with the
* specified key.
**/
public Object parse(GUIResourceBundle bundle, String key, Class type)
throws Exception;
}
Parsing Commands and Actions
For our first ResourceParser implementation, we'll add the ability to parse Action
objects. As we've seen, Action objects are commonly used in GUIs; an Action
includes a number of attributes, such as a description, an icon, and a tooltip, that
may need to be localized. Our ActionParser implementation is based on the Com-
mandAction class shown in Example 10-17, which in turn relies on the reflection
capabilities of the Command class shown in Example 8-2.
In order to implement the ActionParser class, you need to parse Command objects
from a properties file. So let's start with the CommandParser class, shown in Exam-
ple 10-24. This class is quite simple because it relies on the parsing capabilities of
the Command class. The ActionParser listing follows in Example 10-25.
To help you understand how these parser classes work, consider the following
properties, excerpted from the WebBr owserResources.pr operties file used by the
WebBrowser class of Example 10-21:
Search WWH ::




Custom Search