Adapt CMS Web to new assembly approach.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / util / SimpleApp.java
index 780a01d184c4dfbd3dded5fb74f4c094aca2d343..327438b56c58b04690fa5321bf644756d93ed472 100644 (file)
@@ -5,10 +5,12 @@ import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Arrays;
 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;
@@ -24,6 +26,7 @@ import org.argeo.cms.ui.CmsUiProvider;
 import org.argeo.cms.ui.LifeCycleUiProvider;
 import org.argeo.jcr.JcrUtils;
 import org.argeo.node.NodeConstants;
+import org.argeo.node.NodeUtils;
 import org.eclipse.rap.rwt.RWT;
 import org.eclipse.rap.rwt.application.Application;
 import org.eclipse.rap.rwt.application.Application.OperationMode;
@@ -40,6 +43,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;
 
@@ -71,8 +75,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);
@@ -93,6 +96,7 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration {
                        Map<String, String> defaultBranding = null;
                        if (branding.containsKey("*"))
                                defaultBranding = branding.get("*");
+                       String defaultTheme = defaultBranding.get(WebClient.THEME_ID);
 
                        // entry points
                        for (String page : pages.keySet()) {
@@ -103,8 +107,11 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration {
                                }
                                // favicon
                                if (properties.containsKey(WebClient.FAVICON)) {
+                                       String themeId = defaultBranding.get(WebClient.THEME_ID);
+                                       Bundle themeBundle = ThemeUtils.findThemeBundle(bundleContext, themeId);
                                        String faviconRelPath = properties.get(WebClient.FAVICON);
-                                       application.addResource(faviconRelPath, new BundleResourceLoader(bundleContext));
+                                       application.addResource(faviconRelPath,
+                                                       new BundleResourceLoader(themeBundle != null ? themeBundle : bundleContext.getBundle()));
                                        if (log.isTraceEnabled())
                                                log.trace("Favicon " + faviconRelPath);
 
@@ -128,18 +135,30 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration {
                                log.info("Page /" + page);
                        }
 
-                       // stylesheets
+                       // stylesheets and themes
+                       Set<Bundle> themeBundles = new HashSet<>();
                        for (String themeId : styleSheets.keySet()) {
+                               Bundle themeBundle = ThemeUtils.findThemeBundle(bundleContext, themeId);
+                               StyleSheetResourceLoader styleSheetRL = new StyleSheetResourceLoader(
+                                               themeBundle != null ? themeBundle : bundleContext.getBundle());
+                               if (themeBundle != null)
+                                       themeBundles.add(themeBundle);
                                List<String> cssLst = styleSheets.get(themeId);
                                if (log.isDebugEnabled())
                                        log.debug("Theme " + themeId);
                                for (String css : cssLst) {
                                        application.addStyleSheet(themeId, css, styleSheetRL);
-                                       if (log.isTraceEnabled())
-                                               log.trace(" CSS " + css);
+                                       if (log.isDebugEnabled())
+                                               log.debug(" CSS " + css);
                                }
 
                        }
+                       for (Bundle themeBundle : themeBundles) {
+                               BundleResourceLoader themeBRL = new BundleResourceLoader(themeBundle);
+                               ThemeUtils.addThemeResources(application, themeBundle, themeBRL, "*.png");
+                               ThemeUtils.addThemeResources(application, themeBundle, themeBRL, "*.gif");
+                               ThemeUtils.addThemeResources(application, themeBundle, themeBRL, "*.jpg");
+                       }
                } catch (RuntimeException e) {
                        // Easier access to initialisation errors
                        log.error("Unexpected exception when configuring RWT application.", e);
@@ -150,7 +169,8 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration {
        public void init() throws RepositoryException {
                Session session = null;
                try {
-                       session = JcrUtils.loginOrCreateWorkspace(repository, workspace);
+                       session = NodeUtils.openDataAdminSession(repository, workspace);
+                       // session = JcrUtils.loginOrCreateWorkspace(repository, workspace);
                        VersionManager vm = session.getWorkspace().getVersionManager();
                        JcrUtils.mkdirs(session, jcrBasePath);
                        session.save();