| /** |
| * Copyright (c) 2011, 2014 - Lunifera GmbH (Gross Enzersdorf) |
| * 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.addons.app.webapp; |
| |
| import java.util.Collections; |
| import java.util.HashMap; |
| import java.util.HashSet; |
| import java.util.Map; |
| import java.util.Set; |
| |
| 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.workbench.UIEvents; |
| import org.eclipse.e4.ui.workbench.UIEvents.EventTags; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.osbp.vaaclipse.publicapi.events.IWidgetModelAssociations; |
| import org.osgi.service.event.Event; |
| import org.osgi.service.event.EventHandler; |
| |
| import com.vaadin.ui.Component; |
| |
| /** |
| * The Class WidgetModelAssociations. |
| */ |
| public class WidgetModelAssociations implements IWidgetModelAssociations, |
| EventHandler { |
| |
| /** The event broker. */ |
| @Inject |
| private IEventBroker eventBroker; |
| |
| /** The cache. */ |
| private Map<Object, EObject> cache = Collections |
| .synchronizedMap(new HashMap<Object, EObject>()); |
| |
| // third party assocations may provide widget to model element mappings for |
| /** The third party assocations. */ |
| // different UI models, like ECView |
| private Set<IWidgetModelAssociations> thirdPartyAssocations = new HashSet<IWidgetModelAssociations>(); |
| |
| /** |
| * Setup. |
| */ |
| @PostConstruct |
| void setup() { |
| eventBroker.subscribe(UIEvents.UIElement.TOPIC_WIDGET, this); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.publicapi.events.IWidgetModelAssociations#getElement(java.lang.Object) |
| */ |
| @Override |
| public EObject getElement(Object component) { |
| if (component == null) { |
| return null; |
| } |
| |
| // first, try to find the Vaaclipse component in the current cache |
| EObject result = cache.get(component); |
| |
| // if no widget could be found here, lets try the thirdparty |
| // associations like ECView |
| if (result == null) { |
| for (IWidgetModelAssociations thirdParty : thirdPartyAssocations) { |
| result = thirdParty.getElement(component); |
| if (result != null) { |
| break; |
| } |
| } |
| } |
| |
| // at last iterate all parent elements to find the next component in the |
| // Vaaclipse ui element hierarchy |
| if (result == null) { |
| if (component instanceof Component) { |
| result = getElementByParent(((Component) component).getParent()); |
| } |
| } |
| |
| return result; |
| } |
| |
| /** |
| * Gets the element by parent. |
| * |
| * @param component |
| * the component |
| * @return the element by parent |
| */ |
| public EObject getElementByParent(Object component) { |
| if (component == null) { |
| return null; |
| } |
| |
| EObject result = cache.get(component); |
| if (result == null) { |
| if (component instanceof Component) { |
| result = getElementByParent(((Component) component).getParent()); |
| } |
| } |
| |
| return result; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.publicapi.events.IWidgetModelAssociations#getWidget(org.eclipse.emf.ecore.EObject) |
| */ |
| @Override |
| public Object getWidget(EObject element) { |
| if (cache.values().contains(element)) { |
| for (Map.Entry<Object, EObject> entry : cache.entrySet()) { |
| if (entry.getValue() == element) { |
| return entry.getKey(); |
| } |
| } |
| } |
| |
| // if no widget could be found here, lets try the thirdparty |
| // associations |
| for (IWidgetModelAssociations thirdParty : thirdPartyAssocations) { |
| Object widget = thirdParty.getWidget(element); |
| if (widget != null) { |
| return widget; |
| } |
| } |
| |
| return null; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.osgi.service.event.EventHandler#handleEvent(org.osgi.service.event.Event) |
| */ |
| @Override |
| public void handleEvent(Event event) { |
| if (UIEvents.EventTypes.SET.equals(event.getProperty(EventTags.TYPE))) { |
| EObject changedObj = (EObject) event.getProperty(EventTags.ELEMENT); |
| Object widget = event.getProperty(EventTags.WIDGET); |
| if (widget == null) { |
| cache.remove(changedObj); |
| } else { |
| cache.put(widget, changedObj); |
| } |
| } |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.publicapi.events.IWidgetModelAssociations#addThirdParty(org.eclipse.osbp.vaaclipse.publicapi.events.IWidgetModelAssociations) |
| */ |
| public boolean addThirdParty(IWidgetModelAssociations e) { |
| return thirdPartyAssocations.add(e); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.publicapi.events.IWidgetModelAssociations#removeThirdParty(org.eclipse.osbp.vaaclipse.publicapi.events.IWidgetModelAssociations) |
| */ |
| public boolean removeThirdParty(IWidgetModelAssociations o) { |
| return thirdPartyAssocations.remove(o); |
| } |
| |
| /** |
| * Destroy. |
| */ |
| @PreDestroy |
| void destroy() { |
| eventBroker.unsubscribe(this); |
| } |
| |
| } |