KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > ui > context > support > ResourceBundleThemeSource


1 /*
2  * Copyright 2002-2005 the original author or authors.
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 org.springframework.ui.context.support;
18
19 import java.util.Collections JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import org.springframework.context.HierarchicalMessageSource;
28 import org.springframework.context.MessageSource;
29 import org.springframework.context.support.ResourceBundleMessageSource;
30 import org.springframework.ui.context.HierarchicalThemeSource;
31 import org.springframework.ui.context.Theme;
32 import org.springframework.ui.context.ThemeSource;
33
34 /**
35  * ThemeSource implementation that looks up an individual ResourceBundle
36  * per theme. The theme name gets interpreted as ResourceBundle basename,
37  * supporting a common basename prefix for all themes.
38  *
39  * @author Jean-Pierre Pawlak
40  * @author Juergen Hoeller
41  * @see #setBasenamePrefix
42  * @see java.util.ResourceBundle
43  * @see org.springframework.context.support.ResourceBundleMessageSource
44  */

45 public class ResourceBundleThemeSource implements HierarchicalThemeSource {
46
47     protected final Log logger = LogFactory.getLog(getClass());
48
49     private ThemeSource parentThemeSource;
50
51     private String JavaDoc basenamePrefix = "";
52
53     /** Map from theme name to Theme instance */
54     private Map JavaDoc themeMap = Collections.synchronizedMap(new HashMap JavaDoc());
55
56
57     public void setParentThemeSource(ThemeSource parent) {
58         this.parentThemeSource = parent;
59
60         // Update existing Theme objects.
61
// Usually there shouldn't be any at the time of this call.
62
Iterator JavaDoc it = this.themeMap.values().iterator();
63         while (it.hasNext()) {
64             initParent((Theme) it.next());
65         }
66     }
67
68     public ThemeSource getParentThemeSource() {
69         return parentThemeSource;
70     }
71
72     /**
73      * Set the prefix that gets applied to the ResourceBundle basenames,
74      * i.e. the theme names.
75      * E.g.: basenamePrefix="test.", themeName="theme" -> basename="test.theme".
76      * @param basenamePrefix prefix for ResourceBundle basenames
77      * @see java.util.ResourceBundle
78      */

79     public void setBasenamePrefix(String JavaDoc basenamePrefix) {
80         this.basenamePrefix = (basenamePrefix != null ? basenamePrefix : "");
81     }
82
83
84     /**
85      * This implementation returns a SimpleTheme instance, holding a
86      * ResourceBundle-based MessageSource whose basename corresponds to
87      * the given theme name (prefixed by the configured "basenamePrefix").
88      * <p>SimpleTheme instances are cached per theme name. Use a reloadable
89      * MessageSource if themes should reflect changes to the underlying files.
90      * @see #setBasenamePrefix
91      * @see #createMessageSource
92      */

93     public Theme getTheme(String JavaDoc themeName) {
94         if (themeName == null) {
95             return null;
96         }
97         Theme theme = (Theme) this.themeMap.get(themeName);
98         if (theme == null) {
99             String JavaDoc basename = this.basenamePrefix + themeName;
100             MessageSource messageSource = createMessageSource(basename);
101             theme = new SimpleTheme(themeName, messageSource);
102             initParent(theme);
103             this.themeMap.put(themeName, theme);
104             if (logger.isInfoEnabled()) {
105                 logger.info("Theme created: name '" + themeName + "', basename [" + basename + "]");
106             }
107         }
108         return theme;
109     }
110
111     /**
112      * Create a MessageSource for the given basename,
113      * to be used as MessageSource for the corresponding theme.
114      * <p>Default implementation creates a ResourceBundleMessageSource.
115      * for the given basename. A subclass could create a specifically
116      * configured ReloadableResourceBundleMessageSource, for example.
117      * @param basename the basename to create a MessageSource for
118      * @return the MessageSource
119      * @see org.springframework.context.support.ResourceBundleMessageSource
120      * @see org.springframework.context.support.ReloadableResourceBundleMessageSource
121      */

122     protected MessageSource createMessageSource(String JavaDoc basename) {
123         ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
124         messageSource.setBasename(basename);
125         return messageSource;
126     }
127
128     /**
129      * Initialize the MessageSource of the given theme with the
130      * one from the corresponding parent of this ThemeSource.
131      */

132     protected void initParent(Theme theme) {
133         if (theme.getMessageSource() instanceof HierarchicalMessageSource) {
134             HierarchicalMessageSource messageSource = (HierarchicalMessageSource) theme.getMessageSource();
135             if (getParentThemeSource() != null && messageSource.getParentMessageSource() == null) {
136                 Theme parentTheme = getParentThemeSource().getTheme(theme.getName());
137                 if (parentTheme != null) {
138                     messageSource.setParentMessageSource(parentTheme.getMessageSource());
139                 }
140             }
141         }
142     }
143
144 }
145
Popular Tags