blob: a78542c668316bf8a79557997f1c2ddef7819cca [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 java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.eclipse.apogy.common.topology.GroupNode;
import org.eclipse.apogy.common.topology.Node;
import org.eclipse.apogy.common.topology.bindings.AbstractTopologyBinding;
import org.eclipse.apogy.common.topology.bindings.EnumerationCase;
import org.eclipse.apogy.common.topology.bindings.EnumerationSwitchBinding;
import org.eclipse.emf.common.util.Enumerator;
import org.eclipse.emf.ecore.EEnumLiteral;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class EnumerationSwitchBindingCustomImpl extends EnumerationSwitchBindingImpl {
private static final Logger Logger = LoggerFactory.getLogger(EnumerationSwitchBindingImpl.class);
private final Map<EnumerationCase, Node> enumerationCaseToTopologyRootCopy = new HashMap<EnumerationCase, Node>();
@Override
protected synchronized void valueChanged(Object newValue) {
try {
Enumerator enumerator = (Enumerator) newValue;
EnumerationCase newCase = getEnumerationCase(enumerator);
// If a new case is now active.
if (newCase != getActiveCase()) {
// Deactivates the previous case.
deActivateCase(getActiveCase());
// Activates the new case.
activateCase(newCase);
}
} catch (Throwable t) {
Logger.error(t.getMessage(), t);
}
}
@Override
public AbstractTopologyBinding clone(Map<Node, Node> originalToCopyNodeMap) {
EnumerationSwitchBinding enumerationSwitchBindingCopy = EcoreUtil.copy(this);
enumerationSwitchBindingCopy.setParentNode((GroupNode) originalToCopyNodeMap.get(this.getParentNode()));
return enumerationSwitchBindingCopy;
}
private void deActivateCase(EnumerationCase enumerationCase) {
if (enumerationCase != null) {
// If a sub-topology is defined.
Node topologyRoot = this.enumerationCaseToTopologyRootCopy.get(enumerationCase);
if (topologyRoot != null) {
getParentNode().getChildren().remove(topologyRoot);
}
}
}
private void activateCase(EnumerationCase enumerationCase) {
if (enumerationCase != null && enumerationCase.getTopologyRoot() != null) {
Node topologyRoot = this.enumerationCaseToTopologyRootCopy.get(enumerationCase);
if (topologyRoot == null) {
// Copies the topologyRoot and put it in the
// enumerationCaseToTopologyRootCopy
// map.
topologyRoot = EcoreUtil.copy(enumerationCase.getTopologyRoot());
this.enumerationCaseToTopologyRootCopy.put(enumerationCase, topologyRoot);
}
getParentNode().getChildren().add(topologyRoot);
}
setActiveCase(enumerationCase);
}
private EnumerationCase getEnumerationCase(Enumerator enumerator) {
EnumerationCase enumerationCase = null;
if (enumerator != null) {
// Check each case until a case that match the enumLiteral is found.
Iterator<EnumerationCase> it = getCases().iterator();
while (enumerationCase == null && it.hasNext()) {
EnumerationCase tmp = it.next();
for (EEnumLiteral literal : tmp.getEnumerationLiterals()) {
if (literal.getValue() == enumerator.getValue()) {
enumerationCase = tmp;
}
}
}
}
// If no case has been found or the enumerator is null, tries to find a
// default
// case, which is one with no literal defined.
Iterator<EnumerationCase> it = getCases().iterator();
while (enumerationCase == null && it.hasNext()) {
EnumerationCase tmp = it.next();
if (tmp.getEnumerationLiterals().isEmpty()) {
enumerationCase = tmp;
}
}
return enumerationCase;
}
@Override
public Class<?> getSupportedFeatureType() {
return Enumerator.class;
}
@Override
public void unbind() {
try {
deActivateCase(getActiveCase());
this.enumerationCaseToTopologyRootCopy.clear();
} catch (Throwable t) {
Logger.error(t.getMessage(), t);
}
super.unbind();
}
} // EnumerationSwitchBindingImpl