Example usage for org.springframework.ui.context ThemeSource getTheme

List of usage examples for org.springframework.ui.context ThemeSource getTheme

Introduction

In this page you can find the example usage for org.springframework.ui.context ThemeSource getTheme.

Prototype

@Nullable
Theme getTheme(String themeName);

Source Link

Document

Return the Theme instance for the given theme name.

Usage

From source file:org.dspace.webmvc.view.freemarker.SpringThemeAwareFreemarkerConfiguration.java

@Override
public Template getTemplate(String name, Locale locale, String encoding, boolean parse) throws IOException {

    String themePath = SpringThemeContextUtils.getProperty("theme.template.path");

    // If we have a theme path, attempt locating the template within the theme first
    if (themePath != null) {
        try {//from w ww  .j a  va2  s. c  o  m
            return super.getTemplate(themePath + name, locale, encoding, parse);
        } catch (IOException ioe) {
            // Will throw an exception if there is no template in the theme path,
            // we will ignore it, and try the standard path locations
        }
    }

    String themeParentName = SpringThemeContextUtils.getProperty("theme.parent", null);
    if (!StringUtils.isEmpty(themeParentName)) {
        RequestAttributes ra = RequestContextHolder.currentRequestAttributes();
        if (ra instanceof ServletRequestAttributes) {
            ServletRequestAttributes sra = (ServletRequestAttributes) ra;
            ThemeSource ts = RequestContextUtils.getThemeSource(sra.getRequest());

            if (ts != null) {
                Theme parentTheme = ts.getTheme(themeParentName);

                while (parentTheme != null) {
                    themePath = SpringThemeUtils.getProperty(parentTheme, "theme.template.path", locale, null);
                    if (themePath != null) {
                        try {
                            return super.getTemplate(themePath + name, locale, encoding, parse);
                        } catch (IOException ioe) {
                            // Will throw an exception if there is no template in the theme path,
                            // we will ignore it, and try the standard path locations
                        }
                    }

                    themeParentName = SpringThemeUtils.getProperty(parentTheme, "theme.parent", locale, null);
                    parentTheme = ts.getTheme(themeParentName);
                }

            }
        }
    }

    // No template in the theme path, so get a template from the standard location
    return super.getTemplate(name, locale, encoding, parse);
}