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; |
50 |
|
|
51 |
/** |
/** |
52 |
* Represents a {@link HashMap} of translations. toString() returns the |
* Represents a {@link HashMap} of translations. toString() returns the |
53 |
* appropriate translation |
* appropriate translation |
56 |
* Krüger</a> |
* Krüger</a> |
57 |
*/ |
*/ |
58 |
|
|
59 |
public class Translation extends HashMap<String, String> { |
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"; |
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 |
/* |
/* |
85 |
* fromOneLine() |
* fromOneLine() |
86 |
*/ |
*/ |
87 |
public Translation clone() { |
public Translation clone() { |
88 |
return (Translation) super.clone(); |
throw new RuntimeException("use copy()"); |
89 |
|
// return (Translation) super.clone(); |
90 |
} |
} |
91 |
|
|
92 |
/** |
/** |
199 |
* @author Stefan Alfons Krüger |
* @author Stefan Alfons Krüger |
200 |
*/ |
*/ |
201 |
public void fromOneLine(final String oneLineCoded) { |
public void fromOneLine(final String oneLineCoded) { |
202 |
|
|
203 |
clear(); |
clear(); |
|
|
|
|
if ((oneLineCoded == null) || (oneLineCoded.equals(""))) { |
|
|
put(DEFAULT_KEY, ""); |
|
|
return; |
|
|
} |
|
204 |
|
|
205 |
if (oneLineCoded.indexOf("}") == -1) { |
try { |
206 |
// log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);"); |
|
207 |
put(DEFAULT_KEY, oneLineCoded); |
if ((oneLineCoded == null) || (oneLineCoded.equals(""))) { |
208 |
} |
put(DEFAULT_KEY, ""); |
209 |
|
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) { |
231 |
|
log.warn("Error while reading the oneLineCode '" + oneLineCoded |
232 |
|
+ "'", e); |
233 |
|
log.warn("Translation will be empty!"); |
234 |
} |
} |
235 |
} |
} |
236 |
|
|
268 |
// MS: |
// MS: |
269 |
else { |
else { |
270 |
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."); |
|
271 |
return get(DEFAULT_KEY); |
return get(DEFAULT_KEY); |
272 |
} |
} |
273 |
|
|
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 |
/** |
/** |
|
* Copy this {@link Translation} to another {@link Translation} e.g. for |
|
|
* editing |
|
|
* |
|
|
* @return the destination {@link Translation} |
|
|
*/ |
|
|
public Translation copy(Translation backup) { |
|
|
if (backup == null) |
|
|
throw new IllegalArgumentException( |
|
|
"Target translation may not be null."); |
|
|
for (String s : keySet()) { |
|
|
backup.put(s, get(s)); |
|
|
} |
|
|
return backup; |
|
|
} |
|
|
|
|
|
/** |
|
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. |
288 |
* |
* |
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); |
actionListeners.add(actionListener); |
316 |
} |
} |
317 |
|
|
318 |
|
/** |
319 |
|
* The listeneras are stored in a {@link WeakHashSet}! You don't have to |
320 |
|
* remove the listener, as long as you throw away the reference to the |
321 |
|
* listener. |
322 |
|
*/ |
323 |
public boolean removeTranslationChangeListener(ActionListener actionListener) { |
public boolean removeTranslationChangeListener(ActionListener actionListener) { |
324 |
return actionListeners.remove(actionListener); |
return actionListeners.remove(actionListener); |
325 |
} |
} |
326 |
|
|
327 |
public void fireTranslationChangedEvents(String lang) { |
public void fireTranslationChangedEvents(String lang) { |
328 |
ActionEvent ae = new ActionEvent(this, new Random().nextInt(), lang); |
ActionEvent ae = new ActionEvent(this, new Random().nextInt(), lang); |
329 |
|
|
330 |
for (ActionListener al : actionListeners) { |
final Iterator<ActionListener> iterator = actionListeners.iterator(); |
331 |
al.actionPerformed( ae); |
while (iterator.hasNext()) { |
332 |
|
ActionListener al = iterator.next(); |
333 |
|
al.actionPerformed(ae); |
334 |
} |
} |
335 |
} |
} |
336 |
|
|
337 |
@Override |
@Override |
338 |
public String put(String lang, String value) { |
public String put(String lang, String value) { |
339 |
String result = super.put(lang, value); |
String result = super.put(lang, value); |
344 |
public void fromOneLine(InternationalString iString) { |
public void fromOneLine(InternationalString iString) { |
345 |
if (iString != null) |
if (iString != null) |
346 |
fromOneLine(iString.toString()); |
fromOneLine(iString.toString()); |
347 |
else |
else |
348 |
fromOneLine((String)null); |
fromOneLine((String) null); |
349 |
} |
} |
350 |
|
|
351 |
|
/** |
352 |
|
* Copy this {@link Translation} to another {@link Translation} e.g. for |
353 |
|
* editing |
354 |
|
* |
355 |
|
* @return the destination {@link Translation} |
356 |
|
*/ |
357 |
|
@Override |
358 |
|
public Translation copyTo(Translation translation2) { |
359 |
|
|
360 |
|
if (translation2 == null) |
361 |
|
// throw new IllegalArgumentException( |
362 |
|
// "Target translation may not be null."); |
363 |
|
return copy(); |
364 |
|
for (String s : keySet()) { |
365 |
|
translation2.put(s, get(s)); |
366 |
|
} |
367 |
|
|
368 |
|
return translation2; |
369 |
|
} |
370 |
|
|
371 |
|
@Override |
372 |
|
public Translation copy() { |
373 |
|
return copyTo(new Translation()); |
374 |
|
} |
375 |
|
|
376 |
|
/** |
377 |
|
* Checks if the {@link String}s stored in the {@link Translation} are all |
378 |
|
* valid. |
379 |
|
* |
380 |
|
* @return <code>true</code> if all good |
381 |
|
*/ |
382 |
|
public static boolean checkValid(Translation translationToCheck) { |
383 |
|
|
384 |
|
for (String l : translationToCheck.values()) { |
385 |
|
|
386 |
|
if (l.contains("{") || l.contains("}")) { |
387 |
|
|
388 |
|
return false; |
389 |
|
} |
390 |
|
} |
391 |
|
return true; |
392 |
|
} |
393 |
|
|
394 |
} |
} |