[Schmitzm-commits] r34 - in trunk/src/skrueger: i8n swing

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sat Mar 28 19:08:42 CET 2009


Author: alfonx
Date: 2009-03-28 19:08:42 +0100 (Sat, 28 Mar 2009)
New Revision: 34

Modified:
   trunk/src/skrueger/i8n/Translation.java
   trunk/src/skrueger/swing/TranslationAskJDialog.java
Log:
Added Listener support to Translation. This allows to register a swing  PropertyChangeListener for Locales changes. 

Modified: trunk/src/skrueger/i8n/Translation.java
===================================================================
--- trunk/src/skrueger/i8n/Translation.java	2009-03-28 17:06:27 UTC (rev 33)
+++ trunk/src/skrueger/i8n/Translation.java	2009-03-28 18:08:42 UTC (rev 34)
@@ -1,12 +1,12 @@
 package skrueger.i8n;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
 
-import javax.swing.JColorChooser;
 import javax.swing.JComponent;
-import javax.swing.JFileChooser;
-import javax.swing.JOptionPane;
 
 import org.apache.log4j.Logger;
 
@@ -17,12 +17,15 @@
  * @author @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons Kr&uuml;ger</a>
  */
 
-public class Translation extends HashMap<String, String> {
+public class Translation extends HashMap<String, String>{
+	public static final String LOCALECHANGE_PROPERTY = "localechange";
 	public static final String NO_TRANSLATION = "NO TRANSLATION";
 	public static final String DEFAULT_KEY = "default";
 	static final Logger log = Logger.getLogger( Translation.class );
 	static String activeLang = "fr";
 
+	static protected List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();
+	
 	static {
 		
 		//TODO default aus Locale auslesen und mit möglichen vergleichen... mmm.. vor laden von atlasml immer DEFAULT_KEY, also hier nicht
@@ -33,6 +36,9 @@
 	}
 	
 	@Override
+	/**
+	 * @comment To make a copy of a translation see methods toOneLine() and fromOneLine()
+	 */
 	public Translation clone() {
 		return (Translation) super.clone();
 	}
@@ -63,11 +69,14 @@
 		JComponent.setDefaultLocale(newLocale);
 		
 		Translation.activeLang = activeLang;
-		log.info("Translation-system switched to "+activeLang);
+		
+		fireChangeEvents();
+		
+		log.info("skrueger.i8n.Translation switched ActiveLang to "+activeLang);
 	}
 
 	/**
-	 * Initilises a new {@link Translation} with a default translation.
+	 * Initializes a new {@link Translation} with a default translation.
 	 * Other translations may be added later.
 	 *
 	 * @param defaultTranslation
@@ -83,10 +92,10 @@
 	}
 
 	/**
-	 * Initilises a new {@link Translation}, an uses the given String to
-	 * initialise the {@link Translation} for all languages codes passed.
+	 * Initializes a new {@link Translation}, an uses the given String to
+	 * initialize the {@link Translation} for all languages codes passed.
 	 *
-	 * The translations can be cahnged later
+	 * The translations can be changed later
 	 */
 	public Translation(List<String> languages, String defaultTranslation) {
 		// put(DEFAULT_KEY, defaultTranslation);
@@ -203,5 +212,30 @@
 		}
 		return backup;
 	}
+	
+	
+	/**
+	 * {@link PropertyChangeListener} can be registered to be informed when the
+	 * {@link Locale} changed.
+	 * 
+	 * @param propertyChangeListener
+	 */
+	public static void addLocaleChangeListener(PropertyChangeListener propertyChangeListener) {
+		listeners.add(propertyChangeListener);
+	}
 
+	/**
+	 * Informs all registered {@link PropertyChangeListener}s about a change of the 
+	 * the {@link Locale}.  
+	 */
+	public static void fireChangeEvents() {
+		PropertyChangeEvent pce = new PropertyChangeEvent(new Translation(new ArrayList<String>(), "fakeSource"), LOCALECHANGE_PROPERTY,
+				null, getActiveLang());
+		for (PropertyChangeListener pcl : listeners) {
+			if (pcl != null)
+				pcl.propertyChange(pce);
+		}
+	}
+
+
 }

Modified: trunk/src/skrueger/swing/TranslationAskJDialog.java
===================================================================
--- trunk/src/skrueger/swing/TranslationAskJDialog.java	2009-03-28 17:06:27 UTC (rev 33)
+++ trunk/src/skrueger/swing/TranslationAskJDialog.java	2009-03-28 18:08:42 UTC (rev 34)
@@ -103,8 +103,9 @@
 			if (component instanceof TranslationEditJPanel) {
 				TranslationEditJPanel tep = (TranslationEditJPanel) component;
 				Translation orig = tep.getTranslation();
-				// We dont' want to overwrite the Translation object on
-				// restore(). We just want to change its values.
+				
+				// We don't want to overwrite the Translation object on
+				// restore(). We just want to change its value.
 
 				backup[count++] = orig.toOneLine();
 			}
@@ -245,10 +246,10 @@
 	}
 
 	/**
-	 * After the modal dialog has been closed, this allows to find out, wether
+	 * After the modal dialog has been closed, this allows to find out, whether
 	 * the dialog has been canceled.
 	 * 
-	 * @return
+	 * @return <code>true</code> if the {@link JDialog} has been canceled.
 	 */
 	public boolean isHasBeenCanceled() {
 		return hasBeenCanceled;



More information about the Schmitzm-commits mailing list