blob: 5b5228fc711a5e1b3b10a792c8819106af0c558d [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 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.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 {
@Inject
private IEventBroker eventBroker;
@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);
}
} else if (lifecycle.getType().equals(ILifecycleEvent.CONTEXT_DISPOSED)) {
IViewEditpart editpart = (IViewEditpart) lifecycle.getEditpart();
views.remove(editpart);
}
}
@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.");
}
}