29 |
******************************************************************************/ |
******************************************************************************/ |
30 |
package skrueger.i8n; |
package skrueger.i8n; |
31 |
|
|
32 |
|
import java.awt.event.ActionEvent; |
33 |
|
import java.awt.event.ActionListener; |
34 |
import java.beans.PropertyChangeEvent; |
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; |
42 |
|
import java.util.WeakHashMap; |
43 |
|
|
44 |
import javax.swing.JComponent; |
import javax.swing.JComponent; |
45 |
|
|
46 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
47 |
|
import org.geotools.util.WeakHashSet; |
48 |
|
import org.opengis.util.InternationalString; |
49 |
|
|
50 |
|
import skrueger.geotools.Copyable; |
51 |
|
|
52 |
/** |
/** |
53 |
* Represents a {@link HashMap} of translations. toString() returns the |
* Represents a {@link HashMap} of translations. toString() returns the |
57 |
* Krüger</a> |
* Krüger</a> |
58 |
*/ |
*/ |
59 |
|
|
60 |
public class Translation extends HashMap<String, String> { |
public class Translation extends HashMap<String, String> implements |
61 |
|
Copyable<Translation> { |
62 |
public static final String LOCALECHANGE_PROPERTY = "localechange"; |
public static final String LOCALECHANGE_PROPERTY = "localechange"; |
63 |
public static final String NO_TRANSLATION = "NO TRANSLATION"; |
public static final String NO_TRANSLATION = "NO TRANSLATION"; |
64 |
public static final String DEFAULT_KEY = "default"; |
public static final String DEFAULT_KEY = "default"; |
65 |
static final Logger log = Logger.getLogger(Translation.class); |
static final Logger LOGGER = Logger.getLogger(Translation.class); |
66 |
static String activeLang = Locale.getDefault().getLanguage(); |
static String activeLang = Locale.getDefault().getLanguage(); |
67 |
|
|
68 |
static protected List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>(); |
static protected WeakHashSet<PropertyChangeListener> listeners = new WeakHashSet<PropertyChangeListener>( |
69 |
|
PropertyChangeListener.class); |
70 |
|
|
71 |
static { |
static { |
72 |
|
|
78 |
setActiveLang(locale.getLanguage()); |
setActiveLang(locale.getLanguage()); |
79 |
} |
} |
80 |
|
|
81 |
|
private WeakHashSet<ActionListener> actionListeners = new WeakHashSet<ActionListener>( |
82 |
|
ActionListener.class); |
83 |
|
|
84 |
@Override |
@Override |
85 |
/* |
/* |
86 |
* @comment To make a copy of a translation see methods toOneLine() and |
* @comment To make a copy of a translation see methods toOneLine() and |
87 |
* fromOneLine() |
* fromOneLine() |
88 |
*/ |
*/ |
89 |
public Translation clone() { |
public Translation clone() { |
90 |
return (Translation) super.clone(); |
throw new RuntimeException("use copy()"); |
91 |
|
// return (Translation) super.clone(); |
92 |
} |
} |
93 |
|
|
94 |
/** |
/** |
145 |
|
|
146 |
fireLocaleChangeEvents(); |
fireLocaleChangeEvents(); |
147 |
|
|
148 |
log.info("skrueger.i8n.Translation switched ActiveLang to " + newLang); |
LOGGER.info("skrueger.i8n.Translation switched ActiveLang to " |
149 |
|
+ newLang); |
150 |
} |
} |
151 |
|
|
152 |
/** |
/** |
178 |
put(DEFAULT_KEY, defaultTranslation); |
put(DEFAULT_KEY, defaultTranslation); |
179 |
} else |
} else |
180 |
for (String code : languages) { |
for (String code : languages) { |
181 |
if (code.equals(getActiveLang())) { |
// if (code.equals(getActiveLang())) { |
182 |
put(code, defaultTranslation); |
put(code, defaultTranslation); |
183 |
} |
// } |
184 |
} |
} |
185 |
} |
} |
186 |
|
|
202 |
* @author Stefan Alfons Krüger |
* @author Stefan Alfons Krüger |
203 |
*/ |
*/ |
204 |
public void fromOneLine(final String oneLineCoded) { |
public void fromOneLine(final String oneLineCoded) { |
205 |
|
|
206 |
clear(); |
clear(); |
|
if ((oneLineCoded == null) || (oneLineCoded.equals(""))) { |
|
|
put(DEFAULT_KEY, ""); |
|
|
return; |
|
|
} |
|
207 |
|
|
208 |
if (oneLineCoded.indexOf("}") == -1) { |
try { |
209 |
// log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);"); |
|
210 |
put(DEFAULT_KEY, oneLineCoded); |
if ((oneLineCoded == null) || (oneLineCoded.equals(""))) { |
211 |
} |
put(DEFAULT_KEY, ""); |
212 |
|
return; |
213 |
|
} |
214 |
|
|
215 |
String eatUp = oneLineCoded; |
if (oneLineCoded.indexOf("}") == -1) { |
216 |
while (eatUp.indexOf("}") != -1) { |
// log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);"); |
217 |
String substring = eatUp.substring(0, eatUp.indexOf("}")); |
put(DEFAULT_KEY, oneLineCoded); |
218 |
|
} |
219 |
// log.debug("substring = "+substring); |
|
220 |
String key = substring.substring(0, substring.indexOf("{")); |
String eatUp = oneLineCoded; |
221 |
String value = substring.substring(substring.indexOf("{") + 1, |
while (eatUp.indexOf("}") != -1) { |
222 |
substring.length()); |
String substring = eatUp.substring(0, eatUp.indexOf("}")); |
223 |
// log.debug("key="+key); |
|
224 |
// log.debug("value="+value); |
// log.debug("substring = "+substring); |
225 |
put(key, value); |
String key = substring.substring(0, substring.indexOf("{")); |
226 |
eatUp = eatUp.substring(eatUp.indexOf("}") + 1); |
String value = substring.substring(substring.indexOf("{") + 1, |
227 |
|
substring.length()); |
228 |
|
// log.debug("key="+key); |
229 |
|
// log.debug("value="+value); |
230 |
|
put(key, value); |
231 |
|
eatUp = eatUp.substring(eatUp.indexOf("}") + 1); |
232 |
|
} |
233 |
|
} catch (Exception e) { |
234 |
|
LOGGER.warn("Error while reading the oneLineCode '" + oneLineCoded |
235 |
|
+ "'", e); |
236 |
|
LOGGER.warn("Translation will be empty!"); |
237 |
} |
} |
238 |
} |
} |
239 |
|
|
271 |
// MS: |
// MS: |
272 |
else { |
else { |
273 |
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."); |
|
274 |
return get(DEFAULT_KEY); |
return get(DEFAULT_KEY); |
275 |
} |
} |
276 |
|
|
281 |
return s; |
return s; |
282 |
} |
} |
283 |
} |
} |
284 |
// log.warn("No translation found!"); |
// log.warn("No translation found!"); |
285 |
return NO_TRANSLATION; |
return NO_TRANSLATION; |
286 |
} |
} |
287 |
|
|
288 |
/** |
/** |
289 |
* Copy this {@link Translation} to another {@link Translation} e.g. for |
* {@link PropertyChangeListener} can be registered to be informed when the |
290 |
* editing |
* {@link Locale} changed.<br> |
291 |
|
* The listeners are kept in a {@link WeakHashMap}, so you have to keep a |
292 |
|
* reference to the listener or it will be removed! |
293 |
* |
* |
294 |
* @return the destination {@link Translation} |
* @param propertyChangeListener |
295 |
|
* A {@link PropertyChangeListener} that will be called when |
296 |
|
* {@link #setActiveLang(String)} changes the language. |
297 |
*/ |
*/ |
298 |
public Translation copy(Translation backup) { |
public static void addLocaleChangeListener( |
299 |
if (backup == null) |
PropertyChangeListener propertyChangeListener) { |
300 |
throw new IllegalArgumentException( |
listeners.add(propertyChangeListener); |
|
"Target translation may not be null."); |
|
|
for (String s : keySet()) { |
|
|
backup.put(s, get(s)); |
|
|
} |
|
|
return backup; |
|
301 |
} |
} |
302 |
|
|
303 |
/** |
/** |
304 |
* {@link PropertyChangeListener} can be registered to be informed when the |
* {@link PropertyChangeListener} can be registered to be informed when the |
305 |
* {@link Locale} changed. |
* {@link Locale} changed.<br> |
306 |
|
* The listeners are kept in a {@link WeakHashMap}, so you have to keep a |
307 |
|
* reference to the listener or it will be removed! |
308 |
* |
* |
309 |
* @param propertyChangeListener |
* @param propertyChangeListener |
310 |
|
* A {@link PropertyChangeListener} that will be called when |
311 |
|
* {@link #setActiveLang(String)} changes the language. |
312 |
*/ |
*/ |
313 |
public static void addLocaleChangeListener( |
public static boolean removeLocaleChangeListener( |
314 |
PropertyChangeListener propertyChangeListener) { |
PropertyChangeListener propertyChangeListener) { |
315 |
listeners.add(propertyChangeListener); |
return listeners.remove(propertyChangeListener); |
316 |
} |
} |
317 |
|
|
318 |
/** |
/** |
329 |
} |
} |
330 |
} |
} |
331 |
|
|
332 |
|
/** |
333 |
|
* The listeneras are stored in a {@link WeakHashSet}! So you HAVE TO KEEP a |
334 |
|
* reference as long as you need the listener. |
335 |
|
*/ |
336 |
|
public void addTranslationChangeListener(ActionListener actionListener) { |
337 |
|
if (actionListeners.add(actionListener)) { |
338 |
|
// LOGGER |
339 |
|
// .debug("registering a new translationChangeActionListener in the WeakHashSet"); |
340 |
|
} |
341 |
|
} |
342 |
|
|
343 |
|
/** |
344 |
|
* The listeneras are stored in a {@link WeakHashSet}! You don't have to |
345 |
|
* remove the listener, as long as you throw away the reference to the |
346 |
|
* listener. |
347 |
|
*/ |
348 |
|
public boolean removeTranslationChangeListener(ActionListener actionListener) { |
349 |
|
return actionListeners.remove(actionListener); |
350 |
|
} |
351 |
|
|
352 |
|
public void fireTranslationChangedEvents(String lang) { |
353 |
|
ActionEvent ae = new ActionEvent(this, new Random().nextInt(), lang); |
354 |
|
|
355 |
|
final Iterator<ActionListener> iterator = actionListeners.iterator(); |
356 |
|
while (iterator.hasNext()) { |
357 |
|
ActionListener al = iterator.next(); |
358 |
|
al.actionPerformed(ae); |
359 |
|
} |
360 |
|
} |
361 |
|
|
362 |
|
@Override |
363 |
|
public String put(String lang, String value) { |
364 |
|
String result = super.put(lang, value); |
365 |
|
fireTranslationChangedEvents(lang); |
366 |
|
return result; |
367 |
|
} |
368 |
|
|
369 |
|
public void fromOneLine(InternationalString iString) { |
370 |
|
if (iString != null) |
371 |
|
fromOneLine(iString.toString()); |
372 |
|
else |
373 |
|
fromOneLine((String) null); |
374 |
|
} |
375 |
|
|
376 |
|
/** |
377 |
|
* Copy this {@link Translation} to another {@link Translation} e.g. for |
378 |
|
* editing |
379 |
|
* |
380 |
|
* @return the destination {@link Translation} |
381 |
|
*/ |
382 |
|
@Override |
383 |
|
public Translation copyTo(Translation translation2) { |
384 |
|
|
385 |
|
if (translation2 == null) |
386 |
|
// throw new IllegalArgumentException( |
387 |
|
// "Target translation may not be null."); |
388 |
|
return copy(); |
389 |
|
for (String s : keySet()) { |
390 |
|
translation2.put(s, get(s)); |
391 |
|
} |
392 |
|
|
393 |
|
return translation2; |
394 |
|
} |
395 |
|
|
396 |
|
@Override |
397 |
|
public Translation copy() { |
398 |
|
return copyTo(new Translation()); |
399 |
|
} |
400 |
|
|
401 |
|
/** |
402 |
|
* Checks if the {@link String}s stored in the {@link Translation} are all |
403 |
|
* valid. |
404 |
|
* |
405 |
|
* @return <code>true</code> if all good |
406 |
|
*/ |
407 |
|
public static boolean checkValid(Translation translationToCheck) { |
408 |
|
|
409 |
|
for (String l : translationToCheck.values()) { |
410 |
|
|
411 |
|
if (l.contains("{") || l.contains("}")) { |
412 |
|
|
413 |
|
return false; |
414 |
|
} |
415 |
|
} |
416 |
|
return true; |
417 |
|
} |
418 |
|
|
419 |
} |
} |