Improve CMS theming.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / web / CmsWebApp.java
index cae02fde50784233d9d93277725ebf806d230903..4c55d4eabf11bd6ba837af4d00593b1458ff0494 100644 (file)
@@ -1,18 +1,73 @@
 package org.argeo.cms.web;
 
+import java.util.Dictionary;
+import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.argeo.cms.ui.CmsApp;
+import org.argeo.cms.ui.CmsAppListener;
+import org.argeo.cms.ui.CmsTheme;
+import org.argeo.util.LangUtils;
+import org.eclipse.rap.rwt.RWT;
 import org.eclipse.rap.rwt.application.AbstractEntryPoint;
 import org.eclipse.rap.rwt.application.Application;
+import org.eclipse.rap.rwt.application.ApplicationConfiguration;
+import org.eclipse.rap.rwt.client.WebClient;
 import org.eclipse.swt.widgets.Composite;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
 
-public class CmsWebApp extends MinimalWebApp {
+/** An RWT web app integrating with a {@link CmsApp}. */
+public class CmsWebApp implements ApplicationConfiguration, CmsAppListener {
+       private final static Log log = LogFactory.getLog(CmsWebApp.class);
+
+       private BundleContext bundleContext;
        private CmsApp cmsApp;
 
+       private ServiceRegistration<ApplicationConfiguration> rwtAppReg;
+
+       private final static String CONTEXT_NAME = "contextName";
+       private String contextName;
+
+       public void init(BundleContext bundleContext, Map<String, String> properties) {
+               this.bundleContext = bundleContext;
+               contextName = properties.get(CONTEXT_NAME);
+//             registerIfAllThemesAvailable();
+       }
+
+       public void destroy(BundleContext bundleContext, Map<String, String> properties) {
+
+       }
+
        @Override
-       protected void addEntryPoints(Application application, Map<String, String> properties) {
+       public void configure(Application application) {
                for (String uiName : cmsApp.getUiNames()) {
+                       CmsTheme theme = cmsApp.getTheme(uiName);
+                       if (theme != null)
+                               WebThemeUtils.apply(application, theme);
+               }
+//             for (CmsTheme theme : themes.values())
+//                     WebThemeUtils.apply(application, theme);
+
+               Map<String, String> properties = new HashMap<>();
+               addEntryPoints(application, properties);
+
+       }
+
+       protected void addEntryPoints(Application application, Map<String, String> commonProperties) {
+               for (String uiName : cmsApp.getUiNames()) {
+                       Map<String, String> properties = new HashMap<>(commonProperties);
+                       CmsTheme theme = cmsApp.getTheme(uiName);
+                       if (theme != null) {
+                               properties.put(WebClient.THEME_ID, theme.getThemeId());
+                               properties.put(WebClient.HEAD_HTML, theme.getHtmlHeaders());
+                       } else {
+                               properties.put(WebClient.THEME_ID, RWT.DEFAULT_THEME_ID);
+//                             if (themeId != null)
+//                                     log.warn("Theme id " + themeId + " was specified but it was not found, using default RWT theme.");
+                       }
                        application.addEntryPoint("/" + uiName, () -> {
                                return new AbstractEntryPoint() {
                                        private static final long serialVersionUID = -9153259126766694485L;
@@ -27,12 +82,51 @@ public class CmsWebApp extends MinimalWebApp {
                }
        }
 
+//     private void registerIfAllThemesAvailable() {
+//             boolean themeMissing = false;
+//             uiNames: for (String uiName : cmsApp.getUiNames()) {
+//                     String themeId = cmsApp.getThemeId(uiName);
+//                     if (RWT.DEFAULT_THEME_ID.equals(themeId))
+//                             continue uiNames;
+//                     if (!themes.containsKey(themeId)) {
+//                             themeMissing = true;
+//                             break uiNames;
+//                     }
+//             }
+//             if (!themeMissing) {
+//                     Dictionary<String, Object> regProps = LangUtils.dict(CONTEXT_NAME, contextName);
+//                     if (bundleContext != null) {
+//                             rwtAppReg = bundleContext.registerService(ApplicationConfiguration.class, this, regProps);
+//                             log.info("Published CMS web app /" + (contextName != null ? contextName : ""));
+//                     }
+//             }
+//     }
+
        public CmsApp getCmsApp() {
                return cmsApp;
        }
 
-       public void setCmsApp(CmsApp cmsApp) {
+       public void setCmsApp(CmsApp cmsApp, Map<String, String> properties) {
                this.cmsApp = cmsApp;
+               this.cmsApp.addCmsAppListener(this);
+//             registerIfAllThemesAvailable();
+       }
+
+       public void unsetCmsApp(CmsApp cmsApp, Map<String, String> properties) {
+               if (rwtAppReg != null)
+                       rwtAppReg.unregister();
+               this.cmsApp = null;
+       }
+
+       @Override
+       public void themingUpdated() {
+               Dictionary<String, Object> regProps = LangUtils.dict(CONTEXT_NAME, contextName);
+               if (rwtAppReg != null)
+                       rwtAppReg.unregister();
+               if (bundleContext != null) {
+                       rwtAppReg = bundleContext.registerService(ApplicationConfiguration.class, this, regProps);
+                       log.info("Published CMS web app /" + (contextName != null ? contextName : ""));
+               }
        }
 
 }