1. /*
  2. * Copyright 2002-2004 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. package org.springframework.ui.context.support;
  17. import java.util.HashMap;
  18. import java.util.Iterator;
  19. import java.util.Map;
  20. import org.apache.commons.logging.Log;
  21. import org.apache.commons.logging.LogFactory;
  22. import org.springframework.context.support.ResourceBundleMessageSource;
  23. import org.springframework.ui.context.HierarchicalThemeSource;
  24. import org.springframework.ui.context.Theme;
  25. import org.springframework.ui.context.ThemeSource;
  26. /**
  27. * ThemeSource implementation that looks up an individual ResourceBundle
  28. * per theme. The theme name gets interpreted as ResourceBundle basename,
  29. * supporting a common basename prefix for all themes.
  30. * @author Jean-Pierre Pawlak
  31. * @author Juergen Hoeller
  32. * @see #setBasenamePrefix
  33. */
  34. public class ResourceBundleThemeSource implements HierarchicalThemeSource {
  35. protected final Log logger = LogFactory.getLog(getClass());
  36. private ThemeSource parentThemeSource;
  37. private String basenamePrefix = "";
  38. /** Map from theme name to Theme instance */
  39. private Map themes = new HashMap();
  40. public void setParentThemeSource(ThemeSource parent) {
  41. this.parentThemeSource = parent;
  42. Iterator it = this.themes.values().iterator();
  43. while (it.hasNext()) {
  44. initParent((Theme) it.next());
  45. }
  46. }
  47. public ThemeSource getParentThemeSource() {
  48. return parentThemeSource;
  49. }
  50. /**
  51. * Set the prefix that gets applied to the ResourceBundle basenames,
  52. * i.e. the theme names.
  53. * E.g.: basenamePrefix="test.", themeName="theme" -> basename="test.theme".
  54. * @param basenamePrefix prefix for ResourceBundle basenames
  55. */
  56. public void setBasenamePrefix(String basenamePrefix) {
  57. this.basenamePrefix = (basenamePrefix != null) ? basenamePrefix : "";
  58. }
  59. public Theme getTheme(String themeName) {
  60. if (themeName == null) {
  61. return null;
  62. }
  63. Theme theme = (Theme) this.themes.get(themeName);
  64. if (theme == null) {
  65. ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
  66. logger.info("Theme created: name=" + themeName + ", baseName=" + this.basenamePrefix + themeName);
  67. messageSource.setBasename(this.basenamePrefix + themeName);
  68. theme = new SimpleTheme(themeName, messageSource);
  69. initParent(theme);
  70. this.themes.put(themeName, theme);
  71. }
  72. return theme;
  73. }
  74. /**
  75. * Initialize the MessageSource of the given theme with the
  76. * one from the respective parentThemeSource of this ThemeSource.
  77. */
  78. protected void initParent(Theme theme) {
  79. ResourceBundleMessageSource messageSource = (ResourceBundleMessageSource) theme.getMessageSource();
  80. if (this.parentThemeSource != null) {
  81. Theme parentTheme = this.parentThemeSource.getTheme(theme.getName());
  82. if (parentTheme != null) {
  83. messageSource.setParentMessageSource(parentTheme.getMessageSource());
  84. }
  85. }
  86. else {
  87. messageSource.setParentMessageSource(null);
  88. }
  89. }
  90. }