blob: 01dc856c81c9efec1152340e5c846609179b2760 [file] [log] [blame]
package org.eclipse.papyrus.robotml.diagram.common.migration;
import java.util.Collection;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.papyrus.infra.architecture.ArchitectureDomainManager;
import org.eclipse.papyrus.infra.core.architecture.ArchitectureDescription;
import org.eclipse.papyrus.infra.core.architecture.RepresentationKind;
import org.eclipse.papyrus.infra.core.architecture.merged.MergedArchitectureContext;
import org.eclipse.papyrus.infra.core.architecture.merged.MergedArchitectureDescriptionLanguage;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.sasheditor.DiModelUtils;
import org.eclipse.papyrus.infra.emf.gmf.command.EMFtoGMFCommandWrapper;
import org.eclipse.papyrus.infra.gmfdiag.common.reconciler.DiagramReconciler_1_3_0;
import org.eclipse.papyrus.infra.gmfdiag.representation.PapyrusDiagram;
import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusView;
import org.eclipse.papyrus.infra.viewpoints.style.PapyrusViewStyle;
public class Neon2OxygenRobotMLDiagramReconciler extends DiagramReconciler_1_3_0 {
private static final String ARCHITECTURE_DIAGRAM_URI = "platform:/plugin/org.eclipse.papyrus.robotml.diagram.common/viewpoints/robotml.configuration#_0kzWAPL_EeONXPQ2mbw9vA";
private static final String COMPONENTDEF_DIAGRAM_URI = "platform:/plugin/org.eclipse.papyrus.robotml.diagram.common/viewpoints/robotml.configuration#_0kzWBfL_EeONXPQ2mbw9vA";
private static final String DATATYPEDEF_DIAGRAM_URI = "platform:/plugin/org.eclipse.papyrus.robotml.diagram.common/viewpoints/robotml.configuration#_0kzWCvL_EeONXPQ2mbw9vA";
private static final String INTERFACEDEF_DIAGRAM_URI = "platform:/plugin/org.eclipse.papyrus.robotml.diagram.common/viewpoints/robotml.configuration#_0kz9EvL_EeONXPQ2mbw9vA";
private static final String CONTEXT_ID = "org.eclipse.papyrus.robotml.context";
/**
* @see org.eclipse.papyrus.infra.gmfdiag.common.reconciler.DiagramReconciler_1_3_0#getPapyrusDiagram(org.eclipse.papyrus.infra.viewpoints.style.PapyrusViewStyle)
*
* @param oldStyle
* @return
*/
@Override
protected PapyrusDiagram getDiagramKind(Diagram diagram, PapyrusViewStyle oldStyle) {
if (oldStyle != null) {
PapyrusView configuration = oldStyle.getConfiguration();
if (configuration != null) {
URI uri = EcoreUtil.getURI(configuration);
if (uri != null) {
switch (uri.toString()) {
case ARCHITECTURE_DIAGRAM_URI:
return getPapyrusDiagramById("org.eclipse.papyrus.robotml.diagram.architecture");
case COMPONENTDEF_DIAGRAM_URI :
return getPapyrusDiagramById("org.eclipse.papyrus.robotml.diagram.componentdef");
case DATATYPEDEF_DIAGRAM_URI :
return getPapyrusDiagramById("org.eclipse.papyrus.robotml.diagram.datatypeD");
case INTERFACEDEF_DIAGRAM_URI :
return getPapyrusDiagramById("org.eclipse.papyrus.robotml.diagram.interfeceI");
default:
break;
}
}
}
}
return null;
}
@Override
public ICommand getReconcileCommand(Diagram diagram) {
ICommand reconcileCommand = super.getReconcileCommand(diagram);
if (reconcileCommand != null && diagram != null) {
Resource eResource = diagram.eResource();
if (eResource != null) {
ResourceSet resourceSet = eResource.getResourceSet();
if (resourceSet instanceof ModelSet) {
CompositeCommand compositeCommand = new CompositeCommand("Update viewpoints from configuration to architecture and set RobotML as default architecture");
compositeCommand.add(reconcileCommand);
Command setContextCommand = getSetContextCommand((ModelSet) resourceSet, CONTEXT_ID);
compositeCommand.add(EMFtoGMFCommandWrapper.wrap(setContextCommand));
return compositeCommand;
}
}
}
return reconcileCommand;
}
///////////////////////////////////////////////////////////////////////////////
// FIXME: should be in Papyrus core API
protected Command getSetContextCommand(final ModelSet modelSet, final String contextId) {
return new RecordingCommand(modelSet.getTransactionalEditingDomain()) {
@Override
protected void doExecute() {
ArchitectureDescription description = DiModelUtils.getOrAddArchitectureDescription(modelSet);
description.setContextId(contextId);
}
};
}
protected PapyrusDiagram getPapyrusDiagramById(String id) {
ArchitectureDomainManager manager = ArchitectureDomainManager.getInstance();
Collection<MergedArchitectureContext> visibleArchitectureContexts = manager.getVisibleArchitectureContexts();
for (MergedArchitectureContext mergedArchitectureContext : visibleArchitectureContexts) {
if (mergedArchitectureContext instanceof MergedArchitectureDescriptionLanguage) {
MergedArchitectureDescriptionLanguage mergedArchitectureDescriptionLanguage = (MergedArchitectureDescriptionLanguage) mergedArchitectureContext;
for (RepresentationKind representationKind : mergedArchitectureDescriptionLanguage
.getRepresentationKinds()) {
if (representationKind instanceof PapyrusDiagram && representationKind.getId().equals(id)) {
return (PapyrusDiagram) representationKind;
}
}
}
}
return null;
}
////////////////////////////////////////////////////////////////////////////////
}