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