Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a35860bca813cd82fc3d5f3dc85823427f737579 (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
/*******************************************************************************
 * Copyright (c) 2010, 2015 BestSolution.at and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
 * Lars Vogel <Lars.Vogel@gmail.com> - Bug 421453
 ******************************************************************************/
package org.eclipse.e4.tools.compat.internal;

import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
import org.eclipse.e4.core.contexts.EclipseContextFactory;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.services.log.ILoggerProvider;
import org.eclipse.e4.core.services.log.Logger;
import org.eclipse.e4.tools.services.IClipboardService;
import org.eclipse.e4.ui.css.swt.theme.IThemeEngine;
import org.eclipse.e4.ui.css.swt.theme.IThemeManager;
import org.eclipse.e4.ui.di.UISynchronize;
import org.eclipse.e4.ui.services.IServiceConstants;
import org.eclipse.e4.ui.services.IStylingEngine;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.internal.services.IWorkbenchLocationService;
import org.eclipse.ui.services.AbstractServiceFactory;
import org.eclipse.ui.services.IServiceLocator;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.w3c.dom.css.CSSStyleDeclaration;

@SuppressWarnings("restriction")
public class ContextServiceFactory extends AbstractServiceFactory {

	@Override
	public Object create(Class serviceInterface, IServiceLocator parentLocator,
		IServiceLocator locator) {
		if (!IEclipseContext.class.equals(serviceInterface)) {
			return null;
		}

		final IWorkbenchLocationService wls = locator.getService(IWorkbenchLocationService.class);
		final IWorkbenchWindow window = wls.getWorkbenchWindow();
		final IWorkbenchPartSite site = wls.getPartSite();

		@SuppressWarnings("unchecked")
		final Object o = parentLocator.getService(serviceInterface);

		// This happens when we run in plain 3.x
		// We need to create a parent service context
		if (window == null && site == null) {
			final Bundle bundle = FrameworkUtil.getBundle(ContextServiceFactory.class);
			final BundleContext bundleContext = bundle.getBundleContext();
			final IEclipseContext serviceContext = EclipseContextFactory.getServiceContext(bundleContext);

			final IEclipseContext appContext = serviceContext.createChild("WorkbenchContext"); //$NON-NLS-1$
			appContext.set(Display.class, Display.getCurrent());
			appContext.set(Logger.class, new WorkbenchLogger());
			appContext.set(IClipboardService.class, new ClipboardServiceImpl());
			appContext.set(Realm.class, Realm.getDefault());

			final Display d = Display.getCurrent();
			appContext.set(UISynchronize.class, new UISynchronize() {

				@Override
				public void syncExec(Runnable runnable) {
					d.syncExec(runnable);
				}

				@Override
				public void asyncExec(Runnable runnable) {
					d.asyncExec(runnable);
				}
			});

			final IThemeManager manager = serviceContext.get(IThemeManager.class);
			final IThemeEngine engine = manager.getEngineForDisplay(Display.getCurrent());
			appContext.set(IThemeEngine.class, engine);

			appContext.set(IStylingEngine.class, new IStylingEngine() {

				@Override
				public void setClassname(Object widget, String classname) {
					((Widget) widget).setData("org.eclipse.e4.ui.css.CssClassName", classname); //$NON-NLS-1$
					engine.applyStyles(widget, true);
				}

				@Override
				public void setId(Object widget, String id) {
					((Widget) widget).setData("org.eclipse.e4.ui.css.id", id); //$NON-NLS-1$
					engine.applyStyles(widget, true);
				}

				@Override
				public void style(Object widget) {
					engine.applyStyles(widget, true);
				}

				@Override
				public CSSStyleDeclaration getStyle(Object widget) {
					return engine.getStyle(widget);
				}

				@Override
				public void setClassnameAndId(Object widget, String classname,
					String id) {
					((Widget) widget).setData("org.eclipse.e4.ui.css.CssClassName", classname); //$NON-NLS-1$
					((Widget) widget).setData("org.eclipse.e4.ui.css.id", id); //$NON-NLS-1$
					engine.applyStyles(widget, true);
				}
			});

			if (appContext.get(ILoggerProvider.class) == null) {
				appContext.set(ILoggerProvider.class,
					ContextInjectionFactory.make(DefaultLoggerProvider.class, appContext));
			}

			return appContext;
		} else if (o != null && site == null) {
			final IEclipseContext windowContext = ((IEclipseContext) o).createChild("WindowContext(" + window + ")"); //$NON-NLS-1$ //$NON-NLS-2$
			windowContext.set(ISelectionService.class, window.getSelectionService());

			windowContext.declareModifiable(IServiceConstants.ACTIVE_SELECTION);
			window.getSelectionService().addSelectionListener(new ISelectionListener() {

				@Override
				public void selectionChanged(IWorkbenchPart part, ISelection selection) {
					if (!selection.isEmpty()) {
						if (selection instanceof IStructuredSelection) {
							final IStructuredSelection s = (IStructuredSelection) selection;
							if (s.size() == 1) {
								windowContext.set(IServiceConstants.ACTIVE_SELECTION, s.getFirstElement());
							} else {
								windowContext.set(IServiceConstants.ACTIVE_SELECTION, s.toList());
							}
						} else {
							windowContext.set(IServiceConstants.ACTIVE_SELECTION, selection);
						}
					}
				}
			});
			return windowContext;
		}

		return o;
	}
}

Back to the top