View Javadoc
1   package org.argeo.cms.e4.rap;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import org.argeo.cms.ui.dialogs.CmsFeedback;
7   import org.eclipse.rap.e4.E4ApplicationConfig;
8   import org.eclipse.rap.rwt.application.Application;
9   import org.eclipse.rap.rwt.application.Application.OperationMode;
10  import org.eclipse.rap.rwt.application.ApplicationConfiguration;
11  import org.eclipse.rap.rwt.application.ExceptionHandler;
12  import org.eclipse.rap.rwt.client.WebClient;
13  import org.osgi.framework.BundleContext;
14  
15  /** Base class for CMS RAP applications. */
16  public abstract class AbstractRapE4App implements ApplicationConfiguration {
17  	private String e4Xmi;
18  	private String path;
19  	private String lifeCycleUri = "bundleclass://org.argeo.cms.e4.rap/org.argeo.cms.e4.rap.CmsLoginLifecycle";
20  
21  	private Map<String, String> baseProperties = new HashMap<String, String>();
22  
23  	private BundleContext bundleContext;
24  	public final static String CONTEXT_NAME_PROPERTY = "contextName";
25  	private String contextName;
26  
27  	/**
28  	 * To be overridden in order to add multiple entry points, directly or using
29  	 * {@link #addE4EntryPoint(Application, String, String, Map)}.
30  	 */
31  	protected void addEntryPoints(Application application) {
32  	}
33  
34  	public void configure(Application application) {
35  		application.setExceptionHandler(new ExceptionHandler() {
36  
37  			@Override
38  			public void handleException(Throwable throwable) {
39  				CmsFeedback.show("Unexpected RWT exception", throwable);
40  			}
41  		});
42  
43  		if (e4Xmi != null) {// backward compatibility
44  			addE4EntryPoint(application, path, e4Xmi, getBaseProperties());
45  		} else {
46  			addEntryPoints(application);
47  		}
48  	}
49  
50  	protected Map<String, String> getBaseProperties() {
51  		return baseProperties;
52  	}
53  
54  //	protected void addEntryPoint(Application application, E4ApplicationConfig config, Map<String, String> properties) {
55  //		CmsE4EntryPointFactory entryPointFactory = new CmsE4EntryPointFactory(config);
56  //		application.addEntryPoint(path, entryPointFactory, properties);
57  //		application.setOperationMode(OperationMode.SWT_COMPATIBILITY);
58  //	}
59  
60  	protected void addE4EntryPoint(Application application, String path, String e4Xmi, Map<String, String> properties) {
61  		E4ApplicationConfig config = createE4ApplicationConfig(e4Xmi);
62  		CmsE4EntryPointFactory entryPointFactory = new CmsE4EntryPointFactory(config);
63  		application.addEntryPoint(path, entryPointFactory, properties);
64  		application.setOperationMode(OperationMode.SWT_COMPATIBILITY);
65  	}
66  
67  	/**
68  	 * To be overridden for further configuration.
69  	 * 
70  	 * @see E4ApplicationConfig
71  	 */
72  	protected E4ApplicationConfig createE4ApplicationConfig(String e4Xmi) {
73  		return new E4ApplicationConfig(e4Xmi, lifeCycleUri, null, null, false, true, true);
74  	}
75  
76  	@Deprecated
77  	public void setPageTitle(String pageTitle) {
78  		if (pageTitle != null)
79  			baseProperties.put(WebClient.PAGE_TITLE, pageTitle);
80  	}
81  
82  	/** Returns a new map used to customise and entry point. */
83  	public Map<String, String> customise(String pageTitle) {
84  		Map<String, String> custom = new HashMap<>(getBaseProperties());
85  		if (pageTitle != null)
86  			custom.put(WebClient.PAGE_TITLE, pageTitle);
87  		return custom;
88  	}
89  
90  	@Deprecated
91  	public void setE4Xmi(String e4Xmi) {
92  		this.e4Xmi = e4Xmi;
93  	}
94  
95  	@Deprecated
96  	public void setPath(String path) {
97  		this.path = path;
98  	}
99  
100 	public void setLifeCycleUri(String lifeCycleUri) {
101 		this.lifeCycleUri = lifeCycleUri;
102 	}
103 
104 	protected BundleContext getBundleContext() {
105 		return bundleContext;
106 	}
107 
108 	public String getContextName() {
109 		return contextName;
110 	}
111 
112 	public void setContextName(String contextName) {
113 		this.contextName = contextName;
114 	}
115 
116 	public void init(BundleContext bundleContext, Map<String, Object> properties) {
117 		this.bundleContext = bundleContext;
118 		for (String key : properties.keySet()) {
119 			Object value = properties.get(key);
120 			if (value != null)
121 				baseProperties.put(key, value.toString());
122 		}
123 
124 		if (properties.containsKey(CONTEXT_NAME_PROPERTY)) {
125 			assert properties.get(CONTEXT_NAME_PROPERTY) != null;
126 			contextName = properties.get(CONTEXT_NAME_PROPERTY).toString();
127 		} else {
128 			contextName = "<unknown context>";
129 		}
130 	}
131 
132 	public void destroy(Map<String, Object> properties) {
133 
134 	}
135 }