blob: e9695aaba4551572a624d250e410bb02e1adc839 [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,
* Olivier L. Larouche - initial API and implementation
>>>>>>> refs/heads/eclipse_pa
* SPDX-License-Identifier: EPL-1.0
*
*******************************************************************************/
package org.eclipse.apogy.common.emf;
import java.util.List;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
public class EListAdapter<T> implements Adapter {
private final ListEventDelegate<T> delegate;
private final int expectedFeatureId;
private final Class<?> expectedClass;
/**
* Creates a new instance.
*
* @param expectedFeatureId the feature id we want to listen to.
* @param delegate the delegate beeing used to delegate the events.
* @param expectedClass the class we are listening.
*
* @throws IllegalArgumentException if delegate or expectedClass is null.
*/
public EListAdapter(int expectedFeatureId, ListEventDelegate<T> delegate, Class<?> expectedClass) {
if (delegate == null) {
throw new IllegalArgumentException("delegate is null");
}
if (expectedClass == null) {
throw new IllegalArgumentException("expectedClass is null");
}
this.delegate = delegate;
this.expectedFeatureId = expectedFeatureId;
this.expectedClass = expectedClass;
}
@Override
public Notifier getTarget() {
return null;
}
@Override
public boolean isAdapterForType(Object type) {
return false;
}
@SuppressWarnings("unchecked")
@Override
public void notifyChanged(Notification notification) {
int featureId = notification.getFeatureID(this.expectedClass);
if (featureId == this.expectedFeatureId) {
if (notification.getEventType() == Notification.ADD) {
if (notification.getNewValue() != null) {
T element = (T) notification.getNewValue();
this.delegate.added(element);
}
} else if (notification.getEventType() == Notification.ADD_MANY) {
if (notification.getNewValue() != null) {
List<T> elements = (List<T>) notification.getNewValue();
this.delegate.addedMany(elements);
}
} else if (notification.getEventType() == Notification.REMOVE) {
if (notification.getOldValue() != null) {
T element = (T) notification.getOldValue();
this.delegate.removed(element);
}
} else if (notification.getEventType() == Notification.REMOVE_MANY) {
if (notification.getOldValue() != null) {
List<T> elements = (List<T>) notification.getOldValue();
this.delegate.removedMany(elements);
}
}
}
}
@Override
public void setTarget(Notifier newTarget) {
}
}