KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > i18n > I18nText


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 /*
32  * Created on Nov 21, 2004
33  * by alex
34  */

35 package com.nightlabs.i18n;
36
37 import java.io.Serializable JavaDoc;
38 import java.util.Collection JavaDoc;
39 import java.util.Locale JavaDoc;
40 import java.util.Map JavaDoc;
41 import java.util.Set JavaDoc;
42
43 /**
44  * Abstract class to be used for multilangual String members. <br/>
45  * Subclass this, implement the methods and make the Map you return in
46  * {@link #getI18nMap()} persistence-modifier="persistent" and the member
47  * you return in {@link #getText()} persistence-modifier="transactional".
48  *
49  * @author Alexander Bieber <alex[AT]nightlabs[DOT]de>
50  */

51 public abstract class I18nText
52     implements Serializable JavaDoc, Localizable
53 {
54     
55     public static final String JavaDoc DEFAULT_LANGUAGEID = Locale.ENGLISH.getLanguage();
56     /**
57      * Implementors are responsible to
58      * return a non null Map with
59      * String languageIDs as keys and String values as entries
60      */

61     protected abstract Map JavaDoc getI18nMap();
62     /**
63      * Implementors are responsible to set the member
64      * clients access when localized.
65      * @param localizedValue
66      */

67     protected abstract void setText(String JavaDoc localizedValue);
68     /**
69      * Implementors are responsible to return the member
70      * clients access when localized and not null.<br/>
71      * Correct value is only set after a call to {@link #localize(String)}
72      */

73     public abstract String JavaDoc getText();
74     /**
75      * Implementors can return a String here used as
76      * fallback for the member when not even one value
77      * is set.
78      * @param languageID
79      */

80     protected abstract String JavaDoc getFallBackValue(String JavaDoc languageID);
81     
82     /**
83      * Checks if the value returned by {@link #getI18nMap()} is null
84      * and throws a {@link IllegalStateException} if so.
85      */

86     private Map JavaDoc getEnsuredMap() {
87         Map JavaDoc result = getI18nMap();
88         if (result == null)
89             throw new IllegalStateException JavaDoc("Subclasses of I18nText must not return null in implementations of getI18nMap()!");
90         return result;
91     }
92
93     /**
94      * Localizes the member.<br/>
95      * Checks if a value is set for the given languageID.<br/>
96      * If not tries for the {@link #DEFAULT_LANGUAGEID}.<br/>
97      * If that fails too checks if at least one value is set.<br/>
98      * Finally calls {@link #getFallBackValue(String)} for fallback.
99      * @param languageID
100      */

101     public void localize(String JavaDoc languageID)
102     {
103         localize(languageID, this);
104     }
105
106     /**
107      * This method localizes this instance of I18nText after it has been detached.
108      * It fetches its localized value from the given livingInstance (which is still
109      * connected to the datastore).
110      *
111      * @param languageID
112      * @param livingInstance
113      */

114     public void localize(String JavaDoc languageID, I18nText livingInstance)
115     {
116         Map JavaDoc map = livingInstance.getEnsuredMap();
117         String JavaDoc text = (String JavaDoc) map.get(languageID);
118         if (text == null)
119             text = (String JavaDoc) map.get(DEFAULT_LANGUAGEID);
120         if (text == null && !map.isEmpty())
121             text = (String JavaDoc) map.values().iterator().next();
122         if (text == null)
123             text = livingInstance.getFallBackValue(languageID);
124         setText(text);
125     }
126
127     /**
128      * Returns the String for the given languageID.
129      * If no value is found for the given language the one for
130      * {@link I18nText#DEFAULT_LANGUAGEID} is returned. If this
131      * doesn't exist and the Map is not empty, then just one arbitrary
132      * value is returned. Thus, the result might be null.
133      *
134      * @param languageID Language to find the String for
135      * @return String in the given language
136      */

137     public String JavaDoc getText(String JavaDoc languageID)
138     {
139         Map JavaDoc map = getEnsuredMap();
140         String JavaDoc result = (String JavaDoc)map.get(languageID);
141         if (result == null)
142             result = (String JavaDoc)map.get(DEFAULT_LANGUAGEID);
143         if (result == null && !map.isEmpty())
144             result = (String JavaDoc)map.values().iterator().next();
145         if (result == null)
146             result = getFallBackValue(languageID);
147         return result;
148     }
149
150     /**
151      * Sets the text for a given langugageID
152      * @param languageID The language the text should be set for.
153      * @param text The text to be set.
154      */

155     public void setText(String JavaDoc languageID, String JavaDoc text) {
156         if ("".equals(text) || text == null)
157             getEnsuredMap().remove(languageID);
158         else
159             getEnsuredMap().put(languageID, text);
160     }
161     
162     /**
163      * Returns true if no value is set for the text, false otherwise
164      */

165     public boolean isEmpty() {
166         return getEnsuredMap().isEmpty();
167     }
168
169     /**
170      * @return Returns instances of {@link Map.Entry} with <tt>key</tt> being the
171      * <tt>languageID</tt> and <tt>value</tt> being the text in the given language.
172      */

173     public Set JavaDoc getTexts()
174     {
175         return getEnsuredMap().entrySet();
176     }
177     
178     /**
179      *
180      * @return a {@link java.utilSet} of all languageIDs where localized Strings exists for
181      */

182     public Set JavaDoc getLanguageIDs() {
183         return getEnsuredMap().keySet();
184     }
185 }
186
Popular Tags