KickJava   Java API By Example, From Geeks To Geeks.

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


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 Mar 4, 2005
33  */

34 package com.nightlabs.i18n;
35
36 import java.util.HashMap JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.Map.Entry;
40
41 /**
42  * This class is a simple implementation of <tt>I18nText</tt>. It is meant to be able
43  * to store temporarily i18n data when not yet having a real <tt>I18nText</tt> available
44  * (e.g. in a wizard before creating the real object).
45  * <p>
46  * Hence, this implementation has methods to <tt>load</tt> from and to <tt>store</tt> to
47  * another I18nText.
48  *
49  * @author Marco Schulze - marco at nightlabs dot de
50  */

51 public class I18nTextBuffer extends I18nText
52 {
53     /**
54      * key: String languageID<br/>
55      * value: String text
56      */

57     private Map JavaDoc texts = new HashMap JavaDoc();
58
59     /**
60      * @see com.nightlabs.i18n.I18nText#getI18nMap()
61      */

62     protected Map JavaDoc getI18nMap()
63     {
64         return texts;
65     }
66     
67     private String JavaDoc text;
68
69     /**
70      * @see com.nightlabs.i18n.I18nText#setText(java.lang.String)
71      */

72     protected void setText(String JavaDoc localizedValue)
73     {
74         text = localizedValue;
75     }
76
77     /**
78      * @see com.nightlabs.i18n.I18nText#getText()
79      */

80     public String JavaDoc getText()
81     {
82         return text;
83     }
84
85     /**
86      * @see com.nightlabs.i18n.I18nText#getFallBackValue(java.lang.String)
87      */

88     protected String JavaDoc getFallBackValue(String JavaDoc languageID)
89     {
90         return languageID;
91     }
92
93     /**
94      * This method stores all entries from this object into another
95      * <tt>I18nText</tt> and overwrites all previously stored data.
96      * <p>
97      * Note, that the fallbackValue is ignored by this method!
98      *
99      * @param other The <tt>I18nText</tt> instance into which to store the data.
100      */

101     public void store(I18nText other)
102     {
103         Map JavaDoc otherI18nMap = other.getI18nMap();
104         otherI18nMap.clear();
105         for (Iterator JavaDoc it = texts.entrySet().iterator(); it.hasNext(); ) {
106             Map.Entry JavaDoc me = (Entry) it.next();
107             Object JavaDoc key = me.getKey();
108             Object JavaDoc value = me.getValue();
109
110             if (value == null)
111                 continue;
112
113             otherI18nMap.put(key, value);
114         }
115     }
116
117     /**
118      * This method loads all entries from another <tt>I18nText</tt> and
119      * overwrites all data that has been stored previously in this object.
120      * <p>
121      * Note, that the fallbackValue is ignored by this method!
122      *
123      * @param other The <tt>I18nText</tt> instance from which to load the data.
124      */

125     public void load(I18nText other)
126     {
127         texts.clear();
128         for (Iterator JavaDoc it = other.getI18nMap().entrySet().iterator(); it.hasNext(); ) {
129             Map.Entry JavaDoc me = (Entry) it.next();
130             Object JavaDoc key = me.getKey();
131             Object JavaDoc value = me.getValue();
132             
133             if (value == null)
134                 continue;
135             
136             if (!(key instanceof String JavaDoc))
137                 throw new IllegalArgumentException JavaDoc("other's i18nMap contains a key which is an instance of " + key == null ? "null" : key.getClass().getName() + "! Must be String!");
138             if (!(value instanceof String JavaDoc))
139                 throw new IllegalArgumentException JavaDoc("other's i18nMap contains a value which is an instance of " + value.getClass().getName() + "! Must be String!");
140             
141             texts.put(key, value);
142         }
143     }
144 }
145
Popular Tags