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.List; |
import java.util.List; |
39 |
import java.util.Locale; |
import java.util.Locale; |
40 |
|
import java.util.Random; |
41 |
|
|
42 |
import javax.swing.JComponent; |
import javax.swing.JComponent; |
|
import javax.swing.JTable; |
|
43 |
|
|
44 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
45 |
|
import org.opengis.util.InternationalString; |
46 |
|
|
47 |
|
import skrueger.geotools.Copyable; |
48 |
|
|
49 |
/** |
/** |
50 |
* Represents a {@link HashMap} of translations. toString() returns the |
* Represents a {@link HashMap} of translations. toString() returns the |
54 |
* Krüger</a> |
* Krüger</a> |
55 |
*/ |
*/ |
56 |
|
|
57 |
public class Translation extends HashMap<String, String> { |
public class Translation extends HashMap<String, String> implements Copyable<Translation>{ |
58 |
public static final String LOCALECHANGE_PROPERTY = "localechange"; |
public static final String LOCALECHANGE_PROPERTY = "localechange"; |
59 |
public static final String NO_TRANSLATION = "NO TRANSLATION"; |
public static final String NO_TRANSLATION = "NO TRANSLATION"; |
60 |
public static final String DEFAULT_KEY = "default"; |
public static final String DEFAULT_KEY = "default"; |
61 |
static final Logger log = Logger.getLogger(Translation.class); |
static final Logger log = Logger.getLogger(Translation.class); |
62 |
static String activeLang = "fr"; |
static String activeLang = Locale.getDefault().getLanguage(); |
63 |
|
|
64 |
static protected List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>(); |
static protected List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>(); |
65 |
|
|
73 |
setActiveLang(locale.getLanguage()); |
setActiveLang(locale.getLanguage()); |
74 |
} |
} |
75 |
|
|
76 |
|
private List<ActionListener> actionListeners = new ArrayList<ActionListener>(); |
77 |
|
|
78 |
@Override |
@Override |
79 |
/* |
/* |
80 |
* @comment To make a copy of a translation see methods toOneLine() and |
* @comment To make a copy of a translation see methods toOneLine() and |
81 |
* fromOneLine() |
* fromOneLine() |
82 |
*/ |
*/ |
83 |
public Translation clone() { |
public Translation clone() { |
84 |
return (Translation) super.clone(); |
throw new RuntimeException("use copy()"); |
85 |
|
// return (Translation) super.clone(); |
86 |
} |
} |
87 |
|
|
88 |
/** |
/** |
195 |
* @author Stefan Alfons Krüger |
* @author Stefan Alfons Krüger |
196 |
*/ |
*/ |
197 |
public void fromOneLine(final String oneLineCoded) { |
public void fromOneLine(final String oneLineCoded) { |
198 |
|
|
199 |
clear(); |
clear(); |
200 |
|
|
201 |
|
try { |
202 |
|
|
203 |
if ((oneLineCoded == null) || (oneLineCoded.equals(""))) { |
if ((oneLineCoded == null) || (oneLineCoded.equals(""))) { |
204 |
put(DEFAULT_KEY, ""); |
put(DEFAULT_KEY, ""); |
205 |
return; |
return; |
223 |
put(key, value); |
put(key, value); |
224 |
eatUp = eatUp.substring(eatUp.indexOf("}") + 1); |
eatUp = eatUp.substring(eatUp.indexOf("}") + 1); |
225 |
} |
} |
226 |
|
} catch (Exception e) { |
227 |
|
log.warn("Error while reading the oneLineCode '"+oneLineCoded+"'", e); |
228 |
|
log.warn("Translation will be empty!"); |
229 |
|
} |
230 |
} |
} |
231 |
|
|
232 |
/** |
/** |
263 |
// MS: |
// MS: |
264 |
else { |
else { |
265 |
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."); |
|
266 |
return get(DEFAULT_KEY); |
return get(DEFAULT_KEY); |
267 |
} |
} |
268 |
|
|
277 |
return NO_TRANSLATION; |
return NO_TRANSLATION; |
278 |
} |
} |
279 |
|
|
|
/** |
|
|
* 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; |
|
|
} |
|
280 |
|
|
281 |
/** |
/** |
282 |
* {@link PropertyChangeListener} can be registered to be informed when the |
* {@link PropertyChangeListener} can be registered to be informed when the |
303 |
} |
} |
304 |
} |
} |
305 |
|
|
306 |
|
public void addTranslationChangeListener(ActionListener actionListener) { |
307 |
|
actionListeners.add(actionListener); |
308 |
|
} |
309 |
|
|
310 |
|
public boolean removeTranslationChangeListener(ActionListener actionListener) { |
311 |
|
return actionListeners.remove(actionListener); |
312 |
|
} |
313 |
|
|
314 |
|
public void fireTranslationChangedEvents(String lang) { |
315 |
|
ActionEvent ae = new ActionEvent(this, new Random().nextInt(), lang); |
316 |
|
|
317 |
|
for (ActionListener al : actionListeners) { |
318 |
|
al.actionPerformed( ae); |
319 |
|
} |
320 |
|
} |
321 |
|
|
322 |
|
@Override |
323 |
|
public String put(String lang, String value) { |
324 |
|
String result = super.put(lang, value); |
325 |
|
fireTranslationChangedEvents(lang); |
326 |
|
return result; |
327 |
|
} |
328 |
|
|
329 |
|
public void fromOneLine(InternationalString iString) { |
330 |
|
if (iString != null) |
331 |
|
fromOneLine(iString.toString()); |
332 |
|
else |
333 |
|
fromOneLine((String)null); |
334 |
|
} |
335 |
|
|
336 |
|
/** |
337 |
|
* Copy this {@link Translation} to another {@link Translation} e.g. for |
338 |
|
* editing |
339 |
|
* |
340 |
|
* @return the destination {@link Translation} |
341 |
|
*/ |
342 |
|
@Override |
343 |
|
public Translation copyTo(Translation translation2) { |
344 |
|
|
345 |
|
if (translation2 == null) |
346 |
|
throw new IllegalArgumentException( |
347 |
|
"Target translation may not be null."); |
348 |
|
for (String s : keySet()) { |
349 |
|
translation2.put(s, get(s)); |
350 |
|
} |
351 |
|
|
352 |
|
return translation2; |
353 |
|
} |
354 |
|
|
355 |
|
|
356 |
|
@Override |
357 |
|
public Translation copy() { |
358 |
|
return copyTo(new Translation()); |
359 |
|
} |
360 |
|
|
361 |
|
/** |
362 |
|
* Checks if the {@link String}s stored in the {@link Translation} are all valid. |
363 |
|
* @return <code>true</code> if all good |
364 |
|
*/ |
365 |
|
public static boolean checkValid(Translation translationToCheck) { |
366 |
|
|
367 |
|
for (String l : translationToCheck.values()) { |
368 |
|
|
369 |
|
if (l.contains("{") || l.contains("}")) { |
370 |
|
|
371 |
|
return false; |
372 |
|
} |
373 |
|
} |
374 |
|
return true; |
375 |
|
} |
376 |
|
|
377 |
|
|
378 |
} |
} |