]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/CmsApplication.java
Work on rap migration.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / 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.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;
24
25 /** Configures an Argeo CMS RWT application. */
26 public class CmsApplication implements CmsConstants, ApplicationConfiguration,
27 BundleContextAware {
28 final static Log log = LogFactory.getLog(CmsApplication.class);
29
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>>();
33
34 private List<String> resources = new ArrayList<String>();
35
36 // private Bundle clientScriptingBundle;
37 private BundleContext bundleContext;
38
39 public void configure(Application application) {
40 try {
41 application.setOperationMode(OperationMode.SWT_COMPATIBILITY);
42 application.setExceptionHandler(new CmsExceptionHandler());
43
44 // TODO load all pics under icons
45 // loading animated gif
46 application.addResource(LOADING_IMAGE,
47 createResourceLoader(LOADING_IMAGE));
48 // empty image
49 application.addResource(NO_IMAGE, createResourceLoader(NO_IMAGE));
50
51 for (String resource : resources) {
52 // URL res = bundleContext.getBundle().getResource(resource);
53 // if (res == null)
54 // throw new CmsException("Resource " + resource
55 // + " not found");
56 application.addResource(resource, new BundleResourceLoader(
57 bundleContext));
58 if (log.isDebugEnabled())
59 log.debug("Registered resource " + resource);
60 }
61
62 // entry points
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(
71 // faviconRelPath);
72 application.addResource(faviconRelPath,
73 new BundleResourceLoader(bundleContext));
74 if (log.isTraceEnabled())
75 log.trace("Registered favicon " + faviconRelPath);
76
77 }
78
79 if (!properties.containsKey(WebClient.BODY_HTML))
80 properties.put(WebClient.BODY_HTML,
81 DEFAULT_LOADING_BODY);
82 }
83
84 application.addEntryPoint("/" + entryPoint,
85 entryPoints.get(entryPoint), properties);
86 log.info("Registered entry point /" + entryPoint);
87 }
88
89 // stylesheets
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);
94 // if (res == null)
95 // throw new CmsException("Stylesheet " + css
96 // + " not found");
97 application.addStyleSheet(themeId, css,
98 new BundleResourceLoader(bundleContext));
99 }
100
101 }
102
103 application.addPhaseListener(new CmsPhaseListener());
104
105 // registerClientScriptingResources(application);
106 } catch (RuntimeException e) {
107 // Easier access to initialisation errors
108 log.error("Unexpected exception when configuring RWT application.",
109 e);
110 throw e;
111 }
112 }
113
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";
119 // try {
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);
127 // }
128 // }
129 // }
130
131 private static ResourceLoader createResourceLoader(final String resourceName) {
132 return new ResourceLoader() {
133 public InputStream getResourceAsStream(String resourceName)
134 throws IOException {
135 return getClass().getClassLoader().getResourceAsStream(
136 resourceName);
137 }
138 };
139 }
140
141 public void setEntryPoints(
142 Map<String, EntryPointFactory> entryPointFactories) {
143 this.entryPoints = entryPointFactories;
144 }
145
146 public void setEntryPointsBranding(
147 Map<String, Map<String, String>> entryPointBranding) {
148 this.entryPointsBranding = entryPointBranding;
149 }
150
151 public void setStyleSheets(Map<String, List<String>> styleSheets) {
152 this.styleSheets = styleSheets;
153 }
154
155 // public void setClientScriptingBundle(Bundle clientScriptingBundle) {
156 // this.clientScriptingBundle = clientScriptingBundle;
157 // }
158
159 public void setBundleContext(BundleContext bundleContext) {
160 this.bundleContext = bundleContext;
161 }
162
163 public void setResources(List<String> resources) {
164 this.resources = resources;
165 }
166
167 class CmsExceptionHandler implements ExceptionHandler {
168
169 @Override
170 public void handleException(Throwable throwable) {
171 CmsSession.current.get().exception(throwable);
172 }
173
174 }
175
176 class CmsPhaseListener implements PhaseListener {
177 private static final long serialVersionUID = -1966645586738534609L;
178
179 @Override
180 public PhaseId getPhaseId() {
181 return PhaseId.RENDER;
182 }
183
184 @Override
185 public void beforePhase(PhaseEvent event) {
186 CmsSession cmsSession = CmsSession.current.get();
187 String state = cmsSession.getState();
188 if (state == null)
189 cmsSession.navigateTo("~");
190 }
191
192 @Override
193 public void afterPhase(PhaseEvent event) {
194 }
195 }
196
197 /*
198 * TEXTS
199 */
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\"/>"
203 + "</div>";
204 }