]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui.rap/src/org/argeo/cms/web/CmsWebApp.java
Stop trying if UI loop is not supported.
[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.client.WebClient;
19 import org.eclipse.swt.widgets.Display;
20 import org.osgi.framework.BundleContext;
21 import org.osgi.framework.ServiceRegistration;
22 import org.osgi.service.event.EventAdmin;
23
24 /** An RWT web app integrating with a {@link CmsApp}. */
25 public class CmsWebApp implements ApplicationConfiguration, ExceptionHandler, CmsAppListener {
26 private final static Log log = LogFactory.getLog(CmsWebApp.class);
27
28 private BundleContext bundleContext;
29 private CmsApp cmsApp;
30 private EventAdmin eventAdmin;
31
32 private ServiceRegistration<ApplicationConfiguration> rwtAppReg;
33
34 private final static String CONTEXT_NAME = "contextName";
35 private String contextName;
36
37 public void init(BundleContext bundleContext, Map<String, String> properties) {
38 this.bundleContext = bundleContext;
39 contextName = properties.get(CONTEXT_NAME);
40 if (cmsApp != null)
41 themingUpdated();
42 // registerIfAllThemesAvailable();
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 for (String uiName : cmsApp.getUiNames()) {
53 CmsTheme theme = cmsApp.getTheme(uiName);
54 if (theme != null)
55 WebThemeUtils.apply(application, theme);
56 }
57 // for (CmsTheme theme : themes.values())
58 // WebThemeUtils.apply(application, theme);
59
60 Map<String, String> properties = new HashMap<>();
61 addEntryPoints(application, properties);
62
63 }
64
65 @Override
66 public void handleException(Throwable throwable) {
67 Display display = Display.getCurrent();
68 if (display != null && !display.isDisposed()) {
69 CmsView cmsView = CmsView.getCmsView(display.getActiveShell());
70 cmsView.exception(throwable);
71 } else {
72 log.error("Unexpected exception outside an UI thread", throwable);
73 }
74
75 }
76
77 protected void addEntryPoints(Application application, Map<String, String> commonProperties) {
78 for (String uiName : cmsApp.getUiNames()) {
79 Map<String, String> properties = new HashMap<>(commonProperties);
80 CmsTheme theme = cmsApp.getTheme(uiName);
81 if (theme != null) {
82 properties.put(WebClient.THEME_ID, theme.getThemeId());
83 properties.put(WebClient.HEAD_HTML, theme.getHtmlHeaders());
84 } else {
85 properties.put(WebClient.THEME_ID, RWT.DEFAULT_THEME_ID);
86 // if (themeId != null)
87 // log.warn("Theme id " + themeId + " was specified but it was not found, using default RWT theme.");
88 }
89 String entryPointName = !uiName.equals("") ? "/" + uiName : "/";
90 application.addEntryPoint(entryPointName, () -> {
91 CmsWebEntryPoint entryPoint = new CmsWebEntryPoint(this, uiName);
92 entryPoint.setEventAdmin(eventAdmin);
93 return entryPoint;
94 }, properties);
95 if (log.isDebugEnabled())
96 log.info("Added web entry point " + (contextName != null ? "/" + contextName : "") + entryPointName);
97 }
98 if (log.isDebugEnabled())
99 log.debug("Published CMS web app /" + (contextName != null ? contextName : ""));
100 }
101
102 // private void registerIfAllThemesAvailable() {
103 // boolean themeMissing = false;
104 // uiNames: for (String uiName : cmsApp.getUiNames()) {
105 // String themeId = cmsApp.getThemeId(uiName);
106 // if (RWT.DEFAULT_THEME_ID.equals(themeId))
107 // continue uiNames;
108 // if (!themes.containsKey(themeId)) {
109 // themeMissing = true;
110 // break uiNames;
111 // }
112 // }
113 // if (!themeMissing) {
114 // Dictionary<String, Object> regProps = LangUtils.dict(CONTEXT_NAME, contextName);
115 // if (bundleContext != null) {
116 // rwtAppReg = bundleContext.registerService(ApplicationConfiguration.class, this, regProps);
117 // log.info("Published CMS web app /" + (contextName != null ? contextName : ""));
118 // }
119 // }
120 // }
121
122 CmsApp getCmsApp() {
123 return cmsApp;
124 }
125
126 public void setCmsApp(CmsApp cmsApp, Map<String, String> properties) {
127 this.cmsApp = cmsApp;
128 this.cmsApp.addCmsAppListener(this);
129 // registerIfAllThemesAvailable();
130 }
131
132 public void unsetCmsApp(CmsApp cmsApp, Map<String, String> properties) {
133 if (rwtAppReg != null)
134 rwtAppReg.unregister();
135 this.cmsApp = null;
136 }
137
138 @Override
139 public void themingUpdated() {
140 Dictionary<String, Object> regProps = LangUtils.dict(CONTEXT_NAME, contextName);
141 if (rwtAppReg != null)
142 rwtAppReg.unregister();
143 if (bundleContext != null) {
144 rwtAppReg = bundleContext.registerService(ApplicationConfiguration.class, this, regProps);
145 if (log.isDebugEnabled())
146 log.debug("Publishing CMS web app /" + (contextName != null ? contextName : "") + " ...");
147 }
148 }
149
150 public void setEventAdmin(EventAdmin eventAdmin) {
151 this.eventAdmin = eventAdmin;
152 }
153
154 }