edu.northwestern.at.utils
Class Resources

java.lang.Object
  extended by edu.northwestern.at.utils.Resources

public class Resources
extends java.lang.Object

Retrieves text from a global string resource bundle.

Many items in the utils classes require textual information. This class provides a simple mechanism for consolidating the location of the text via a resource bundle. Wherever a text string would normally be used in the code, a reference to the "get" methods of this class should be used instead.

For example, when creating a button, instead of writing
JButton button = new JButton( "bogus" ); write
JButton button = new JButton( Resources.get( "bogus" , "bogus" ) ); instead. The first string is the name of the resource, and the second is a default value, typically in English.

The file "utils.properties" contains strings in English and is found in the resources/ subdirectory underneath this class. You may add corresponding properties files for other languages, and call one of the "setLocale" methods here to use these other properties files. For example, to define French strings, create the file resources/utils_fr.properties and call:

Resources.setLocale( "fr" );

to use your French strings.

If you define an alternative properties file for another language, you should provide translation for all the strings defined in en.properties . Undefined strings will revert the to default values in English declared in the "get" method calls.


Field Summary
protected static java.util.Locale defaultLocale
          Default locale.
protected static java.util.ResourceBundle resourceBundle
          String resource bundle.
protected static java.lang.String resourceName
          Resource base name.
 
Constructor Summary
protected Resources()
          Don't allow instantiation, do allow overrides.
 
Method Summary
static java.lang.String get(java.lang.String resourceName)
          Get string from ResourceBundle.
static java.lang.String get(java.lang.String resourceName, java.lang.String defaultValue)
          Get string from ResourceBundle.
static java.lang.String[] gets(java.lang.String resourceName, java.lang.String[] defaults)
          Parse ResourceBundle for a String array.
static void setLocale(java.util.Locale locale)
          Set the resource bundle to use based upon the locale.
static void setLocale(java.lang.String language)
          Set the translation language using the language name.
static void setLocale(java.lang.String language, java.lang.String country)
          Set the translation language using the language name and country.
static java.lang.String[] tokenize(java.lang.String input)
          Split string into a series of substrings on whitespace boundries.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

resourceName

protected static java.lang.String resourceName
Resource base name.


resourceBundle

protected static java.util.ResourceBundle resourceBundle
String resource bundle.


defaultLocale

protected static java.util.Locale defaultLocale
Default locale.

Constructor Detail

Resources

protected Resources()
Don't allow instantiation, do allow overrides.

Method Detail

setLocale

public static void setLocale(java.util.Locale locale)
Set the resource bundle to use based upon the locale.

Parameters:
locale - Locate specifying language and country.

setLocale

public static void setLocale(java.lang.String language,
                             java.lang.String country)
Set the translation language using the language name and country.

Parameters:
language - The java style language name. Example: French is "fr".
country - The java style country name. Example: France is "FR".

setLocale

public static void setLocale(java.lang.String language)
Set the translation language using the language name.

Parameters:
language - The java style language name. Example: French is "fr".

get

public static java.lang.String get(java.lang.String resourceName,
                                   java.lang.String defaultValue)
Get string from ResourceBundle. If no string is found, a default string is used.

Parameters:
resourceName - Name of resource to retrieve.
defaultValue - Default value for resource.
Returns:
String value from resource bundle if resourceName found there, otherwise defaultValue.

Underline "_" characters are replaced by spaces.


get

public static java.lang.String get(java.lang.String resourceName)
Get string from ResourceBundle. If no string is found, an empty string is returned.

Parameters:
resourceName - Name of resource to retrieve.
Returns:
String value from resource bundle if resourceName found there, otherwise empty string.

Underline "_" characters are replaced by spaces.


gets

public static java.lang.String[] gets(java.lang.String resourceName,
                                      java.lang.String[] defaults)
Parse ResourceBundle for a String array.

Parameters:
resourceName - Name of resource.
defaults - Array of default string values.
Returns:
Array of strings if resource name found in resources, otherwise default values.

tokenize

public static java.lang.String[] tokenize(java.lang.String input)
Split string into a series of substrings on whitespace boundries.

Parameters:
input - Input string.
Returns:
The array of strings after splitting input.

This is useful for retrieving an array of strings from the resource file. Underline "_" characters are replaced by spaces.