X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms.ui%2Fsrc%2Forg%2Fargeo%2Fcms%2Futil%2FSimpleApp.java;h=b75c700078d7bff8a81840390a94f3a99c2c30da;hb=88a7a1d6ad9120a88afca54e0d63f2c7294dc25a;hp=39e75070b450c032cc3665b427925802b680b82d;hpb=972528f4de2d00690362c01d3ce843ca9cd10250;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms.ui/src/org/argeo/cms/util/SimpleApp.java b/org.argeo.cms.ui/src/org/argeo/cms/util/SimpleApp.java index 39e75070b..b75c70007 100644 --- a/org.argeo.cms.ui/src/org/argeo/cms/util/SimpleApp.java +++ b/org.argeo.cms.ui/src/org/argeo/cms/util/SimpleApp.java @@ -2,13 +2,17 @@ package org.argeo.cms.util; import java.io.IOException; import java.io.InputStream; +import java.net.URL; import java.util.ArrayList; import java.util.Arrays; +import java.util.Enumeration; import java.util.HashMap; +import java.util.HashSet; import java.util.Hashtable; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Set; import javax.jcr.Repository; import javax.jcr.RepositoryException; @@ -18,11 +22,12 @@ import javax.jcr.version.VersionManager; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.argeo.cms.CmsConstants; import org.argeo.cms.CmsException; -import org.argeo.cms.CmsUiProvider; -import org.argeo.cms.LifeCycleUiProvider; +import org.argeo.cms.ui.CmsConstants; +import org.argeo.cms.ui.CmsUiProvider; +import org.argeo.cms.ui.LifeCycleUiProvider; import org.argeo.jcr.JcrUtils; +import org.argeo.node.NodeConstants; import org.eclipse.rap.rwt.RWT; import org.eclipse.rap.rwt.application.Application; import org.eclipse.rap.rwt.application.Application.OperationMode; @@ -39,6 +44,7 @@ import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; +import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; @@ -58,8 +64,8 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration { private Repository repository; private String workspace = null; private String jcrBasePath = "/"; - private List roPrincipals = Arrays.asList("anonymous", "everyone"); - private List rwPrincipals = Arrays.asList("everyone"); + private List roPrincipals = Arrays.asList(NodeConstants.ROLE_ANONYMOUS, NodeConstants.ROLE_USER); + private List rwPrincipals = Arrays.asList(NodeConstants.ROLE_USER); private CmsUiProvider header; private Map pages = new LinkedHashMap(); @@ -70,10 +76,7 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration { public void configure(Application application) { try { - StyleSheetResourceLoader styleSheetRL = new StyleSheetResourceLoader( - bundleContext); - BundleResourceLoader bundleRL = new BundleResourceLoader( - bundleContext); + BundleResourceLoader bundleRL = new BundleResourceLoader(bundleContext.getBundle()); application.setOperationMode(OperationMode.SWT_COMPATIBILITY); // application.setOperationMode(OperationMode.JEE_COMPATIBILITY); @@ -81,8 +84,7 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration { application.setExceptionHandler(new CmsExceptionHandler()); // loading animated gif - application.addResource(LOADING_IMAGE, - createResourceLoader(LOADING_IMAGE)); + application.addResource(LOADING_IMAGE, createResourceLoader(LOADING_IMAGE)); // empty image application.addResource(NO_IMAGE, createResourceLoader(NO_IMAGE)); @@ -95,19 +97,22 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration { Map defaultBranding = null; if (branding.containsKey("*")) defaultBranding = branding.get("*"); + String defaultTheme = defaultBranding.get(WebClient.THEME_ID); // entry points for (String page : pages.keySet()) { - Map properties = defaultBranding != null ? new HashMap( - defaultBranding) : new HashMap(); + Map properties = defaultBranding != null ? new HashMap(defaultBranding) + : new HashMap(); if (branding.containsKey(page)) { properties.putAll(branding.get(page)); } // favicon if (properties.containsKey(WebClient.FAVICON)) { + String themeId = defaultBranding.get(WebClient.THEME_ID); + Bundle themeBundle = findThemeBundle(themeId); String faviconRelPath = properties.get(WebClient.FAVICON); application.addResource(faviconRelPath, - new BundleResourceLoader(bundleContext)); + new BundleResourceLoader(themeBundle != null ? themeBundle : bundleContext.getBundle())); if (log.isTraceEnabled()) log.trace("Favicon " + faviconRelPath); @@ -116,10 +121,7 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration { // page title if (!properties.containsKey(WebClient.PAGE_TITLE)) { if (page.length() > 0) - properties.put( - WebClient.PAGE_TITLE, - Character.toUpperCase(page.charAt(0)) - + page.substring(1)); + properties.put(WebClient.PAGE_TITLE, Character.toUpperCase(page.charAt(0)) + page.substring(1)); } // default body HTML @@ -129,14 +131,19 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration { // // ADD ENTRY POINT // - application.addEntryPoint("/" + page, new CmsEntryPointFactory( - pages.get(page), repository, workspace, properties), - properties); + application.addEntryPoint("/" + page, + new CmsEntryPointFactory(pages.get(page), repository, workspace, properties), properties); log.info("Page /" + page); } - // stylesheets + // stylesheets and themes + Set themeBundles = new HashSet<>(); for (String themeId : styleSheets.keySet()) { + Bundle themeBundle = findThemeBundle(themeId); + StyleSheetResourceLoader styleSheetRL = new StyleSheetResourceLoader( + themeBundle != null ? themeBundle : bundleContext.getBundle()); + if (themeBundle != null) + themeBundles.add(themeBundle); List cssLst = styleSheets.get(themeId); if (log.isDebugEnabled()) log.debug("Theme " + themeId); @@ -147,36 +154,74 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration { } } + for (Bundle themeBundle : themeBundles) { + BundleResourceLoader themeBRL = new BundleResourceLoader(themeBundle); + addThemeResources(application, themeBundle, themeBRL, "*.png"); + addThemeResources(application, themeBundle, themeBRL, "*.gif"); + addThemeResources(application, themeBundle, themeBRL, "*.jpg"); + } } catch (RuntimeException e) { // Easier access to initialisation errors - log.error("Unexpected exception when configuring RWT application.", - e); + log.error("Unexpected exception when configuring RWT application.", e); throw e; } } + private Bundle findThemeBundle(String themeId) { + if (themeId == null) + return null; + // TODO optimize + // TODO deal with multiple versions + Bundle themeBundle = null; + if (themeId != null) { + for (Bundle bundle : bundleContext.getBundles()) + if (themeId.equals(bundle.getSymbolicName())) { + themeBundle = bundle; + break; + } + } + return themeBundle; + } + + private void addThemeResources(Application application, Bundle themeBundle, BundleResourceLoader themeBRL, + String pattern) { + Enumeration themeResources = themeBundle.findEntries("/", pattern, true); + if (themeResources == null) + return; + while (themeResources.hasMoreElements()) { + String resource = themeResources.nextElement().getPath(); + // remove first '/' so that RWT registers it + resource = resource.substring(1); + if (!resource.endsWith("/")) { + application.addResource(resource, themeBRL); + if (log.isTraceEnabled()) + log.trace("Registered " + resource + " from theme " + themeBundle); + } + + } + + } + public void init() throws RepositoryException { Session session = null; try { session = JcrUtils.loginOrCreateWorkspace(repository, workspace); VersionManager vm = session.getWorkspace().getVersionManager(); - if (!vm.isCheckedOut("/")) - vm.checkout("/"); JcrUtils.mkdirs(session, jcrBasePath); + session.save(); + if (!vm.isCheckedOut(jcrBasePath)) + vm.checkout(jcrBasePath); for (String principal : rwPrincipals) - JcrUtils.addPrivilege(session, jcrBasePath, principal, - Privilege.JCR_WRITE); + JcrUtils.addPrivilege(session, jcrBasePath, principal, Privilege.JCR_WRITE); for (String principal : roPrincipals) - JcrUtils.addPrivilege(session, jcrBasePath, principal, - Privilege.JCR_READ); + JcrUtils.addPrivilege(session, jcrBasePath, principal, Privilege.JCR_READ); for (String pageName : pages.keySet()) { try { initPage(session, pages.get(pageName)); session.save(); } catch (Exception e) { - throw new CmsException( - "Cannot initialize page " + pageName, e); + throw new CmsException("Cannot initialize page " + pageName, e); } } @@ -188,8 +233,7 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration { register(); } - protected void initPage(Session adminSession, CmsUiProvider page) - throws RepositoryException { + protected void initPage(Session adminSession, CmsUiProvider page) throws RepositoryException { if (page instanceof LifeCycleUiProvider) ((LifeCycleUiProvider) page).init(adminSession); } @@ -210,8 +254,7 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration { Hashtable props = new Hashtable(); if (contextName != null) props.put("contextName", contextName); - appReg = bundleContext.registerService(ApplicationConfiguration.class, - this, props); + appReg = bundleContext.registerService(ApplicationConfiguration.class, this, props); if (log.isDebugEnabled()) log.debug("Registered " + (contextName == null ? "/" : contextName)); } @@ -219,8 +262,7 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration { protected void unregister() { appReg.unregister(); if (log.isDebugEnabled()) - log.debug("Unregistered " - + (contextName == null ? "/" : contextName)); + log.debug("Unregistered " + (contextName == null ? "/" : contextName)); } public void setRepository(Repository repository) { @@ -291,8 +333,8 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration { private final String workspace; private final Map properties; - public CmsEntryPointFactory(CmsUiProvider page, Repository repository, - String workspace, Map properties) { + public CmsEntryPointFactory(CmsUiProvider page, Repository repository, String workspace, + Map properties) { this.page = page; this.repository = repository; this.workspace = workspace; @@ -301,8 +343,8 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration { @Override public EntryPoint create() { - SimpleErgonomics entryPoint = new SimpleErgonomics(repository, - workspace, jcrBasePath, page, properties) { + SimpleErgonomics entryPoint = new SimpleErgonomics(repository, workspace, jcrBasePath, page, properties) { + private static final long serialVersionUID = -637940404865527290L; @Override protected void createAdminArea(Composite parent) { @@ -316,13 +358,8 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration { @Override public void widgetSelected(SelectionEvent e) { long timeBeforeReload = 1000; - RWT.getClient() - .getService(JavaScriptExecutor.class) - .execute( - "setTimeout(function() { " - + "location.reload();" - + "}," + timeBeforeReload - + ");"); + RWT.getClient().getService(JavaScriptExecutor.class).execute( + "setTimeout(function() { " + "location.reload();" + "}," + timeBeforeReload + ");"); reloadApp(); } }); @@ -348,10 +385,8 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration { private static ResourceLoader createResourceLoader(final String resourceName) { return new ResourceLoader() { - public InputStream getResourceAsStream(String resourceName) - throws IOException { - return getClass().getClassLoader().getResourceAsStream( - resourceName); + public InputStream getResourceAsStream(String resourceName) throws IOException { + return getClass().getClassLoader().getResourceAsStream(resourceName); } }; } @@ -371,6 +406,5 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration { private static String DEFAULT_LOADING_BODY = "" + "" - + ""; + + "\" width=\"32\" height=\"32\" style=\"margin: 16px 16px\"/>" + ""; }