]> git.argeo.org Git - lgpl/argeo-commons.git/blob - web/CmsWebApp.java
Prepare next development cycle
[lgpl/argeo-commons.git] / 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.CmsLog;
11 import org.argeo.api.cms.ux.CmsTheme;
12 import org.argeo.api.cms.ux.CmsView;
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.ServiceRegistration;
24 import org.osgi.service.event.EventAdmin;
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 // private String cmsAppId;
33 private EventAdmin eventAdmin;
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 entryPoint.setEventAdmin(eventAdmin);
107 return entryPoint;
108 }, properties);
109 if (log.isDebugEnabled())
110 log.info("Added web entry point " + (contextName != null ? "/" + contextName : "") + entryPointName);
111 }
112 // if (log.isDebugEnabled())
113 // log.debug("Published CMS web app /" + (contextName != null ? contextName : ""));
114 }
115
116 CmsApp getCmsApp() {
117 return cmsApp;
118 }
119
120 BundleContext getBundleContext() {
121 return bundleContext;
122 }
123
124 public void setCmsApp(CmsApp cmsApp, Map<String, String> properties) {
125 this.cmsApp = cmsApp;
126 // this.cmsAppId = properties.get(Constants.SERVICE_PID);
127 this.cmsApp.addCmsAppListener(this);
128 }
129
130 public void unsetCmsApp(CmsApp cmsApp, Map<String, String> properties) {
131 String contextName = properties.get(CmsApp.CONTEXT_NAME_PROPERTY);
132 if (!contextName.equals(this.contextName))
133 return;
134 if (this.cmsApp != null) {
135 this.cmsApp.removeCmsAppListener(this);
136 }
137 if (rwtAppReg != null)
138 rwtAppReg.unregister();
139 this.cmsApp = null;
140 }
141
142 @Override
143 public void themingUpdated() {
144 if (cmsApp != null && cmsApp.allThemesAvailable())
145 publishWebApp();
146 }
147
148 protected void publishWebApp() {
149 Dictionary<String, Object> regProps = LangUtils.dict(CONTEXT_NAME, contextName);
150 if (rwtAppReg != null)
151 rwtAppReg.unregister();
152 if (bundleContext != null) {
153 rwtAppReg = bundleContext.registerService(ApplicationConfiguration.class, this, regProps);
154 if (log.isDebugEnabled())
155 log.debug("Publishing CMS web app /" + (contextName != null ? contextName : "") + " ...");
156 }
157 }
158
159 public void setEventAdmin(EventAdmin eventAdmin) {
160 this.eventAdmin = eventAdmin;
161 }
162
163 }