/[schmitzm]/branches/1.0-gt2-2.6/src/skrueger/i8n/Translation.java
ViewVC logotype

Diff of /branches/1.0-gt2-2.6/src/skrueger/i8n/Translation.java

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

trunk/src/skrueger/i8n/Translation.java revision 244 by alfonx, Wed Jul 29 09:33:33 2009 UTC branches/1.0-gt2-2.6/src/skrueger/i8n/Translation.java revision 572 by alfonx, Wed Nov 25 07:03:58 2009 UTC
# Line 2  Line 2 
2   * Copyright (c) 2009 Martin O. J. Schmitz.   * Copyright (c) 2009 Martin O. J. Schmitz.
3   *   *
4   * This file is part of the SCHMITZM library - a collection of utility   * This file is part of the SCHMITZM library - a collection of utility
5   * classes based on Java 1.6, focussing (not only) on Java Swing   * classes based on Java 1.6, focusing (not only) on Java Swing
6   * and the Geotools library.   * and the Geotools library.
7   *   *
8   * The SCHMITZM project is hosted at:   * The SCHMITZM project is hosted at:
# Line 29  Line 29 
29   ******************************************************************************/   ******************************************************************************/
30  package skrueger.i8n;  package skrueger.i8n;
31    
32    import java.awt.event.ActionEvent;
33    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.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;
42    import java.util.WeakHashMap;
43    
44  import javax.swing.JComponent;  import javax.swing.JComponent;
 import javax.swing.JTable;  
45    
46  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
47    import org.geotools.util.WeakHashSet;
48    import org.opengis.util.InternationalString;
49    
50    import skrueger.geotools.Copyable;
51    
52  /**  /**
53   * Represents a {@link HashMap} of translations. toString() returns the   * Represents a {@link HashMap} of translations. toString() returns the
# Line 49  import org.apache.log4j.Logger; Line 57  import org.apache.log4j.Logger;
57   *         Kr&uuml;ger</a>   *         Kr&uuml;ger</a>
58   */   */
59    
60  public class Translation extends HashMap<String, String> {  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 = "fr";          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 68  public class Translation extends HashMap Line 78  public class Translation extends HashMap
78                  setActiveLang(locale.getLanguage());                  setActiveLang(locale.getLanguage());
79          }          }
80    
81            private WeakHashSet<ActionListener> actionListeners = new WeakHashSet<ActionListener>(
82                            ActionListener.class);
83    
84          @Override          @Override
85          /*          /*
86           * @comment To make a copy of a translation see methods toOneLine() and           * @comment To make a copy of a translation see methods toOneLine() and
87           * fromOneLine()           * fromOneLine()
88           */           */
89          public Translation clone() {          public Translation clone() {
90                  return (Translation) super.clone();                  throw new RuntimeException("use copy()");
91                    // return (Translation) super.clone();
92          }          }
93    
94          /**          /**
# Line 131  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 187  public class Translation extends HashMap Line 202  public class Translation extends HashMap
202           * @author Stefan Alfons Krüger           * @author Stefan Alfons Krüger
203           */           */
204          public void fromOneLine(final String oneLineCoded) {          public void fromOneLine(final String oneLineCoded) {
205    
206                  clear();                  clear();
                 if ((oneLineCoded == null) || (oneLineCoded.equals(""))) {  
                         put(DEFAULT_KEY, "");  
                         return;  
                 }  
207    
208                  if (oneLineCoded.indexOf("}") == -1) {                  try {
209                          // log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);");  
210                          put(DEFAULT_KEY, oneLineCoded);                          if ((oneLineCoded == null) || (oneLineCoded.equals(""))) {
211                  }                                  put(DEFAULT_KEY, "");
212                                    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) {
234                            LOGGER.warn("Error while reading the oneLineCode '" + oneLineCoded
235                                            + "'", e);
236                            LOGGER.warn("Translation will be empty!");
237                  }                  }
238          }          }
239    
# Line 247  public class Translation extends HashMap Line 271  public class Translation extends HashMap
271                  // MS:                  // MS:
272                  else {                  else {
273                          if (get(DEFAULT_KEY) != null) {                          if (get(DEFAULT_KEY) != null) {
                                 // log.debug("default lang returned, cuz the translation to "+activeLang+" was not found. Schmeiss raus martin, wenn du das mit der default trans geklärt hast.");  
274                                  return get(DEFAULT_KEY);                                  return get(DEFAULT_KEY);
275                          }                          }
276    
# Line 258  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          /**          /**
          * Copy this {@link Translation} to another {@link Translation} e.g. for  
          * editing  
          *  
          * @return the destination {@link Translation}  
          */  
         public Translation copy(Translation backup) {  
                 if (backup == null)  
                         throw new IllegalArgumentException(  
                                         "Target translation may not be null.");  
                 for (String s : keySet()) {  
                         backup.put(s, get(s));  
                 }  
                 return backup;  
         }  
   
         /**  
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 303  public class Translation extends HashMap Line 314  public class Translation extends HashMap
314                  }                  }
315          }          }
316    
317            /**
318             * The listeneras are stored in a {@link WeakHashSet}! So you HAVE TO KEEP a
319             * reference as long as you need the listener.
320             */
321            public void addTranslationChangeListener(ActionListener actionListener) {
322                    if (actionListeners.add(actionListener)) {
323                            LOGGER
324                                            .debug("registering a new translationChangeActionListener in the WeakHashSet");
325                    }
326            }
327    
328            /**
329             * The listeneras are stored in a {@link WeakHashSet}! You don't have to
330             * remove the listener, as long as you throw away the reference to the
331             * listener.
332             */
333            public boolean removeTranslationChangeListener(ActionListener actionListener) {
334                    return actionListeners.remove(actionListener);
335            }
336    
337            public void fireTranslationChangedEvents(String lang) {
338                    ActionEvent ae = new ActionEvent(this, new Random().nextInt(), lang);
339    
340                    final Iterator<ActionListener> iterator = actionListeners.iterator();
341                    while (iterator.hasNext()) {
342                            ActionListener al = iterator.next();
343                            al.actionPerformed(ae);
344                    }
345            }
346    
347            @Override
348            public String put(String lang, String value) {
349                    String result = super.put(lang, value);
350                    fireTranslationChangedEvents(lang);
351                    return result;
352            }
353    
354            public void fromOneLine(InternationalString iString) {
355                    if (iString != null)
356                            fromOneLine(iString.toString());
357                    else
358                            fromOneLine((String) null);
359            }
360    
361            /**
362             * Copy this {@link Translation} to another {@link Translation} e.g. for
363             * editing
364             *
365             * @return the destination {@link Translation}
366             */
367            @Override
368            public Translation copyTo(Translation translation2) {
369    
370                    if (translation2 == null)
371                            // throw new IllegalArgumentException(
372                            // "Target translation may not be null.");
373                            return copy();
374                    for (String s : keySet()) {
375                            translation2.put(s, get(s));
376                    }
377    
378                    return translation2;
379            }
380    
381            @Override
382            public Translation copy() {
383                    return copyTo(new Translation());
384            }
385    
386            /**
387             * Checks if the {@link String}s stored in the {@link Translation} are all
388             * valid.
389             *
390             * @return <code>true</code> if all good
391             */
392            public static boolean checkValid(Translation translationToCheck) {
393    
394                    for (String l : translationToCheck.values()) {
395    
396                            if (l.contains("{") || l.contains("}")) {
397    
398                                    return false;
399                            }
400                    }
401                    return true;
402            }
403    
404  }  }

Legend:
Removed from v.244  
changed lines
  Added in v.572

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26