blob: 5638ac43901e9fd566380c6d9b02e122fc95f997 [file] [log] [blame]
/**
* 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 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:
* 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.MPart;
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)) {
return;
}
views.add(editpart);
if (editpart.getContext() == null) {
return;
}
MPart part = editpart.getContext().getService(MPart.class.getName());
if (part == null) {
return;
}
MPerspective mPersp = modelService.getPerspectiveFor(part);
if (mPersp == null) {
return;
}
@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);
if (editpart.getContext() == null) {
return;
}
MPart part = editpart.getContext().getService(MPart.class.getName());
if (part == null) {
return;
}
MPerspective mPersp = modelService.getPerspectiveFor(part);
if (mPersp == null) {
return;
}
@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 mPersp) {
if (mPersp != null) {
@SuppressWarnings("unchecked")
List<IViewContext> contexts = (List<IViewContext>) mPersp.getTransientData().get(ECVIEWS);
if (contexts != null) {
return Collections.unmodifiableList(contexts);
}
}
return Collections.emptyList();
}
}