]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui.rap/src/org/argeo/cms/web/CmsWebApp.java
[maven-release-plugin] prepare release argeo-commons-2.1.100
[lgpl/argeo-commons.git] / org.argeo.cms.ui.rap / src / org / argeo / cms / web / CmsWebApp.java
1 package org.argeo.cms.web;
2
3 import java.util.Dictionary;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import org.argeo.cms.ui.CmsApp;
10 import org.argeo.cms.ui.CmsAppListener;
11 import org.argeo.cms.ui.CmsTheme;
12 import org.argeo.cms.ui.CmsView;
13 import org.argeo.util.LangUtils;
14 import org.eclipse.rap.rwt.RWT;
15 import org.eclipse.rap.rwt.application.Application;
16 import org.eclipse.rap.rwt.application.ApplicationConfiguration;
17 import org.eclipse.rap.rwt.application.ExceptionHandler;
18 import org.eclipse.rap.rwt.application.Application.OperationMode;
19 import org.eclipse.rap.rwt.client.WebClient;
20 import org.eclipse.swt.widgets.Display;
21 import org.osgi.framework.BundleContext;
22 import org.osgi.framework.ServiceRegistration;
23 import org.osgi.service.event.EventAdmin;
24
25 /** An RWT web app integrating with a {@link CmsApp}. */
26 public class CmsWebApp implements ApplicationConfiguration, ExceptionHandler, CmsAppListener {
27 private final static Log log = LogFactory.getLog(CmsWebApp.class);
28
29 private BundleContext bundleContext;
30 private CmsApp cmsApp;
31 private EventAdmin eventAdmin;
32
33 private ServiceRegistration<ApplicationConfiguration> rwtAppReg;
34
35 private final static String CONTEXT_NAME = "contextName";
36 private String contextName;
37
38 public void init(BundleContext bundleContext, Map<String, String> properties) {
39 this.bundleContext = bundleContext;
40 contextName = properties.get(CONTEXT_NAME);
41 if (cmsApp != null)
42 themingUpdated();
43 }
44
45 public void destroy(BundleContext bundleContext, Map<String, String> properties) {
46 if (cmsApp != null)
47 cmsApp.removeCmsAppListener(this);
48 }
49
50 @Override
51 public void configure(Application application) {
52 // TODO make it configurable?
53 // SWT compatibility is required for:
54 // - Browser.execute()
55 // - blocking dialogs
56 application.setOperationMode(OperationMode.SWT_COMPATIBILITY);
57 for (String uiName : cmsApp.getUiNames()) {
58 CmsTheme theme = cmsApp.getTheme(uiName);
59 if (theme != null)
60 WebThemeUtils.apply(application, theme);
61 }
62
63 Map<String, String> properties = new HashMap<>();
64 addEntryPoints(application, properties);
65 application.setExceptionHandler(this);
66 }
67
68 @Override
69 public void handleException(Throwable throwable) {
70 Display display = Display.getCurrent();
71 if (display != null && !display.isDisposed()) {
72 CmsView cmsView = CmsView.getCmsView(display.getActiveShell());
73 cmsView.exception(throwable);
74 } else {
75 log.error("Unexpected exception outside an UI thread", throwable);
76 }
77
78 }
79
80 protected void addEntryPoints(Application application, Map<String, String> commonProperties) {
81 for (String uiName : cmsApp.getUiNames()) {
82 Map<String, String> properties = new HashMap<>(commonProperties);
83 CmsTheme theme = cmsApp.getTheme(uiName);
84 if (theme != null) {
85 properties.put(WebClient.THEME_ID, theme.getThemeId());
86 properties.put(WebClient.HEAD_HTML, theme.getHtmlHeaders());
87 } else {
88 properties.put(WebClient.THEME_ID, RWT.DEFAULT_THEME_ID);
89 }
90 String entryPointName = !uiName.equals("") ? "/" + uiName : "/";
91 application.addEntryPoint(entryPointName, () -> {
92 CmsWebEntryPoint entryPoint = new CmsWebEntryPoint(this, uiName);
93 entryPoint.setEventAdmin(eventAdmin);
94 return entryPoint;
95 }, properties);
96 if (log.isDebugEnabled())
97 log.info("Added web entry point " + (contextName != null ? "/" + contextName : "") + entryPointName);
98 }
99 if (log.isDebugEnabled())
100 log.debug("Published CMS web app /" + (contextName != null ? contextName : ""));
101 }
102
103 CmsApp getCmsApp() {
104 return cmsApp;
105 }
106
107 BundleContext getBundleContext() {
108 return bundleContext;
109 }
110
111 public void setCmsApp(CmsApp cmsApp, Map<String, String> properties) {
112 this.cmsApp = cmsApp;
113 this.cmsApp.addCmsAppListener(this);
114 }
115
116 public void unsetCmsApp(CmsApp cmsApp, Map<String, String> properties) {
117 if (rwtAppReg != null)
118 rwtAppReg.unregister();
119 this.cmsApp = null;
120 }
121
122 @Override
123 public void themingUpdated() {
124 Dictionary<String, Object> regProps = LangUtils.dict(CONTEXT_NAME, contextName);
125 if (rwtAppReg != null)
126 rwtAppReg.unregister();
127 if (bundleContext != null) {
128 rwtAppReg = bundleContext.registerService(ApplicationConfiguration.class, this, regProps);
129 if (log.isDebugEnabled())
130 log.debug("Publishing CMS web app /" + (contextName != null ? contextName : "") + " ...");
131 }
132 }
133
134 public void setEventAdmin(EventAdmin eventAdmin) {
135 this.eventAdmin = eventAdmin;
136 }
137
138 }