01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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 THEME_SOURCE_BEAN_NAME = "themeSource";
43:
44: private static final Log logger = LogFactory
45: .getLog(UiApplicationContextUtils.class);
46:
47: /**
48: * Initialize the ThemeSource for the given application context,
49: * autodetecting a bean with the name "themeSource". If no such
50: * bean is found, a default (empty) ThemeSource will be used.
51: * @param context current application context
52: * @return the initialized theme source (will never be <code>null</code>)
53: * @see #THEME_SOURCE_BEAN_NAME
54: */
55: public static ThemeSource initThemeSource(ApplicationContext context) {
56: if (context.containsLocalBean(THEME_SOURCE_BEAN_NAME)) {
57: ThemeSource themeSource = (ThemeSource) context.getBean(
58: THEME_SOURCE_BEAN_NAME, ThemeSource.class);
59: // Make ThemeSource aware of parent ThemeSource.
60: if (context.getParent() instanceof ThemeSource
61: && themeSource instanceof HierarchicalThemeSource) {
62: HierarchicalThemeSource hts = (HierarchicalThemeSource) themeSource;
63: if (hts.getParentThemeSource() == null) {
64: // Only set parent context as parent ThemeSource if no parent ThemeSource
65: // registered already.
66: hts.setParentThemeSource((ThemeSource) context
67: .getParent());
68: }
69: }
70: if (logger.isDebugEnabled()) {
71: logger.debug("Using ThemeSource [" + themeSource + "]");
72: }
73: return themeSource;
74: } else {
75: // Use default ThemeSource to be able to accept getTheme calls, either
76: // delegating to parent context's default or to local ResourceBundleThemeSource.
77: HierarchicalThemeSource themeSource = null;
78: if (context.getParent() instanceof ThemeSource) {
79: themeSource = new DelegatingThemeSource();
80: themeSource.setParentThemeSource((ThemeSource) context
81: .getParent());
82: } else {
83: themeSource = new ResourceBundleThemeSource();
84: }
85: if (logger.isDebugEnabled()) {
86: logger.debug("Unable to locate ThemeSource with name '"
87: + THEME_SOURCE_BEAN_NAME + "': using default ["
88: + themeSource + "]");
89: }
90: return themeSource;
91: }
92: }
93:
94: }
|