FileDocCategorySizeDatePackage
GUIResourceBundle.javaAPI DocExample9503Sat Jan 24 10:44:34 GMT 2004je3.gui

GUIResourceBundle

public class GUIResourceBundle extends ResourceBundle
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.

Fields Summary
Object
root
ResourceBundle
bundle
static HashMap
parsers
A hashtable for mapping resource types to resource parsers
Constructors Summary
public GUIResourceBundle(Object root, ResourceBundle bundle)
Create a GUIResourceBundle wrapper around a specified bundle

	this.root = root;
	this.bundle = bundle;
    
public GUIResourceBundle(Object root, String bundleName)
Load a named bundle and create a GUIResourceBundle around it. This constructor takes advantage of the internationalization features of the ResourceBundle.getBundle() method.

	this.root = root;
	this.bundle = ResourceBundle.getBundle(bundleName);
    
public GUIResourceBundle(Object root, InputStream propertiesStream)
Create a PropertyResourceBundle from the specified stream and then create a GUIResourceBundle wrapper for it

	this.root = root;
	this.bundle = new PropertyResourceBundle(propertiesStream);
    
public GUIResourceBundle(Object root, File propertiesFile)
Create a PropertyResourceBundle from the specified properties file and then create a GUIResourceBundle wrapper for it.

	this(root, new FileInputStream(propertiesFile));
    
Methods Summary
public booleangetBoolean(java.lang.String key)
Look up the named resource and try to interpret it as a boolean.

	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);
	}
    
public booleangetBoolean(java.lang.String key, boolean defaultValue)
As above, but return the default instead of throwing an exception

	try { return getBoolean(key); }
	catch(MissingResourceException e) {
	    if (e instanceof MalformedResourceException)
		System.err.println("WARNING: " + e.getMessage());
	    return defaultValue;
	}
    
public java.awt.ColorgetColor(java.lang.String key)
Look up the named resource, and convert to a Color

	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);
	}
    
public java.awt.ColorgetColor(java.lang.String key, java.awt.Color defaultValue)
As above, but with a default value

	try { return getColor(key); }
	catch(MissingResourceException e) {
	    if (e instanceof MalformedResourceException)
		System.err.println("WARNING: " + e.getMessage());
	    return defaultValue;
	}
    
public doublegetDouble(java.lang.String key)
Return a resource of type double

	String s = bundle.getString(key);
	
	try {
	    return Double.parseDouble(s);
	} catch (NumberFormatException e) {
	    throw new MalformedResourceException("double", key);
	}
    
public doublegetDouble(java.lang.String key, double defaultValue)
As above, but with a default value

	try { return getDouble(key); }
	catch(MissingResourceException e) {
	    if (e instanceof MalformedResourceException)
		System.err.println("WARNING: " + e.getMessage());
	    return defaultValue;
	}
    
public java.awt.FontgetFont(java.lang.String key)
Look up the named resource and convert to a Font

	// 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));
    
public java.awt.FontgetFont(java.lang.String key, java.awt.Font defaultValue)
As above, but with a default value

	try { return getFont(key); }
	catch (MissingResourceException e) { return defaultValue; }
    
public intgetInt(java.lang.String key)
Like getBoolean(), but for integers

	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);
	}
    
public intgetInt(java.lang.String key, int defaultValue)
As above, but with a default value

	try { return getInt(key); }
	catch(MissingResourceException e) {
	    if (e instanceof MalformedResourceException)
		System.err.println("WARNING: " + e.getMessage());
	    return defaultValue;
	}
    
public java.util.EnumerationgetKeys()
This is one of the abstract methods of ResourceBundle

 return bundle.getKeys(); 
public java.lang.ObjectgetResource(java.lang.String key, java.lang.Class type)
Look for a ResourceParser for the named type, and if one is found, ask it to parse and return the named resource

	// 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);
	}
    
public java.lang.ObjectgetResource(java.lang.String key, java.lang.Class type, java.lang.Object defaultValue)
Like the 2-argument version of getResource, but return a default value instead of throwing a MissingResourceException

	try {  return getResource(key, type); }
	catch (MissingResourceException e) {
	    if (e instanceof MalformedResourceException)
		System.err.println("WARNING: " + e.getMessage());
	    return defaultValue;
	}
    
public static ResourceParsergetResourceParser(java.lang.Class type)
Look up a ResourceParser for the specified resource type

	return (ResourceParser) parsers.get(type);
    
public java.lang.ObjectgetRoot()
This is a property accessor method for our root object

 return root; 
public java.lang.StringgetString(java.lang.String key, java.lang.String defaultValue)
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

	try { return bundle.getString(key); }
	catch(MissingResourceException e) { return defaultValue; }
    
public java.util.ListgetStringList(java.lang.String key, java.util.List defaultValue)
Like above, but return a default instead of throwing an exception

	try { return getStringList(key); }
	catch(MissingResourceException e) { return defaultValue; }
    
public java.util.ListgetStringList(java.lang.String key)
Look up the named resource and parse it as a list of strings separated by spaces, tabs, or commas.

	String s = getString(key);
	StringTokenizer t = new StringTokenizer(s, ", \t", false);
	ArrayList list = new ArrayList();
	while(t.hasMoreTokens()) list.add(t.nextToken());
	return list;
    
protected java.lang.ObjecthandleGetObject(java.lang.String key)
This is the other abstract method of ResourceBundle

	return bundle.getObject(key);  // simply defer to the wrapped bundle
    
public static voidregisterResourceParser(ResourceParser parser)
An extension mechanism: register a parser for new resource types


               
         
	// 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);