Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bf676a237cabe89d46ea9a5314a29ef116e825eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*******************************************************************************
 * Copyright (c) 2012 EclipseSource and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Ralf Sternberg - initial API and implementation
 *******************************************************************************/
package org.eclipse.rap.e4;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.e4.core.contexts.ContextInjectionFactory;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.ui.internal.workbench.E4Workbench;
import org.eclipse.e4.ui.internal.workbench.swt.E4Application;
import org.eclipse.e4.ui.workbench.IWorkbench;
import org.eclipse.e4.ui.workbench.lifecycle.PreSave;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.rap.rwt.application.EntryPoint;
import org.eclipse.rap.rwt.application.EntryPointFactory;
import org.eclipse.rap.rwt.service.ServerPushSession;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

@SuppressWarnings("restriction")
public class E4EntryPointFactory implements EntryPointFactory {

	private static final String PLUGIN_ID = "org.eclipse.e4.ui.workbench.rap";

	private String productName;

	private E4ApplicationConfig config;

	public E4EntryPointFactory(E4ApplicationConfig config) {
		this.config = config;
	}

	public EntryPoint create() {
		return new EntryPoint() {

			public int createUI() {
				try {
					// E4Starter.createAndRunApplication(productName);
					createWorkbench();
				} catch (Exception exception) {
					logProblem(exception);
				}
				return 0;
			}
		};
	}

	private int createWorkbench() {
		Display display = new Display();
		E4Application e4App = new E4Application();
		E4Workbench workbench = e4App.createE4Workbench(getApplicationContext(config),
				display);

		if( config.isDefaultPush() ) {
			ServerPushSession session = new ServerPushSession();
			session.start();
		}

//		instanceLocation = (Location) workbench.getContext().get(
//				E4Workbench.INSTANCE_LOCATION);
		Shell shell = display.getActiveShell();
		if (shell == null) {
			shell = new Shell();
			// place it off so it's not visible
			shell.setLocation(0, 10000);
		}
//		if (!checkInstanceLocation(instanceLocation, shell,
//				workbench.getContext()))
//			return EXIT_OK;

		if( workbench != null ) {
			IEclipseContext workbenchContext = workbench.getContext();
			IEclipseContext serviceContext = workbenchContext.getParent();

			// Create and run the UI (if any)
			workbench.createAndRunUI(workbench.getApplication());

			// Save the model into the targetURI
			if (e4App.lcManager != null) {
				ContextInjectionFactory.invoke(e4App.lcManager, PreSave.class,
						workbenchContext, null);
			}
			e4App.saveModel();
			workbench.close();
			serviceContext.dispose();

			if (workbench.isRestart()) {
				return IApplication.EXIT_RESTART;
			}
		}

		return IApplication.EXIT_OK;
	}

	private IApplicationContext getApplicationContext(final E4ApplicationConfig config) {
		return new IApplicationContext() {

			public org.osgi.framework.Bundle getBrandingBundle() {
				return null;
			}

			@Override
			public void setResult(Object result, IApplication application) {
				// TODO Auto-generated method stub

			}

			@Override
			public String getBrandingProperty(String key) {
				if( IWorkbench.XMI_URI_ARG.equals(key) ) {
					return config.getXmiURI();
				} else if( IWorkbench.LIFE_CYCLE_URI_ARG.equals(key) ) {
					return config.getLifecycleURI();
				} else if( IWorkbench.PRESENTATION_URI_ARG .equals(key)) {
					return config.getPresentationURI();
				} else if( IWorkbench.CLEAR_PERSISTED_STATE.equals(key) ) {
					return config.isClearPersistedState() + "";
				} else if( IWorkbench.PERSIST_STATE.equals(key) ) {
					return config.isPersistState() + "";
				}
				return null;
			}

			@Override
			public String getBrandingName() {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public String getBrandingId() {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public String getBrandingDescription() {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public String getBrandingApplication() {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public Map getArguments() {
				Map<Object, Object> rv = new HashMap<Object, Object>();
				rv.put(IApplicationContext.APPLICATION_ARGS, new String[0]);
				return rv;
			}

			@Override
			public void applicationRunning() {
				// TODO Auto-generated method stub

			}
		};
	}

	private void logProblem(Throwable problem) {
//		String message = "Error when starting application, productName="
//				+ productName;
//		Logger logger = new WorkbenchLogger(PLUGIN_ID);
//		logger.error(new RuntimeException(message, problem));
		System.err.println("Problem");
		problem.printStackTrace();
	}

}

Back to the top