1 package org
.argeo
.cms
.web
;
3 import java
.util
.Dictionary
;
4 import java
.util
.HashMap
;
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
;
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);
28 private BundleContext bundleContext
;
29 private CmsApp cmsApp
;
30 private EventAdmin eventAdmin
;
32 private ServiceRegistration
<ApplicationConfiguration
> rwtAppReg
;
34 private final static String CONTEXT_NAME
= "contextName";
35 private String contextName
;
37 public void init(BundleContext bundleContext
, Map
<String
, String
> properties
) {
38 this.bundleContext
= bundleContext
;
39 contextName
= properties
.get(CONTEXT_NAME
);
42 // registerIfAllThemesAvailable();
45 public void destroy(BundleContext bundleContext
, Map
<String
, String
> properties
) {
47 cmsApp
.removeCmsAppListener(this);
51 public void configure(Application application
) {
52 for (String uiName
: cmsApp
.getUiNames()) {
53 CmsTheme theme
= cmsApp
.getTheme(uiName
);
55 WebThemeUtils
.apply(application
, theme
);
57 // for (CmsTheme theme : themes.values())
58 // WebThemeUtils.apply(application, theme);
60 Map
<String
, String
> properties
= new HashMap
<>();
61 addEntryPoints(application
, properties
);
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
);
72 log
.error("Unexpected exception outside an UI thread", throwable
);
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
);
82 properties
.put(WebClient
.THEME_ID
, theme
.getThemeId());
83 properties
.put(WebClient
.HEAD_HTML
, theme
.getHtmlHeaders());
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.");
89 String entryPointName
= !uiName
.equals("") ?
"/" + uiName
: "/";
90 application
.addEntryPoint(entryPointName
, () -> {
91 CmsWebEntryPoint entryPoint
= new CmsWebEntryPoint(this, uiName
);
92 entryPoint
.setEventAdmin(eventAdmin
);
95 if (log
.isDebugEnabled())
96 log
.info("Added web entry point " + (contextName
!= null ?
"/" + contextName
: "") + entryPointName
);
98 if (log
.isDebugEnabled())
99 log
.debug("Published CMS web app /" + (contextName
!= null ? contextName
: ""));
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))
108 // if (!themes.containsKey(themeId)) {
109 // themeMissing = true;
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 : ""));
126 public void setCmsApp(CmsApp cmsApp
, Map
<String
, String
> properties
) {
127 this.cmsApp
= cmsApp
;
128 this.cmsApp
.addCmsAppListener(this);
129 // registerIfAllThemesAvailable();
132 public void unsetCmsApp(CmsApp cmsApp
, Map
<String
, String
> properties
) {
133 if (rwtAppReg
!= null)
134 rwtAppReg
.unregister();
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
: "") + " ...");
150 public void setEventAdmin(EventAdmin eventAdmin
) {
151 this.eventAdmin
= eventAdmin
;