qflib 0.98.0

de.qfs.lib.util
Class MapResourceBundle

java.lang.Object
  |
  +--java.util.ResourceBundle
        |
        +--de.qfs.lib.util.MapResourceBundle

public class MapResourceBundle
extends java.util.ResourceBundle

The MapResourceBundle class extends the standard ResourceBundle to provide a more convenient overall handling of resources and property files.

Instead of using the parent mechanism, all key/value pairs are flattened into one Hashtable, with values added later via addResources overriding the previous setting for an identical key.

Additional queries have been added that return a default value for a non-existing key instead of throwing a MissingResourceException: getString, getInteger and getBoolean.

There is special support for Icons through getIcon, setIcon and setResolvingClass. It adds one level of indirection. Instead of fetching Icons directly, hardwiring their locations into the program, a property that maps to a String containing the Icon's location is used. The MapResourceBundle takes care of fetching and caching the icon itself.

With fetchProperties resources can be added from a property file that is bundled with a jar archive, something that doesn't always work with ResourceBundle.getBundle, especially in Applets.

Version:
$Revision: 1.12 $
Author:
Gregor Schmid

Fields inherited from class java.util.ResourceBundle
parent
 
Constructor Summary
MapResourceBundle()
          Create a new MapResourceBundle.
MapResourceBundle(java.util.ResourceBundle rb)
          Create a new MapResourceBundle from a ResourceBundle, copying its key/value pairs.
 
Method Summary
 void addResources(java.util.Properties properties)
          Add a some Properties to the MapResourceBundle, overriding existing resources with identical keys.
 void addResources(java.util.ResourceBundle bundle)
          Add a ResourceBundle's resources to the MapResourceBundle, overriding existing resources with identical keys.
 void fetchProperties(java.lang.String basename, java.lang.Class resolve)
          This is a replacement for ResourceBundle.getBundle that can handle resources from jar files.
 void fetchProperties(java.lang.String basename, java.util.Locale locale, java.lang.Class resolve)
          This is a replacement for ResourceBundle.getBundle that can handle resources from jar files.
 boolean getBoolean(java.lang.String key, boolean def)
          Get a boolean from the MapResourceBundle.
 javax.swing.Icon getIcon(java.lang.String key, javax.swing.Icon def)
          Get an icon for a resource name.
 int getInt(java.lang.String key, int def)
          Get an int value from the MapResourceBundle.
 int getInteger(java.lang.String key, int def)
          Deprecated. Replaced in versio 0.98.0 with getInt for the sake of conformance with OptionSet and ArgsParser.
 java.util.Enumeration getKeys()
          Return an enumeration of the keys.
 java.lang.String getString(java.lang.String key, java.lang.String def)
          Get a String from the MapResourceBundle.
 java.lang.Object handleGetObject(java.lang.String key)
          Get an object from the MapResourceBundle.
 void setIcon(java.lang.String key, javax.swing.Icon icon)
          Set an icon for a resource name.
 void setResolvingClass(java.lang.Class clazz)
          Set the Class to use for getting Icon resources.
 
Methods inherited from class java.util.ResourceBundle
getBundle, getBundle, getBundle, getLocale, getObject, getString, getStringArray, setParent
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MapResourceBundle

public MapResourceBundle()
Create a new MapResourceBundle.

MapResourceBundle

public MapResourceBundle(java.util.ResourceBundle rb)
Create a new MapResourceBundle from a ResourceBundle, copying its key/value pairs.
Parameters:
rb - The resources to start with.
Method Detail

addResources

public void addResources(java.util.ResourceBundle bundle)
Add a ResourceBundle's resources to the MapResourceBundle, overriding existing resources with identical keys.
Parameters:
bundle - The ResourceBundle to add.

addResources

public void addResources(java.util.Properties properties)
Add a some Properties to the MapResourceBundle, overriding existing resources with identical keys.
Parameters:
properties - The Properties to add.

fetchProperties

public void fetchProperties(java.lang.String basename,
                            java.util.Locale locale,
                            java.lang.Class resolve)
                     throws java.util.MissingResourceException
This is a replacement for ResourceBundle.getBundle that can handle resources from jar files. This version searches for .properties files only. The properties are then added to the MapResourceBundle.
Parameters:
basename - Basename for resource file.
locale - The locale to use.
resolve - A class from the same jar file as the resource.
Throws:
java.util.MissingResourceException - If the resource cannot be found.

fetchProperties

public void fetchProperties(java.lang.String basename,
                            java.lang.Class resolve)
                     throws java.util.MissingResourceException
This is a replacement for ResourceBundle.getBundle that can handle resources from jar files. This version searches for .properties files only. The properties are then added to the MapResourceBundle.
Parameters:
basename - Basename for resource file.
resolve - A class from the same jar file as the resource.
Throws:
java.util.MissingResourceException - If the resource cannot be found.

getKeys

public java.util.Enumeration getKeys()
Return an enumeration of the keys.
Overrides:
getKeys in class java.util.ResourceBundle
Returns:
The enumeration.

handleGetObject

public java.lang.Object handleGetObject(java.lang.String key)
                                 throws java.util.MissingResourceException
Get an object from the MapResourceBundle.
Overrides:
handleGetObject in class java.util.ResourceBundle
Parameters:
key - The key to look for.
Returns:
The Object associated with the key.
Throws:
java.util.MissingResourceException - If the key is not found.

getString

public java.lang.String getString(java.lang.String key,
                                  java.lang.String def)
Get a String from the MapResourceBundle. If it ain't there, return the supplied default value.
Parameters:
key - The key to look for.
def - The default value in case key doesn't exist.
Returns:
The String associated with the key.

getInteger

public int getInteger(java.lang.String key,
                      int def)
Deprecated. Replaced in versio 0.98.0 with getInt for the sake of conformance with OptionSet and ArgsParser.

Get an int value from the MapResourceBundle. If it ain't there, or if it is not a valid integer, return the supplied default value.
Parameters:
key - The key to look for.
def - The default value in case key doesn't exist.
Returns:
The value associated with the key.

getInt

public int getInt(java.lang.String key,
                  int def)
Get an int value from the MapResourceBundle. If it ain't there, or if it is not a valid integer, return the supplied default value.
Parameters:
key - The key to look for.
def - The default value in case key doesn't exist.
Returns:
The value associated with the key.
Since:
0.98.0

getBoolean

public boolean getBoolean(java.lang.String key,
                          boolean def)
Get a boolean from the MapResourceBundle. If it ain't there, return the supplied default value.
Parameters:
key - The key to look for.
def - The default value in case key doesn't exist.
Returns:
The boolean associated with the key. Anything that is not the string "TRUE" (independent of case) is considered false.

setResolvingClass

public void setResolvingClass(java.lang.Class clazz)
Set the Class to use for getting Icon resources. This is useful with applets. Resources will be located in the jar file that clazz came from.
Parameters:
clazz - The Class to use with getResource.

getIcon

public javax.swing.Icon getIcon(java.lang.String key,
                                javax.swing.Icon def)
Get an icon for a resource name. The resource name should map to the icon's resource path.
Parameters:
key - The resource name.
def - The default value in case key doesn't exist.
Returns:
The icon, if found, or the default value.

setIcon

public void setIcon(java.lang.String key,
                    javax.swing.Icon icon)
Set an icon for a resource name. This can be useful with with some browsers, e.g. netscape, that don't implement getResource correctly.
Parameters:
key - The resource name.
icon - The icon to set.

qflib 0.98.0