]> git.argeo.org Git - lgpl/argeo-commons.git/blob - swt/rap/org.argeo.cms.swt.rap/src/org/argeo/cms/web/CmsWebApp.java
Merge tag 'v2.3.27' into testing
[lgpl/argeo-commons.git] / swt / rap / org.argeo.cms.swt.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 import java.util.Set;
7
8 import org.argeo.api.cms.CmsApp;
9 import org.argeo.api.cms.CmsAppListener;
10 import org.argeo.api.cms.CmsEventBus;
11 import org.argeo.api.cms.CmsLog;
12 import org.argeo.api.cms.ux.CmsTheme;
13 import org.argeo.api.cms.ux.CmsView;
14 import org.argeo.cms.swt.CmsSwtUtils;
15 import org.argeo.cms.util.LangUtils;
16 import org.eclipse.rap.rwt.RWT;
17 import org.eclipse.rap.rwt.application.Application;
18 import org.eclipse.rap.rwt.application.Application.OperationMode;
19 import org.eclipse.rap.rwt.application.ApplicationConfiguration;
20 import org.eclipse.rap.rwt.application.ExceptionHandler;
21 import org.eclipse.rap.rwt.client.WebClient;
22 import org.eclipse.swt.widgets.Display;
23 import org.osgi.framework.BundleContext;
24 import org.osgi.framework.ServiceRegistration;
25
26 /** An RWT web app integrating with a {@link CmsApp}. */
27 public class CmsWebApp implements ApplicationConfiguration, ExceptionHandler, CmsAppListener {
28 private final static CmsLog log = CmsLog.getLog(CmsWebApp.class);
29
30 private BundleContext bundleContext;
31 private CmsApp cmsApp;
32
33 private CmsEventBus cmsEventBus;
34
35 private ServiceRegistration<ApplicationConfiguration> rwtAppReg;
36
37 private final static String CONTEXT_NAME = "contextName";
38 private String contextName;
39
40 private final static String FAVICON_PNG = "favicon.png";
41
42 public void init(BundleContext bundleContext, Map<String, String> properties) {
43 this.bundleContext = bundleContext;
44 contextName = properties.get(CONTEXT_NAME);
45 if (cmsApp != null) {
46 if (cmsApp.allThemesAvailable())
47 publishWebApp();
48 }
49 }
50
51 public void destroy(BundleContext bundleContext, Map<String, String> properties) {
52 if (cmsApp != null) {
53 cmsApp.removeCmsAppListener(this);
54 cmsApp = null;
55 }
56 }
57
58 @Override
59 public void configure(Application application) {
60 // TODO make it configurable?
61 // SWT compatibility is required for:
62 // - Browser.execute()
63 // - blocking dialogs
64 application.setOperationMode(OperationMode.SWT_COMPATIBILITY);
65 for (String uiName : cmsApp.getUiNames()) {
66 CmsTheme theme = cmsApp.getTheme(uiName);
67 if (theme != null)
68 WebThemeUtils.apply(application, theme);
69 }
70
71 Map<String, String> properties = new HashMap<>();
72 addEntryPoints(application, properties);
73 application.setExceptionHandler(this);
74 }
75
76 @Override
77 public void handleException(Throwable throwable) {
78 Display display = Display.getCurrent();
79 if (display != null && !display.isDisposed()) {
80 CmsView cmsView = CmsSwtUtils.getCmsView(display.getActiveShell());
81 cmsView.exception(throwable);
82 } else {
83 log.error("Unexpected exception outside an UI thread", throwable);
84 }
85
86 }
87
88 protected void addEntryPoints(Application application, Map<String, String> commonProperties) {
89 for (String uiName : cmsApp.getUiNames()) {
90 Map<String, String> properties = new HashMap<>(commonProperties);
91 CmsTheme theme = cmsApp.getTheme(uiName);
92 if (theme != null) {
93 properties.put(WebClient.THEME_ID, theme.getThemeId());
94 properties.put(WebClient.HEAD_HTML, theme.getHtmlHeaders());
95 properties.put(WebClient.BODY_HTML, theme.getBodyHtml());
96 Set<String> imagePaths = theme.getImagesPaths();
97 if (imagePaths.contains(FAVICON_PNG)) {
98 properties.put(WebClient.FAVICON, FAVICON_PNG);
99 }
100 } else {
101 properties.put(WebClient.THEME_ID, RWT.DEFAULT_THEME_ID);
102 }
103 String entryPointName = !uiName.equals("") ? "/" + uiName : "/";
104 application.addEntryPoint(entryPointName, () -> {
105 CmsWebEntryPoint entryPoint = new CmsWebEntryPoint(this, uiName);
106 return entryPoint;
107 }, properties);
108 if (log.isDebugEnabled())
109 log.info("Added web entry point " + (contextName != null ? "/" + contextName : "") + entryPointName);
110 }
111 // if (log.isDebugEnabled())
112 // log.debug("Published CMS web app /" + (contextName != null ? contextName : ""));
113 }
114
115 public CmsApp getCmsApp() {
116 return cmsApp;
117 }
118
119 BundleContext getBundleContext() {
120 return bundleContext;
121 }
122
123 public void setCmsApp(CmsApp cmsApp) {
124 this.cmsApp = cmsApp;
125 // this.cmsAppId = properties.get(Constants.SERVICE_PID);
126 this.cmsApp.addCmsAppListener(this);
127 }
128
129 public void unsetCmsApp(CmsApp cmsApp, Map<String, String> properties) {
130 String contextName = properties.get(CmsApp.CONTEXT_NAME_PROPERTY);
131 if (!contextName.equals(this.contextName))
132 return;
133 if (this.cmsApp != null) {
134 this.cmsApp.removeCmsAppListener(this);
135 }
136 if (rwtAppReg != null)
137 rwtAppReg.unregister();
138 this.cmsApp = null;
139 }
140
141 @Override
142 public void themingUpdated() {
143 if (cmsApp != null && cmsApp.allThemesAvailable())
144 publishWebApp();
145 }
146
147 protected void publishWebApp() {
148 Dictionary<String, Object> regProps = LangUtils.dict(CONTEXT_NAME, contextName);
149 if (rwtAppReg != null)
150 rwtAppReg.unregister();
151 if (bundleContext != null) {
152 rwtAppReg = bundleContext.registerService(ApplicationConfiguration.class, this, regProps);
153 if (log.isDebugEnabled())
154 log.debug("Publishing CMS web app /" + (contextName != null ? contextName : "") + " ...");
155 }
156 }
157
158 public void setCmsEventBus(CmsEventBus cmsEventBus) {
159 this.cmsEventBus = cmsEventBus;
160 }
161
162 public CmsEventBus getCmsEventBus() {
163 return cmsEventBus;
164 }
165
166 public void setContextName(String contextName) {
167 this.contextName = contextName;
168 }
169
170 }