blob: 73a455fb21eef299aaa30f3d58292a087541e56c [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:
<<<<<<< HEAD
* Pierre Allard - initial API and implementation
* Regent L'Archeveque
*
=======
* Pierre Allard,
* Regent L'Archeveque - initial API and implementation
*
>>>>>>> refs/heads/eclipse_pa
* SPDX-License-Identifier: EPL-1.0
*
*******************************************************************************/
package org.eclipse.apogy.common.emf.databinding;
import org.eclipse.core.databinding.observable.Diffs;
import org.eclipse.core.databinding.observable.value.ValueDiff;
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.databinding.EObjectObservableValue;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.ExtendedMetaData;
public class EMFReferenceIObervableValue extends EObjectObservableValue {
protected Adapter valueInternalAdapter = null;
protected EObject featurevalue;
public EMFReferenceIObervableValue(EObject eObject, EStructuralFeature eStructuralFeature) {
super(eObject, eStructuralFeature);
this.featurevalue = (EObject) (ExtendedMetaData.INSTANCE.getAffiliation(eObject.eClass(),
eStructuralFeature) == null ? null : eObject.eGet(eStructuralFeature));
// Register interval value listener to new value if applicable.
if (this.featurevalue != null) {
this.featurevalue.eAdapters().add(getValueInternalAdapter());
}
}
@Override
protected Object doGetValue() {
return this.featurevalue;
}
@Override
protected void firstListenerAdded() {
this.listener = new AdapterImpl() {
@Override
public void notifyChanged(Notification notification) {
if (EMFReferenceIObervableValue.this.eStructuralFeature == notification.getFeature()
&& !notification.isTouch()) {
// Unregister interval value listener from old value if applicable.
if (notification.getOldValue() instanceof EObject) {
((EObject) notification.getOldValue()).eAdapters().remove(getValueInternalAdapter());
}
// Register interval value listener to new value if applicable.
if (notification.getNewValue() instanceof EObject) {
((EObject) notification.getNewValue()).eAdapters().add(getValueInternalAdapter());
EMFReferenceIObervableValue.this.featurevalue = ((EObject) notification.getNewValue());
} else {
EMFReferenceIObervableValue.this.featurevalue = null;
}
final ValueDiff<Object> diff = Diffs.createValueDiff(notification.getOldValue(),
notification.getNewValue());
getRealm().exec(new Runnable() {
@Override
public void run() {
fireValueChange(diff);
}
});
}
}
};
this.eObject.eAdapters().add(this.listener);
}
@Override
protected void lastListenerRemoved() {
// Removes valueInternalAdapter if applicable.
if (this.featurevalue != null)
this.featurevalue.eAdapters().remove(getValueInternalAdapter());
super.lastListenerRemoved();
}
protected Adapter getValueInternalAdapter() {
if (this.valueInternalAdapter == null) {
this.valueInternalAdapter = new AdapterImpl() {
@Override
public void notifyChanged(Notification msg) {
// Forces update
Object obj = msg.getNotifier();
if (obj instanceof EObject) {
EObject oldvalue = (EObject) obj;
EMFReferenceIObervableValue.this.featurevalue = EcoreUtil.copy(oldvalue);
final ValueDiff diff = Diffs.createValueDiff(null,
EMFReferenceIObervableValue.this.featurevalue);
getRealm().exec(new Runnable() {
@Override
public void run() {
fireValueChange(diff);
}
});
}
};
};
}
return this.valueInternalAdapter;
}
}