Skip to main content
summaryrefslogtreecommitdiffstats
blob: 37a0bb5885074c0053ce0d012d45d2e29ab6d5bd (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
/**
 * Copyright (c) 2011, 2014 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
 * 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: 
 * 		Florian Pirchner - Initial implementation
 */
package org.eclipse.osbp.vaaclipse.common.ecview;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;

import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.ui.advanced.MPerspective;
import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
import org.eclipse.e4.ui.workbench.modeling.EModelService;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.osbp.ecview.core.common.context.IViewContext;
import org.eclipse.osbp.ecview.core.common.editpart.IViewEditpart;
import org.eclipse.osbp.ecview.core.common.model.core.YElement;
import org.eclipse.osbp.ecview.core.common.notification.ILifecycleEvent;
import org.eclipse.osbp.ecview.core.common.services.IWidgetAssocationsService;
import org.eclipse.osbp.vaaclipse.common.ecview.api.IECViewContainer;
import org.eclipse.osbp.vaaclipse.publicapi.events.IWidgetModelAssociations;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;

public class ECViewContainer implements IECViewContainer, EventHandler, IWidgetModelAssociations {

	private static final String ECVIEWS = "ecviews";

	@Inject
	private IEventBroker eventBroker;

	@Inject
	private EModelService modelService;

	@Inject
	private MApplication mApp;

	@Inject
	private IWidgetModelAssociations masterAssociations;

	private List<IViewEditpart> views = Collections.synchronizedList(new ArrayList<IViewEditpart>());

	@Override
	public List<IViewEditpart> getECViews() {
		return Collections.unmodifiableList(views);
	}

	@Override
	public void handleEvent(Event event) {
		ILifecycleEvent lifecycle = (ILifecycleEvent) event.getProperty(IEventBroker.DATA);
		if (lifecycle.getType().equals(ILifecycleEvent.CONTEXT_CREATED)) {
			IViewEditpart editpart = (IViewEditpart) lifecycle.getEditpart();
			if (!views.contains(editpart)) {
				views.add(editpart);

				MPerspective mPersp = modelService.getActivePerspective(mApp.getChildren().get(0));
				if (mPersp != null) {
					@SuppressWarnings("unchecked")
					List<IViewContext> contexts = (List<IViewContext>) mPersp.getTransientData().get(ECVIEWS);
					if (contexts == null) {
						contexts = new ArrayList<>();
						mPersp.getTransientData().put(ECVIEWS, contexts);
					}
					contexts.add(editpart.getContext());
				}

			}
		} else if (lifecycle.getType().equals(ILifecycleEvent.CONTEXT_DISPOSED)) {
			IViewEditpart editpart = (IViewEditpart) lifecycle.getEditpart();
			views.remove(editpart);
			MPerspective mPersp = modelService.getActivePerspective(mApp.getChildren().get(0));
			if (mPersp != null) {
				@SuppressWarnings("unchecked")
				List<IViewContext> contexts = (List<IViewContext>) mPersp.getTransientData().get(ECVIEWS);
				if (contexts != null) {
					contexts.remove(editpart.getContext());
				}
			}
		}
	}

	@PostConstruct
	protected void setup() {
		// lets register this manager as a thirdparty delegate to find
		// assocations
		masterAssociations.addThirdParty(this);
		eventBroker.subscribe(IViewContext.TOPIC_LIFECYCLE, this);
	}

	@PreDestroy
	protected void destroy() {
		eventBroker.unsubscribe(this);
		masterAssociations.removeThirdParty(this);
	}

	@Override
	public EObject getElement(Object component) {
		synchronized (views) {
			for (IViewEditpart ep : views) {
				IWidgetAssocationsService<Object, YElement> service = ep.getContext()
						.getService(IWidgetAssocationsService.ID);
				EObject result = service.getModelElement(component);
				if (result != null) {
					return result;
				}
			}
		}

		return null;
	}

	@Override
	public Object getWidget(EObject element) {
		if (!(element instanceof YElement)) {
			return null;
		}
		synchronized (views) {
			for (IViewEditpart ep : views) {
				IWidgetAssocationsService<Object, YElement> service = ep.getContext()
						.getService(IWidgetAssocationsService.ID);
				Object result = service.getWidget((YElement) element);
				if (result != null) {
					return result;
				}
			}
		}

		return null;
	}

	@Override
	public boolean addThirdParty(IWidgetModelAssociations e) {
		throw new UnsupportedOperationException(
				"Not allowed for a thirdparty assocation! Use the main assocation instance to register thirdparties.");
	}

	@Override
	public boolean removeThirdParty(IWidgetModelAssociations o) {
		throw new UnsupportedOperationException(
				"Not allowed for a thirdparty assocation! Use the main assocation instance to register thirdparties.");
	}

	@Override
	public List<IViewContext> getECViews(MPerspective perspective) {
		MPerspective mPersp = modelService.getActivePerspective(mApp.getChildren().get(0));
		if (mPersp != null) {
			@SuppressWarnings("unchecked")
			List<IViewContext> contexts = (List<IViewContext>) mPersp.getTransientData().get(ECVIEWS);
			if (contexts != null) {
				return Collections.unmodifiableList(contexts);
			}
		}
		return Collections.emptyList();
	}

}

Back to the top