25 |
* |
* |
26 |
* Contributors: |
* Contributors: |
27 |
* Martin O. J. Schmitz - initial API and implementation |
* Martin O. J. Schmitz - initial API and implementation |
28 |
* Stefan A. Krüger - additional utility classes |
* Stefan A. Tzeggai - additional utility classes |
29 |
******************************************************************************/ |
******************************************************************************/ |
30 |
package skrueger.i8n; |
package skrueger.i8n; |
31 |
|
|
33 |
import java.awt.event.ActionListener; |
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.io.Serializable; |
37 |
import java.util.ArrayList; |
import java.util.ArrayList; |
38 |
import java.util.HashMap; |
import java.util.HashMap; |
39 |
|
import java.util.Iterator; |
40 |
import java.util.List; |
import java.util.List; |
41 |
import java.util.Locale; |
import java.util.Locale; |
42 |
import java.util.Random; |
import java.util.Random; |
43 |
|
import java.util.WeakHashMap; |
44 |
|
|
45 |
import javax.swing.JComponent; |
import javax.swing.JComponent; |
46 |
|
|
47 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
48 |
|
import org.geotools.util.WeakHashSet; |
49 |
import org.opengis.util.InternationalString; |
import org.opengis.util.InternationalString; |
50 |
|
|
51 |
|
import skrueger.geotools.Copyable; |
52 |
|
|
53 |
/** |
/** |
54 |
* Represents a {@link HashMap} of translations. toString() returns the |
* Represents a {@link HashMap} of translations. toString() returns the |
55 |
* appropriate translation |
* appropriate translation |
58 |
* Krüger</a> |
* Krüger</a> |
59 |
*/ |
*/ |
60 |
|
|
61 |
public class Translation extends HashMap<String, String> { |
public class Translation extends HashMap<String, String> implements |
62 |
|
Copyable<Translation>, Serializable { |
63 |
|
|
64 |
|
private static final long serialVersionUID = -347702744122305245L; |
65 |
|
|
66 |
public static final String LOCALECHANGE_PROPERTY = "localechange"; |
public static final String LOCALECHANGE_PROPERTY = "localechange"; |
67 |
public static final String NO_TRANSLATION = "NO TRANSLATION"; |
public static final String NO_TRANSLATION = "NO TRANSLATION"; |
68 |
public static final String DEFAULT_KEY = "default"; |
public static final String DEFAULT_KEY = "default"; |
69 |
static final Logger log = Logger.getLogger(Translation.class); |
static final Logger LOGGER = Logger.getLogger(Translation.class); |
70 |
static String activeLang = Locale.getDefault().getLanguage(); |
static String activeLang = Locale.getDefault().getLanguage(); |
71 |
|
|
72 |
static protected List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>(); |
static protected WeakHashSet<PropertyChangeListener> listeners = new WeakHashSet<PropertyChangeListener>( |
73 |
|
PropertyChangeListener.class); |
74 |
|
|
75 |
static { |
static { |
76 |
|
|
81 |
Locale locale = Locale.getDefault(); |
Locale locale = Locale.getDefault(); |
82 |
setActiveLang(locale.getLanguage()); |
setActiveLang(locale.getLanguage()); |
83 |
} |
} |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
private List<ActionListener> actionListeners = new ArrayList<ActionListener>(); |
private WeakHashSet<ActionListener> actionListeners = new WeakHashSet<ActionListener>( |
88 |
|
ActionListener.class); |
89 |
|
|
90 |
@Override |
@Override |
91 |
/* |
/* |
93 |
* fromOneLine() |
* fromOneLine() |
94 |
*/ |
*/ |
95 |
public Translation clone() { |
public Translation clone() { |
96 |
return (Translation) super.clone(); |
throw new RuntimeException("use copy()"); |
97 |
|
// return (Translation) super.clone(); |
98 |
} |
} |
99 |
|
|
100 |
/** |
/** |
151 |
|
|
152 |
fireLocaleChangeEvents(); |
fireLocaleChangeEvents(); |
153 |
|
|
154 |
log.info("skrueger.i8n.Translation switched ActiveLang to " + newLang); |
LOGGER.info("skrueger.i8n.Translation switched ActiveLang to " |
155 |
|
+ newLang); |
156 |
} |
} |
157 |
|
|
158 |
/** |
/** |
184 |
put(DEFAULT_KEY, defaultTranslation); |
put(DEFAULT_KEY, defaultTranslation); |
185 |
} else |
} else |
186 |
for (String code : languages) { |
for (String code : languages) { |
187 |
if (code.equals(getActiveLang())) { |
// if (code.equals(getActiveLang())) { |
188 |
put(code, defaultTranslation); |
put(code, defaultTranslation); |
189 |
} |
// } |
190 |
} |
} |
191 |
} |
} |
192 |
|
|
205 |
* <li>If format can't be recognized, the {@link String} is interpreted as |
* <li>If format can't be recognized, the {@link String} is interpreted as |
206 |
* the translation in the <code>{@value #DEFAULT_KEY}</code> language |
* the translation in the <code>{@value #DEFAULT_KEY}</code> language |
207 |
* |
* |
208 |
* @author Stefan Alfons Krüger |
* @author Stefan Alfons Tzeggai |
209 |
*/ |
*/ |
210 |
public void fromOneLine(final String oneLineCoded) { |
public void fromOneLine(final String oneLineCoded) { |
211 |
|
|
212 |
clear(); |
clear(); |
|
|
|
|
if ((oneLineCoded == null) || (oneLineCoded.equals(""))) { |
|
|
put(DEFAULT_KEY, ""); |
|
|
return; |
|
|
} |
|
213 |
|
|
214 |
if (oneLineCoded.indexOf("}") == -1) { |
try { |
215 |
// log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);"); |
|
216 |
put(DEFAULT_KEY, oneLineCoded); |
if ((oneLineCoded == null) || (oneLineCoded.equals(""))) { |
217 |
} |
put(DEFAULT_KEY, ""); |
218 |
|
return; |
219 |
|
} |
220 |
|
|
221 |
|
if (oneLineCoded.indexOf("}") == -1) { |
222 |
|
// log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);"); |
223 |
|
put(DEFAULT_KEY, oneLineCoded); |
224 |
|
} |
225 |
|
|
226 |
String eatUp = oneLineCoded; |
String eatUp = oneLineCoded; |
227 |
while (eatUp.indexOf("}") != -1) { |
while (eatUp.indexOf("}") != -1) { |
228 |
String substring = eatUp.substring(0, eatUp.indexOf("}")); |
String substring = eatUp.substring(0, eatUp.indexOf("}")); |
229 |
|
|
230 |
// log.debug("substring = "+substring); |
// log.debug("substring = "+substring); |
231 |
String key = substring.substring(0, substring.indexOf("{")); |
String key = substring.substring(0, substring.indexOf("{")); |
232 |
String value = substring.substring(substring.indexOf("{") + 1, |
String value = substring.substring(substring.indexOf("{") + 1, |
233 |
substring.length()); |
substring.length()); |
234 |
// log.debug("key="+key); |
// log.debug("key="+key); |
235 |
// log.debug("value="+value); |
// log.debug("value="+value); |
236 |
put(key, value); |
put(key, value); |
237 |
eatUp = eatUp.substring(eatUp.indexOf("}") + 1); |
eatUp = eatUp.substring(eatUp.indexOf("}") + 1); |
238 |
|
} |
239 |
|
} catch (Exception e) { |
240 |
|
LOGGER.warn("Error while reading the oneLineCode '" + oneLineCoded |
241 |
|
+ "'", e); |
242 |
|
LOGGER.warn("Translation will be empty!"); |
243 |
} |
} |
244 |
} |
} |
245 |
|
|
246 |
/** |
/** |
247 |
* Exports the Translations to a String of the Format: "de{Baum}en{tree}" |
* Exports the Translations to a String of the Format: "de{Baum}en{tree}" |
248 |
* |
* |
249 |
* @author Stefan Alfons Krüger |
* @author Stefan Alfons Tzeggai |
250 |
*/ |
*/ |
251 |
public String toOneLine() { |
public String toOneLine() { |
252 |
return I8NUtil.toOneLine(this); |
return I8NUtil.toOneLine(this); |
277 |
// MS: |
// MS: |
278 |
else { |
else { |
279 |
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."); |
|
280 |
return get(DEFAULT_KEY); |
return get(DEFAULT_KEY); |
281 |
} |
} |
282 |
|
|
287 |
return s; |
return s; |
288 |
} |
} |
289 |
} |
} |
290 |
// log.warn("No translation found!"); |
// log.warn("No translation found!"); |
291 |
return NO_TRANSLATION; |
return NO_TRANSLATION; |
292 |
} |
} |
293 |
|
|
294 |
/** |
/** |
295 |
* Copy this {@link Translation} to another {@link Translation} e.g. for |
* {@link PropertyChangeListener} can be registered to be informed when the |
296 |
* editing |
* {@link Locale} changed.<br> |
297 |
|
* The listeners are kept in a {@link WeakHashMap}, so you have to keep a |
298 |
|
* reference to the listener or it will be removed! |
299 |
* |
* |
300 |
* @return the destination {@link Translation} |
* @param propertyChangeListener |
301 |
|
* A {@link PropertyChangeListener} that will be called when |
302 |
|
* {@link #setActiveLang(String)} changes the language. |
303 |
*/ |
*/ |
304 |
public Translation copy(Translation backup) { |
public static void addLocaleChangeListener( |
305 |
if (backup == null) |
PropertyChangeListener propertyChangeListener) { |
306 |
throw new IllegalArgumentException( |
listeners.add(propertyChangeListener); |
|
"Target translation may not be null."); |
|
|
for (String s : keySet()) { |
|
|
backup.put(s, get(s)); |
|
|
} |
|
|
return backup; |
|
307 |
} |
} |
308 |
|
|
309 |
/** |
/** |
310 |
* {@link PropertyChangeListener} can be registered to be informed when the |
* {@link PropertyChangeListener} can be registered to be informed when the |
311 |
* {@link Locale} changed. |
* {@link Locale} changed.<br> |
312 |
|
* The listeners are kept in a {@link WeakHashMap}, so you have to keep a |
313 |
|
* reference to the listener or it will be removed! |
314 |
* |
* |
315 |
* @param propertyChangeListener |
* @param propertyChangeListener |
316 |
|
* A {@link PropertyChangeListener} that will be called when |
317 |
|
* {@link #setActiveLang(String)} changes the language. |
318 |
*/ |
*/ |
319 |
public static void addLocaleChangeListener( |
public static boolean removeLocaleChangeListener( |
320 |
PropertyChangeListener propertyChangeListener) { |
PropertyChangeListener propertyChangeListener) { |
321 |
listeners.add(propertyChangeListener); |
return listeners.remove(propertyChangeListener); |
322 |
} |
} |
323 |
|
|
324 |
/** |
/** |
335 |
} |
} |
336 |
} |
} |
337 |
|
|
338 |
|
/** |
339 |
|
* The listeneras are stored in a {@link WeakHashSet}! So you HAVE TO KEEP a |
340 |
|
* reference as long as you need the listener. |
341 |
|
*/ |
342 |
public void addTranslationChangeListener(ActionListener actionListener) { |
public void addTranslationChangeListener(ActionListener actionListener) { |
343 |
actionListeners.add(actionListener); |
if (actionListeners.add(actionListener)) { |
344 |
|
// LOGGER |
345 |
|
// .debug("registering a new translationChangeActionListener in the WeakHashSet"); |
346 |
|
} |
347 |
} |
} |
348 |
|
|
349 |
|
/** |
350 |
|
* The listeneras are stored in a {@link WeakHashSet}! You don't have to |
351 |
|
* remove the listener, as long as you throw away the reference to the |
352 |
|
* listener. |
353 |
|
*/ |
354 |
public boolean removeTranslationChangeListener(ActionListener actionListener) { |
public boolean removeTranslationChangeListener(ActionListener actionListener) { |
355 |
return actionListeners.remove(actionListener); |
return actionListeners.remove(actionListener); |
356 |
} |
} |
357 |
|
|
358 |
public void fireTranslationChangedEvents(String lang) { |
public void fireTranslationChangedEvents(String lang) { |
359 |
ActionEvent ae = new ActionEvent(this, new Random().nextInt(), lang); |
ActionEvent ae = new ActionEvent(this, new Random().nextInt(), lang); |
360 |
|
|
361 |
for (ActionListener al : actionListeners) { |
final Iterator<ActionListener> iterator = actionListeners.iterator(); |
362 |
al.actionPerformed( ae); |
while (iterator.hasNext()) { |
363 |
|
ActionListener al = iterator.next(); |
364 |
|
al.actionPerformed(ae); |
365 |
} |
} |
366 |
} |
} |
367 |
|
|
368 |
@Override |
@Override |
369 |
public String put(String lang, String value) { |
public String put(String lang, String value) { |
370 |
String result = super.put(lang, value); |
String result = super.put(lang, value); |
375 |
public void fromOneLine(InternationalString iString) { |
public void fromOneLine(InternationalString iString) { |
376 |
if (iString != null) |
if (iString != null) |
377 |
fromOneLine(iString.toString()); |
fromOneLine(iString.toString()); |
378 |
else |
else |
379 |
fromOneLine((String)null); |
fromOneLine((String) null); |
380 |
} |
} |
381 |
|
|
382 |
|
/** |
383 |
|
* Copy this {@link Translation} to another {@link Translation} e.g. for |
384 |
|
* editing |
385 |
|
* |
386 |
|
* @return the destination {@link Translation} |
387 |
|
*/ |
388 |
|
@Override |
389 |
|
public Translation copyTo(Translation translation2) { |
390 |
|
|
391 |
|
if (translation2 == null) |
392 |
|
// throw new IllegalArgumentException( |
393 |
|
// "Target translation may not be null."); |
394 |
|
return copy(); |
395 |
|
for (String s : keySet()) { |
396 |
|
translation2.put(s, get(s)); |
397 |
|
} |
398 |
|
|
399 |
|
return translation2; |
400 |
|
} |
401 |
|
|
402 |
|
@Override |
403 |
|
public Translation copy() { |
404 |
|
return copyTo(new Translation()); |
405 |
|
} |
406 |
|
|
407 |
|
/** |
408 |
|
* Checks if the {@link String}s stored in the {@link Translation} are all |
409 |
|
* valid. |
410 |
|
* |
411 |
|
* @return <code>true</code> if all good |
412 |
|
*/ |
413 |
|
public static boolean checkValid(Translation translationToCheck) { |
414 |
|
|
415 |
|
for (String l : translationToCheck.values()) { |
416 |
|
|
417 |
|
if (l.contains("{") || l.contains("}")) { |
418 |
|
|
419 |
|
return false; |
420 |
|
} |
421 |
|
} |
422 |
|
return true; |
423 |
|
} |
424 |
|
|
425 |
} |
} |