3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.util
.ArrayList
;
6 import java
.util
.HashMap
;
10 import org
.apache
.commons
.logging
.Log
;
11 import org
.apache
.commons
.logging
.LogFactory
;
12 import org
.eclipse
.gemini
.blueprint
.context
.BundleContextAware
;
13 import org
.eclipse
.rap
.rwt
.application
.Application
;
14 import org
.eclipse
.rap
.rwt
.application
.Application
.OperationMode
;
15 import org
.eclipse
.rap
.rwt
.application
.ApplicationConfiguration
;
16 import org
.eclipse
.rap
.rwt
.application
.EntryPointFactory
;
17 import org
.eclipse
.rap
.rwt
.application
.ExceptionHandler
;
18 import org
.eclipse
.rap
.rwt
.client
.WebClient
;
19 import org
.eclipse
.rap
.rwt
.lifecycle
.PhaseEvent
;
20 import org
.eclipse
.rap
.rwt
.lifecycle
.PhaseId
;
21 import org
.eclipse
.rap
.rwt
.lifecycle
.PhaseListener
;
22 import org
.eclipse
.rap
.rwt
.service
.ResourceLoader
;
23 import org
.osgi
.framework
.BundleContext
;
25 /** Configures an Argeo CMS RWT application. */
26 public class CmsApplication
implements CmsConstants
, ApplicationConfiguration
,
28 final static Log log
= LogFactory
.getLog(CmsApplication
.class);
30 private Map
<String
, EntryPointFactory
> entryPoints
= new HashMap
<String
, EntryPointFactory
>();
31 private Map
<String
, Map
<String
, String
>> entryPointsBranding
= new HashMap
<String
, Map
<String
, String
>>();
32 private Map
<String
, List
<String
>> styleSheets
= new HashMap
<String
, List
<String
>>();
34 private List
<String
> resources
= new ArrayList
<String
>();
36 // private Bundle clientScriptingBundle;
37 private BundleContext bundleContext
;
39 public void configure(Application application
) {
41 application
.setOperationMode(OperationMode
.SWT_COMPATIBILITY
);
42 application
.setExceptionHandler(new CmsExceptionHandler());
44 // TODO load all pics under icons
45 // loading animated gif
46 application
.addResource(LOADING_IMAGE
,
47 createResourceLoader(LOADING_IMAGE
));
49 application
.addResource(NO_IMAGE
, createResourceLoader(NO_IMAGE
));
51 for (String resource
: resources
) {
52 // URL res = bundleContext.getBundle().getResource(resource);
54 // throw new CmsException("Resource " + resource
56 application
.addResource(resource
, new BundleResourceLoader(
58 if (log
.isDebugEnabled())
59 log
.debug("Registered resource " + resource
);
63 for (String entryPoint
: entryPoints
.keySet()) {
64 Map
<String
, String
> properties
= new HashMap
<String
, String
>();
65 if (entryPointsBranding
.containsKey(entryPoint
)) {
66 properties
= entryPointsBranding
.get(entryPoint
);
67 if (properties
.containsKey(WebClient
.FAVICON
)) {
68 String faviconRelPath
= properties
69 .get(WebClient
.FAVICON
);
70 // URL res = bundleContext.getBundle().getResource(
72 application
.addResource(faviconRelPath
,
73 new BundleResourceLoader(bundleContext
));
74 if (log
.isTraceEnabled())
75 log
.trace("Registered favicon " + faviconRelPath
);
79 if (!properties
.containsKey(WebClient
.BODY_HTML
))
80 properties
.put(WebClient
.BODY_HTML
,
81 DEFAULT_LOADING_BODY
);
84 application
.addEntryPoint("/" + entryPoint
,
85 entryPoints
.get(entryPoint
), properties
);
86 log
.info("Registered entry point /" + entryPoint
);
90 for (String themeId
: styleSheets
.keySet()) {
91 List
<String
> cssLst
= styleSheets
.get(themeId
);
92 for (String css
: cssLst
) {
93 // URL res = bundleContext.getBundle().getResource(css);
95 // throw new CmsException("Stylesheet " + css
97 application
.addStyleSheet(themeId
, css
,
98 new BundleResourceLoader(bundleContext
));
103 application
.addPhaseListener(new CmsPhaseListener());
105 // registerClientScriptingResources(application);
106 } catch (RuntimeException e
) {
107 // Easier access to initialisation errors
108 log
.error("Unexpected exception when configuring RWT application.",
114 // see Eclipse.org bug 369957
115 // private void registerClientScriptingResources(Application application) {
116 // if (clientScriptingBundle != null) {
117 // String className =
118 // "org.eclipse.rap.clientscripting.internal.resources.ClientScriptingResources";
120 // Class<?> resourceClass = clientScriptingBundle
121 // .loadClass(className);
122 // Method registerMethod = resourceClass.getMethod("register",
123 // Application.class);
124 // registerMethod.invoke(null, application);
125 // } catch (Exception exception) {
126 // throw new RuntimeException(exception);
131 private static ResourceLoader
createResourceLoader(final String resourceName
) {
132 return new ResourceLoader() {
133 public InputStream
getResourceAsStream(String resourceName
)
135 return getClass().getClassLoader().getResourceAsStream(
141 public void setEntryPoints(
142 Map
<String
, EntryPointFactory
> entryPointFactories
) {
143 this.entryPoints
= entryPointFactories
;
146 public void setEntryPointsBranding(
147 Map
<String
, Map
<String
, String
>> entryPointBranding
) {
148 this.entryPointsBranding
= entryPointBranding
;
151 public void setStyleSheets(Map
<String
, List
<String
>> styleSheets
) {
152 this.styleSheets
= styleSheets
;
155 // public void setClientScriptingBundle(Bundle clientScriptingBundle) {
156 // this.clientScriptingBundle = clientScriptingBundle;
159 public void setBundleContext(BundleContext bundleContext
) {
160 this.bundleContext
= bundleContext
;
163 public void setResources(List
<String
> resources
) {
164 this.resources
= resources
;
167 class CmsExceptionHandler
implements ExceptionHandler
{
170 public void handleException(Throwable throwable
) {
171 CmsSession
.current
.get().exception(throwable
);
176 class CmsPhaseListener
implements PhaseListener
{
177 private static final long serialVersionUID
= -1966645586738534609L;
180 public PhaseId
getPhaseId() {
181 return PhaseId
.RENDER
;
185 public void beforePhase(PhaseEvent event
) {
186 CmsSession cmsSession
= CmsSession
.current
.get();
187 String state
= cmsSession
.getState();
189 cmsSession
.navigateTo("~");
193 public void afterPhase(PhaseEvent event
) {
200 private static String DEFAULT_LOADING_BODY
= "<div"
201 + " style=\"position: absolute; left: 50%; top: 50%; margin: -32px -32px; width: 64px; height:64px\">"
202 + "<img src=\"./rwt-resources/icons/loading.gif\" width=\"32\" height=\"32\" style=\"margin: 16px 16px\"/>"