/[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 292 by alfonx, Wed Aug 5 12:34:15 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 42  import java.util.Random; Line 42  import java.util.Random;
42  import javax.swing.JComponent;  import javax.swing.JComponent;
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 51  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";
# Line 78  public class Translation extends HashMap Line 81  public class Translation extends HashMap
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 191  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 215  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 251  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 266  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 315  public class Translation extends HashMap Line 311  public class Translation extends HashMap
311                  return actionListeners.remove(actionListener);                  return actionListeners.remove(actionListener);
312          }          }
313    
314          public void fireTranslationChangedEvents() {          public void fireTranslationChangedEvents(String lang) {
315                  ActionEvent ae = new ActionEvent(this, new Random().nextInt(),  "");                  ActionEvent ae = new ActionEvent(this, new Random().nextInt(), lang);
316                                    
317                  for (ActionListener al : actionListeners) {                  for (ActionListener al : actionListeners) {
318                          al.actionPerformed( ae);                          al.actionPerformed( ae);
# Line 324  public class Translation extends HashMap Line 320  public class Translation extends HashMap
320          }          }
321                    
322          @Override          @Override
323          public String put(String key, String value) {          public String put(String lang, String value) {
324                  String result = super.put(key, value);                  String result = super.put(lang, value);
325                  fireTranslationChangedEvents();                  fireTranslationChangedEvents(lang);
326                  return result;                  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.292  
changed lines
  Added in v.453

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26