View Javadoc
1   // ******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2024.
6   //
7   // This file is part of Force Field X.
8   //
9   // Force Field X is free software; you can redistribute it and/or modify it
10  // under the terms of the GNU General Public License version 3 as published by
11  // the Free Software Foundation.
12  //
13  // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  // details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA 02111-1307 USA
21  //
22  // Linking this library statically or dynamically with other modules is making a
23  // combined work based on this library. Thus, the terms and conditions of the
24  // GNU General Public License cover the whole combination.
25  //
26  // As a special exception, the copyright holders of this library give you
27  // permission to link this library with independent modules to produce an
28  // executable, regardless of the license terms of these independent modules, and
29  // to copy and distribute the resulting executable under terms of your choice,
30  // provided that you also meet, for each linked independent module, the terms
31  // and conditions of the license of that module. An independent module is a
32  // module which is not derived from or based on this library. If you modify this
33  // library, you may extend this exception to your version of the library, but
34  // you are not obligated to do so. If you do not wish to do so, delete this
35  // exception statement from your version.
36  //
37  // ******************************************************************************
38  package ffx.ui.properties;
39  
40  import java.util.Enumeration;
41  import java.util.Hashtable;
42  import java.util.Locale;
43  import java.util.PropertyResourceBundle;
44  import java.util.ResourceBundle;
45  import java.util.logging.Logger;
46  
47  /**
48   * The FFXLocale class will encapsulate internationalization features.
49   *
50   * @author Michael J. Schnieders
51   * @since 1.0
52   */
53  public class FFXLocale {
54  
55    private static final Logger logger = Logger.getLogger(FFXLocale.class.getName());
56    private Locale currentLocale;
57    private PropertyResourceBundle ffxLabels;
58    private Hashtable<String, String> reverseLookUp = new Hashtable<String, String>();
59  
60    /** Constructor for FFXLocale. */
61    public FFXLocale() {
62      currentLocale = Locale.getDefault();
63      ffxLabels =
64          (PropertyResourceBundle)
65              ResourceBundle.getBundle("ffx.ui.properties.StringBundle", currentLocale);
66      loadHashtable();
67    }
68  
69    /**
70     * Constructor for FFXLocale.
71     *
72     * @param language a {@link java.lang.String} object.
73     * @param country a {@link java.lang.String} object.
74     */
75    public FFXLocale(String language, String country) {
76      setLocale(language, country);
77    }
78  
79    /**
80     * getKey
81     *
82     * @param string a {@link java.lang.String} object.
83     * @return a {@link java.lang.String} object.
84     */
85    public String getKey(String string) {
86      return reverseLookUp.get(string);
87    }
88  
89    /**
90     * getValue
91     *
92     * @param key a {@link java.lang.String} object.
93     * @return a {@link java.lang.String} object.
94     */
95    public String getValue(String key) {
96      return ffxLabels.getString(key).trim();
97    }
98  
99    /** list */
100   public void list() {
101     for (String value : reverseLookUp.keySet()) {
102       String key = reverseLookUp.get(value);
103       logger.info("key = " + key + ", " + "value = " + value);
104     }
105   }
106 
107   /**
108    * setLocale
109    *
110    * @param language a {@link java.lang.String} object.
111    * @param country a {@link java.lang.String} object.
112    * @return a boolean.
113    */
114   public boolean setLocale(String language, String country) {
115     Locale locale = Locale.of(language, country);
116     try {
117       ffxLabels =
118           (PropertyResourceBundle)
119               ResourceBundle.getBundle("ffx.ui.properties.StringBundle", locale);
120     } catch (Exception ex) {
121       Logger.getLogger("ffx").severe("" + ex);
122       return false;
123     }
124     loadHashtable();
125     currentLocale = locale;
126     return true;
127   }
128 
129   private void loadHashtable() {
130     reverseLookUp.clear();
131     Enumeration<String> e = ffxLabels.getKeys();
132     while (e.hasMoreElements()) {
133       String key = e.nextElement();
134       String value = getValue(key);
135       reverseLookUp.put(value, key);
136     }
137   }
138 }