001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.ui.context.support; 018 019import org.apache.commons.logging.Log; 020import org.apache.commons.logging.LogFactory; 021 022import org.springframework.context.ApplicationContext; 023import org.springframework.ui.context.HierarchicalThemeSource; 024import org.springframework.ui.context.ThemeSource; 025 026/** 027 * Utility class for UI application context implementations. 028 * Provides support for a special bean named "themeSource", 029 * of type {@link org.springframework.ui.context.ThemeSource}. 030 * 031 * @author Jean-Pierre Pawlak 032 * @author Juergen Hoeller 033 * @since 17.06.2003 034 */ 035public abstract class UiApplicationContextUtils { 036 037 /** 038 * Name of the ThemeSource bean in the factory. 039 * If none is supplied, theme resolution is delegated to the parent. 040 * @see org.springframework.ui.context.ThemeSource 041 */ 042 public static final String THEME_SOURCE_BEAN_NAME = "themeSource"; 043 044 045 private static final Log logger = LogFactory.getLog(UiApplicationContextUtils.class); 046 047 048 /** 049 * Initialize the ThemeSource for the given application context, 050 * autodetecting a bean with the name "themeSource". If no such 051 * bean is found, a default (empty) ThemeSource will be used. 052 * @param context current application context 053 * @return the initialized theme source (will never be {@code null}) 054 * @see #THEME_SOURCE_BEAN_NAME 055 */ 056 public static ThemeSource initThemeSource(ApplicationContext context) { 057 if (context.containsLocalBean(THEME_SOURCE_BEAN_NAME)) { 058 ThemeSource themeSource = context.getBean(THEME_SOURCE_BEAN_NAME, ThemeSource.class); 059 // Make ThemeSource aware of parent ThemeSource. 060 if (context.getParent() instanceof ThemeSource && themeSource instanceof HierarchicalThemeSource) { 061 HierarchicalThemeSource hts = (HierarchicalThemeSource) themeSource; 062 if (hts.getParentThemeSource() == null) { 063 // Only set parent context as parent ThemeSource if no parent ThemeSource 064 // registered already. 065 hts.setParentThemeSource((ThemeSource) context.getParent()); 066 } 067 } 068 if (logger.isDebugEnabled()) { 069 logger.debug("Using ThemeSource [" + themeSource + "]"); 070 } 071 return themeSource; 072 } 073 else { 074 // Use default ThemeSource to be able to accept getTheme calls, either 075 // delegating to parent context's default or to local ResourceBundleThemeSource. 076 HierarchicalThemeSource themeSource = null; 077 if (context.getParent() instanceof ThemeSource) { 078 themeSource = new DelegatingThemeSource(); 079 themeSource.setParentThemeSource((ThemeSource) context.getParent()); 080 } 081 else { 082 themeSource = new ResourceBundleThemeSource(); 083 } 084 if (logger.isDebugEnabled()) { 085 logger.debug("Unable to locate ThemeSource with name '" + THEME_SOURCE_BEAN_NAME + 086 "': using default [" + themeSource + "]"); 087 } 088 return themeSource; 089 } 090 } 091 092}