KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2007 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 org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import org.springframework.context.ApplicationContext;
23 import org.springframework.ui.context.HierarchicalThemeSource;
24 import org.springframework.ui.context.ThemeSource;
25
26 /**
27  * Utility class for UI application context implementations.
28  * Provides support for a special bean named "themeSource",
29  * of type {@link org.springframework.ui.context.ThemeSource}.
30  *
31  * @author Jean-Pierre Pawlak
32  * @author Juergen Hoeller
33  * @since 17.06.2003
34  */

35 public abstract class UiApplicationContextUtils {
36
37     /**
38      * Name of the ThemeSource bean in the factory.
39      * If none is supplied, theme resolution is delegated to the parent.
40      * @see org.springframework.ui.context.ThemeSource
41      */

42     public static final String JavaDoc THEME_SOURCE_BEAN_NAME = "themeSource";
43
44
45     private static final Log logger = LogFactory.getLog(UiApplicationContextUtils.class);
46
47
48     /**
49      * Initialize the ThemeSource for the given application context,
50      * autodetecting a bean with the name "themeSource". If no such
51      * bean is found, a default (empty) ThemeSource will be used.
52      * @param context current application context
53      * @return the initialized theme source (will never be <code>null</code>)
54      * @see #THEME_SOURCE_BEAN_NAME
55      */

56     public static ThemeSource initThemeSource(ApplicationContext context) {
57         if (context.containsLocalBean(THEME_SOURCE_BEAN_NAME)) {
58             ThemeSource themeSource = (ThemeSource) context.getBean(THEME_SOURCE_BEAN_NAME, ThemeSource.class);
59             // Make ThemeSource aware of parent ThemeSource.
60
if (context.getParent() instanceof ThemeSource && themeSource instanceof HierarchicalThemeSource) {
61                 HierarchicalThemeSource hts = (HierarchicalThemeSource) themeSource;
62                 if (hts.getParentThemeSource() == null) {
63                     // Only set parent context as parent ThemeSource if no parent ThemeSource
64
// registered already.
65
hts.setParentThemeSource((ThemeSource) context.getParent());
66                 }
67             }
68             if (logger.isDebugEnabled()) {
69                 logger.debug("Using ThemeSource [" + themeSource + "]");
70             }
71             return themeSource;
72         }
73         else {
74             // Use default ThemeSource to be able to accept getTheme calls, either
75
// delegating to parent context's default or to local ResourceBundleThemeSource.
76
HierarchicalThemeSource themeSource = null;
77             if (context.getParent() instanceof ThemeSource) {
78                 themeSource = new DelegatingThemeSource();
79                 themeSource.setParentThemeSource((ThemeSource) context.getParent());
80             }
81             else {
82                 themeSource = new ResourceBundleThemeSource();
83             }
84             if (logger.isDebugEnabled()) {
85                 logger.debug("Unable to locate ThemeSource with name '" + THEME_SOURCE_BEAN_NAME +
86                         "': using default [" + themeSource + "]");
87             }
88             return themeSource;
89         }
90     }
91
92 }
93
Popular Tags