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; |
import java.util.Random; |
43 |
import javax.swing.JComponent; |
import javax.swing.JComponent; |
44 |
|
|
45 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
46 |
|
import org.geotools.util.WeakHashSet; |
47 |
import org.opengis.util.InternationalString; |
import org.opengis.util.InternationalString; |
48 |
|
|
49 |
import skrueger.geotools.Copyable; |
import skrueger.geotools.Copyable; |
56 |
* Krüger</a> |
* Krüger</a> |
57 |
*/ |
*/ |
58 |
|
|
59 |
public class Translation extends HashMap<String, String> implements Copyable<Translation>{ |
public class Translation extends HashMap<String, String> implements |
60 |
|
Copyable<Translation> { |
61 |
public static final String LOCALECHANGE_PROPERTY = "localechange"; |
public static final String LOCALECHANGE_PROPERTY = "localechange"; |
62 |
public static final String NO_TRANSLATION = "NO TRANSLATION"; |
public static final String NO_TRANSLATION = "NO TRANSLATION"; |
63 |
public static final String DEFAULT_KEY = "default"; |
public static final String DEFAULT_KEY = "default"; |
64 |
static final Logger log = Logger.getLogger(Translation.class); |
static final Logger LOGGER = Logger.getLogger(Translation.class); |
65 |
static String activeLang = Locale.getDefault().getLanguage(); |
static String activeLang = Locale.getDefault().getLanguage(); |
66 |
|
|
67 |
static protected List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>(); |
static protected List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>(); |
76 |
setActiveLang(locale.getLanguage()); |
setActiveLang(locale.getLanguage()); |
77 |
} |
} |
78 |
|
|
79 |
private List<ActionListener> actionListeners = new ArrayList<ActionListener>(); |
private WeakHashSet<ActionListener> actionListeners = new WeakHashSet<ActionListener>( |
80 |
|
ActionListener.class); |
81 |
|
|
82 |
@Override |
@Override |
83 |
/* |
/* |
86 |
*/ |
*/ |
87 |
public Translation clone() { |
public Translation clone() { |
88 |
throw new RuntimeException("use copy()"); |
throw new RuntimeException("use copy()"); |
89 |
// return (Translation) super.clone(); |
// return (Translation) super.clone(); |
90 |
} |
} |
91 |
|
|
92 |
/** |
/** |
143 |
|
|
144 |
fireLocaleChangeEvents(); |
fireLocaleChangeEvents(); |
145 |
|
|
146 |
log.info("skrueger.i8n.Translation switched ActiveLang to " + newLang); |
LOGGER.info("skrueger.i8n.Translation switched ActiveLang to " + newLang); |
147 |
} |
} |
148 |
|
|
149 |
/** |
/** |
201 |
public void fromOneLine(final String oneLineCoded) { |
public void fromOneLine(final String oneLineCoded) { |
202 |
|
|
203 |
clear(); |
clear(); |
204 |
|
|
205 |
try { |
try { |
|
|
|
|
if ((oneLineCoded == null) || (oneLineCoded.equals(""))) { |
|
|
put(DEFAULT_KEY, ""); |
|
|
return; |
|
|
} |
|
206 |
|
|
207 |
if (oneLineCoded.indexOf("}") == -1) { |
if ((oneLineCoded == null) || (oneLineCoded.equals(""))) { |
208 |
// log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);"); |
put(DEFAULT_KEY, ""); |
209 |
put(DEFAULT_KEY, oneLineCoded); |
return; |
210 |
} |
} |
211 |
|
|
212 |
String eatUp = oneLineCoded; |
if (oneLineCoded.indexOf("}") == -1) { |
213 |
while (eatUp.indexOf("}") != -1) { |
// log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);"); |
214 |
String substring = eatUp.substring(0, eatUp.indexOf("}")); |
put(DEFAULT_KEY, oneLineCoded); |
215 |
|
} |
216 |
// log.debug("substring = "+substring); |
|
217 |
String key = substring.substring(0, substring.indexOf("{")); |
String eatUp = oneLineCoded; |
218 |
String value = substring.substring(substring.indexOf("{") + 1, |
while (eatUp.indexOf("}") != -1) { |
219 |
substring.length()); |
String substring = eatUp.substring(0, eatUp.indexOf("}")); |
220 |
// log.debug("key="+key); |
|
221 |
// log.debug("value="+value); |
// log.debug("substring = "+substring); |
222 |
put(key, value); |
String key = substring.substring(0, substring.indexOf("{")); |
223 |
eatUp = eatUp.substring(eatUp.indexOf("}") + 1); |
String value = substring.substring(substring.indexOf("{") + 1, |
224 |
} |
substring.length()); |
225 |
|
// log.debug("key="+key); |
226 |
|
// log.debug("value="+value); |
227 |
|
put(key, value); |
228 |
|
eatUp = eatUp.substring(eatUp.indexOf("}") + 1); |
229 |
|
} |
230 |
} catch (Exception e) { |
} catch (Exception e) { |
231 |
log.warn("Error while reading the oneLineCode '"+oneLineCoded+"'", e); |
LOGGER.warn("Error while reading the oneLineCode '" + oneLineCoded |
232 |
log.warn("Translation will be empty!"); |
+ "'", e); |
233 |
|
LOGGER.warn("Translation will be empty!"); |
234 |
} |
} |
235 |
} |
} |
236 |
|
|
278 |
return s; |
return s; |
279 |
} |
} |
280 |
} |
} |
281 |
// log.warn("No translation found!"); |
// log.warn("No translation found!"); |
282 |
return NO_TRANSLATION; |
return NO_TRANSLATION; |
283 |
} |
} |
284 |
|
|
|
|
|
285 |
/** |
/** |
286 |
* {@link PropertyChangeListener} can be registered to be informed when the |
* {@link PropertyChangeListener} can be registered to be informed when the |
287 |
* {@link Locale} changed. |
* {@link Locale} changed. |
307 |
} |
} |
308 |
} |
} |
309 |
|
|
310 |
|
/** |
311 |
|
* The listeneras are stored in a {@link WeakHashSet}! So you HAVE TO KEEP a |
312 |
|
* reference as long as you need the listener. |
313 |
|
*/ |
314 |
public void addTranslationChangeListener(ActionListener actionListener) { |
public void addTranslationChangeListener(ActionListener actionListener) { |
315 |
actionListeners.add(actionListener); |
if (actionListeners.add(actionListener)) { |
316 |
|
LOGGER.debug("registering a new translationChangeActionListener in the WeakHashSet"); |
317 |
|
} |
318 |
} |
} |
319 |
|
|
320 |
|
/** |
321 |
|
* The listeneras are stored in a {@link WeakHashSet}! You don't have to |
322 |
|
* remove the listener, as long as you throw away the reference to the |
323 |
|
* listener. |
324 |
|
*/ |
325 |
public boolean removeTranslationChangeListener(ActionListener actionListener) { |
public boolean removeTranslationChangeListener(ActionListener actionListener) { |
326 |
return actionListeners.remove(actionListener); |
return actionListeners.remove(actionListener); |
327 |
} |
} |
328 |
|
|
329 |
public void fireTranslationChangedEvents(String lang) { |
public void fireTranslationChangedEvents(String lang) { |
330 |
ActionEvent ae = new ActionEvent(this, new Random().nextInt(), lang); |
ActionEvent ae = new ActionEvent(this, new Random().nextInt(), lang); |
331 |
|
|
332 |
for (ActionListener al : actionListeners) { |
final Iterator<ActionListener> iterator = actionListeners.iterator(); |
333 |
al.actionPerformed( ae); |
while (iterator.hasNext()) { |
334 |
|
ActionListener al = iterator.next(); |
335 |
|
al.actionPerformed(ae); |
336 |
} |
} |
337 |
} |
} |
338 |
|
|
339 |
@Override |
@Override |
340 |
public String put(String lang, String value) { |
public String put(String lang, String value) { |
341 |
String result = super.put(lang, value); |
String result = super.put(lang, value); |
346 |
public void fromOneLine(InternationalString iString) { |
public void fromOneLine(InternationalString iString) { |
347 |
if (iString != null) |
if (iString != null) |
348 |
fromOneLine(iString.toString()); |
fromOneLine(iString.toString()); |
349 |
else |
else |
350 |
fromOneLine((String)null); |
fromOneLine((String) null); |
351 |
} |
} |
352 |
|
|
353 |
/** |
/** |
354 |
* Copy this {@link Translation} to another {@link Translation} e.g. for |
* Copy this {@link Translation} to another {@link Translation} e.g. for |
355 |
* editing |
* editing |
358 |
*/ |
*/ |
359 |
@Override |
@Override |
360 |
public Translation copyTo(Translation translation2) { |
public Translation copyTo(Translation translation2) { |
361 |
|
|
362 |
if (translation2 == null) |
if (translation2 == null) |
363 |
// throw new IllegalArgumentException( |
// throw new IllegalArgumentException( |
364 |
// "Target translation may not be null."); |
// "Target translation may not be null."); |
365 |
return copy(); |
return copy(); |
366 |
for (String s : keySet()) { |
for (String s : keySet()) { |
367 |
translation2.put(s, get(s)); |
translation2.put(s, get(s)); |
368 |
} |
} |
369 |
|
|
370 |
return translation2; |
return translation2; |
371 |
} |
} |
|
|
|
372 |
|
|
373 |
@Override |
@Override |
374 |
public Translation copy() { |
public Translation copy() { |
376 |
} |
} |
377 |
|
|
378 |
/** |
/** |
379 |
* Checks if the {@link String}s stored in the {@link Translation} are all valid. |
* Checks if the {@link String}s stored in the {@link Translation} are all |
380 |
|
* valid. |
381 |
|
* |
382 |
* @return <code>true</code> if all good |
* @return <code>true</code> if all good |
383 |
*/ |
*/ |
384 |
public static boolean checkValid(Translation translationToCheck) { |
public static boolean checkValid(Translation translationToCheck) { |
385 |
|
|
386 |
for (String l : translationToCheck.values()) { |
for (String l : translationToCheck.values()) { |
387 |
|
|
388 |
if (l.contains("{") || l.contains("}")) { |
if (l.contains("{") || l.contains("}")) { |
389 |
|
|
390 |
return false; |
return false; |
391 |
} |
} |
392 |
} |
} |
393 |
return true; |
return true; |
394 |
} |
} |
395 |
|
|
|
|
|
396 |
} |
} |