QJCC homepage

biz.chitec.quarterback.util
Class RB

java.lang.Object
  extended bybiz.chitec.quarterback.util.RB

public final class RB
extends java.lang.Object

Loads hierarchical ResourceBundles with respect to class hierarchy. The three static getBundle() methods try to load the resources for a class and return the appropriate resource bundle. ResourceBundles fitting into this scheme are basically named <classname>_p. Standard rules for localization apply.

To allow not only resource hierarchies along with the localization but also along with the class hierarchy, the returned resource bundle is of the class HierarchicalResourceBundle. That bundle is initialized in a way so that all bundles of the classes superclass are also loaded and used as fallbacks.

Example: Assume we have a class hierarchy JPanel > StandardPanel > ExtendedPanel . So, the first step is to have resource bundles for both new Panels. Things could look like this:

Class Standard resource French resource
StandardPanel StandardPanel_p.properties StandardPanel_p_fr.properties
ExtendedPanel ExtendedPanel_p.properties ExtendedPanel_p_fr.properties
Let's have a french localized platform. Loading a resource out of the bundle for ExtendedPanel performs now a search (in this order) through ExtendedPanel_p_fr , ExtendedPanel_p , StandardPanel_p_fr , and finally StandardPanel_p . As you see, the different localizations for one panel are searched before querying the resources of the current panel's superclass are queried.

If no resource bundle exists for ExtendedPanel , the resources for StandardPanel (and any super classes) are used.

RB makes a sharp distinction between bundles requested with an explicit locale and those without. The bundles with explicit locale will be delivered and never changed after. The other bundles, however, are delivered with the default locale and stored internally. If the default locales changes afterwards AND defaultLocaleChanged() is called afterwards, all those bundles will reload their internal data and deliver all content with that new default locale. This can be used to change the locale during program operation.

Version:
$Id: 9f1e8b38a86b586c5ecbc2cbb1e1f09b0d15b62e $
Author:
Dirk Hillbrecht 1999-2000, chitec/Dirk Hillbrecht 2000,2002. Distributed under the terms of the GNU LGPL.

Field Summary
private static java.util.Map components
           
private static java.util.Locale fallbacklocale
          Additional locale that can be set to provide an explicit fallback.
private static java.util.Map locales
           
 
Constructor Summary
private RB()
          This class is uninstanciable.
 
Method Summary
static void defaultLocaleChanged()
          Notifies all non-locale-specific bundles still in use about the change of the default locale.
static java.util.ResourceBundle getBundle(java.lang.Class aclass)
          Loads hierarchical bundle for a class and the default locale.
static java.util.ResourceBundle getBundle(java.lang.Class aclass, java.util.Locale locale)
          Loads hierarchical bundle for a class and a certain locale.
static java.util.ResourceBundle getBundle(java.lang.Object anobject)
          Loads hierarchical bundle for an instance and the default locale
static java.util.ResourceBundle getBundle(java.lang.Object anobject, java.util.Locale locale)
          Loads hierarchical bundle for an instance and a certain locale
static java.util.ResourceBundle getBundle(java.lang.String aclassname)
          Loads a bundle for the default locale
static java.util.ResourceBundle getBundle(java.lang.String aclassname, java.util.Locale locale)
          Loads hierarchical bundle for a string-named class.
static java.util.ResourceBundle getBundleWithException(java.lang.String aclassname)
          Loads hierarchical bundle for given class and the default locale.
static java.util.ResourceBundle getBundleWithException(java.lang.String aclassname, java.util.Locale locale)
          Loads hierarchical bundle for given class and the given locale.
static java.util.Locale getLocale(java.lang.String description)
          Constructs a Locale from a describing String.
static java.util.ResourceBundle getStoredBundleForComponent(java.awt.Component c)
          Returns the bundle created for a certain component which is not a JComponent.
static java.lang.String getString(java.util.ResourceBundle rb, java.lang.String propname)
          Return a String with the guaratee that no exception is thrown
static java.lang.String getStringWithModifiers(java.util.ResourceBundle rb, java.lang.String base, java.lang.String mod1, java.lang.String mod2, java.lang.String mod3)
          Returns a localized property with some modifiers to the name.
static void printRB(java.util.ResourceBundle rb)
          Debugging helper method: Prints visible content of the given ResourceBundle.
static void setFallbackLocale(java.util.Locale l)
          Load explicit additional Locale.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

locales

private static java.util.Map locales

components

private static java.util.Map components

fallbacklocale

private static java.util.Locale fallbacklocale
Additional locale that can be set to provide an explicit fallback.

Constructor Detail

RB

private RB()
This class is uninstanciable.

Method Detail

setFallbackLocale

public static void setFallbackLocale(java.util.Locale l)
Load explicit additional Locale. If set, for each bundle loading through this class the system will not only load the bundle of the default locale, but also the bundle for this additional Locale. By this way, an explicit fallback locale can be given.

Parameters:
l - The new fallbacklocale value

getBundle

public static java.util.ResourceBundle getBundle(java.lang.Object anobject,
                                                 java.util.Locale locale)
Loads hierarchical bundle for an instance and a certain locale

Parameters:
anobject - Object to get the ResourceBundle for.
locale - The locale to load for
Returns:
The ResourceBundle

getBundle

public static java.util.ResourceBundle getBundle(java.lang.Object anobject)
Loads hierarchical bundle for an instance and the default locale

Parameters:
anobject - Object to get the ResourceBundle for.
Returns:
The ResourceBundle

defaultLocaleChanged

public static void defaultLocaleChanged()
Notifies all non-locale-specific bundles still in use about the change of the default locale. To be called after the default locale has been changed.


getStoredBundleForComponent

public static java.util.ResourceBundle getStoredBundleForComponent(java.awt.Component c)
Returns the bundle created for a certain component which is not a JComponent. This method is only needed inside the QJCC and allows TOM to ask for the bundle of a non-JComponent element when it comes to changing the default locale.

Parameters:
c -
Returns:

getBundle

public static java.util.ResourceBundle getBundle(java.lang.Class aclass,
                                                 java.util.Locale locale)
Loads hierarchical bundle for a class and a certain locale. Can be used for static bundle holder.

Parameters:
aclass - Class to get the ResourceBundle for.
locale - The locale to load for
Returns:
The ResourceBundle

getBundle

public static java.util.ResourceBundle getBundle(java.lang.Class aclass)
Loads hierarchical bundle for a class and the default locale. Can be used for static bundle holder.

Parameters:
aclass - Class to get the ResourceBundle for.
Returns:
The ResourceBundle

getBundle

public static java.util.ResourceBundle getBundle(java.lang.String aclassname,
                                                 java.util.Locale locale)
Loads hierarchical bundle for a string-named class. Can be used for static bundle holder. If no ressources exist for the class or any of its superclasses, an empty bundle is returned which will answer any query with a MissingRessourceException

Parameters:
aclassname - Class name
locale - The locale for which the bundle shell be loaded
Returns:
The ResourceBundle

getBundle

public static java.util.ResourceBundle getBundle(java.lang.String aclassname)
Loads a bundle for the default locale


getBundleWithException

public static java.util.ResourceBundle getBundleWithException(java.lang.String aclassname,
                                                              java.util.Locale locale)
Loads hierarchical bundle for given class and the given locale. If no bundle is found, MissingRessourceException is thrown.

Parameters:
aclassname - Class name
Returns:
The ResourceBundle

getBundleWithException

public static java.util.ResourceBundle getBundleWithException(java.lang.String aclassname)
Loads hierarchical bundle for given class and the default locale. If no bundle is found, MissingRessourceException is thrown.

Parameters:
aclassname - Class name
Returns:
The ResourceBundle

getString

public static java.lang.String getString(java.util.ResourceBundle rb,
                                         java.lang.String propname)
Return a String with the guaratee that no exception is thrown

Parameters:
rb - ResourceBundle to take property from
propname - Name of the property to read
Returns:
The property's value

getStringWithModifiers

public static java.lang.String getStringWithModifiers(java.util.ResourceBundle rb,
                                                      java.lang.String base,
                                                      java.lang.String mod1,
                                                      java.lang.String mod2,
                                                      java.lang.String mod3)
Returns a localized property with some modifiers to the name.

The property is searched with every combination of the three modifiers where only those modifiers are taken into respect which are not null. First combination matches where bigger number of matching modifiers precede lower index of matching modifiers. Last resort is always non-modified key (base part only). If even that cannot be found, the base itself is returned.

Modifiers are automatically surrounded with dots. For missing modifier, no dot is inserted either.

Parameters:
rb - ResourceBundle to search in
base - Base part of the key
mod1 - First modifier to the key, may be null
mod2 - Second modifier to the key, may be null
mod3 - Last modifier to the key, may be null
Returns:
Localized property

getLocale

public static java.util.Locale getLocale(java.lang.String description)
Constructs a Locale from a describing String. The String has the usual notion of lang_COUNTRY_variant with one of the later ones possibly missing. If null or an empty String is given, the system's default locale is returned.

Parameters:
description - Locale description of type lang_COUNTRY_variant
Returns:
Locale object

printRB

public static void printRB(java.util.ResourceBundle rb)
Debugging helper method: Prints visible content of the given ResourceBundle. This method simply prints out everything which is in the bundle. It does not deface internal structures, it justs works with ResourceBundle's interface.

Parameters:
rb - ResourceBundle to print out.

QJCC homepage