blob: b286358dddf5d484a8ac5e4dd6392a7ad8c77f2e [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2018 Agence spatiale canadienne / Canadian Space Agency
* 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:
* Pierre Allard,
* Regent L'Archeveque,
* Sebastien Gemme - initial API and implementation
* SPDX-License-Identifier: EPL-1.0
*
*******************************************************************************/
package org.eclipse.apogy.common.topology.bindings.impl;
import org.eclipse.apogy.common.emf.ApogyCommonEMFFactory;
import org.eclipse.apogy.common.emf.ApogyCommonEMFPackage;
import org.eclipse.apogy.common.emf.FeatureNodeAdapter;
import org.eclipse.apogy.common.topology.bindings.Activator;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.ecore.EObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class AbstractTopologyBindingCustomImpl extends AbstractTopologyBindingImpl {
private static final Logger Logger = LoggerFactory.getLogger(AbstractTopologyBindingImpl.class);
protected Adapter adapter = null;
@Override
public void setSource(EObject newSource) {
if (isBinded()) {
// Unbind from the previous source.
unbind();
// Updates the source of the FeatureNodeAdapter.
getFeatureNodeAdapter().setSourceObject(newSource);
// Updates the source.
super.setSource(newSource);
// Binds to the new source.
bind();
} else {
// Updates the source.
super.setSource(newSource);
}
super.setSource(newSource);
}
@Override
public FeatureNodeAdapter getFeatureNodeAdapter() {
if (super.getFeatureNodeAdapter() == null) {
this.featureNodeAdapter = ApogyCommonEMFFactory.eINSTANCE.createFeatureNodeAdapter();
this.featureNodeAdapter.eAdapters().add(getAdapter());
}
return super.getFeatureNodeAdapter();
}
@Override
public void bind() {
// Register listener to the current source.
if (getSource() != null) {
// Sets the FeatureNodeAdapter attributes.
getFeatureNodeAdapter().setSourceObject(getSource());
getFeatureNodeAdapter().setFeatureNode(getFeatureNode());
// Updates value.
valueChanged(getFeatureNodeAdapter().getCurrentValue());
// Updates the binded attribute.
setBinded(true);
// Adds the binding to the registry if required.
if (!Activator.getBindedBindings().contains(this)) {
Activator.getBindedBindings().add(this);
}
} else {
Logger.warn("Could not bind: source is not set");
}
}
@Override
public void unbind() {
// Unregister from the current source.
if (getSource() != null) {
getFeatureNodeAdapter().setSourceObject(null);
getFeatureNodeAdapter().setFeatureNode(null);
// Updates the binded attribute.
setBinded(false);
// Removes the binding from the registry.
Activator.getBindedBindings().remove(this);
}
}
protected abstract void valueChanged(Object newValue);
/**
* Returns the Adapter that listens to changes in the FeatureNode.
*
* @return
*/
protected Adapter getAdapter() {
if (this.adapter == null) {
this.adapter = new AdapterImpl() {
@Override
public void notifyChanged(Notification notification) {
if (notification.getFeatureID(
FeatureNodeAdapter.class) == ApogyCommonEMFPackage.FEATURE_NODE_ADAPTER__CURRENT_VALUE) {
valueChanged(notification.getNewValue());
}
}
};
}
return this.adapter;
}
} // AbstractTopologyBindingImpl