1 16 17 package org.springframework.ui.context.support; 18 19 import java.util.Collections ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 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 45 public class ResourceBundleThemeSource implements HierarchicalThemeSource { 46 47 protected final Log logger = LogFactory.getLog(getClass()); 48 49 private ThemeSource parentThemeSource; 50 51 private String basenamePrefix = ""; 52 53 54 private Map themeMap = Collections.synchronizedMap(new HashMap ()); 55 56 57 public void setParentThemeSource(ThemeSource parent) { 58 this.parentThemeSource = parent; 59 60 Iterator 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 79 public void setBasenamePrefix(String basenamePrefix) { 80 this.basenamePrefix = (basenamePrefix != null ? basenamePrefix : ""); 81 } 82 83 84 93 public Theme getTheme(String themeName) { 94 if (themeName == null) { 95 return null; 96 } 97 Theme theme = (Theme) this.themeMap.get(themeName); 98 if (theme == null) { 99 String 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 122 protected MessageSource createMessageSource(String basename) { 123 ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); 124 messageSource.setBasename(basename); 125 return messageSource; 126 } 127 128 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 |