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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon Oct 11 02:07:20 CEST 2010


Author: alfonx
Date: 2010-10-11 02:07:14 +0200 (Mon, 11 Oct 2010)
New Revision: 1100

Modified:
   trunk/src/schmitzm/swing/ExceptionDialog.java
   trunk/src/schmitzm/swing/SwingUtil.java
   trunk/src/skrueger/i8n/I8NUtil.java
   trunk/src/skrueger/i8n/SwitchLanguageDialog.java
   trunk/src/skrueger/i8n/Translation.java
   trunk/src_junit/schmitzm/swing/SwingUtilTest.java
   trunk/src_junit/schmitzm/swing/TestingUtil.java
   trunk/src_junit/skrueger/i8n/I8NUtilTest.java
Log:
Geopublisher can now switch languages for the GUI and the atlas data mostly independent.

Modified: trunk/src/schmitzm/swing/ExceptionDialog.java
===================================================================
--- trunk/src/schmitzm/swing/ExceptionDialog.java	2010-10-10 22:58:24 UTC (rev 1099)
+++ trunk/src/schmitzm/swing/ExceptionDialog.java	2010-10-11 00:07:14 UTC (rev 1100)
@@ -312,8 +312,11 @@
 	public static void show(Throwable err) {
 		if (GraphicsEnvironment.isHeadless())
 			err.printStackTrace();
-		else
+		else {
+			// TODO Hier sollte überprüft werden, ob man sich auf dem Swing Thread befindet!
+			// if (SwingUtilities.isEventDispatchThread())
 			new ExceptionDialog(null, err).setVisible(true);
+		}
 	}
 
 	/**

Modified: trunk/src/schmitzm/swing/SwingUtil.java
===================================================================
--- trunk/src/schmitzm/swing/SwingUtil.java	2010-10-10 22:58:24 UTC (rev 1099)
+++ trunk/src/schmitzm/swing/SwingUtil.java	2010-10-11 00:07:14 UTC (rev 1100)
@@ -68,6 +68,7 @@
 import javax.swing.JScrollPane;
 import javax.swing.JTable;
 import javax.swing.JTree;
+import javax.swing.SwingUtilities;
 import javax.swing.table.TableCellRenderer;
 import javax.swing.tree.TreeNode;
 import javax.swing.tree.TreePath;
@@ -1431,4 +1432,24 @@
 			LOGGER.info("failed", e);
 		}
 	}
+	
+
+	/**
+	 * Throws a {@link RuntimeException} if this method is called on the EDT.
+	 */
+	public static void checkNotOnEDT() {
+		if (SwingUtilities.isEventDispatchThread())
+			throw new RuntimeException("On EDT!");
+	}
+
+	/**
+	 * Throws a {@link RuntimeException} if this method is NOT called on the
+	 * EDT.
+	 */
+	public static void checkOnEDT() {
+		if (!SwingUtilities.isEventDispatchThread()) {
+			LOGGER.error("Not on EDT");
+			throw new RuntimeException("Not on EDT!");
+		}
+	}
 }

Modified: trunk/src/skrueger/i8n/I8NUtil.java
===================================================================
--- trunk/src/skrueger/i8n/I8NUtil.java	2010-10-10 22:58:24 UTC (rev 1099)
+++ trunk/src/skrueger/i8n/I8NUtil.java	2010-10-11 00:07:14 UTC (rev 1100)
@@ -29,19 +29,21 @@
  ******************************************************************************/
 package skrueger.i8n;
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
 import java.util.Set;
 import java.util.TreeSet;
 
 import org.apache.log4j.Logger;
+import org.geotools.resources.i18n.Locales;
 
 public class I8NUtil {
 	static final Logger LOGGER = Logger.getLogger(I8NUtil.class);
 
 	private static Set<String> languageCodes = new TreeSet<String>();
 	static {
-		for (Locale locale : java.util.Locale.getAvailableLocales()) {
+		for (final Locale locale : java.util.Locale.getAvailableLocales()) {
 			getLanguageCodes().add(locale.getLanguage());
 		}
 		// for (String code : java.util.Locale.getISOLanguages()) {
@@ -59,7 +61,7 @@
 	 * @author Stefan Alfons Tzeggai
 	 */
 	public static Translation createFromOneLine(final String oneLineCoded) {
-		Translation result = new Translation();
+		final Translation result = new Translation();
 		result.fromOneLine(oneLineCoded);
 		return result;
 	}
@@ -70,9 +72,9 @@
 	 * 
 	 * @author Stefan Alfons Tzeggai
 	 */
-	public static String toOneLine(Translation source) {
-		StringBuffer oneLine = new StringBuffer();
-		for (String key : source.keySet()) {
+	public static String toOneLine(final Translation source) {
+		final StringBuffer oneLine = new StringBuffer();
+		for (final String key : source.keySet()) {
 			oneLine.append(key + "{" + source.get(key) + "}");
 		}
 		return oneLine.toString();
@@ -83,7 +85,7 @@
 	 * @param code
 	 * @return true if the code paramter is a valid ISO Language code
 	 */
-	public static boolean isValidISOLangCode(String code) {
+	public static boolean isValidISOLangCode(final String code) {
 		return getLanguageCodes().contains(code);
 	}
 
@@ -96,19 +98,41 @@
 	}
 
 	/**
+	 * Lookup {@link Locale} where they speak the 2 letter code language.
+	 * 
 	 * @param code
 	 *            A two-letter language code.
 	 * @return <code>null</code> or one (of many possible) {@link Locale} that
 	 *         uses this language.
 	 */
-	public static Locale getLocaleFor(String code) {
-		for (Locale l : Locale.getAvailableLocales()) {
+	public static List<Locale> getLocalesForLang(final String code) {
+
+		final ArrayList<Locale> locales = new ArrayList<Locale>();
+
+		for (final Locale l : Locale.getAvailableLocales()) {
 			if (l.getLanguage().equals(code.toLowerCase())) {
-				return l;
+				locales.add(l);
 			}
-			// LOGGER.debug(l.getLanguage() + " not = " + code);
 		}
 
+		return locales;
+	}
+
+	/**
+	 * Lookup first country where they speak the 2 letter code language.
+	 * 
+	 * @param code
+	 *            A two-letter language code.
+	 * @return <code>null</code> or one (of many possible) {@link Locale} that
+	 *         uses this language.
+	 */
+	public static Locale getFirstLocaleForLang(final String code) {
+
+		List<Locale> locales = getLocalesForLang(code);
+
+		if (locales.size() > 0)
+			return locales.get(0);
+
 		LOGGER.error("Can't create a Locale for code " + code
 				+ "! Returning the system default locale to avoid NPEs.");
 
@@ -174,17 +198,18 @@
 	/**
 	 * The German Umlaute have standard ASCII alternatives that are sometimes
 	 * use. This method will replace any possible ASCII-Umlaut Representation
-	 * into real Umlaute. E.g. "ae" to "ä" and "ue" to "ü". Umlaute are returned as inline-UTF8. 
+	 * into real Umlaute. E.g. "ae" to "ä" and "ue" to "ü". Umlaute are returned
+	 * as inline-UTF8.
 	 */
-	public static String mitUmlaute(String withoutUmlaute) {
+	public static String mitUmlaute(final String withoutUmlaute) {
 		String replaced = withoutUmlaute;
-		
+
 		replaced = replaced.replaceAll("ue", "\u00FC");
 		replaced = replaced.replaceAll("Ue", "\u00DC");
-		
+
 		replaced = replaced.replaceAll("oe", "\u00F6");
 		replaced = replaced.replaceAll("Oe", "\u00D6");
-		
+
 		replaced = replaced.replaceAll("ae", "\u00E4");
 		replaced = replaced.replaceAll("Ae", "\u00C4");
 		return replaced;

Modified: trunk/src/skrueger/i8n/SwitchLanguageDialog.java
===================================================================
--- trunk/src/skrueger/i8n/SwitchLanguageDialog.java	2010-10-10 22:58:24 UTC (rev 1099)
+++ trunk/src/skrueger/i8n/SwitchLanguageDialog.java	2010-10-11 00:07:14 UTC (rev 1100)
@@ -279,7 +279,7 @@
 			String[] langNames = new String[languages.size() + 1];
 			for (int i = 0; i < languages.size(); i++) {
 
-				Locale locale = I8NUtil.getLocaleFor(languages.get(i));
+				Locale locale = I8NUtil.getFirstLocaleForLang(languages.get(i));
 
 				langNames[i] = locale.getDisplayLanguage(locale) + " / "
 						+ locale.getDisplayLanguage() + " / "

Modified: trunk/src/skrueger/i8n/Translation.java
===================================================================
--- trunk/src/skrueger/i8n/Translation.java	2010-10-10 22:58:24 UTC (rev 1099)
+++ trunk/src/skrueger/i8n/Translation.java	2010-10-11 00:07:14 UTC (rev 1100)
@@ -65,7 +65,7 @@
 
 	private static final long serialVersionUID = -347702744122305245L;
 
-	public static final String LOCALECHANGE_PROPERTY = "localechange";
+	public static final String LOCALE_CHANGE_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";
@@ -160,15 +160,8 @@
 		Locale newLocale = new Locale(newLang);
 		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);
+			setDefaultLocale(newLocale);
 
-			fireLocaleChangeEvents();
-
 			LOGGER.info(Translation.class.getName()
 					+ " switched ActiveLang and Locale to " + newLang);
 		} else {
@@ -380,11 +373,11 @@
 
 	/**
 	 * Informs all registered {@link PropertyChangeListener}s about a change of
-	 * the the {@link Locale}.
+	 * type LOCALE_CHANGE_PROPERTY the the {@link Locale}.
 	 */
 	public static void fireLocaleChangeEvents() {
 		PropertyChangeEvent pce = new PropertyChangeEvent(new Translation(
-				new ArrayList<String>(), "fakeSource"), LOCALECHANGE_PROPERTY,
+				new ArrayList<String>(), "fakeSource"), LOCALE_CHANGE_PROPERTY,
 				null, getActiveLang());
 		for (PropertyChangeListener pcl : listenersLocaleChange) {
 			if (pcl != null)
@@ -394,13 +387,13 @@
 
 	/**
 	 * Informs all registered {@link PropertyChangeListener}s about a change of
-	 * the the {@link Locale}.
+	 * type ACTIVELANG_CHANGE_PROPERTY 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) {
+		for (PropertyChangeListener pcl : listenersLocaleChange) {
 			if (pcl != null)
 				pcl.propertyChange(pce);
 		}
@@ -527,4 +520,28 @@
 	public String toString(String lang) {
 		return get(lang);
 	}
+
+	/**
+	 * Will set the default Locale (if not already equal) and fire Locale change
+	 * events.
+	 * 
+	 * @param if <code>null</code> will do nothing.
+	 */
+	public static void setDefaultLocale(Locale locale) {
+
+		if (locale == null)
+			return;
+
+		if (Locale.getDefault().equals(locale))
+			return;
+		Locale.setDefault(locale);
+		/**
+		 * Setting default locale for Swing JComponents to work around bug
+		 * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4884480
+		 */
+		JComponent.setDefaultLocale(locale);
+
+		fireLocaleChangeEvents();
+	}
+
 }

Modified: trunk/src_junit/schmitzm/swing/SwingUtilTest.java
===================================================================
--- trunk/src_junit/schmitzm/swing/SwingUtilTest.java	2010-10-10 22:58:24 UTC (rev 1099)
+++ trunk/src_junit/schmitzm/swing/SwingUtilTest.java	2010-10-11 00:07:14 UTC (rev 1100)
@@ -6,6 +6,9 @@
 
 import org.junit.Test;
 
+/**
+ * Tests for the {@link SwingUtil} class.
+ */
 public class SwingUtilTest {
 
 	@Test

Modified: trunk/src_junit/schmitzm/swing/TestingUtil.java
===================================================================
--- trunk/src_junit/schmitzm/swing/TestingUtil.java	2010-10-10 22:58:24 UTC (rev 1099)
+++ trunk/src_junit/schmitzm/swing/TestingUtil.java	2010-10-11 00:07:14 UTC (rev 1100)
@@ -43,7 +43,7 @@
 import schmitzm.lang.LangUtil;
 
 /**
- * Helpers to test Swing applications.
+ * Helpers to test Swing applications in general.
  */
 
 @Ignore

Modified: trunk/src_junit/skrueger/i8n/I8NUtilTest.java
===================================================================
--- trunk/src_junit/skrueger/i8n/I8NUtilTest.java	2010-10-10 22:58:24 UTC (rev 1099)
+++ trunk/src_junit/skrueger/i8n/I8NUtilTest.java	2010-10-11 00:07:14 UTC (rev 1100)
@@ -6,6 +6,7 @@
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Locale;
 
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -50,4 +51,17 @@
 		assertEquals(0.5, I8NUtil.qmTranslation(langs4, t), .00000000001);
 	}
 
+	@Test
+	public void testGetFirstLocaleForLang() {
+		assertEquals(new Locale("de_DE"), I8NUtil.getFirstLocaleForLang("po"));
+	}
+
+	@Test
+	public void testGetLocalesForLang() {
+		List<Locale> locales = I8NUtil.getLocalesForLang("en");
+
+		assertTrue(locales.contains(new Locale("en", "za")));
+		assertTrue(locales.contains(new Locale("en", "sg")));
+	}
+
 }



More information about the Schmitzm-commits mailing list