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

revision 453 by mojays, Fri Oct 9 21:50:48 2009 UTC revision 482 by alfonx, Tue Oct 20 13:51:11 2009 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;
# Line 42  import java.util.Random; Line 43  import java.util.Random;
43  import javax.swing.JComponent;  import javax.swing.JComponent;
44    
45  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
46    import org.geotools.util.WeakHashSet;
47  import org.opengis.util.InternationalString;  import org.opengis.util.InternationalString;
48    
49  import skrueger.geotools.Copyable;  import skrueger.geotools.Copyable;
# Line 54  import skrueger.geotools.Copyable; Line 56  import skrueger.geotools.Copyable;
56   *         Kr&uuml;ger</a>   *         Kr&uuml;ger</a>
57   */   */
58    
59  public class Translation extends HashMap<String, String> implements Copyable<Translation>{  public class Translation extends HashMap<String, String> implements
60                    Copyable<Translation> {
61          public static final String LOCALECHANGE_PROPERTY = "localechange";          public static final String LOCALECHANGE_PROPERTY = "localechange";
62          public static final String NO_TRANSLATION = "NO TRANSLATION";          public static final String NO_TRANSLATION = "NO TRANSLATION";
63          public static final String DEFAULT_KEY = "default";          public static final String DEFAULT_KEY = "default";
# Line 73  public class Translation extends HashMap Line 76  public class Translation extends HashMap
76                  setActiveLang(locale.getLanguage());                  setActiveLang(locale.getLanguage());
77          }          }
78    
79          private List<ActionListener> actionListeners = new ArrayList<ActionListener>();          private WeakHashSet<ActionListener> actionListeners = new WeakHashSet<ActionListener>(
80                            ActionListener.class);
81    
82          @Override          @Override
83          /*          /*
# Line 82  public class Translation extends HashMap Line 86  public class Translation extends HashMap
86           */           */
87          public Translation clone() {          public Translation clone() {
88                  throw new RuntimeException("use copy()");                  throw new RuntimeException("use copy()");
89  //              return (Translation) super.clone();                  // return (Translation) super.clone();
90          }          }
91    
92          /**          /**
# Line 197  public class Translation extends HashMap Line 201  public class Translation extends HashMap
201          public void fromOneLine(final String oneLineCoded) {          public void fromOneLine(final String oneLineCoded) {
202    
203                  clear();                  clear();
204                    
205                  try {                  try {
                           
                 if ((oneLineCoded == null) || (oneLineCoded.equals(""))) {  
                         put(DEFAULT_KEY, "");  
                         return;  
                 }  
206    
207                  if (oneLineCoded.indexOf("}") == -1) {                          if ((oneLineCoded == null) || (oneLineCoded.equals(""))) {
208                          // log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);");                                  put(DEFAULT_KEY, "");
209                          put(DEFAULT_KEY, oneLineCoded);                                  return;
210                  }                          }
211    
212                  String eatUp = oneLineCoded;                          if (oneLineCoded.indexOf("}") == -1) {
213                  while (eatUp.indexOf("}") != -1) {                                  // log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);");
214                          String substring = eatUp.substring(0, eatUp.indexOf("}"));                                  put(DEFAULT_KEY, oneLineCoded);
215                            }
216                          // log.debug("substring = "+substring);  
217                          String key = substring.substring(0, substring.indexOf("{"));                          String eatUp = oneLineCoded;
218                          String value = substring.substring(substring.indexOf("{") + 1,                          while (eatUp.indexOf("}") != -1) {
219                                          substring.length());                                  String substring = eatUp.substring(0, eatUp.indexOf("}"));
220                          // log.debug("key="+key);  
221                          // log.debug("value="+value);                                  // log.debug("substring = "+substring);
222                          put(key, value);                                  String key = substring.substring(0, substring.indexOf("{"));
223                          eatUp = eatUp.substring(eatUp.indexOf("}") + 1);                                  String value = substring.substring(substring.indexOf("{") + 1,
224                  }                                                  substring.length());
225                                    // log.debug("key="+key);
226                                    // log.debug("value="+value);
227                                    put(key, value);
228                                    eatUp = eatUp.substring(eatUp.indexOf("}") + 1);
229                            }
230                  } catch (Exception e) {                  } catch (Exception e) {
231                          log.warn("Error while reading the oneLineCode '"+oneLineCoded+"'", e);                          log.warn("Error while reading the oneLineCode '" + oneLineCoded
232                                            + "'", e);
233                          log.warn("Translation will be empty!");                          log.warn("Translation will be empty!");
234                  }                  }
235          }          }
# Line 273  public class Translation extends HashMap Line 278  public class Translation extends HashMap
278                                                  return s;                                                  return s;
279                                  }                                  }
280                  }                  }
281  //              log.warn("No translation found!");                  // log.warn("No translation found!");
282                  return NO_TRANSLATION;                  return NO_TRANSLATION;
283          }          }
284    
   
285          /**          /**
286           * {@link PropertyChangeListener} can be registered to be informed when the           * {@link PropertyChangeListener} can be registered to be informed when the
287           * {@link Locale} changed.           * {@link Locale} changed.
# Line 303  public class Translation extends HashMap Line 307  public class Translation extends HashMap
307                  }                  }
308          }          }
309    
310            /**
311             * The listeneras are stored in a {@link WeakHashSet}! So you HAVE TO KEEP a
312             * reference as long as you need the listener.
313             */
314          public void addTranslationChangeListener(ActionListener actionListener) {          public void addTranslationChangeListener(ActionListener actionListener) {
315                  actionListeners.add(actionListener);                  actionListeners.add(actionListener);
316          }          }
317            
318            /**
319             * The listeneras are stored in a {@link WeakHashSet}! You don't have to
320             * remove the listener, as long as you throw away the reference to the
321             * listener.
322             */
323          public boolean removeTranslationChangeListener(ActionListener actionListener) {          public boolean removeTranslationChangeListener(ActionListener actionListener) {
324                  return actionListeners.remove(actionListener);                  return actionListeners.remove(actionListener);
325          }          }
326    
327          public void fireTranslationChangedEvents(String lang) {          public void fireTranslationChangedEvents(String lang) {
328                  ActionEvent ae = new ActionEvent(this, new Random().nextInt(), lang);                  ActionEvent ae = new ActionEvent(this, new Random().nextInt(), lang);
329                    
330                  for (ActionListener al : actionListeners) {                  final Iterator<ActionListener> iterator = actionListeners.iterator();
331                          al.actionPerformed( ae);                  while (iterator.hasNext()) {
332                            ActionListener al = iterator.next();
333                            al.actionPerformed(ae);
334                  }                  }
335          }          }
336            
337          @Override          @Override
338          public String put(String lang, String value) {          public String put(String lang, String value) {
339                  String result = super.put(lang, value);                  String result = super.put(lang, value);
# Line 329  public class Translation extends HashMap Line 344  public class Translation extends HashMap
344          public void fromOneLine(InternationalString iString) {          public void fromOneLine(InternationalString iString) {
345                  if (iString != null)                  if (iString != null)
346                          fromOneLine(iString.toString());                          fromOneLine(iString.toString());
347                  else                  else
348                          fromOneLine((String)null);                          fromOneLine((String) null);
349          }          }
350            
351          /**          /**
352           * Copy this {@link Translation} to another {@link Translation} e.g. for           * Copy this {@link Translation} to another {@link Translation} e.g. for
353           * editing           * editing
# Line 341  public class Translation extends HashMap Line 356  public class Translation extends HashMap
356           */           */
357          @Override          @Override
358          public Translation copyTo(Translation translation2) {          public Translation copyTo(Translation translation2) {
359                    
360                  if (translation2 == null)                  if (translation2 == null)
361  //          throw new IllegalArgumentException(                          // throw new IllegalArgumentException(
362  //              "Target translation may not be null.");                          // "Target translation may not be null.");
363                    return copy();                          return copy();
364                  for (String s : keySet()) {                  for (String s : keySet()) {
365                          translation2.put(s, get(s));                          translation2.put(s, get(s));
366                  }                  }
367                    
368                  return translation2;                  return translation2;
369          }          }
           
370    
371          @Override          @Override
372          public Translation copy() {          public Translation copy() {
# Line 360  public class Translation extends HashMap Line 374  public class Translation extends HashMap
374          }          }
375    
376          /**          /**
377           * 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
378             * valid.
379             *
380           * @return <code>true</code> if all good           * @return <code>true</code> if all good
381           */           */
382          public static boolean checkValid(Translation translationToCheck) {          public static boolean checkValid(Translation translationToCheck) {
383                    
384                  for (String l : translationToCheck.values()) {                  for (String l : translationToCheck.values()) {
385                            
386                          if (l.contains("{") || l.contains("}")) {                          if (l.contains("{") || l.contains("}")) {
387                    
388                                  return false;                                  return false;
389                          }                          }
390                  }                  }
391                  return true;                  return true;
392          }          }
393            
           
394  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26