use of org.pentaho.platform.api.ui.ModuleThemeInfo in project pentaho-platform by pentaho.
the class ThemeServlet method handleRequest.
public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
// look for a passed in theme context (content generator, other named area)
String moduleName = req.getParameter("context");
OutputStream out = resp.getOutputStream();
// $NON-NLS-1$
resp.setContentType("text/javascript");
// $NON-NLS-1$
resp.setHeader("Cache-Control", "no-cache");
IUserSettingService settingsService = PentahoSystem.get(IUserSettingService.class, getPentahoSession(req));
// NOTE: this code should be kept in sync with that of PentahoWebContextFilter.java
String activeTheme = (String) getPentahoSession(req).getAttribute("pentaho-user-theme");
String ua = req.getHeader("User-Agent");
// check if we're coming from a mobile device, if so, lock to system default (ruby)
if (!StringUtils.isEmpty(ua) && ua.matches(".*(?i)(iPad|iPod|iPhone|Android).*")) {
activeTheme = PentahoSystem.getSystemSetting("default-theme", "ruby");
}
if (activeTheme == null) {
try {
activeTheme = settingsService.getUserSetting("pentaho-user-theme", null).getSettingValue();
} catch (Exception ignored) {
// the user settings service is not valid in the agile-bi deployment of the
// server
}
if (activeTheme == null) {
activeTheme = PentahoSystem.getSystemSetting("default-theme", "ruby");
}
}
out.write(("\n\n// Theming scripts. This file is generated by (" + getClass().getName() + ") and cannot be found on disk\n").getBytes());
out.write(("var active_theme = \"" + activeTheme + "\";\n\n").getBytes());
// Build-up JSON graph for system theme.
JSONObject root = new JSONObject();
JSONObject themeObject;
for (String systemThemeName : themeManager.getSystemThemeIds()) {
Theme theme = themeManager.getSystemTheme(systemThemeName);
themeObject = new JSONObject();
root.put(theme.getId(), themeObject);
themeObject.put("rootDir", theme.getThemeRootDir());
for (ThemeResource res : theme.getResources()) {
themeObject.append("resources", res.getLocation());
}
}
out.write(("var core_theme_tree = " + root.toString() + ";\n\n").getBytes());
out.write("// Inject the theme script to handle the insertion of requested theme resources\n\n".getBytes());
ModuleThemeInfo moduleThemeinfo = themeManager.getModuleThemeInfo(moduleName);
if (moduleThemeinfo != null) {
// Build-up JSON graph for module theme.
root = new JSONObject();
for (Theme theme : moduleThemeinfo.getModuleThemes()) {
themeObject = new JSONObject();
root.put(theme.getName(), themeObject);
themeObject.put("rootDir", theme.getThemeRootDir());
for (ThemeResource res : theme.getResources()) {
themeObject.append("resources", res.getLocation());
}
}
out.write(("var module_theme_tree = " + root.toString() + ";\n\n").getBytes());
}
// createElement & insertBefore
out.write(("(function() {\n" + "var script = document.createElement('script');\n" + "script.type = 'text/javascript';\n" + // "script.async = false;\n" +
"script.src = CONTEXT_PATH + 'js/themeResources.js';\n" + "var existing = document.getElementsByTagName('script')[0];\n" + "existing.parentNode.insertBefore(script, existing);\n" + "}());").getBytes());
} catch (IOException e) {
logger.debug("IO exception creating Theme info", e);
throw new ServletException(e);
} catch (JSONException e) {
logger.debug("JSON exception creating Theme info", e);
throw new ServletException(e);
}
}
use of org.pentaho.platform.api.ui.ModuleThemeInfo in project pentaho-platform by pentaho.
the class DefaultThemeManager method collectAllModuleThemes.
public Map<String, ModuleThemeInfo> collectAllModuleThemes() {
Map<String, ModuleThemeInfo> moduleThemes = new HashMap<String, ModuleThemeInfo>();
for (IThemeResolver resolver : resolvers) {
Map<String, ModuleThemeInfo> moduleInfo = resolver.getModuleThemes();
for (String moduleName : moduleInfo.keySet()) {
// populate the cache
cache.putInRegionCache(THEME_CACHE_REGION, MODULE_THEMES + "-" + moduleName, moduleInfo.get(moduleName));
}
moduleThemes.putAll(moduleInfo);
}
return moduleThemes;
}
use of org.pentaho.platform.api.ui.ModuleThemeInfo in project pentaho-platform by pentaho.
the class PluginThemeResolver method findPluginThemes.
private void findPluginThemes(String pluginId) {
try {
InputStream pluginManifestStream = resLoader.getResourceAsStream(pluginManager.getClassLoader(pluginId), "themes.xml");
if (pluginManifestStream == null) {
return;
}
ResourceBundle resourceBundle = null;
try {
resourceBundle = ResourceBundle.getBundle("themes", LocaleHelper.getDefaultLocale(), pluginManager.getClassLoader(pluginId));
} catch (Exception ignored) {
/* optional bundle */
}
SAXReader rdr = XMLParserFactoryProducer.getSAXReader(null);
Document pluginManifest = rdr.read(pluginManifestStream);
String rootThemeFolder = pluginManifest.getRootElement().attributeValue("root-folder");
// Plugins supplying styles whether local or system must have a root_theme_folder
if (rootThemeFolder == null) {
return;
}
// Things look good. Build-up theme information for this plugin.
ModuleThemeInfo moduleThemeInfo = new ModuleThemeInfo(pluginId);
List<Element> pluginNodes = pluginManifest.getRootElement().elements();
for (Element themeNode : pluginNodes) {
String themeId = themeNode.getName();
String themeName = StringUtils.defaultIfEmpty(themeNode.attributeValue("display-name"), themeId);
if (themeName.startsWith("${")) {
themeName = resourceBundle.getString(themeName);
}
Theme theme = new Theme(themeId, themeName, "content/" + pluginId + "/" + rootThemeFolder + "/" + themeId + "/");
theme.setHidden("true".equals(themeNode.attributeValue("hidden")));
if ("true".equals(themeNode.attributeValue("system"))) {
moduleThemeInfo.getSystemThemes().add(theme);
} else {
moduleThemeInfo.getModuleThemes().add(theme);
}
List<Element> resourceList = themeNode.elements();
for (Element res : resourceList) {
theme.addResource(new ThemeResource(theme, res.getText()));
}
}
moduleThemes.put(pluginId, moduleThemeInfo);
} catch (Exception e) {
logger.debug("Error parsing plugin themes", e);
}
}
use of org.pentaho.platform.api.ui.ModuleThemeInfo in project pentaho-platform by pentaho.
the class PluginThemeResolver method getSystemThemes.
public Map<String, Theme> getSystemThemes() {
Map<String, Theme> systemThemes = new HashMap<String, Theme>();
// Find the declared default plugin for themes. Add it's themes to the map first. Any that come in later will
// overwrite it.
String defaultThemePlugin = PentahoSystem.getSystemSetting("default-theme-plugin", "userconsole");
ModuleThemeInfo defaultThemePluginInfo = moduleThemes.get(defaultThemePlugin);
if (defaultThemePluginInfo == null) {
logger.debug("Unable to find the default theme plugin: " + defaultThemePlugin);
} else {
// Add all system themes from the default plugin in now.
for (Theme theme : defaultThemePluginInfo.getSystemThemes()) {
systemThemes.put(theme.getId(), theme);
}
}
// theme plugin will override the default.
for (String module : moduleThemes.keySet()) {
if (module.equals(defaultThemePlugin)) {
continue;
}
for (Theme theme : moduleThemes.get(module).getSystemThemes()) {
systemThemes.put(theme.getId(), theme);
}
}
return systemThemes;
}
use of org.pentaho.platform.api.ui.ModuleThemeInfo in project pentaho-platform by pentaho.
the class DefaultThemeManager method getModuleTheme.
public Theme getModuleTheme(String moduleName, String themeId) {
if (themeId == null) {
return null;
}
Theme theme = null;
ModuleThemeInfo moduleThemeInfo = (ModuleThemeInfo) cache.getFromRegionCache(THEME_CACHE_REGION, MODULE_THEMES + "-" + moduleName);
if (moduleThemeInfo == null) {
// may have been flushed, try to fetch it
moduleThemeInfo = collectAllModuleThemes().get(moduleName);
if (moduleThemeInfo == null) {
logger.debug("Unable to retrieve module theme for (" + moduleName + ") as the module theme definition was not found");
return null;
}
}
for (Theme t : moduleThemeInfo.getModuleThemes()) {
if (t.getId().equals(themeId)) {
theme = t;
break;
}
}
if (theme == null) {
logger.error(MessageFormat.format("Unable to find requested module theme: %s module: %s", themeId, moduleName));
}
return theme;
}
Aggregations