]> git.argeo.org Git - lgpl/argeo-commons.git/blob - CmsApplication.java
44dd8255360c4e0c0f48ac6c5e010538b2cf13b3
[lgpl/argeo-commons.git] / CmsApplication.java
1 package org.argeo.cms;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
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.service.ResourceLoader;
20 import org.osgi.framework.BundleContext;
21
22 /** Configures an Argeo CMS RWT application. */
23 public class CmsApplication implements CmsConstants, ApplicationConfiguration,
24 BundleContextAware {
25 final static Log log = LogFactory.getLog(CmsApplication.class);
26
27 private Map<String, EntryPointFactory> entryPoints = new HashMap<String, EntryPointFactory>();
28 private Map<String, Map<String, String>> entryPointsBranding = new HashMap<String, Map<String, String>>();
29 private Map<String, List<String>> styleSheets = new HashMap<String, List<String>>();
30
31 private List<String> resources = new ArrayList<String>();
32
33 // private Bundle clientScriptingBundle;
34 private BundleContext bundleContext;
35
36 public void configure(Application application) {
37 try {
38 application.setOperationMode(OperationMode.SWT_COMPATIBILITY);
39 application.setExceptionHandler(new CmsExceptionHandler());
40
41 // TODO load all pics under icons
42 // loading animated gif
43 application.addResource(LOADING_IMAGE,
44 createResourceLoader(LOADING_IMAGE));
45 // empty image
46 application.addResource(NO_IMAGE, createResourceLoader(NO_IMAGE));
47
48 for (String resource : resources) {
49 application.addResource(resource, new BundleResourceLoader(
50 bundleContext));
51 if (log.isDebugEnabled())
52 log.debug("Registered resource " + resource);
53 }
54
55 // entry points
56 for (String entryPoint : entryPoints.keySet()) {
57 Map<String, String> properties = new HashMap<String, String>();
58 if (entryPointsBranding.containsKey(entryPoint)) {
59 properties = entryPointsBranding.get(entryPoint);
60 if (properties.containsKey(WebClient.FAVICON)) {
61 String faviconRelPath = properties
62 .get(WebClient.FAVICON);
63 application.addResource(faviconRelPath,
64 new BundleResourceLoader(bundleContext));
65 if (log.isTraceEnabled())
66 log.trace("Registered favicon " + faviconRelPath);
67
68 }
69
70 if (!properties.containsKey(WebClient.BODY_HTML))
71 properties.put(WebClient.BODY_HTML,
72 DEFAULT_LOADING_BODY);
73 }
74
75 application.addEntryPoint("/" + entryPoint,
76 entryPoints.get(entryPoint), properties);
77 log.info("Registered entry point /" + entryPoint);
78 }
79
80 // stylesheets
81 for (String themeId : styleSheets.keySet()) {
82 List<String> cssLst = styleSheets.get(themeId);
83 for (String css : cssLst) {
84 application.addStyleSheet(themeId, css,
85 new BundleResourceLoader(bundleContext));
86 }
87
88 }
89
90 // application.addPhaseListener(new CmsPhaseListener());
91
92 } catch (RuntimeException e) {
93 // Easier access to initialisation errors
94 log.error("Unexpected exception when configuring RWT application.",
95 e);
96 throw e;
97 }
98 }
99
100 private static ResourceLoader createResourceLoader(final String resourceName) {
101 return new ResourceLoader() {
102 public InputStream getResourceAsStream(String resourceName)
103 throws IOException {
104 return getClass().getClassLoader().getResourceAsStream(
105 resourceName);
106 }
107 };
108 }
109
110 public void setEntryPoints(
111 Map<String, EntryPointFactory> entryPointFactories) {
112 this.entryPoints = entryPointFactories;
113 }
114
115 public void setEntryPointsBranding(
116 Map<String, Map<String, String>> entryPointBranding) {
117 this.entryPointsBranding = entryPointBranding;
118 }
119
120 public void setStyleSheets(Map<String, List<String>> styleSheets) {
121 this.styleSheets = styleSheets;
122 }
123
124 public void setBundleContext(BundleContext bundleContext) {
125 this.bundleContext = bundleContext;
126 }
127
128 public void setResources(List<String> resources) {
129 this.resources = resources;
130 }
131
132 class CmsExceptionHandler implements ExceptionHandler {
133
134 @Override
135 public void handleException(Throwable throwable) {
136 CmsSession.current.get().exception(throwable);
137 }
138
139 }
140
141 // class CmsPhaseListener implements PhaseListener {
142 // private static final long serialVersionUID = -1966645586738534609L;
143 //
144 // @Override
145 // public PhaseId getPhaseId() {
146 // return PhaseId.RENDER;
147 // }
148 //
149 // @Override
150 // public void beforePhase(PhaseEvent event) {
151 // CmsSession cmsSession = CmsSession.current.get();
152 // String state = cmsSession.getState();
153 // if (state == null)
154 // cmsSession.navigateTo("~");
155 // }
156 //
157 // @Override
158 // public void afterPhase(PhaseEvent event) {
159 // }
160 // }
161
162 /*
163 * TEXTS
164 */
165 private static String DEFAULT_LOADING_BODY = "<div"
166 + " style=\"position: absolute; left: 50%; top: 50%; margin: -32px -32px; width: 64px; height:64px\">"
167 + "<img src=\"./rwt-resources/icons/loading.gif\" width=\"32\" height=\"32\" style=\"margin: 16px 16px\"/>"
168 + "</div>";
169 }