KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > api > cintoo > messages > Messages


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

16
17 package api.cintoo.messages;
18
19 import api.cintoo.messages.bundle.BundleManager;
20 import api.cintoo.messages.context.Context;
21 import cintoo.messages.DefaultMessageHandler;
22 import cintoo.messages.bundle.DefaultBundleManager;
23 import cintoo.messages.context.DefaultContextCache;
24
25 import java.util.Locale JavaDoc;
26 import java.util.ResourceBundle JavaDoc;
27
28 /**
29  * Messages is a frontend to a message handler which
30  * uses a message handler singleton and provides
31  * static methods for easier access to a message handler
32  *
33  * @author Stephan J. Schmidt
34  * @version $id$
35  * @since 1.0
36  */

37 public class Messages {
38
39   private static MessageHandler messageHandler = new DefaultMessageHandler(new DefaultBundleManager());
40   /**
41    * Set the locale with the language and country for the
42    * current thread.
43    *
44    * @param locale locale to set
45    */

46   public static void setThreadLocale(Locale JavaDoc locale) {
47     handler().setThreadLocale(locale);
48   }
49
50   /**
51    * Set the locale with the language and country for the
52    * current thread.
53    *
54    * @param language language code of the locale
55    */

56   public static void setThreadLocale(String JavaDoc language) {
57     handler().setThreadLocale(language);
58   }
59
60   /**
61    * Set the locale with the language and country for the
62    * current thread.
63    *
64    * @param language language code of the locale
65    * @param country country code of the locale
66    */

67   public static void setThreadLocale(String JavaDoc language, String JavaDoc country) {
68     handler().setThreadLocale(language, country);
69   }
70
71   /**
72    * Set the locale with the language and country for the
73    * current thread.
74    *
75    * @param language language code of the locale
76    * @param country country code of the locale
77    * @param variant variant code of the locale
78    */

79   public static void setThreadLocale(String JavaDoc language, String JavaDoc country, String JavaDoc variant) {
80     handler().setThreadLocale(language, country, variant);
81   }
82
83   /**
84    * Set globally the locale with the language and country
85    *
86    * @param language language code of the locale
87    */

88   public static void setLocale(String JavaDoc language) {
89     handler().setLocale(language);
90   }
91
92   /**
93    * Set globally the locale with the language and country
94    *
95    * @param language language code of the locale
96    * @param country country code of the locale
97    */

98   public static void setLocale(String JavaDoc language, String JavaDoc country) {
99     handler().setLocale(language, country);
100   }
101
102     /**
103    * Set globally the locale with the language and country
104    *
105    * @param language language code of the locale
106    * @param country country code of the locale
107    */

108   public static void setLocale(String JavaDoc language, String JavaDoc country, String JavaDoc variant) {
109     handler().setLocale(language, country, variant);
110   }
111
112   /**
113    * Set globally the locale with the language and country
114    *
115    * @param locale locale to se
116    */

117   public static void setLocale(Locale JavaDoc locale) {
118     handler().setLocale(locale);
119   }
120
121   /**
122    * Return currently used locale, either for thread or global.
123    *
124    * @return currently used locale
125    */

126   public static Locale JavaDoc getLocale() {
127     return handler().getLocale();
128   }
129
130   /**
131    * Set global bundle name
132    *
133    * @param bundleName bundle name for the bundle to be used
134    */

135   public static void setBundle(String JavaDoc bundleName) {
136     handler().setBundle(bundleName);
137   }
138
139   /**
140    * Set bundle for the package scope
141    *
142    * @param bundleName name of the bundle to set
143    * @param packageName package name which defines the scope
144    */

145   public static void setBundle(String JavaDoc bundleName, String JavaDoc packageName) {
146     handler().setBundle(bundleName, packageName);
147   }
148
149   /**
150    * Set bundle for the context scope
151    *
152    * @param bundleName name of the bundle to set
153    * @param context context wich defines the scope
154    */

155   public static void setBundle(String JavaDoc bundleName, Context context) {
156     handler().setBundle(bundleName, context);
157   }
158
159   /**
160    * Format a message
161    *
162    * @param context context wich defines the scope
163    * @param key key for the message
164    * @return formatted message
165    */

166   public static String JavaDoc format(Object JavaDoc context, String JavaDoc key) {
167     return handler().format(context, key);
168   }
169
170   /**
171    * Format a message
172    *
173    * @param context context wich defines the scope
174    * @param key key for the message
175    * @return formatted message
176    */

177   public static String JavaDoc _(Object JavaDoc context, String JavaDoc key) {
178     return handler().format(context, key);
179   }
180
181   /**
182    * Format a message
183    *
184    * @param context context wich defines the scope
185    * @param key key for the message
186    * @return formatted message
187    */

188   public static String JavaDoc $(Object JavaDoc context, String JavaDoc key) {
189     return handler().format(context, key);
190   }
191
192   /**
193    * Format a message with the top level context
194    *
195    * @param key key for the message
196    * @param arg argument to use in message
197    * @return formatted message
198    */

199   public static String JavaDoc format(String JavaDoc key, Object JavaDoc arg) {
200     return handler().format(key, arg);
201   }
202
203   /**
204    * Format a message with the top level context
205    *
206    * @param key key for the message
207    * @param arg argument to use in message
208    * @return formatted message
209    */

210   public static String JavaDoc _(String JavaDoc key, Object JavaDoc arg) {
211     return handler().format(key, arg);
212   }
213
214   /**
215    * Format a message with the top level context
216    *
217    * @param key key for the message
218    * @param arg argument to use in message
219    * @return formatted message
220    */

221   public static String JavaDoc $(String JavaDoc key, Object JavaDoc arg) {
222     return handler().format(key, arg);
223   }
224
225   /**
226    * Format a message with the top level context
227    *
228    * @param key key for the message
229    * @param args arguments to use in message
230    * @return formatted message
231    */

232   public static String JavaDoc format(String JavaDoc key, Object JavaDoc... args) {
233     return handler().format(key, args);
234   }
235
236   /**
237    * Format a message with the top level context
238    *
239    * @param key key for the message
240    * @param args arguments to use in message
241    * @return formatted message
242    */

243   public static String JavaDoc _(String JavaDoc key, Object JavaDoc... args) {
244     return handler().format(key, args);
245   }
246
247   /**
248    * Format a message with the top level context
249    *
250    * @param key key for the message
251    * @param args arguments to use in message
252    * @return formatted message
253    */

254   public static String JavaDoc $(String JavaDoc key, Object JavaDoc... args) {
255     return handler().format(key, args);
256   }
257
258   /**
259    * Format a message with the top level context
260    *
261    * @param key key for the message
262    * @return formatted message
263    */

264   public static String JavaDoc format(String JavaDoc key) {
265     return handler().format(key);
266   }
267
268   /**
269    * Format a message with the top level context
270    *
271    * @param key key for the message
272    * @return formatted message
273    */

274   public static String JavaDoc _(String JavaDoc key) {
275     return handler().format(key);
276   }
277
278   /**
279    * Format a message with the top level context
280    *
281    * @param key key for the message
282    * @return formatted message
283    */

284   public static String JavaDoc $(String JavaDoc key) {
285     return handler().format(key);
286   }
287
288   /**
289    * Format a message
290    *
291    * @param context context wich defines the scope
292    * @param key key for the message
293    * @param arg argument to use in message
294    * @return formatted message
295    */

296   public static String JavaDoc format(Object JavaDoc context, String JavaDoc key, Object JavaDoc arg) {
297     return handler().format(context, key, arg);
298   }
299
300   /**
301    * Format a message
302    *
303    * @param context context wich defines the scope
304    * @param key key for the message
305    * @param arg argument to use in message
306    * @return formatted message
307    */

308   public static String JavaDoc _(Object JavaDoc context, String JavaDoc key, Object JavaDoc arg) {
309     return handler().format(context, key, arg);
310   }
311
312   /**
313    * Format a message
314    *
315    * @param context context wich defines the scope
316    * @param key key for the message
317    * @param arg argument to use in message
318    * @return formatted message
319    */

320   public static String JavaDoc $(Object JavaDoc context, String JavaDoc key, Object JavaDoc arg) {
321     return handler().format(context, key, arg);
322   }
323
324   /**
325    * Format a message
326    *
327    * @param context context wich defines the scope
328    * @param key key for the message
329    * @param args arguments to use in message
330    * @return formatted message
331    */

332   public static String JavaDoc format(Object JavaDoc context, String JavaDoc key, Object JavaDoc... args) {
333     return handler().format(context, key, args);
334   }
335
336   /**
337    * Format a message
338    *
339    * @param context context wich defines the scope
340    * @param key key for the message
341    * @param args arguments to use in message
342    * @return formatted message
343    */

344   public static String JavaDoc _(Object JavaDoc context, String JavaDoc key, Object JavaDoc... args) {
345     return handler().format(context, key, args);
346   }
347
348   /**
349    * Format a message
350    *
351    * @param context context wich defines the scope
352    * @param key key for the message
353    * @param args arguments to use in message
354    * @return formatted message
355    */

356   public static String JavaDoc $(Object JavaDoc context, String JavaDoc key, Object JavaDoc... args) {
357     return handler().format(context, key, args);
358   }
359
360   /**
361    * Return the full scope bundle
362    *
363    * @return bundle with the full scope
364    */

365   private static ResourceBundle JavaDoc getBundle() {
366     return handler().getBundle(getLocale());
367   }
368
369   /**
370    * Return the bundle for the context
371    *
372    * @param context context for which the bundle should be found
373    * @return corresponding bunlde
374    */

375   public static ResourceBundle JavaDoc getBundle(Object JavaDoc context) {
376     return handler().getBundle(context);
377   }
378
379   /**
380    * Create singleton for message handler
381    *
382    * @return message handler
383    */

384   private static MessageHandler handler() {
385     return messageHandler;
386   }
387
388   public static MessageHandler getHandler() {
389     return handler();
390   }
391
392   public static void clear() {
393     messageHandler.clear();
394   }
395 }
396
Popular Tags