blob: 29064b7e8469991c1eb71d1cffab141a21cbe966 [file] [log] [blame]
/**
********************************************************************************
* Copyright (c) 2021 Robert Bosch GmbH and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
********************************************************************************
*/
package org.eclipse.app4mc.amalthea.model.edit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.eclipse.app4mc.amalthea.model.AmaltheaPackage;
import org.eclipse.app4mc.amalthea.model.HWModel;
import org.eclipse.app4mc.amalthea.model.OSModel;
import org.eclipse.app4mc.amalthea.model.edit.os.container.OsDefinitionContainerIP;
import org.eclipse.app4mc.amalthea.model.edit.os.container.OsSemaphoreContainerIP;
import org.eclipse.app4mc.amalthea.model.provider.OSModelItemProvider;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CommandWrapper;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.provider.ViewerNotification;
public class ExtendedOSModelIP extends OSModelItemProvider {
private static final Set<EStructuralFeature> SPECIAL_FEATURES = new HashSet<>(
Arrays.asList(
AmaltheaPackage.eINSTANCE.getOSModel_SchedulerDefinitions(), // OS_MODEL__SCHEDULER_DEFINITIONS
AmaltheaPackage.eINSTANCE.getOSModel_SchedulingParameterDefinitions(), // OS_MODEL__SCHEDULING_PARAMETER_DEFINITIONS
AmaltheaPackage.eINSTANCE.getOSModel_OsOverheads(), // OS_MODEL__OS_OVERHEADS
AmaltheaPackage.eINSTANCE.getOSModel_Semaphores() // OS_MODEL__SEMAPHORES
));
private static final Set<Integer> SPECIAL_FEATURE_IDS =
SPECIAL_FEATURES.stream()
.mapToInt(EStructuralFeature::getFeatureID).boxed()
.collect(Collectors.toCollection(HashSet::new));
// Container item providers
protected OsDefinitionContainerIP osDefinitionCIP;
protected OsSemaphoreContainerIP osSemaphoreCIP;
public ExtendedOSModelIP(final AdapterFactory adapterFactory) {
super(adapterFactory);
}
public OsDefinitionContainerIP getOsDefinitionsContainerIP(final OSModel osModel) {
if (this.osDefinitionCIP == null) {
this.osDefinitionCIP = new OsDefinitionContainerIP(this.adapterFactory, osModel);
}
return this.osDefinitionCIP;
}
public OsSemaphoreContainerIP getOsSemaphoresContainerIP(final OSModel osModel) {
if (this.osSemaphoreCIP == null) {
this.osSemaphoreCIP = new OsSemaphoreContainerIP(this.adapterFactory, osModel);
}
return this.osSemaphoreCIP;
}
@Override
protected EStructuralFeature getChildFeature(Object object, Object child) {
for (EStructuralFeature feature : super.getChildrenFeatures(object)) {
if (isValidValue(object, child, feature)) {
return feature;
}
}
return null;
}
@Override
public Collection<? extends EStructuralFeature> getChildrenFeatures(final Object object) {
// Do NOT modify cached collection!
Set<? extends EStructuralFeature> result = new HashSet<>(super.getChildrenFeatures(object));
// reduce result
result.removeAll(SPECIAL_FEATURES);
return result;
}
@Override
public Collection<?> getChildren(final Object object) {
final List<Object> children = new ArrayList<>(super.getChildren(object));
final OSModel osModel = (OSModel) object;
// only display virtual folders if not empty (on top of the list)
if (!osModel.getSemaphores().isEmpty())
children.add(0, getOsSemaphoresContainerIP(osModel));
if (!(osModel.getSchedulerDefinitions().isEmpty()
&& osModel.getSchedulingParameterDefinitions().isEmpty()
&& osModel.getOsOverheads().isEmpty()))
children.add(0, getOsDefinitionsContainerIP(osModel));
return children;
}
@Override
protected Command createAddCommand(final EditingDomain domain, final EObject owner,
final EStructuralFeature feature, final Collection<?> collection, final int index) {
return createWrappedCommand(super.createAddCommand(domain, owner, feature, collection, index), owner, feature);
}
@Override
protected Command createRemoveCommand(final EditingDomain domain, final EObject owner,
final EStructuralFeature feature, final Collection<?> collection) {
return createWrappedCommand(super.createRemoveCommand(domain, owner, feature, collection), owner, feature);
}
protected Command createWrappedCommand(final Command command, final EObject owner, final EStructuralFeature feature) {
int featureID = feature.getFeatureID();
if (!SPECIAL_FEATURE_IDS.contains(featureID)) {
return command;
}
return new CommandWrapper(command) {
@Override
public Collection<?> getAffectedObjects() {
Collection<?> affected = super.getAffectedObjects();
if (affected.contains(owner)) {
if (featureID == AmaltheaPackage.OS_MODEL__SEMAPHORES) {
affected = Collections.singleton(getOsSemaphoresContainerIP((OSModel) owner));
} else if (featureID == AmaltheaPackage.OS_MODEL__SCHEDULER_DEFINITIONS
|| featureID == AmaltheaPackage.OS_MODEL__SCHEDULING_PARAMETER_DEFINITIONS
|| featureID == AmaltheaPackage.OS_MODEL__OS_OVERHEADS) {
affected = Collections.singleton(getOsDefinitionsContainerIP((OSModel) owner));
}
}
return affected;
}
};
}
@Override
public void dispose() {
if (this.osDefinitionCIP != null) {
this.osDefinitionCIP.dispose();
}
if (this.osSemaphoreCIP != null) {
this.osSemaphoreCIP.dispose();
}
super.dispose();
}
@Override
public void notifyChanged(final Notification notification) {
updateChildren(notification);
int featureID = notification.getFeatureID(HWModel.class);
if (SPECIAL_FEATURE_IDS.contains(featureID)) {
// update virtual folder labels
fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, true));
} else {
// default
super.notifyChanged(notification);
}
}
}