/[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 1097 by alfonx, Sun Oct 10 21:14:05 2010 UTC revision 1373 by alfonx, Wed Jan 19 12:59:51 2011 UTC
# Line 57  import skrueger.geotools.Copyable; Line 57  import skrueger.geotools.Copyable;
57   * Represents a {@link HashMap} of translations. toString() returns the   * Represents a {@link HashMap} of translations. toString() returns the
58   * appropriate translation. This class is mutable.   * appropriate translation. This class is mutable.
59   *   *
60   * @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a>   * @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a>
61   */   */
62    
63  public class Translation extends HashMap<String, String> implements  public class Translation extends HashMap<String, String> implements
# Line 65  public class Translation extends HashMap Line 65  public class Translation extends HashMap
65    
66          private static final long serialVersionUID = -347702744122305245L;          private static final long serialVersionUID = -347702744122305245L;
67    
68          public static final String LOCALECHANGE_PROPERTY = "localechange";          public static final String LOCALE_CHANGE_PROPERTY = "localechange";
69          public static final String ACTIVELANG_CHANGE_PROPERTY = "activelangchange";          public static final String ACTIVELANG_CHANGE_PROPERTY = "activelangchange";
70          public static final String NO_TRANSLATION = "NO TRANSLATION";          public static final String NO_TRANSLATION = "NO TRANSLATION";
71          public static final String DEFAULT_KEY = "default";          public static final String DEFAULT_KEY = "default";
# Line 94  public class Translation extends HashMap Line 94  public class Translation extends HashMap
94           * informed if any of the translations changed. TODO: Should be registerable           * informed if any of the translations changed. TODO: Should be registerable
95           * for specific languages           * for specific languages
96           */           */
97          private WeakHashSet<ActionListener> changeListeners = new WeakHashSet<ActionListener>(          private final WeakHashSet<ActionListener> changeListeners = new WeakHashSet<ActionListener>(
98                          ActionListener.class);                          ActionListener.class);
99    
100          static {          static {
# Line 150  public class Translation extends HashMap Line 150  public class Translation extends HashMap
150                  }                  }
151    
152                  if (!I8NUtil.isValidISOLangCode(newLang)) {                  if (!I8NUtil.isValidISOLangCode(newLang)) {
153                          throw new IllegalArgumentException("'" + newLang  
154                                          + "' is not a valid ISO language code.");                          if (!I8NUtil.isPropertiesLanguage(newLang)) {
155                                    throw new IllegalArgumentException(
156                                                    "'"
157                                                                    + newLang
158                                                                    + "' is not a valid ISO language code, nor is it a valid userdefined Properties code.");
159                            }
160    
161                  }                  }
162    
163                  Translation.activeLang = newLang;                  Translation.activeLang = newLang;
# Line 160  public class Translation extends HashMap Line 166  public class Translation extends HashMap
166                  Locale newLocale = new Locale(newLang);                  Locale newLocale = new Locale(newLang);
167                  if (setDefaultLocale) {                  if (setDefaultLocale) {
168    
169                          Locale.setDefault(newLocale);                          setDefaultLocale(newLocale);
                         /**  
                          * Setting default locale for Swing JComponents to work around bug  
                          * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4884480  
                          */  
                         JComponent.setDefaultLocale(newLocale);  
   
                         fireLocaleChangeEvents();  
170    
171                          LOGGER.info(Translation.class.getName()                          LOGGER.info(Translation.class.getName()
172                                          + " switched ActiveLang and Locale to " + newLang);                                          + " switched ActiveLang and Locale to " + newLang);
# Line 380  public class Translation extends HashMap Line 379  public class Translation extends HashMap
379    
380          /**          /**
381           * Informs all registered {@link PropertyChangeListener}s about a change of           * Informs all registered {@link PropertyChangeListener}s about a change of
382           * the the {@link Locale}.           * type LOCALE_CHANGE_PROPERTY the the {@link Locale}.
383           */           */
384          public static void fireLocaleChangeEvents() {          public static void fireLocaleChangeEvents() {
385                  PropertyChangeEvent pce = new PropertyChangeEvent(new Translation(                  PropertyChangeEvent pce = new PropertyChangeEvent(new Translation(
386                                  new ArrayList<String>(), "fakeSource"), LOCALECHANGE_PROPERTY,                                  new ArrayList<String>(), "fakeSource"), LOCALE_CHANGE_PROPERTY,
387                                  null, getActiveLang());                                  null, getActiveLang());
388                  for (PropertyChangeListener pcl : listenersLocaleChange) {                  for (PropertyChangeListener pcl : listenersLocaleChange) {
389                          if (pcl != null)                          if (pcl != null)
# Line 394  public class Translation extends HashMap Line 393  public class Translation extends HashMap
393    
394          /**          /**
395           * Informs all registered {@link PropertyChangeListener}s about a change of           * Informs all registered {@link PropertyChangeListener}s about a change of
396           * the the {@link Locale}.           * type ACTIVELANG_CHANGE_PROPERTY the the {@link Locale}.
397           */           */
398          public static void fireActiveLangChangeEvents() {          public static void fireActiveLangChangeEvents() {
399                  PropertyChangeEvent pce = new PropertyChangeEvent(new Translation(                  PropertyChangeEvent pce = new PropertyChangeEvent(new Translation(
# Line 527  public class Translation extends HashMap Line 526  public class Translation extends HashMap
526          public String toString(String lang) {          public String toString(String lang) {
527                  return get(lang);                  return get(lang);
528          }          }
529    
530            /**
531             * Will set the default Locale (if not already equal) and fire Locale change
532             * events.
533             *
534             * @param if <code>null</code> will do nothing.
535             */
536            public static void setDefaultLocale(Locale locale) {
537    
538                    if (locale == null)
539                            return;
540    
541                    if (I8NUtil.isPropertiesLanguage(locale.getLanguage())) {
542                            locale = I8NUtil.propLocales.get(locale.getLanguage())
543                                            .getParentLocale();
544                    }
545    
546                    if (Locale.getDefault().equals(locale))
547                            return;
548                    Locale.setDefault(locale);
549                    /**
550                     * Setting default locale for Swing JComponents to work around bug
551                     * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4884480
552                     */
553                    JComponent.setDefaultLocale(locale);
554    
555                    fireLocaleChangeEvents();
556            }
557    
558  }  }

Legend:
Removed from v.1097  
changed lines
  Added in v.1373

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26