skrueger.i8n
Class Translation

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap<String,String>
          extended by skrueger.i8n.Translation
All Implemented Interfaces:
Serializable, Cloneable, Map<String,String>, Copyable<Translation>

public class Translation
extends HashMap<String,String>
implements Copyable<Translation>

Represents a HashMap of translations. toString() returns the appropriate translation

Author:
@author Stefan Alfons Krüger
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
 
Field Summary
static String DEFAULT_KEY
           
protected static  listeners
           
static String LOCALECHANGE_PROPERTY
           
static String NO_TRANSLATION
           
 
Constructor Summary
Translation()
          Sometimes Translations are optional, like for keywords.
Translation(List<String> languages, String defaultTranslation)
          Initializes a new Translation, an uses the given String to initialize the Translation for all languages codes passed.
Translation(String defaultTranslation)
          Initializes a new Translation with a default translation if a simple text is passed.
 
Method Summary
static void addLocaleChangeListener(PropertyChangeListener propertyChangeListener)
          PropertyChangeListener can be registered to be informed when the Locale changed.
 void addTranslationChangeListener(ActionListener actionListener)
          The listeneras are stored in a WeakHashSet!
static boolean checkValid(Translation translationToCheck)
          Checks if the Strings stored in the Translation are all valid.
 Translation clone()
           
 Translation copy()
          Creates a new instance of T and copies all values.
 Translation copyTo(Translation translation2)
          Copy this Translation to another Translation e.g. for editing
static void fireLocaleChangeEvents()
          Informs all registered PropertyChangeListeners about a change of the the Locale.
 void fireTranslationChangedEvents(String lang)
           
 void fromOneLine(InternationalString iString)
           
 void fromOneLine(String oneLineCoded)
          Fills the Translation with the values coded into the String Format of String is: "de{Baum}en{tree}" If oneLineCoded is empty or null, NO TRANSLATION is set.
static String getActiveLang()
          Get the two-letter language sting that is active
 String put(String lang, String value)
           
static boolean removeLocaleChangeListener(PropertyChangeListener propertyChangeListener)
          PropertyChangeListener can be registered to be informed when the Locale changed.
 boolean removeTranslationChangeListener(ActionListener actionListener)
          The listeneras are stored in a WeakHashSet!
static void setActiveLang(String newLang)
          Set up the Translation-system to use language.
static void setActiveLang(String newLang, boolean setDefaultLocale)
          Set up the Translation-system to use language.
 String toOneLine()
          Exports the Translations to a String of the Format: "de{Baum}en{tree}"
 String toString()
          Returns the right translation by using the activeLang field.
 
Methods inherited from class java.util.HashMap
clear, containsKey, containsValue, entrySet, get, isEmpty, keySet, putAll, remove, size, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

LOCALECHANGE_PROPERTY

public static final String LOCALECHANGE_PROPERTY
See Also:
Constant Field Values

NO_TRANSLATION

public static final String NO_TRANSLATION
See Also:
Constant Field Values

DEFAULT_KEY

public static final String DEFAULT_KEY
See Also:
Constant Field Values

listeners

protected static  listeners
Constructor Detail

Translation

public Translation(String defaultTranslation)
Initializes a new Translation with a default translation if a simple text is passed. If a "oneLine" text is parsed, it is interpreted. Other translations may be added later - this is a HashMap

Parameters:
defaultTranslation -
See Also:
Translation(List languages, String defaultTranslation) {

Translation

public Translation(List<String> languages,
                   String defaultTranslation)
Initializes a new Translation, an uses the given String to initialize the Translation for all languages codes passed. The translations can be changed later


Translation

public Translation()
Sometimes Translations are optional, like for keywords.

Method Detail

clone

public Translation clone()
Overrides:
clone in class HashMap<String,String>

getActiveLang

public static String getActiveLang()
Get the two-letter language sting that is active


setActiveLang

public static void setActiveLang(String newLang)
Set up the Translation-system to use language. If a change is performed, events are fired to listeners. Nothing is done if the new language equals the old language. The system's default locale is changed.

Parameters:
newLang - The ISO Code of the new active language

setActiveLang

public static void setActiveLang(String newLang,
                                 boolean setDefaultLocale)
Set up the Translation-system to use language. If a change is performed, events are fired to listeners. Nothing is done if the new language equals the old language.

Parameters:
newLang - The ISO Code of the new active language
setDefaultLocale - Shall the system's default locale be changed?

fromOneLine

public void fromOneLine(String oneLineCoded)
Fills the Translation with the values coded into the String Format of String is: "de{Baum}en{tree}"


toOneLine

public String toOneLine()
Exports the Translations to a String of the Format: "de{Baum}en{tree}"


toString

public String toString()
Returns the right translation by using the activeLang field. If no translation is set, an ugly String NO_TRANSLATION will re returned. This might be changed for the final release. If the correct language was not found, any entry in the Translation HashMap will be returned, that contains more than an empty string.

Overrides:
toString in class AbstractMap<String,String>

addLocaleChangeListener

public static void addLocaleChangeListener(PropertyChangeListener propertyChangeListener)
PropertyChangeListener can be registered to be informed when the Locale changed.
The listeners are kept in a WeakHashMap, so you have to keep a reference to the listener or it will be removed!

Parameters:
propertyChangeListener - A PropertyChangeListener that will be called when setActiveLang(String) changes the language.

removeLocaleChangeListener

public static boolean removeLocaleChangeListener(PropertyChangeListener propertyChangeListener)
PropertyChangeListener can be registered to be informed when the Locale changed.
The listeners are kept in a WeakHashMap, so you have to keep a reference to the listener or it will be removed!

Parameters:
propertyChangeListener - A PropertyChangeListener that will be called when setActiveLang(String) changes the language.

fireLocaleChangeEvents

public static void fireLocaleChangeEvents()
Informs all registered PropertyChangeListeners about a change of the the Locale.


addTranslationChangeListener

public void addTranslationChangeListener(ActionListener actionListener)
The listeneras are stored in a WeakHashSet! So you HAVE TO KEEP a reference as long as you need the listener.


removeTranslationChangeListener

public boolean removeTranslationChangeListener(ActionListener actionListener)
The listeneras are stored in a WeakHashSet! You don't have to remove the listener, as long as you throw away the reference to the listener.


fireTranslationChangedEvents

public void fireTranslationChangedEvents(String lang)

put

public String put(String lang,
                  String value)
Specified by:
put in interface Map<String,String>
Overrides:
put in class HashMap<String,String>

fromOneLine

public void fromOneLine(InternationalString iString)

copyTo

public Translation copyTo(Translation translation2)
Copy this Translation to another Translation e.g. for editing

Specified by:
copyTo in interface Copyable<Translation>
Returns:
the destination Translation

copy

public Translation copy()
Description copied from interface: Copyable
Creates a new instance of T and copies all values.

Specified by:
copy in interface Copyable<Translation>

checkValid

public static boolean checkValid(Translation translationToCheck)
Checks if the Strings stored in the Translation are all valid.

Returns:
true if all good