1
2 package hoplugins.commons.utils;
3
4 import hoplugins.Commons;
5
6 import java.io.File;
7 import java.io.IOException;
8
9 import java.util.Enumeration;
10 import java.util.Locale;
11 import java.util.Properties;
12 import java.util.ResourceBundle;
13
14
15 /***
16 * Property Manager class for HO Plugins
17 *
18 * @author <a href=mailto:draghetto@users.sourceforge.net>Massimiliano Amato </a>
19 */
20 public final class PluginProperty {
21
22
23 private static Properties properties;
24 private static Properties languageCodes;
25
26 /*** TODO Missing Parameter Documentation */
27 private static final String DEFAULT_LANGUAGE = "English";
28 private static String language = DEFAULT_LANGUAGE;
29 private static int languageID = 2;
30
31
32
33 /***
34 * Private default constuctor to prevent class instantiation.
35 */
36 private PluginProperty() {
37 }
38
39
40
41 /***
42 * Convert HT language ID to Java locale
43 *
44 * @return Locale
45 */
46 public static Locale getLocale() {
47 if (languageCodes == null) {
48 loadLanguageCodes();
49 }
50
51 languageID = Commons.getModel().getHelper().getLanguageID();
52
53 String code = languageCodes.getProperty(Integer.toString(languageID));
54 code.trim();
55
56 Locale locale = null;
57
58 if (code.indexOf('_') >= 0) {
59 String[] parts = code.split("_");
60
61 switch (parts.length) {
62 case 2:
63 locale = new Locale(parts[0], parts[1]);
64 break;
65
66 case 3:
67 locale = new Locale(parts[0], parts[1], parts[2]);
68 break;
69
70 default:
71 locale = new Locale(parts[0]);
72 }
73 } else {
74 locale = new Locale(code);
75 }
76
77 return locale;
78 }
79
80 /***
81 * Returns the property value
82 *
83 * @param key property key
84 *
85 * @return the property value, or the key itself if not found
86 */
87 public static String getString(String key) {
88 if (properties == null) {
89 init();
90 }
91
92
93 String value = properties.getProperty(key);
94
95
96 if (value == null) {
97 value = Commons.getModel().getResource().getProperty(key);
98
99
100 if (value == null) {
101 value = '!' + key + '!';
102 }
103
104
105 properties.setProperty(key, value);
106 }
107
108 return value;
109 }
110
111 /***
112 * Loads the own property file of the given plugin.
113 *
114 * @param plugin The package name of the plugin.
115 */
116 public static void loadPluginProperties(String plugin) {
117 if (properties == null) {
118 init();
119 }
120
121 try {
122
123 ResourceBundle bundle = ResourceBundle.getBundle("hoplugins." + plugin + ".sprache.UI",
124 PluginProperty.getLocale());
125
126
127 for (Enumeration e = bundle.getKeys(); e.hasMoreElements();) {
128 String s = (String) e.nextElement();
129
130 if (!properties.contains(s)) {
131 properties.setProperty(s, bundle.getString(s));
132 }
133 }
134 } catch (Exception e) {
135
136
137
138 File languagefile = new File("hoplugins/" + plugin + "/sprache/" + language
139 + ".properties");
140
141 try {
142 if (languagefile.exists()) {
143 properties.load(new java.io.FileInputStream(languagefile));
144 } else {
145 languagefile = new File("hoplugins/" + plugin + "/sprache/" + DEFAULT_LANGUAGE
146 + ".properties");
147 properties.load(new java.io.FileInputStream(languagefile));
148 }
149 } catch (IOException ioe) {
150 }
151 }
152 }
153
154 /***
155 * Initialize the class. Sets the current language and loads common property files of the
156 * plugins.
157 */
158 private static void init() {
159
160 language = Commons.getModel().getHelper().getLanguageName();
161
162
163 properties = new Properties();
164
165
166 loadPluginCommonProperties("shared");
167 loadPluginCommonProperties("TeamAnalyzer");
168 loadPluginCommonProperties("TrainingExperience");
169 loadPluginCommonProperties("Transfers");
170 loadPluginCommonProperties("TeamPlanner");
171 }
172
173 /***
174 * TODO Missing Method Documentation
175 */
176 private static void loadLanguageCodes() {
177 languageCodes = new Properties();
178
179 File file = new File("hoplugins/commons/sprache/LanguageCodes.properties");
180
181 try {
182 languageCodes.load(new java.io.FileInputStream(file));
183 } catch (IOException e) {
184
185 }
186 }
187
188 /***
189 * Loads the properties for a specified plugin
190 *
191 * @param plugin Plugin name
192 */
193 private static void loadPluginCommonProperties(String plugin) {
194 File languagefile = new File("hoplugins/commons/sprache/" + plugin + "/" + language
195 + ".properties");
196
197 try {
198 if (languagefile.exists()) {
199 properties.load(new java.io.FileInputStream(languagefile));
200 } else {
201 languagefile = new File("hoplugins/commons/sprache/" + plugin + "/"
202 + DEFAULT_LANGUAGE + ".properties");
203 properties.load(new java.io.FileInputStream(languagefile));
204 }
205 } catch (IOException e) {
206 properties = new Properties();
207 }
208 }
209 }