org.jhotdraw.util
Class ResourceBundleUtil

java.lang.Object
  extended by org.jhotdraw.util.ResourceBundleUtil
All Implemented Interfaces:
java.io.Serializable

public class ResourceBundleUtil
extends java.lang.Object
implements java.io.Serializable

This is a convenience wrapper for accessing resources stored in a ResourceBundle.

Placeholders
On top of the functionality provided by ResourceBundle, a property value can include text from another property, by specifying the desired property name and format type between "${" and "}".

For example, if there is a "imagedir" property with the value "/org/jhotdraw/undo/images", then this could be used in an attribute like this: ${imagedir}/editUndo.png. This is resolved at run-time as /org/jhotdraw/undo/images/editUndo.png.

Property names in placeholders can contain modifiers. Modifiers are written between @code "[$"} and "]". Each modifier has a fallback chain.

For example, if the property name modifier "os" has the value "win", and its fallback chain is "mac","default", then the property name ${preferences.text.[$os]} is first evaluted to preferences.text.win, and - if no property with this name exists - it is evaluated to preferences.text.mac, and then to preferences.text.default.

The property name modifier "os" is defined by default. It can assume the values "win", "mac" and "other". Its fallback chain is "default".

The format type can be optinally specified after a comma. The following format types are supported:

Version:
$Id: ResourceBundleUtil.java 604 2010-01-09 12:00:29Z rawcoder $
Author:
Werner Randelshofer, Staldenmattweg 2, CH-6405 Immensee, Switzerland
See Also:
Serialized Form

Field Summary
private static java.util.HashSet<java.lang.String> acceleratorKeys
           
private  java.lang.Class baseClass
          The base class
private  java.lang.String baseName
          The base name of the resource bundle.
private static boolean isVerbose
          The global verbose property.
private  java.util.Locale locale
          The locale.
private static java.util.HashMap<java.lang.String,java.lang.String[]> propertyNameModifiers
          The global map of property name modifiers.
private  java.util.ResourceBundle resource
          The wrapped resource bundle.
 
Constructor Summary
ResourceBundleUtil(java.lang.String baseName, java.util.Locale locale)
          Creates a new ResouceBundleUtil which wraps the provided resource bundle.
 
Method Summary
 void configureAction(javax.swing.Action action, java.lang.String argument)
           
 void configureAction(javax.swing.Action action, java.lang.String argument, java.lang.Class baseClass)
           
 void configureButton(javax.swing.AbstractButton button, java.lang.String argument)
           
 void configureButton(javax.swing.AbstractButton button, java.lang.String argument, java.lang.Class baseClass)
           
 void configureMenu(javax.swing.JMenuItem menu, java.lang.String argument)
           
 void configureToolBarButton(javax.swing.AbstractButton button, java.lang.String argument)
           
 void configureToolBarButton(javax.swing.AbstractButton button, java.lang.String argument, java.lang.Class baseClass)
           
 javax.swing.JMenuItem createMenuItem(javax.swing.Action a, java.lang.String baseName)
           
 java.lang.String format(java.lang.String key, java.lang.Object... arguments)
          Returns a formatted string using java.util.Formatter().
private  void generateFallbackKeys(java.lang.String key, java.util.ArrayList<java.lang.String> fallbackKeys)
          Generates fallback keys by processing all property name modifiers in the key.
 javax.swing.KeyStroke getAcceleratorProperty(java.lang.String key)
          Gets a KeyStroke for a JavaBeans "accelerator" property from the ResourceBundle.
 java.lang.Class getBaseClass()
           
static ResourceBundleUtil getBundle(java.lang.String baseName)
          Get the appropriate ResourceBundle subclass.
static ResourceBundleUtil getBundle(java.lang.String baseName, java.util.Locale locale)
          Get the appropriate ResourceBundle subclass.
 java.lang.String getFormatted(java.lang.String key, java.lang.Object... arguments)
          Returns a formatted string using javax.text.MessageFormat.
 javax.swing.ImageIcon getIconProperty(java.lang.String key, java.lang.Class baseClass)
          Get an image icon from the ResourceBundle.
 java.lang.Integer getInteger(java.lang.String key)
          Get an Integer from the ResourceBundle.
 javax.swing.KeyStroke getKeyStroke(java.lang.String key)
          Get a KeyStroke from the ResourceBundle.
 char getMnemonic(java.lang.String key)
          Get a Mnemonic from the ResourceBundle.
 char getMnemonicProperty(java.lang.String key)
          Gets a char for a JavaBeans "mnemonic" property from the ResourceBundle.
 java.lang.String getString(java.lang.String key)
          Get a String from the ResourceBundle.
private  java.lang.String getStringRecursive(java.lang.String key)
          Recursive part of the getString method.
 java.lang.String getTextProperty(java.lang.String key)
          Get a String for a JavaBeans "text" property from the ResourceBundle.
 java.lang.String getToolTipTextProperty(java.lang.String key)
          Get a String for a JavaBeans "toolTipText" property from the ResourceBundle.
 java.util.ResourceBundle getWrappedBundle()
          Returns the wrapped resource bundle.
static boolean isVerbose()
           
static void putPropertyNameModifier(java.lang.String name, java.lang.String... fallbackChain)
          Puts a property name modifier along with a fallback chain.
private  void readObject(java.io.ObjectInputStream in)
          Read object from ObjectInputStream and re-establish ResourceBundle.
static void removePropertyNameModifier(java.lang.String name)
          Removes a property name modifier.
 void setBaseClass(java.lang.Class baseClass)
           
static void setVerbose(boolean newValue)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

acceleratorKeys

private static final java.util.HashSet<java.lang.String> acceleratorKeys

resource

private transient java.util.ResourceBundle resource
The wrapped resource bundle.


locale

private java.util.Locale locale
The locale.


baseClass

private java.lang.Class baseClass
The base class


baseName

private java.lang.String baseName
The base name of the resource bundle.


isVerbose

private static boolean isVerbose
The global verbose property.


propertyNameModifiers

private static java.util.HashMap<java.lang.String,java.lang.String[]> propertyNameModifiers
The global map of property name modifiers. The key of this map is the name of the property name modifier, the value of this map is a fallback chain.

Constructor Detail

ResourceBundleUtil

public ResourceBundleUtil(java.lang.String baseName,
                          java.util.Locale locale)
Creates a new ResouceBundleUtil which wraps the provided resource bundle.

Method Detail

getWrappedBundle

public java.util.ResourceBundle getWrappedBundle()
Returns the wrapped resource bundle.

Returns:
The wrapped resource bundle.

getString

public java.lang.String getString(java.lang.String key)
Get a String from the ResourceBundle.
Convenience method to save casting.

Parameters:
key - The key of the property.
Returns:
The value of the property. Returns the key if the property is missing.

getStringRecursive

private java.lang.String getStringRecursive(java.lang.String key)
                                     throws java.util.MissingResourceException
Recursive part of the getString method.

Parameters:
key -
Throws:
java.util.MissingResourceException

generateFallbackKeys

private void generateFallbackKeys(java.lang.String key,
                                  java.util.ArrayList<java.lang.String> fallbackKeys)
Generates fallback keys by processing all property name modifiers in the key.


getFormatted

public java.lang.String getFormatted(java.lang.String key,
                                     java.lang.Object... arguments)
Returns a formatted string using javax.text.MessageFormat.

Parameters:
key -
arguments -
Returns:
formatted String

format

public java.lang.String format(java.lang.String key,
                               java.lang.Object... arguments)
Returns a formatted string using java.util.Formatter().

Parameters:
key -
arguments -
Returns:
formatted String

getInteger

public java.lang.Integer getInteger(java.lang.String key)
Get an Integer from the ResourceBundle.
Convenience method to save casting.

Parameters:
key - The key of the property.
Returns:
The value of the property. Returns -1 if the property is missing.

getIconProperty

public javax.swing.ImageIcon getIconProperty(java.lang.String key,
                                             java.lang.Class baseClass)
Get an image icon from the ResourceBundle.
Convenience method .

Parameters:
key - The key of the property. This method appends ".icon" to the key.
Returns:
The value of the property. Returns null if the property is missing.

getMnemonic

public char getMnemonic(java.lang.String key)
Get a Mnemonic from the ResourceBundle.
Convenience method.

Parameters:
key - The key of the property.
Returns:
The first char of the value of the property. Returns '\0' if the property is missing.

getMnemonicProperty

public char getMnemonicProperty(java.lang.String key)
Gets a char for a JavaBeans "mnemonic" property from the ResourceBundle.
Convenience method.

Parameters:
key - The key of the property. This method appends ".mnemonic" to the key.
Returns:
The first char of the value of the property. Returns '\0' if the property is missing.

getToolTipTextProperty

public java.lang.String getToolTipTextProperty(java.lang.String key)
Get a String for a JavaBeans "toolTipText" property from the ResourceBundle.
Convenience method.

Parameters:
key - The key of the property. This method appends ".toolTipText" to the key.
Returns:
The ToolTip. Returns null if no tooltip is defined.

getTextProperty

public java.lang.String getTextProperty(java.lang.String key)
Get a String for a JavaBeans "text" property from the ResourceBundle.
Convenience method.

Parameters:
key - The key of the property. This method appends ".text" to the key.
Returns:
The ToolTip. Returns null if no tooltip is defined.

getKeyStroke

public javax.swing.KeyStroke getKeyStroke(java.lang.String key)
Get a KeyStroke from the ResourceBundle.
Convenience method.

Parameters:
key - The key of the property.
Returns:
javax.swing.KeyStroke.getKeyStroke(value). Returns null if the property is missing.

getAcceleratorProperty

public javax.swing.KeyStroke getAcceleratorProperty(java.lang.String key)
Gets a KeyStroke for a JavaBeans "accelerator" property from the ResourceBundle.
Convenience method.

Parameters:
key - The key of the property. This method adds ".accelerator" to the key.
Returns:
javax.swing.KeyStroke.getKeyStroke(value). Returns null if the property is missing.

getBundle

public static ResourceBundleUtil getBundle(java.lang.String baseName)
                                    throws java.util.MissingResourceException
Get the appropriate ResourceBundle subclass.

Throws:
java.util.MissingResourceException
See Also:
ResourceBundle

setBaseClass

public void setBaseClass(java.lang.Class baseClass)

getBaseClass

public java.lang.Class getBaseClass()

configureAction

public void configureAction(javax.swing.Action action,
                            java.lang.String argument)

configureAction

public void configureAction(javax.swing.Action action,
                            java.lang.String argument,
                            java.lang.Class baseClass)

configureButton

public void configureButton(javax.swing.AbstractButton button,
                            java.lang.String argument)

configureButton

public void configureButton(javax.swing.AbstractButton button,
                            java.lang.String argument,
                            java.lang.Class baseClass)

configureToolBarButton

public void configureToolBarButton(javax.swing.AbstractButton button,
                                   java.lang.String argument)

configureToolBarButton

public void configureToolBarButton(javax.swing.AbstractButton button,
                                   java.lang.String argument,
                                   java.lang.Class baseClass)

configureMenu

public void configureMenu(javax.swing.JMenuItem menu,
                          java.lang.String argument)

createMenuItem

public javax.swing.JMenuItem createMenuItem(javax.swing.Action a,
                                            java.lang.String baseName)

getBundle

public static ResourceBundleUtil getBundle(java.lang.String baseName,
                                           java.util.Locale locale)
                                    throws java.util.MissingResourceException
Get the appropriate ResourceBundle subclass.

Throws:
java.util.MissingResourceException
See Also:
ResourceBundle

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

setVerbose

public static void setVerbose(boolean newValue)

isVerbose

public static boolean isVerbose()

putPropertyNameModifier

public static void putPropertyNameModifier(java.lang.String name,
                                           java.lang.String... fallbackChain)
Puts a property name modifier along with a fallback chain.

Parameters:
name - The name of the modifier.
fallbackChain - The fallback chain of the modifier.

removePropertyNameModifier

public static void removePropertyNameModifier(java.lang.String name)
Removes a property name modifier.


readObject

private void readObject(java.io.ObjectInputStream in)
                 throws java.io.IOException,
                        java.lang.ClassNotFoundException
Read object from ObjectInputStream and re-establish ResourceBundle.

Throws:
java.io.IOException
java.lang.ClassNotFoundException