[Schmitzm-commits] r482 - branches/1.0-gt2-2.6/src/skrueger/i8n

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Oct 20 15:51:11 CEST 2009


Author: alfonx
Date: 2009-10-20 15:51:11 +0200 (Tue, 20 Oct 2009)
New Revision: 482

Modified:
   branches/1.0-gt2-2.6/src/skrueger/i8n/Translation.java
Log:
* GP-Chart-Feature: Some smarter default settings for scatter plots.
* GP-Feature: Better listener management in Translation.java

Modified: branches/1.0-gt2-2.6/src/skrueger/i8n/Translation.java
===================================================================
--- branches/1.0-gt2-2.6/src/skrueger/i8n/Translation.java	2009-10-20 13:02:54 UTC (rev 481)
+++ branches/1.0-gt2-2.6/src/skrueger/i8n/Translation.java	2009-10-20 13:51:11 UTC (rev 482)
@@ -35,6 +35,7 @@
 import java.beans.PropertyChangeListener;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
 import java.util.Random;
@@ -42,6 +43,7 @@
 import javax.swing.JComponent;
 
 import org.apache.log4j.Logger;
+import org.geotools.util.WeakHashSet;
 import org.opengis.util.InternationalString;
 
 import skrueger.geotools.Copyable;
@@ -54,7 +56,8 @@
  *         Kr&uuml;ger</a>
  */
 
-public class Translation extends HashMap<String, String> implements Copyable<Translation>{
+public class Translation extends HashMap<String, String> implements
+		Copyable<Translation> {
 	public static final String LOCALECHANGE_PROPERTY = "localechange";
 	public static final String NO_TRANSLATION = "NO TRANSLATION";
 	public static final String DEFAULT_KEY = "default";
@@ -73,7 +76,8 @@
 		setActiveLang(locale.getLanguage());
 	}
 
-	private List<ActionListener> actionListeners = new ArrayList<ActionListener>();
+	private WeakHashSet<ActionListener> actionListeners = new WeakHashSet<ActionListener>(
+			ActionListener.class);
 
 	@Override
 	/*
@@ -82,7 +86,7 @@
 	 */
 	public Translation clone() {
 		throw new RuntimeException("use copy()");
-//		return (Translation) super.clone();
+		// return (Translation) super.clone();
 	}
 
 	/**
@@ -197,34 +201,35 @@
 	public void fromOneLine(final String oneLineCoded) {
 
 		clear();
-		
+
 		try {
-			
-		if ((oneLineCoded == null) || (oneLineCoded.equals(""))) {
-			put(DEFAULT_KEY, "");
-			return;
-		}
 
-		if (oneLineCoded.indexOf("}") == -1) {
-			// log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);");
-			put(DEFAULT_KEY, oneLineCoded);
-		}
+			if ((oneLineCoded == null) || (oneLineCoded.equals(""))) {
+				put(DEFAULT_KEY, "");
+				return;
+			}
 
-		String eatUp = oneLineCoded;
-		while (eatUp.indexOf("}") != -1) {
-			String substring = eatUp.substring(0, eatUp.indexOf("}"));
+			if (oneLineCoded.indexOf("}") == -1) {
+				// log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);");
+				put(DEFAULT_KEY, oneLineCoded);
+			}
 
-			// log.debug("substring = "+substring);
-			String key = substring.substring(0, substring.indexOf("{"));
-			String value = substring.substring(substring.indexOf("{") + 1,
-					substring.length());
-			// log.debug("key="+key);
-			// log.debug("value="+value);
-			put(key, value);
-			eatUp = eatUp.substring(eatUp.indexOf("}") + 1);
-		}
+			String eatUp = oneLineCoded;
+			while (eatUp.indexOf("}") != -1) {
+				String substring = eatUp.substring(0, eatUp.indexOf("}"));
+
+				// log.debug("substring = "+substring);
+				String key = substring.substring(0, substring.indexOf("{"));
+				String value = substring.substring(substring.indexOf("{") + 1,
+						substring.length());
+				// log.debug("key="+key);
+				// log.debug("value="+value);
+				put(key, value);
+				eatUp = eatUp.substring(eatUp.indexOf("}") + 1);
+			}
 		} catch (Exception e) {
-			log.warn("Error while reading the oneLineCode '"+oneLineCoded+"'", e);
+			log.warn("Error while reading the oneLineCode '" + oneLineCoded
+					+ "'", e);
 			log.warn("Translation will be empty!");
 		}
 	}
@@ -273,11 +278,10 @@
 						return s;
 				}
 		}
-//		log.warn("No translation found!");
+		// log.warn("No translation found!");
 		return NO_TRANSLATION;
 	}
 
-
 	/**
 	 * {@link PropertyChangeListener} can be registered to be informed when the
 	 * {@link Locale} changed.
@@ -303,22 +307,33 @@
 		}
 	}
 
+	/**
+	 * 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) {
 		actionListeners.add(actionListener);
 	}
-	
+
+	/**
+	 * The listeneras are stored in a {@link WeakHashSet}! You don't have to
+	 * remove the listener, as long as you throw away the reference to the
+	 * listener.
+	 */
 	public boolean removeTranslationChangeListener(ActionListener actionListener) {
 		return actionListeners.remove(actionListener);
 	}
 
 	public void fireTranslationChangedEvents(String lang) {
 		ActionEvent ae = new ActionEvent(this, new Random().nextInt(), lang);
-		
-		for (ActionListener al : actionListeners) {
-			al.actionPerformed( ae);
+
+		final Iterator<ActionListener> iterator = actionListeners.iterator();
+		while (iterator.hasNext()) {
+			ActionListener al = iterator.next();
+			al.actionPerformed(ae);
 		}
 	}
-	
+
 	@Override
 	public String put(String lang, String value) {
 		String result = super.put(lang, value);
@@ -329,10 +344,10 @@
 	public void fromOneLine(InternationalString iString) {
 		if (iString != null)
 			fromOneLine(iString.toString());
-		else 
-			fromOneLine((String)null);
+		else
+			fromOneLine((String) null);
 	}
-	
+
 	/**
 	 * Copy this {@link Translation} to another {@link Translation} e.g. for
 	 * editing
@@ -341,18 +356,17 @@
 	 */
 	@Override
 	public Translation copyTo(Translation translation2) {
-		
+
 		if (translation2 == null)
-//          throw new IllegalArgumentException(
-//              "Target translation may not be null.");
-		  return copy();
+			// throw new IllegalArgumentException(
+			// "Target translation may not be null.");
+			return copy();
 		for (String s : keySet()) {
 			translation2.put(s, get(s));
 		}
-		
+
 		return translation2;
 	}
-	
 
 	@Override
 	public Translation copy() {
@@ -360,20 +374,21 @@
 	}
 
 	/**
-	 * Checks if the {@link String}s stored in the {@link Translation} are all valid.
+	 * Checks if the {@link String}s stored in the {@link Translation} are all
+	 * valid.
+	 * 
 	 * @return <code>true</code> if all good
 	 */
 	public static boolean checkValid(Translation translationToCheck) {
-		
+
 		for (String l : translationToCheck.values()) {
-			
+
 			if (l.contains("{") || l.contains("}")) {
-		
+
 				return false;
 			}
 		}
 		return true;
 	}
-	
-	
+
 }



More information about the Schmitzm-commits mailing list