[Schmitzm-commits] r1097 - in trunk: src/skrueger/i8n src_junit/schmitzm/swing src_junit/skrueger/i8n

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sun Oct 10 23:14:06 CEST 2010


Author: alfonx
Date: 2010-10-10 23:14:05 +0200 (Sun, 10 Oct 2010)
New Revision: 1097

Added:
   trunk/src_junit/skrueger/i8n/I8NUtilTest.java
   trunk/src_junit/skrueger/i8n/TranslationTest.java
Modified:
   trunk/src/skrueger/i8n/I8NUtil.java
   trunk/src/skrueger/i8n/SwitchLanguageDialog.java
   trunk/src/skrueger/i8n/Translation.java
   trunk/src_junit/schmitzm/swing/TestingUtil.java
Log:
Added a bunch of test cases for Translation, as i want to seperate Locale.default (the GP GUI) and the atlas content langauge (Translation.activeLang).

Modified: trunk/src/skrueger/i8n/I8NUtil.java
===================================================================
--- trunk/src/skrueger/i8n/I8NUtil.java	2010-10-10 20:07:05 UTC (rev 1096)
+++ trunk/src/skrueger/i8n/I8NUtil.java	2010-10-10 21:14:05 UTC (rev 1097)
@@ -58,7 +58,7 @@
 	 * 
 	 * @author Stefan Alfons Tzeggai
 	 */
-	public static Translation createFromOneLIne(final String oneLineCoded) {
+	public static Translation createFromOneLine(final String oneLineCoded) {
 		Translation result = new Translation();
 		result.fromOneLine(oneLineCoded);
 		return result;
@@ -187,8 +187,6 @@
 		
 		replaced = replaced.replaceAll("ae", "\u00E4");
 		replaced = replaced.replaceAll("Ae", "\u00C4");
-		// TODO mehr
-		// TODO besser UTF \u123321 schreibweise
 		return replaced;
 	}
 }

Modified: trunk/src/skrueger/i8n/SwitchLanguageDialog.java
===================================================================
--- trunk/src/skrueger/i8n/SwitchLanguageDialog.java	2010-10-10 20:07:05 UTC (rev 1096)
+++ trunk/src/skrueger/i8n/SwitchLanguageDialog.java	2010-10-10 21:14:05 UTC (rev 1097)
@@ -62,8 +62,6 @@
 public class SwitchLanguageDialog extends JDialog {
 	protected Logger LOGGER = Logger.getLogger(SwitchLanguageDialog.class);
 
-	
-
 	private JPanel jContentPane = null;
 
 	private JLabel jLabelFlagimage = null;
@@ -96,7 +94,7 @@
 		if (languages.size() == 1) {
 			LOGGER.debug("Only language '" + languages.get(0)
 					+ "' is available. It has been selected automatically.");
-//			dispose();
+			// dispose();
 			setModal(false);
 			return;
 		}

Modified: trunk/src/skrueger/i8n/Translation.java
===================================================================
--- trunk/src/skrueger/i8n/Translation.java	2010-10-10 20:07:05 UTC (rev 1096)
+++ trunk/src/skrueger/i8n/Translation.java	2010-10-10 21:14:05 UTC (rev 1097)
@@ -55,47 +55,62 @@
 
 /**
  * Represents a {@link HashMap} of translations. toString() returns the
- * appropriate translation
+ * appropriate translation. This class is mutable.
  * 
- * @author @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons
- *         Tzeggai</a>
+ * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons Tzeggai</a>
  */
 
 public class Translation extends HashMap<String, String> implements
-		Copyable<Translation>, Serializable {
+		Copyable<Translation>, Serializable, Cloneable {
 
 	private static final long serialVersionUID = -347702744122305245L;
 
 	public static final String LOCALECHANGE_PROPERTY = "localechange";
+	public static final String ACTIVELANG_CHANGE_PROPERTY = "activelangchange";
 	public static final String NO_TRANSLATION = "NO TRANSLATION";
 	public static final String DEFAULT_KEY = "default";
 	static final Logger LOGGER = Logger.getLogger(Translation.class);
+
+	/** A static field defining which language should be served **/
 	static String activeLang = Locale.getDefault().getLanguage();
 
-	static protected WeakHashSet<PropertyChangeListener> listeners = new WeakHashSet<PropertyChangeListener>(
+	/**
+	 * A {@link WeakHashSet} of {@link PropertyChangeListener} that will be
+	 * informed if the acitveLang has been changed with a setActiveLang(..,true
+	 * or false).
+	 */
+	static protected WeakHashSet<PropertyChangeListener> listenersActiveLangChange = new WeakHashSet<PropertyChangeListener>(
 			PropertyChangeListener.class);
 
-	static {
+	/**
+	 * A {@link WeakHashSet} of {@link PropertyChangeListener} that will be
+	 * informed if the Locale has been changed due to a setActiveLang(..,true)
+	 */
+	static protected WeakHashSet<PropertyChangeListener> listenersLocaleChange = new WeakHashSet<PropertyChangeListener>(
+			PropertyChangeListener.class);
 
-		// TODO default aus Locale auslesen und mit möglichen vergleichen...
-		// mmm.. vor laden von atlasml immer DEFAULT_KEY, also hier nicht
+	/**
+	 * A {@link WeakHashSet} of {@link PropertyChangeListener} that will be
+	 * informed if any of the translations changed. TODO: Should be registerable
+	 * for specific languages
+	 */
+	private WeakHashSet<ActionListener> changeListeners = new WeakHashSet<ActionListener>(
+			ActionListener.class);
 
+	static {
 		// Get default locale
 		Locale locale = Locale.getDefault();
 		setActiveLang(locale.getLanguage());
 	}
 
-	private WeakHashSet<ActionListener> actionListeners = new WeakHashSet<ActionListener>(
-			ActionListener.class);
-
 	@Override
-	/*
-	 * @comment To make a copy of a translation see methods toOneLine() and
-	 * fromOneLine()
+	/**
+	 * implemented using #toOneLine and #fromOneLine 
 	 */
 	public Translation clone() {
-		throw new RuntimeException("use copy()");
-		// return (Translation) super.clone();
+		Translation clone = new Translation();
+		clone.fromOneLine(toOneLine());
+		return clone;
 	}
 
 	/**
@@ -108,7 +123,8 @@
 	/**
 	 * Set up the {@link 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 {@link Locale} is changed.
+	 * language equals the old language. The system's default {@link Locale} is
+	 * changed.
 	 * 
 	 * @param newLang
 	 *            The ISO Code of the new active language
@@ -138,22 +154,28 @@
 					+ "' is not a valid ISO language code.");
 		}
 
+		Translation.activeLang = newLang;
+		fireActiveLangChangeEvents();
+
 		Locale newLocale = new Locale(newLang);
-		if (setDefaultLocale)
+		if (setDefaultLocale) {
+
 			Locale.setDefault(newLocale);
+			/**
+			 * Setting default locale for Swing JComponents to work around bug
+			 * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4884480
+			 */
+			JComponent.setDefaultLocale(newLocale);
 
-		/**
-		 * Setting default locale for Swing JComponents to work around bug
-		 * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4884480
-		 */
-		JComponent.setDefaultLocale(newLocale);
+			fireLocaleChangeEvents();
 
-		Translation.activeLang = newLang;
+			LOGGER.info(Translation.class.getName()
+					+ " switched ActiveLang and Locale to " + newLang);
+		} else {
+			LOGGER.info(Translation.class.getName()
+					+ " switched ActiveLang to " + newLang);
+		}
 
-		fireLocaleChangeEvents();
-
-		LOGGER.info("skrueger.i8n.Translation switched ActiveLang to "
-				+ newLang);
 	}
 
 	/**
@@ -175,19 +197,21 @@
 
 	/**
 	 * Initializes a new {@link Translation}, an uses the given String to
-	 * initialize the {@link Translation} for all languages codes passed.
+	 * initialize the {@link Translation} for all languages codes passed. The
+	 * translations can be changed later. This class is not immutable.
 	 * 
-	 * The translations can be changed later
+	 * @param languages
+	 *            if empty or null, the given default {@link Translation} till
+	 *            be stored under a special key {@link #DEFAULT_KEY}
+	 * 
+	 * 
 	 */
 	public Translation(List<String> languages, String defaultTranslation) {
-		// put(DEFAULT_KEY, defaultTranslation);
-		if (languages == null) {
+		if (languages == null || languages.isEmpty()) {
 			put(DEFAULT_KEY, defaultTranslation);
 		} else
 			for (String code : languages) {
-				// if (code.equals(getActiveLang())) {
 				put(code, defaultTranslation);
-				// }
 			}
 	}
 
@@ -254,8 +278,8 @@
 	}
 
 	/**
-	 * Returns the right translation by using the {@link #activeLang} field. If
-	 * no translation is set, an ugly String {@link #NO_TRANSLATION} will re
+	 * Returns the correct translation by using the {@link #activeLang} field.
+	 * If no translation is set, an ugly String {@link #NO_TRANSLATION} will re
 	 * returned. This might be changed for the final release. If the correct
 	 * language was not found, any entry in the {@link Translation}
 	 * {@link HashMap} will be returned, that contains more than an empty
@@ -263,7 +287,8 @@
 	 */
 	@Override
 	public String toString() {
-		if (get(activeLang) != null) {
+		final String string = get(activeLang);
+		if (string != null) {
 			return get(activeLang);
 		}
 		// ****************************************************************************
@@ -272,7 +297,8 @@
 		// ****************************************************************************
 		// else return "";
 		// //****************************************************************************
-		// // The following is commented out.. the concept of the default lang
+		// // ST: The following is commented out.. the concept of the default
+		// lang
 		// seems to be bad....
 		// //****************************************************************************
 		// MS:
@@ -304,7 +330,7 @@
 	 */
 	public static void addLocaleChangeListener(
 			PropertyChangeListener propertyChangeListener) {
-		listeners.add(propertyChangeListener);
+		listenersLocaleChange.add(propertyChangeListener);
 	}
 
 	/**
@@ -319,10 +345,40 @@
 	 */
 	public static boolean removeLocaleChangeListener(
 			PropertyChangeListener propertyChangeListener) {
-		return listeners.remove(propertyChangeListener);
+		return listenersLocaleChange.remove(propertyChangeListener);
 	}
 
 	/**
+	 * {@link PropertyChangeListener} can be registered to be informed when the
+	 * {@link Locale} changed.<br>
+	 * The listeners are kept in a {@link WeakHashMap}, so you have to keep a
+	 * reference to the listener or it will be removed!
+	 * 
+	 * @param propertyChangeListener
+	 *            A {@link PropertyChangeListener} that will be called when
+	 *            {@link #setActiveLang(String)} changes the language.
+	 */
+	public static void addActiveLangChangeListener(
+			PropertyChangeListener propertyChangeListener) {
+		listenersActiveLangChange.add(propertyChangeListener);
+	}
+
+	/**
+	 * {@link PropertyChangeListener} can be registered to be informed when the
+	 * {@link Locale} changed.<br>
+	 * The listeners are kept in a {@link WeakHashMap}, so you have to keep a
+	 * reference to the listener or it will be removed!
+	 * 
+	 * @param propertyChangeListener
+	 *            A {@link PropertyChangeListener} that will be called when
+	 *            {@link #setActiveLang(String)} changes the language.
+	 */
+	public static boolean removeActiveLangListener(
+			PropertyChangeListener propertyChangeListener) {
+		return listenersActiveLangChange.remove(propertyChangeListener);
+	}
+
+	/**
 	 * Informs all registered {@link PropertyChangeListener}s about a change of
 	 * the the {@link Locale}.
 	 */
@@ -330,18 +386,32 @@
 		PropertyChangeEvent pce = new PropertyChangeEvent(new Translation(
 				new ArrayList<String>(), "fakeSource"), LOCALECHANGE_PROPERTY,
 				null, getActiveLang());
-		for (PropertyChangeListener pcl : listeners) {
+		for (PropertyChangeListener pcl : listenersLocaleChange) {
 			if (pcl != null)
 				pcl.propertyChange(pce);
 		}
 	}
 
 	/**
+	 * Informs all registered {@link PropertyChangeListener}s about a change of
+	 * the the {@link Locale}.
+	 */
+	public static void fireActiveLangChangeEvents() {
+		PropertyChangeEvent pce = new PropertyChangeEvent(new Translation(
+				new ArrayList<String>(), "fakeSource"),
+				ACTIVELANG_CHANGE_PROPERTY, null, getActiveLang());
+		for (PropertyChangeListener pcl : listenersActiveLangChange) {
+			if (pcl != null)
+				pcl.propertyChange(pce);
+		}
+	}
+
+	/**
 	 * The listeneras are stored in a {@link WeakHashSet}! So you HAVE TO KEEP a
 	 * reference as long as you need the listener.
 	 */
 	public void addTranslationChangeListener(ActionListener actionListener) {
-		if (actionListeners.add(actionListener)) {
+		if (changeListeners.add(actionListener)) {
 			// LOGGER
 			// .debug("registering a new translationChangeActionListener in the WeakHashSet");
 		}
@@ -353,13 +423,13 @@
 	 * listener.
 	 */
 	public boolean removeTranslationChangeListener(ActionListener actionListener) {
-		return actionListeners.remove(actionListener);
+		return changeListeners.remove(actionListener);
 	}
 
 	public void fireTranslationChangedEvents(String lang) {
 		ActionEvent ae = new ActionEvent(this, new Random().nextInt(), lang);
 
-		final Iterator<ActionListener> iterator = actionListeners.iterator();
+		final Iterator<ActionListener> iterator = changeListeners.iterator();
 		while (iterator.hasNext()) {
 			ActionListener al = iterator.next();
 			al.actionPerformed(ae);
@@ -382,21 +452,16 @@
 
 	/**
 	 * Copy this {@link Translation} to another {@link Translation} e.g. for
-	 * editing
+	 * editing and return the target.
 	 * 
 	 * @return the destination {@link Translation}
 	 */
 	@Override
 	public Translation copyTo(Translation translation2) {
-
 		if (translation2 == null)
-			// throw new IllegalArgumentException(
-			// "Target translation may not be null.");
 			return copy();
-		for (String s : keySet()) {
-			translation2.put(s, get(s));
-		}
 
+		translation2.fromOneLine(toOneLine());
 		return translation2;
 	}
 
@@ -424,10 +489,13 @@
 	}
 
 	/**
-	 * Goes through the available languages of the FIRST registered {@link ResourceProvider} and set the active locale to the fist match. 
+	 * Goes through the available languages of the FIRST registered
+	 * {@link ResourceProvider} and set the active locale to the fist match.
 	 * 
-	 * @param fireChangeEvent if <code>true</code>, a Translation.fireLocaleChangeEvents() is issued.
-
+	 * @param fireChangeEvent
+	 *            if <code>true</code>, a Translation.fireLocaleChangeEvents()
+	 *            is issued.
+	 * 
 	 * @return
 	 */
 	public static boolean setFirstmatchingLanguage(List<String> languages,
@@ -452,4 +520,11 @@
 		return false;
 
 	}
+
+	/**
+	 * Returns the translation in a requested language
+	 */
+	public String toString(String lang) {
+		return get(lang);
+	}
 }

Modified: trunk/src_junit/schmitzm/swing/TestingUtil.java
===================================================================
--- trunk/src_junit/schmitzm/swing/TestingUtil.java	2010-10-10 20:07:05 UTC (rev 1096)
+++ trunk/src_junit/schmitzm/swing/TestingUtil.java	2010-10-10 21:14:05 UTC (rev 1097)
@@ -20,6 +20,7 @@
 
 import javax.swing.ImageIcon;
 import javax.swing.JComponent;
+import javax.swing.JDialog;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
@@ -94,6 +95,13 @@
 
 		// If it is a modal dialog, unmodalize it
 		w.setModalExclusionType(ModalExclusionType.NO_EXCLUDE);
+
+		if (w instanceof JDialog) {
+			// JDialog.setModal must be unset additionally.
+			JDialog d = (JDialog) w;
+			d.setModal(false);
+		}
+
 		w.setVisible(true);
 		int countWait = 0;
 		while (w.isVisible() && !stopFlag.get()) {

Added: trunk/src_junit/skrueger/i8n/I8NUtilTest.java
===================================================================
--- trunk/src_junit/skrueger/i8n/I8NUtilTest.java	2010-10-10 20:07:05 UTC (rev 1096)
+++ trunk/src_junit/skrueger/i8n/I8NUtilTest.java	2010-10-10 21:14:05 UTC (rev 1097)
@@ -0,0 +1,53 @@
+package skrueger.i8n;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class I8NUtilTest {
+
+	final static String oneLineCoded = "de{Baum}en{tree}";
+	final static List<String> langs4 = new ArrayList<String>();
+
+	@BeforeClass
+	public static void setup() {
+		langs4.add("de");
+		langs4.add("en");
+		langs4.add("xx");
+		langs4.add("yy");
+	}
+
+	@Test
+	public void testCreateFromOneLine() {
+		Translation t = I8NUtil.createFromOneLine(oneLineCoded);
+		assertEquals("Baum", t.toString("de"));
+		assertEquals("tree", t.toString("en"));
+		assertEquals(oneLineCoded, t.toOneLine());
+	}
+
+	@Test
+	public void testIsValidISOLangCode() {
+		assertTrue(I8NUtil.isValidISOLangCode("de"));
+		assertTrue(I8NUtil.isValidISOLangCode("ar"));
+		assertFalse(I8NUtil.isValidISOLangCode("äü"));
+		assertFalse(I8NUtil.isValidISOLangCode("ara"));
+	}
+
+	@Test
+	public void testMitUmlaute() {
+		assertEquals("öäü", I8NUtil.mitUmlaute("oeaeue"));
+	}
+
+	@Test
+	public void testQmTranslation() {
+		Translation t = I8NUtil.createFromOneLine(oneLineCoded);
+		assertEquals(0.5, I8NUtil.qmTranslation(langs4, t), .00000000001);
+	}
+
+}


Property changes on: trunk/src_junit/skrueger/i8n/I8NUtilTest.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id URL
Name: svn:eol-style
   + native

Added: trunk/src_junit/skrueger/i8n/TranslationTest.java
===================================================================
--- trunk/src_junit/skrueger/i8n/TranslationTest.java	2010-10-10 20:07:05 UTC (rev 1096)
+++ trunk/src_junit/skrueger/i8n/TranslationTest.java	2010-10-10 21:14:05 UTC (rev 1097)
@@ -0,0 +1,128 @@
+package skrueger.i8n;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class TranslationTest {
+
+	@Before
+	public void setUp() throws Exception {
+	}
+
+	@After
+	public void tearDown() throws Exception {
+	}
+
+	@Test
+	public void testCopyToAndEquals() {
+		Translation t = new Translation();
+		Translation t2 = new Translation();
+		t.put("de", "berg");
+		t.put("en", "mountain");
+		t.copyTo(t2);
+		assertEquals(t2.keySet(), t.keySet());
+		assertEquals(t2.toOneLine(), t.toOneLine());
+		assertEquals(t2, t);
+
+		assertEquals(t.copyTo(null), t);
+	}
+
+	@Test
+	public void testCopyAndClone() {
+		Translation t = new Translation();
+		t.put("de", "berg");
+		t.put("en", "mountain");
+		Translation t2 = t.copy();
+
+		assertEquals(t2, t);
+
+		Translation t3 = t2.clone();
+		assertEquals(t3, t);
+	}
+
+	@Test
+	public void testActiveLangChanges() {
+		Translation t = new Translation();
+		final AtomicBoolean calledAl = new AtomicBoolean(false);
+		final AtomicBoolean calledL = new AtomicBoolean(false);
+		final AtomicBoolean calledC = new AtomicBoolean(false);
+
+		Translation.addActiveLangChangeListener(new PropertyChangeListener() {
+
+			@Override
+			public void propertyChange(PropertyChangeEvent evt) {
+				calledAl.set(true);
+			}
+		});
+
+		Translation.addLocaleChangeListener(new PropertyChangeListener() {
+
+			@Override
+			public void propertyChange(PropertyChangeEvent evt) {
+				calledL.set(true);
+			}
+		});
+
+		t.addTranslationChangeListener(new ActionListener() {
+
+			@Override
+			public void actionPerformed(ActionEvent e) {
+				calledC.set(true);
+			}
+		});
+
+		// Change the content
+		t.put("de", "change");
+		assertFalse(calledAl.get());
+		assertFalse(calledL.get());
+		assertTrue(calledC.get());
+
+		// Change active language by default
+		calledAl.set(false);
+		calledL.set(false);
+		calledC.set(false);
+		Translation.setActiveLang("en");
+		assertTrue(calledAl.get());
+		assertTrue(calledL.get());
+		assertFalse(calledC.get());
+
+		calledAl.set(false);
+		calledL.set(false);
+		Translation.setActiveLang("ar", false);
+		assertTrue(calledAl.get());
+		assertFalse(calledL.get());
+		assertFalse(calledC.get());
+
+		calledAl.set(false);
+		Translation.setActiveLang("ar", false);
+		assertFalse(
+				"if the same language is set again, it should not trigger events",
+				calledAl.get());
+		assertFalse(calledC.get());
+	}
+
+	@Test
+	public void testToString() {
+		Translation t = new Translation();
+		t.put("de", "de");
+		t.put("en", "en");
+		t.put("fr", "fr");
+		t.put("ru", "ru");
+
+		Translation.setActiveLang("de");
+		assertEquals("de", t.toString());
+		Translation.setActiveLang("ru");
+		assertEquals("ru", t.toString());
+	}
+}


Property changes on: trunk/src_junit/skrueger/i8n/TranslationTest.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id URL
Name: svn:eol-style
   + native



More information about the Schmitzm-commits mailing list