01: /*--------------------------------------------------------------------------*
02: | Copyright (C) 2006 Christopher Kohlhaas |
03: | |
04: | This program is free software; you can redistribute it and/or modify |
05: | it under the terms of the GNU General Public License as published by the |
06: | Free Software Foundation. A copy of the license has been included with |
07: | these distribution in the COPYING file, if not go to www.fsf.org |
08: | |
09: | As a special exception, you are granted the permissions to link this |
10: | program with every library, which license fulfills the Open Source |
11: | Definition as published by the Open Source Initiative (OSI). |
12: *--------------------------------------------------------------------------*/
13: package org.rapla.entities;
14:
15: import java.util.*;
16:
17: /** Some entities (especially dynamic-types and attributes)
18: can have multiple names to allow easier reuse of created schemas or
19: support for multi-language-environments.
20: @see MultiLanguageNamed
21: */
22: public class MultiLanguageName implements java.io.Serializable {
23: // Don't forget to increase the serialVersionUID when you change the fields
24: private static final long serialVersionUID = 1;
25: private boolean readOnly;
26: TreeMap mapLocales = new TreeMap();
27:
28: public MultiLanguageName(String language, String translation) {
29: setName(language, translation);
30: }
31:
32: public MultiLanguageName(String[][] data) {
33: for (int i = 0; i < data.length; i++)
34: setName(data[i][0], data[i][1]);
35: }
36:
37: public MultiLanguageName() {
38: }
39:
40: public void setReadOnly(boolean readOnly) {
41: this .readOnly = readOnly;
42: }
43:
44: public boolean isReadOnly() {
45: return this .readOnly;
46: }
47:
48: public String getName(String language) {
49: String result = (String) mapLocales.get(language);
50: if (result == null) {
51: result = (String) mapLocales.get("en");
52: }
53:
54: if (result == null) {
55: Iterator it = mapLocales.values().iterator();
56: if (it.hasNext())
57: result = (String) it.next();
58: }
59: if (result == null) {
60: result = "";
61: }
62: return result;
63: }
64:
65: public void setName(String language, String translation) {
66: checkWritable();
67: if (translation != null && !translation.trim().equals("")) {
68: mapLocales.put(language, translation);
69: } else {
70: mapLocales.remove(language);
71: }
72: }
73:
74: private void checkWritable() {
75: if (isReadOnly())
76: throw new ReadOnlyException(
77: "Can't modify this multilanguage name.");
78: }
79:
80: public void setTo(MultiLanguageName newName) {
81: checkWritable();
82: mapLocales = (TreeMap) newName.mapLocales.clone();
83: }
84:
85: public Collection getAvailableLanguages() {
86: return mapLocales.keySet();
87: }
88:
89: public Object clone() {
90: MultiLanguageName newName = new MultiLanguageName();
91: newName.mapLocales.putAll(mapLocales);
92: return newName;
93: }
94:
95: public String toString() {
96: return getName("en");
97: }
98: }
|