KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cintoo > messages > DefaultMessageHandler


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 package cintoo.messages;
17
18 import api.cintoo.messages.MessageHandler;
19 import api.cintoo.messages.bundle.BundleManager;
20 import api.cintoo.messages.context.Context;
21 import cintoo.messages.format.Formatter;
22 import cintoo.messages.locale.ThreadLocale;
23 import cintoo.messages.locale.LocaleManager;
24
25 import java.util.Locale JavaDoc;
26 import java.util.ResourceBundle JavaDoc;
27
28 /**
29  * Default implementation of message handler interface.
30  * Message handler formats texts with arguments.
31  * Texts are read from a resource bundle and retrieved
32  * by a key.
33  *
34  * @author Stephan J. Schmidt
35  * @version $id$
36  * @since 1.0
37  */

38 public class DefaultMessageHandler implements MessageHandler {
39   private BundleManager bundleManager;
40
41   private Formatter formatter;
42
43   /**
44    * Construct message handler with the given bundle manager.
45    *
46    * @param bundleManager bundle manager to use for bundle management
47    */

48   public DefaultMessageHandler(BundleManager bundleManager) {
49     super();
50     this.bundleManager = bundleManager;
51     formatter = new Formatter();
52   }
53
54   public void setBundleManager(BundleManager manager) {
55     this.bundleManager = manager;
56   }
57
58   public void clear() {
59     bundleManager.clear();
60   }
61
62   private LocaleManager localeManager() {
63     return LocaleManager.getManager();
64   }
65
66   /**
67    * Set the locale with the language and country for the
68    * current thread.
69    *
70    * @param language language code of the locale
71    */

72   public void setThreadLocale(String JavaDoc language) {
73     localeManager().setThreadLocale(new Locale(language));
74   }
75
76   /**
77    * Set the locale with the language and country for the
78    * current thread.
79    *
80    * @param language language code of the locale
81    * @param country country code of the locale
82    */

83   public void setThreadLocale(String JavaDoc language, String JavaDoc country) {
84     localeManager().setThreadLocale(new Locale(language, country));
85   }
86
87   /**
88    * Set the locale with the language and country for the
89    * current thread.
90    *
91    * @param language language code of the locale
92    * @param country country code of the locale
93    * @param variant variant code of the locale
94    */

95   public void setThreadLocale(String JavaDoc language, String JavaDoc country, String JavaDoc variant) {
96     localeManager().setThreadLocale(new Locale(language, country, variant));
97   }
98
99   /**
100    * Set the locale with the language and country for the
101    * current thread.
102    *
103    * @param locale locale to set
104    */

105   public void setThreadLocale(Locale locale) {
106     localeManager().setThreadLocale(locale);
107   }
108
109   /**
110    * Set globally the locale with the language and country
111    *
112    * @param language language code of the locale
113    */

114   public void setLocale(String JavaDoc language) {
115     localeManager().setLocale(new Locale(language));
116   }
117
118   /**
119    * Set globally the locale with the language and country
120    *
121    * @param language language code of the locale
122    * @param country country code of the locale
123    */

124   public void setLocale(String JavaDoc language, String JavaDoc country) {
125     localeManager().setLocale(new Locale(language, country));
126   }
127
128   /**
129    * Set globally the locale with the language and country
130    *
131    * @param language language code of the locale
132    * @param country country code of the locale
133    */

134   public void setLocale(String JavaDoc language, String JavaDoc country, String JavaDoc variant) {
135     localeManager().setLocale(new Locale(language, country, variant));
136   }
137
138   /**
139    * Set globally the locale with the language and country
140    *
141    * @param locale locale to set
142    */

143   public void setLocale(Locale locale) {
144     localeManager().setLocale(locale);
145   }
146
147   /**
148    * Return currently used locale, either for thread or global.
149    *
150    * @return currently used locale
151    */

152   public Locale getLocale() {
153     return localeManager().getLocale();
154   }
155
156   /**
157    * Set global bundle name
158    *
159    * @param bundleName bundle name for the bundle to be used
160    */

161   public void setBundle(String JavaDoc bundleName) {
162     bundleManager.setBundle(bundleName);
163   }
164
165   /**
166    * Set bundle for the package scope
167    *
168    * @param bundleName name of the bundle to set
169    * @param packageName package name which defines the scope
170    */

171   public void setBundle(String JavaDoc bundleName, String JavaDoc packageName) {
172     bundleManager.setBundle(bundleName, packageName);
173   }
174
175   /**
176    * Set bundle for the context scope
177    *
178    * @param bundleName name of the bundle to set
179    * @param context context wich defines the scope
180    */

181   public void setBundle(String JavaDoc bundleName, Context context) {
182     bundleManager.setBundle(bundleName, context);
183   }
184
185   public String JavaDoc format(Object JavaDoc context, String JavaDoc key) {
186     return formatter.format(getBundle(context), key);
187   }
188
189   public String JavaDoc format(String JavaDoc key) {
190     return formatter.format(getBundle(), key);
191   }
192
193   public String JavaDoc format(Object JavaDoc context, String JavaDoc key, Object JavaDoc arg) {
194     return formatter.format(getBundle(context), key, arg);
195   }
196
197   public String JavaDoc format(String JavaDoc key, Object JavaDoc arg) {
198     return formatter.format(getBundle(), key, arg);
199   }
200
201   public String JavaDoc format(Object JavaDoc context, String JavaDoc key, Object JavaDoc... args) {
202     return formatter.format(getBundle(context), key, args);
203   }
204
205   public String JavaDoc format(String JavaDoc key, Object JavaDoc... args) {
206     return formatter.format(getBundle(), key, args);
207   }
208
209   /**
210    * Return the full scope bundle
211    *
212    * @return bundle with the full scope
213    */

214   public ResourceBundle JavaDoc getBundle() {
215     return bundleManager.getBundle(getLocale());
216   }
217
218   /**
219    * Return the bundle for the context
220    *
221    * @param context context for which the bundle should be found
222    * @return corresponding bunlde
223    */

224   public ResourceBundle JavaDoc getBundle(Object JavaDoc context) {
225     return bundleManager.getBundle(context, getLocale());
226   }
227 }
228
Popular Tags