/[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

trunk/src/skrueger/i8n/Translation.java revision 256 by alfonx, Fri Jul 31 14:43:47 2009 UTC branches/1.0-gt2-2.6/src/skrueger/i8n/Translation.java revision 453 by mojays, Fri Oct 9 21:50:48 2009 UTC
# 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.List;  import java.util.List;
39  import java.util.Locale;  import java.util.Locale;
40    import java.util.Random;
41    
42  import javax.swing.JComponent;  import javax.swing.JComponent;
 import javax.swing.JTable;  
43    
44  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
45    import org.opengis.util.InternationalString;
46    
47    import skrueger.geotools.Copyable;
48    
49  /**  /**
50   * 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 54  import org.apache.log4j.Logger;
54   *         Kr&uuml;ger</a>   *         Kr&uuml;ger</a>
55   */   */
56    
57  public class Translation extends HashMap<String, String> {  public class Translation extends HashMap<String, String> implements Copyable<Translation>{
58          public static final String LOCALECHANGE_PROPERTY = "localechange";          public static final String LOCALECHANGE_PROPERTY = "localechange";
59          public static final String NO_TRANSLATION = "NO TRANSLATION";          public static final String NO_TRANSLATION = "NO TRANSLATION";
60          public static final String DEFAULT_KEY = "default";          public static final String DEFAULT_KEY = "default";
61          static final Logger log = Logger.getLogger(Translation.class);          static final Logger log = Logger.getLogger(Translation.class);
62          static String activeLang = "fr";          static String activeLang = Locale.getDefault().getLanguage();
63    
64          static protected List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();          static protected List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();
65    
# Line 68  public class Translation extends HashMap Line 73  public class Translation extends HashMap
73                  setActiveLang(locale.getLanguage());                  setActiveLang(locale.getLanguage());
74          }          }
75    
76            private List<ActionListener> actionListeners = new ArrayList<ActionListener>();
77    
78          @Override          @Override
79          /*          /*
80           * @comment To make a copy of a translation see methods toOneLine() and           * @comment To make a copy of a translation see methods toOneLine() and
81           * fromOneLine()           * fromOneLine()
82           */           */
83          public Translation clone() {          public Translation clone() {
84                  return (Translation) super.clone();                  throw new RuntimeException("use copy()");
85    //              return (Translation) super.clone();
86          }          }
87    
88          /**          /**
# Line 187  public class Translation extends HashMap Line 195  public class Translation extends HashMap
195           * @author Stefan Alfons Krüger           * @author Stefan Alfons Krüger
196           */           */
197          public void fromOneLine(final String oneLineCoded) {          public void fromOneLine(final String oneLineCoded) {
198    
199                  clear();                  clear();
200                    
201                    try {
202                            
203                  if ((oneLineCoded == null) || (oneLineCoded.equals(""))) {                  if ((oneLineCoded == null) || (oneLineCoded.equals(""))) {
204                          put(DEFAULT_KEY, "");                          put(DEFAULT_KEY, "");
205                          return;                          return;
# Line 211  public class Translation extends HashMap Line 223  public class Translation extends HashMap
223                          put(key, value);                          put(key, value);
224                          eatUp = eatUp.substring(eatUp.indexOf("}") + 1);                          eatUp = eatUp.substring(eatUp.indexOf("}") + 1);
225                  }                  }
226                    } catch (Exception e) {
227                            log.warn("Error while reading the oneLineCode '"+oneLineCoded+"'", e);
228                            log.warn("Translation will be empty!");
229                    }
230          }          }
231    
232          /**          /**
# Line 247  public class Translation extends HashMap Line 263  public class Translation extends HashMap
263                  // MS:                  // MS:
264                  else {                  else {
265                          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.");  
266                                  return get(DEFAULT_KEY);                                  return get(DEFAULT_KEY);
267                          }                          }
268    
# Line 262  public class Translation extends HashMap Line 277  public class Translation extends HashMap
277                  return NO_TRANSLATION;                  return NO_TRANSLATION;
278          }          }
279    
         /**  
          * 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;  
         }  
280    
281          /**          /**
282           * {@link PropertyChangeListener} can be registered to be informed when the           * {@link PropertyChangeListener} can be registered to be informed when the
# Line 303  public class Translation extends HashMap Line 303  public class Translation extends HashMap
303                  }                  }
304          }          }
305    
306            public void addTranslationChangeListener(ActionListener actionListener) {
307                    actionListeners.add(actionListener);
308            }
309            
310            public boolean removeTranslationChangeListener(ActionListener actionListener) {
311                    return actionListeners.remove(actionListener);
312            }
313    
314            public void fireTranslationChangedEvents(String lang) {
315                    ActionEvent ae = new ActionEvent(this, new Random().nextInt(), lang);
316                    
317                    for (ActionListener al : actionListeners) {
318                            al.actionPerformed( ae);
319                    }
320            }
321            
322            @Override
323            public String put(String lang, String value) {
324                    String result = super.put(lang, value);
325                    fireTranslationChangedEvents(lang);
326                    return result;
327            }
328    
329            public void fromOneLine(InternationalString iString) {
330                    if (iString != null)
331                            fromOneLine(iString.toString());
332                    else
333                            fromOneLine((String)null);
334            }
335            
336            /**
337             * Copy this {@link Translation} to another {@link Translation} e.g. for
338             * editing
339             *
340             * @return the destination {@link Translation}
341             */
342            @Override
343            public Translation copyTo(Translation translation2) {
344                    
345                    if (translation2 == null)
346    //          throw new IllegalArgumentException(
347    //              "Target translation may not be null.");
348                      return copy();
349                    for (String s : keySet()) {
350                            translation2.put(s, get(s));
351                    }
352                    
353                    return translation2;
354            }
355            
356    
357            @Override
358            public Translation copy() {
359                    return copyTo(new Translation());
360            }
361    
362            /**
363             * Checks if the {@link String}s stored in the {@link Translation} are all valid.
364             * @return <code>true</code> if all good
365             */
366            public static boolean checkValid(Translation translationToCheck) {
367                    
368                    for (String l : translationToCheck.values()) {
369                            
370                            if (l.contains("{") || l.contains("}")) {
371                    
372                                    return false;
373                            }
374                    }
375                    return true;
376            }
377            
378            
379  }  }

Legend:
Removed from v.256  
changed lines
  Added in v.453

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26