/[schmitzm]/trunk/src/skrueger/i8n/Translation.java
ViewVC logotype

Diff of /trunk/src/skrueger/i8n/Translation.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

branches/1.0-gt2-2.6/src/skrueger/i8n/Translation.java revision 420 by alfonx, Thu Oct 1 20:22:48 2009 UTC branches/2.0-RC2/src/skrueger/i8n/Translation.java revision 621 by alfonx, Thu Jan 28 10:06:05 2010 UTC
# Line 35  import java.beans.PropertyChangeEvent; Line 35  import java.beans.PropertyChangeEvent;
35  import java.beans.PropertyChangeListener;  import java.beans.PropertyChangeListener;
36  import java.util.ArrayList;  import java.util.ArrayList;
37  import java.util.HashMap;  import java.util.HashMap;
38    import java.util.Iterator;
39  import java.util.List;  import java.util.List;
40  import java.util.Locale;  import java.util.Locale;
41  import java.util.Random;  import java.util.Random;
42    import java.util.WeakHashMap;
43    
44  import javax.swing.JComponent;  import javax.swing.JComponent;
45    
46  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
47    import org.geotools.util.WeakHashSet;
48  import org.opengis.util.InternationalString;  import org.opengis.util.InternationalString;
49    
50  import skrueger.geotools.Copyable;  import skrueger.geotools.Copyable;
# Line 54  import skrueger.geotools.Copyable; Line 57  import skrueger.geotools.Copyable;
57   *         Kr&uuml;ger</a>   *         Kr&uuml;ger</a>
58   */   */
59    
60  public class Translation extends HashMap<String, String> implements Copyable<Translation>{  public class Translation extends HashMap<String, String> implements
61                    Copyable<Translation> {
62          public static final String LOCALECHANGE_PROPERTY = "localechange";          public static final String LOCALECHANGE_PROPERTY = "localechange";
63          public static final String NO_TRANSLATION = "NO TRANSLATION";          public static final String NO_TRANSLATION = "NO TRANSLATION";
64          public static final String DEFAULT_KEY = "default";          public static final String DEFAULT_KEY = "default";
65          static final Logger log = Logger.getLogger(Translation.class);          static final Logger LOGGER = Logger.getLogger(Translation.class);
66          static String activeLang = Locale.getDefault().getLanguage();          static String activeLang = Locale.getDefault().getLanguage();
67    
68          static protected List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();          static protected WeakHashSet<PropertyChangeListener> listeners = new WeakHashSet<PropertyChangeListener>(
69                            PropertyChangeListener.class);
70    
71          static {          static {
72    
# Line 73  public class Translation extends HashMap Line 78  public class Translation extends HashMap
78                  setActiveLang(locale.getLanguage());                  setActiveLang(locale.getLanguage());
79          }          }
80    
81          private List<ActionListener> actionListeners = new ArrayList<ActionListener>();          private WeakHashSet<ActionListener> actionListeners = new WeakHashSet<ActionListener>(
82                            ActionListener.class);
83    
84          @Override          @Override
85          /*          /*
# Line 82  public class Translation extends HashMap Line 88  public class Translation extends HashMap
88           */           */
89          public Translation clone() {          public Translation clone() {
90                  throw new RuntimeException("use copy()");                  throw new RuntimeException("use copy()");
91  //              return (Translation) super.clone();                  // return (Translation) super.clone();
92          }          }
93    
94          /**          /**
# Line 139  public class Translation extends HashMap Line 145  public class Translation extends HashMap
145    
146                  fireLocaleChangeEvents();                  fireLocaleChangeEvents();
147    
148                  log.info("skrueger.i8n.Translation switched ActiveLang to " + newLang);                  LOGGER.info("skrueger.i8n.Translation switched ActiveLang to "
149                                    + newLang);
150          }          }
151    
152          /**          /**
# Line 171  public class Translation extends HashMap Line 178  public class Translation extends HashMap
178                          put(DEFAULT_KEY, defaultTranslation);                          put(DEFAULT_KEY, defaultTranslation);
179                  } else                  } else
180                          for (String code : languages) {                          for (String code : languages) {
181                                  if (code.equals(getActiveLang())) {  //                              if (code.equals(getActiveLang())) {
182                                          put(code, defaultTranslation);                                          put(code, defaultTranslation);
183                                  }  //                              }
184                          }                          }
185          }          }
186    
# Line 197  public class Translation extends HashMap Line 204  public class Translation extends HashMap
204          public void fromOneLine(final String oneLineCoded) {          public void fromOneLine(final String oneLineCoded) {
205    
206                  clear();                  clear();
207                    
208                  try {                  try {
                           
                 if ((oneLineCoded == null) || (oneLineCoded.equals(""))) {  
                         put(DEFAULT_KEY, "");  
                         return;  
                 }  
209    
210                  if (oneLineCoded.indexOf("}") == -1) {                          if ((oneLineCoded == null) || (oneLineCoded.equals(""))) {
211                          // log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);");                                  put(DEFAULT_KEY, "");
212                          put(DEFAULT_KEY, oneLineCoded);                                  return;
213                  }                          }
214    
215                  String eatUp = oneLineCoded;                          if (oneLineCoded.indexOf("}") == -1) {
216                  while (eatUp.indexOf("}") != -1) {                                  // log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);");
217                          String substring = eatUp.substring(0, eatUp.indexOf("}"));                                  put(DEFAULT_KEY, oneLineCoded);
218                            }
219                          // log.debug("substring = "+substring);  
220                          String key = substring.substring(0, substring.indexOf("{"));                          String eatUp = oneLineCoded;
221                          String value = substring.substring(substring.indexOf("{") + 1,                          while (eatUp.indexOf("}") != -1) {
222                                          substring.length());                                  String substring = eatUp.substring(0, eatUp.indexOf("}"));
223                          // log.debug("key="+key);  
224                          // log.debug("value="+value);                                  // log.debug("substring = "+substring);
225                          put(key, value);                                  String key = substring.substring(0, substring.indexOf("{"));
226                          eatUp = eatUp.substring(eatUp.indexOf("}") + 1);                                  String value = substring.substring(substring.indexOf("{") + 1,
227                  }                                                  substring.length());
228                                    // log.debug("key="+key);
229                                    // log.debug("value="+value);
230                                    put(key, value);
231                                    eatUp = eatUp.substring(eatUp.indexOf("}") + 1);
232                            }
233                  } catch (Exception e) {                  } catch (Exception e) {
234                          log.warn("Error while reading the oneLineCode '"+oneLineCoded+"'", e);                          LOGGER.warn("Error while reading the oneLineCode '" + oneLineCoded
235                          log.warn("Translation will be empty!");                                          + "'", e);
236                            LOGGER.warn("Translation will be empty!");
237                  }                  }
238          }          }
239    
# Line 273  public class Translation extends HashMap Line 281  public class Translation extends HashMap
281                                                  return s;                                                  return s;
282                                  }                                  }
283                  }                  }
284  //              log.warn("No translation found!");                  // log.warn("No translation found!");
285                  return NO_TRANSLATION;                  return NO_TRANSLATION;
286          }          }
287    
   
288          /**          /**
289           * {@link PropertyChangeListener} can be registered to be informed when the           * {@link PropertyChangeListener} can be registered to be informed when the
290           * {@link Locale} changed.           * {@link Locale} changed.<br>
291             * The listeners are kept in a {@link WeakHashMap}, so you have to keep a
292             * reference to the listener or it will be removed!
293           *           *
294           * @param propertyChangeListener           * @param propertyChangeListener
295             *            A {@link PropertyChangeListener} that will be called when
296             *            {@link #setActiveLang(String)} changes the language.
297           */           */
298          public static void addLocaleChangeListener(          public static void addLocaleChangeListener(
299                          PropertyChangeListener propertyChangeListener) {                          PropertyChangeListener propertyChangeListener) {
# Line 290  public class Translation extends HashMap Line 301  public class Translation extends HashMap
301          }          }
302    
303          /**          /**
304             * {@link PropertyChangeListener} can be registered to be informed when the
305             * {@link Locale} changed.<br>
306             * The listeners are kept in a {@link WeakHashMap}, so you have to keep a
307             * reference to the listener or it will be removed!
308             *
309             * @param propertyChangeListener
310             *            A {@link PropertyChangeListener} that will be called when
311             *            {@link #setActiveLang(String)} changes the language.
312             */
313            public static boolean removeLocaleChangeListener(
314                            PropertyChangeListener propertyChangeListener) {
315                    return listeners.remove(propertyChangeListener);
316            }
317    
318            /**
319           * Informs all registered {@link PropertyChangeListener}s about a change of           * Informs all registered {@link PropertyChangeListener}s about a change of
320           * the the {@link Locale}.           * the the {@link Locale}.
321           */           */
# Line 303  public class Translation extends HashMap Line 329  public class Translation extends HashMap
329                  }                  }
330          }          }
331    
332            /**
333             * The listeneras are stored in a {@link WeakHashSet}! So you HAVE TO KEEP a
334             * reference as long as you need the listener.
335             */
336          public void addTranslationChangeListener(ActionListener actionListener) {          public void addTranslationChangeListener(ActionListener actionListener) {
337                  actionListeners.add(actionListener);                  if (actionListeners.add(actionListener)) {
338                            LOGGER
339                                            .debug("registering a new translationChangeActionListener in the WeakHashSet");
340                    }
341          }          }
342            
343            /**
344             * The listeneras are stored in a {@link WeakHashSet}! You don't have to
345             * remove the listener, as long as you throw away the reference to the
346             * listener.
347             */
348          public boolean removeTranslationChangeListener(ActionListener actionListener) {          public boolean removeTranslationChangeListener(ActionListener actionListener) {
349                  return actionListeners.remove(actionListener);                  return actionListeners.remove(actionListener);
350          }          }
351    
352          public void fireTranslationChangedEvents(String lang) {          public void fireTranslationChangedEvents(String lang) {
353                  ActionEvent ae = new ActionEvent(this, new Random().nextInt(), lang);                  ActionEvent ae = new ActionEvent(this, new Random().nextInt(), lang);
354                    
355                  for (ActionListener al : actionListeners) {                  final Iterator<ActionListener> iterator = actionListeners.iterator();
356                          al.actionPerformed( ae);                  while (iterator.hasNext()) {
357                            ActionListener al = iterator.next();
358                            al.actionPerformed(ae);
359                  }                  }
360          }          }
361            
362          @Override          @Override
363          public String put(String lang, String value) {          public String put(String lang, String value) {
364                  String result = super.put(lang, value);                  String result = super.put(lang, value);
# Line 329  public class Translation extends HashMap Line 369  public class Translation extends HashMap
369          public void fromOneLine(InternationalString iString) {          public void fromOneLine(InternationalString iString) {
370                  if (iString != null)                  if (iString != null)
371                          fromOneLine(iString.toString());                          fromOneLine(iString.toString());
372                  else                  else
373                          fromOneLine((String)null);                          fromOneLine((String) null);
374          }          }
375            
376          /**          /**
377           * Copy this {@link Translation} to another {@link Translation} e.g. for           * Copy this {@link Translation} to another {@link Translation} e.g. for
378           * editing           * editing
# Line 341  public class Translation extends HashMap Line 381  public class Translation extends HashMap
381           */           */
382          @Override          @Override
383          public Translation copyTo(Translation translation2) {          public Translation copyTo(Translation translation2) {
384                    
385                  if (translation2 == null)                  if (translation2 == null)
386                          throw new IllegalArgumentException(                          // throw new IllegalArgumentException(
387                                          "Target translation may not be null.");                          // "Target translation may not be null.");
388                            return copy();
389                  for (String s : keySet()) {                  for (String s : keySet()) {
390                          translation2.put(s, get(s));                          translation2.put(s, get(s));
391                  }                  }
392                    
393                  return translation2;                  return translation2;
394          }          }
           
395    
396          @Override          @Override
397          public Translation copy() {          public Translation copy() {
# Line 359  public class Translation extends HashMap Line 399  public class Translation extends HashMap
399          }          }
400    
401          /**          /**
402           * 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
403             * valid.
404             *
405           * @return <code>true</code> if all good           * @return <code>true</code> if all good
406           */           */
407          public static boolean checkValid(Translation translationToCheck) {          public static boolean checkValid(Translation translationToCheck) {
408                    
409                  for (String l : translationToCheck.values()) {                  for (String l : translationToCheck.values()) {
410                            
411                          if (l.contains("{") || l.contains("}")) {                          if (l.contains("{") || l.contains("}")) {
412                    
413                                  return false;                                  return false;
414                          }                          }
415                  }                  }
416                  return true;                  return true;
417          }          }
418            
           
419  }  }

Legend:
Removed from v.420  
changed lines
  Added in v.621

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26