/[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 trunk/src/skrueger/i8n/Translation.java revision 772 by alfonx, Sun Mar 21 14:05:26 2010 UTC
# Line 33  import java.awt.event.ActionEvent; Line 33  import java.awt.event.ActionEvent;
33  import java.awt.event.ActionListener;  import java.awt.event.ActionListener;
34  import java.beans.PropertyChangeEvent;  import java.beans.PropertyChangeEvent;
35  import java.beans.PropertyChangeListener;  import java.beans.PropertyChangeListener;
36    import java.io.Serializable;
37  import java.util.ArrayList;  import java.util.ArrayList;
38  import java.util.HashMap;  import java.util.HashMap;
39    import java.util.Iterator;
40  import java.util.List;  import java.util.List;
41  import java.util.Locale;  import java.util.Locale;
42  import java.util.Random;  import java.util.Random;
43    import java.util.WeakHashMap;
44    
45  import javax.swing.JComponent;  import javax.swing.JComponent;
46    
47  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
48    import org.geotools.util.WeakHashSet;
49  import org.opengis.util.InternationalString;  import org.opengis.util.InternationalString;
50    
51  import skrueger.geotools.Copyable;  import skrueger.geotools.Copyable;
# Line 54  import skrueger.geotools.Copyable; Line 58  import skrueger.geotools.Copyable;
58   *         Kr&uuml;ger</a>   *         Kr&uuml;ger</a>
59   */   */
60    
61  public class Translation extends HashMap<String, String> implements Copyable<Translation>{  public class Translation extends HashMap<String, String> implements
62                    Copyable<Translation>, Serializable {
63            
64            private static final long serialVersionUID = -347702744122305245L;
65            
66          public static final String LOCALECHANGE_PROPERTY = "localechange";          public static final String LOCALECHANGE_PROPERTY = "localechange";
67          public static final String NO_TRANSLATION = "NO TRANSLATION";          public static final String NO_TRANSLATION = "NO TRANSLATION";
68          public static final String DEFAULT_KEY = "default";          public static final String DEFAULT_KEY = "default";
69          static final Logger log = Logger.getLogger(Translation.class);          static final Logger LOGGER = Logger.getLogger(Translation.class);
70          static String activeLang = Locale.getDefault().getLanguage();          static String activeLang = Locale.getDefault().getLanguage();
71    
72          static protected List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();          static protected WeakHashSet<PropertyChangeListener> listeners = new WeakHashSet<PropertyChangeListener>(
73                            PropertyChangeListener.class);
74    
75          static {          static {
76    
# Line 72  public class Translation extends HashMap Line 81  public class Translation extends HashMap
81                  Locale locale = Locale.getDefault();                  Locale locale = Locale.getDefault();
82                  setActiveLang(locale.getLanguage());                  setActiveLang(locale.getLanguage());
83          }          }
84            
85            
86    
87          private List<ActionListener> actionListeners = new ArrayList<ActionListener>();          private WeakHashSet<ActionListener> actionListeners = new WeakHashSet<ActionListener>(
88                            ActionListener.class);
89    
90          @Override          @Override
91          /*          /*
# Line 82  public class Translation extends HashMap Line 94  public class Translation extends HashMap
94           */           */
95          public Translation clone() {          public Translation clone() {
96                  throw new RuntimeException("use copy()");                  throw new RuntimeException("use copy()");
97  //              return (Translation) super.clone();                  // return (Translation) super.clone();
98          }          }
99    
100          /**          /**
# Line 139  public class Translation extends HashMap Line 151  public class Translation extends HashMap
151    
152                  fireLocaleChangeEvents();                  fireLocaleChangeEvents();
153    
154                  log.info("skrueger.i8n.Translation switched ActiveLang to " + newLang);                  LOGGER.info("skrueger.i8n.Translation switched ActiveLang to "
155                                    + newLang);
156          }          }
157    
158          /**          /**
# Line 171  public class Translation extends HashMap Line 184  public class Translation extends HashMap
184                          put(DEFAULT_KEY, defaultTranslation);                          put(DEFAULT_KEY, defaultTranslation);
185                  } else                  } else
186                          for (String code : languages) {                          for (String code : languages) {
187                                  if (code.equals(getActiveLang())) {  //                              if (code.equals(getActiveLang())) {
188                                          put(code, defaultTranslation);                                          put(code, defaultTranslation);
189                                  }  //                              }
190                          }                          }
191          }          }
192    
# Line 197  public class Translation extends HashMap Line 210  public class Translation extends HashMap
210          public void fromOneLine(final String oneLineCoded) {          public void fromOneLine(final String oneLineCoded) {
211    
212                  clear();                  clear();
213                    
214                  try {                  try {
                           
                 if ((oneLineCoded == null) || (oneLineCoded.equals(""))) {  
                         put(DEFAULT_KEY, "");  
                         return;  
                 }  
215    
216                  if (oneLineCoded.indexOf("}") == -1) {                          if ((oneLineCoded == null) || (oneLineCoded.equals(""))) {
217                          // log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);");                                  put(DEFAULT_KEY, "");
218                          put(DEFAULT_KEY, oneLineCoded);                                  return;
219                  }                          }
220    
221                  String eatUp = oneLineCoded;                          if (oneLineCoded.indexOf("}") == -1) {
222                  while (eatUp.indexOf("}") != -1) {                                  // log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);");
223                          String substring = eatUp.substring(0, eatUp.indexOf("}"));                                  put(DEFAULT_KEY, oneLineCoded);
224                            }
225                          // log.debug("substring = "+substring);  
226                          String key = substring.substring(0, substring.indexOf("{"));                          String eatUp = oneLineCoded;
227                          String value = substring.substring(substring.indexOf("{") + 1,                          while (eatUp.indexOf("}") != -1) {
228                                          substring.length());                                  String substring = eatUp.substring(0, eatUp.indexOf("}"));
229                          // log.debug("key="+key);  
230                          // log.debug("value="+value);                                  // log.debug("substring = "+substring);
231                          put(key, value);                                  String key = substring.substring(0, substring.indexOf("{"));
232                          eatUp = eatUp.substring(eatUp.indexOf("}") + 1);                                  String value = substring.substring(substring.indexOf("{") + 1,
233                  }                                                  substring.length());
234                                    // log.debug("key="+key);
235                                    // log.debug("value="+value);
236                                    put(key, value);
237                                    eatUp = eatUp.substring(eatUp.indexOf("}") + 1);
238                            }
239                  } catch (Exception e) {                  } catch (Exception e) {
240                          log.warn("Error while reading the oneLineCode '"+oneLineCoded+"'", e);                          LOGGER.warn("Error while reading the oneLineCode '" + oneLineCoded
241                          log.warn("Translation will be empty!");                                          + "'", e);
242                            LOGGER.warn("Translation will be empty!");
243                  }                  }
244          }          }
245    
# Line 273  public class Translation extends HashMap Line 287  public class Translation extends HashMap
287                                                  return s;                                                  return s;
288                                  }                                  }
289                  }                  }
290  //              log.warn("No translation found!");                  // log.warn("No translation found!");
291                  return NO_TRANSLATION;                  return NO_TRANSLATION;
292          }          }
293    
   
294          /**          /**
295           * {@link PropertyChangeListener} can be registered to be informed when the           * {@link PropertyChangeListener} can be registered to be informed when the
296           * {@link Locale} changed.           * {@link Locale} changed.<br>
297             * The listeners are kept in a {@link WeakHashMap}, so you have to keep a
298             * reference to the listener or it will be removed!
299           *           *
300           * @param propertyChangeListener           * @param propertyChangeListener
301             *            A {@link PropertyChangeListener} that will be called when
302             *            {@link #setActiveLang(String)} changes the language.
303           */           */
304          public static void addLocaleChangeListener(          public static void addLocaleChangeListener(
305                          PropertyChangeListener propertyChangeListener) {                          PropertyChangeListener propertyChangeListener) {
# Line 290  public class Translation extends HashMap Line 307  public class Translation extends HashMap
307          }          }
308    
309          /**          /**
310             * {@link PropertyChangeListener} can be registered to be informed when the
311             * {@link Locale} changed.<br>
312             * The listeners are kept in a {@link WeakHashMap}, so you have to keep a
313             * reference to the listener or it will be removed!
314             *
315             * @param propertyChangeListener
316             *            A {@link PropertyChangeListener} that will be called when
317             *            {@link #setActiveLang(String)} changes the language.
318             */
319            public static boolean removeLocaleChangeListener(
320                            PropertyChangeListener propertyChangeListener) {
321                    return listeners.remove(propertyChangeListener);
322            }
323    
324            /**
325           * Informs all registered {@link PropertyChangeListener}s about a change of           * Informs all registered {@link PropertyChangeListener}s about a change of
326           * the the {@link Locale}.           * the the {@link Locale}.
327           */           */
# Line 303  public class Translation extends HashMap Line 335  public class Translation extends HashMap
335                  }                  }
336          }          }
337    
338            /**
339             * The listeneras are stored in a {@link WeakHashSet}! So you HAVE TO KEEP a
340             * reference as long as you need the listener.
341             */
342          public void addTranslationChangeListener(ActionListener actionListener) {          public void addTranslationChangeListener(ActionListener actionListener) {
343                  actionListeners.add(actionListener);                  if (actionListeners.add(actionListener)) {
344    //                      LOGGER
345    //                                      .debug("registering a new translationChangeActionListener in the WeakHashSet");
346                    }
347          }          }
348            
349            /**
350             * The listeneras are stored in a {@link WeakHashSet}! You don't have to
351             * remove the listener, as long as you throw away the reference to the
352             * listener.
353             */
354          public boolean removeTranslationChangeListener(ActionListener actionListener) {          public boolean removeTranslationChangeListener(ActionListener actionListener) {
355                  return actionListeners.remove(actionListener);                  return actionListeners.remove(actionListener);
356          }          }
357    
358          public void fireTranslationChangedEvents(String lang) {          public void fireTranslationChangedEvents(String lang) {
359                  ActionEvent ae = new ActionEvent(this, new Random().nextInt(), lang);                  ActionEvent ae = new ActionEvent(this, new Random().nextInt(), lang);
360                    
361                  for (ActionListener al : actionListeners) {                  final Iterator<ActionListener> iterator = actionListeners.iterator();
362                          al.actionPerformed( ae);                  while (iterator.hasNext()) {
363                            ActionListener al = iterator.next();
364                            al.actionPerformed(ae);
365                  }                  }
366          }          }
367            
368          @Override          @Override
369          public String put(String lang, String value) {          public String put(String lang, String value) {
370                  String result = super.put(lang, value);                  String result = super.put(lang, value);
# Line 329  public class Translation extends HashMap Line 375  public class Translation extends HashMap
375          public void fromOneLine(InternationalString iString) {          public void fromOneLine(InternationalString iString) {
376                  if (iString != null)                  if (iString != null)
377                          fromOneLine(iString.toString());                          fromOneLine(iString.toString());
378                  else                  else
379                          fromOneLine((String)null);                          fromOneLine((String) null);
380          }          }
381            
382          /**          /**
383           * Copy this {@link Translation} to another {@link Translation} e.g. for           * Copy this {@link Translation} to another {@link Translation} e.g. for
384           * editing           * editing
# Line 341  public class Translation extends HashMap Line 387  public class Translation extends HashMap
387           */           */
388          @Override          @Override
389          public Translation copyTo(Translation translation2) {          public Translation copyTo(Translation translation2) {
390                    
391                  if (translation2 == null)                  if (translation2 == null)
392                          throw new IllegalArgumentException(                          // throw new IllegalArgumentException(
393                                          "Target translation may not be null.");                          // "Target translation may not be null.");
394                            return copy();
395                  for (String s : keySet()) {                  for (String s : keySet()) {
396                          translation2.put(s, get(s));                          translation2.put(s, get(s));
397                  }                  }
398                    
399                  return translation2;                  return translation2;
400          }          }
           
401    
402          @Override          @Override
403          public Translation copy() {          public Translation copy() {
# Line 359  public class Translation extends HashMap Line 405  public class Translation extends HashMap
405          }          }
406    
407          /**          /**
408           * 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
409             * valid.
410             *
411           * @return <code>true</code> if all good           * @return <code>true</code> if all good
412           */           */
413          public static boolean checkValid(Translation translationToCheck) {          public static boolean checkValid(Translation translationToCheck) {
414                    
415                  for (String l : translationToCheck.values()) {                  for (String l : translationToCheck.values()) {
416                            
417                          if (l.contains("{") || l.contains("}")) {                          if (l.contains("{") || l.contains("}")) {
418                    
419                                  return false;                                  return false;
420                          }                          }
421                  }                  }
422                  return true;                  return true;
423          }          }
424            
           
425  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26