KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > i18n > LocalizableSupport


1 /*
2
3    Copyright 2000-2001 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.i18n;
19
20 import java.text.MessageFormat JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.ResourceBundle JavaDoc;
23
24 /**
25  * This class provides a default implementation of the Localizable interface.
26  * You can use it as a base class or as a member field and delegates various
27  * work to it.<p>
28  * For example, to implement Localizable, the following code can be used:
29  * <pre>
30  * package mypackage;
31  * ...
32  * public class MyClass implements Localizable {
33  * // This code fragment requires a file named
34  * // 'mypackage/resources/Messages.properties', or a
35  * // 'mypackage.resources.Messages' class which extends
36  * // java.util.ResourceBundle, accessible using the current
37  * // classpath.
38  * LocalizableSupport localizableSupport =
39  * new LocalizableSupport("mypackage.resources.Messages");
40  *
41  * public void setLocale(Locale l) {
42  * localizableSupport.setLocale(l);
43  * }
44  * public Local getLocale() {
45  * return localizableSupport.getLocale();
46  * }
47  * public String formatMessage(String key, Object[] args) {
48  * return localizableSupport.formatMessage(key, args);
49  * }
50  * }
51  * </pre>
52  * The algorithm for the Locale lookup in a LocalizableSupport object is:
53  * <ul>
54  * <li>
55  * if a Locale has been set by a call to setLocale(), use this Locale,
56  * else,
57  * <li/>
58  * <li>
59  * if a Locale has been set by a call to the setDefaultLocale() method
60  * of a LocalizableSupport object in the current LocaleGroup, use this
61  * Locale, else,
62  * </li>
63  * <li>
64  * use the object returned by Locale.getDefault() (and set by
65  * Locale.setDefault()).
66  * <li/>
67  * </ul>
68  * This offers the possibility to have a different Locale for each object,
69  * a Locale for a group of object and/or a Locale for the JVM instance.
70  * <p>
71  * Note: if no group is specified a LocalizableSupport object belongs to a
72  * default group common to each instance of LocalizableSupport.
73  *
74  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
75  * @version $Id: LocalizableSupport.java,v 1.7 2004/08/18 07:14:45 vhardy Exp $
76  */

77 public class LocalizableSupport implements Localizable {
78     /**
79      * The locale group to which this object belongs.
80      */

81     protected LocaleGroup localeGroup = LocaleGroup.DEFAULT;
82
83     /**
84      * The resource bundle classname.
85      */

86     protected String JavaDoc bundleName;
87
88     /**
89      * The classloader to use to create the resource bundle.
90      */

91     protected ClassLoader JavaDoc classLoader;
92
93     /**
94      * The current locale.
95      */

96     protected Locale JavaDoc locale;
97
98     /**
99      * The locale in use.
100      */

101     protected Locale JavaDoc usedLocale;
102
103     /**
104      * The resources
105      */

106     protected ResourceBundle JavaDoc resourceBundle;
107
108     /**
109      * Same as LocalizableSupport(s, null).
110      */

111     public LocalizableSupport(String JavaDoc s) {
112         this(s, null);
113     }
114
115     /**
116      * Creates a new Localizable object.
117      * The resource bundle class name is required allows the use of custom
118      * classes of resource bundles.
119      * @param s must be the name of the class to use to get the appropriate
120      * resource bundle given the current locale.
121      * @param cl is the classloader used to create the resource bundle,
122      * or null.
123      * @see java.util.ResourceBundle
124      */

125     public LocalizableSupport(String JavaDoc s, ClassLoader JavaDoc cl) {
126         bundleName = s;
127         classLoader = cl;
128     }
129
130     /**
131      * Implements {@link org.apache.batik.i18n.Localizable#setLocale(Locale)}.
132      */

133     public void setLocale(Locale JavaDoc l) {
134         if (locale != l) {
135             locale = l;
136             resourceBundle = null;
137         }
138     }
139
140     /**
141      * Implements {@link org.apache.batik.i18n.Localizable#getLocale()}.
142      */

143     public Locale JavaDoc getLocale() {
144         return locale;
145     }
146
147     /**
148      * Implements {@link
149      * org.apache.batik.i18n.ExtendedLocalizable#setLocaleGroup(LocaleGroup)}.
150      */

151     public void setLocaleGroup(LocaleGroup lg) {
152         localeGroup = lg;
153     }
154
155     /**
156      * Implements {@link
157      * org.apache.batik.i18n.ExtendedLocalizable#getLocaleGroup()}.
158      */

159     public LocaleGroup getLocaleGroup() {
160         return localeGroup;
161     }
162
163     /**
164      * Implements {@link
165      * org.apache.batik.i18n.ExtendedLocalizable#setDefaultLocale(Locale)}.
166      * Later invocations of the instance methods will lead to update the
167      * resource bundle used.
168      */

169     public void setDefaultLocale(Locale JavaDoc l) {
170         localeGroup.setLocale(l);
171     }
172
173     /**
174      * Implements {@link
175      * org.apache.batik.i18n.ExtendedLocalizable#getDefaultLocale()}.
176      */

177     public Locale JavaDoc getDefaultLocale() {
178         return localeGroup.getLocale();
179     }
180
181     /**
182      * Implements {@link
183      * org.apache.batik.i18n.Localizable#formatMessage(String,Object[])}.
184      */

185     public String JavaDoc formatMessage(String JavaDoc key, Object JavaDoc[] args) {
186         getResourceBundle();
187         return MessageFormat.format(resourceBundle.getString(key), args);
188     }
189
190     /**
191      * Implements {@link
192      * org.apache.batik.i18n.ExtendedLocalizable#getResourceBundle()}.
193      */

194     public ResourceBundle JavaDoc getResourceBundle() {
195         Locale JavaDoc l;
196
197         if (resourceBundle == null) {
198             if (locale == null) {
199                 if ((l = localeGroup.getLocale()) == null) {
200                     usedLocale = Locale.getDefault();
201                 } else {
202                     usedLocale = l;
203                 }
204             } else {
205                 usedLocale = locale;
206             }
207             if (classLoader == null) {
208                 resourceBundle = ResourceBundle.getBundle(bundleName,
209                                                           usedLocale);
210             } else {
211                 resourceBundle = ResourceBundle.getBundle(bundleName,
212                                                           usedLocale,
213                                                           classLoader);
214             }
215         } else if (locale == null) {
216             // Check for group Locale and JVM default locale changes.
217
if ((l = localeGroup.getLocale()) == null) {
218                 if (usedLocale != (l = Locale.getDefault())) {
219                     usedLocale = l;
220                     if (classLoader == null) {
221                         resourceBundle = ResourceBundle.getBundle(bundleName,
222                                                                   usedLocale);
223                     } else {
224                         resourceBundle = ResourceBundle.getBundle(bundleName,
225                                                                   usedLocale,
226                                                                   classLoader);
227                     }
228                 }
229             } else if (usedLocale != l) {
230                 usedLocale = l;
231                 if (classLoader == null) {
232                     resourceBundle = ResourceBundle.getBundle(bundleName,
233                                                               usedLocale);
234                 } else {
235                     resourceBundle = ResourceBundle.getBundle(bundleName,
236                                                               usedLocale,
237                                                               classLoader);
238                 }
239             }
240         }
241
242         return resourceBundle;
243     }
244 }
245
Popular Tags