blob: 772695c07ce26f1ba8cacfea6a07b67b328a52aa [file] [log] [blame]
package org.eclipse.papyrus.robotics.library.advice;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.edithelper.AbstractEditHelperAdvice;
import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.GetEditContextRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest;
import org.eclipse.papyrus.designer.languages.common.base.ElementUtils;
import org.eclipse.papyrus.infra.emf.gmf.command.EMFtoGMFCommandWrapper;
import org.eclipse.papyrus.uml.tools.utils.StereotypeUtil;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Property;
public class ThreadAdvice extends AbstractEditHelperAdvice {
/**
* Allow creation only if container is a component definition
*/
protected boolean approveCreateElementRequest(CreateElementRequest request) {
IElementType type = request.getElementType();
EObject container = request.getContainer();
if (type != null && container instanceof Class) {
if (StereotypeUtil.isApplied((Class) container, "robotics::components::ComponentDefinition")) {
return true;
}
}
return false;
}
/**
* qualified name of the thread element within the RobMoSys library
*/
public static final String RMS_THREAD_QN = "robotics::execcontainer::Thread";
@Override
public boolean approveRequest(IEditCommandRequest request) {
if (request instanceof GetEditContextRequest) {
GetEditContextRequest context = (GetEditContextRequest) request;
if (context.getEditCommandRequest() instanceof CreateElementRequest) {
return approveCreateElementRequest((CreateElementRequest) context.getEditCommandRequest());
}
}
return super.approveRequest(request);
}
/**
* @see org.eclipse.gmf.runtime.emf.type.core.edithelper.AbstractEditHelperAdvice#getAfterConfigureCommand(ogrg.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest)
*
* Add data-type.
*/
@Override
protected ICommand getAfterConfigureCommand(ConfigureRequest request) {
// Get element from the request
EObject newElement = request.getElementToConfigure();
// Check it is a kind of Class
if (!(newElement instanceof Class)) {
//Activator.log.debug("");
return super.getAfterConfigureCommand(request);
}
Class cls = (Class) newElement;
// Check it is a nested Classifier of a ComponentDefinition
Element eClsOwner = cls.getOwner();
if (!(eClsOwner instanceof Class && StereotypeUtil.isApplied((Class) eClsOwner, "robotics::components::ComponentDefinition"))) {
//Activator.log.debug("");
return super.getAfterConfigureCommand(request);
}
Class clsOwner = (Class) eClsOwner;
// Check the associated RS element that corresponds to a Thread is a Classifier
NamedElement threadType = ElementUtils.getQualifiedElementFromRS(cls, RMS_THREAD_QN);
if (!(threadType instanceof Classifier)) {
//Activator.log.debug("");
return super.getAfterConfigureCommand(request);
}
// build commands
CompositeCommand compositeCommand = new CompositeCommand("Activity creation command");
RecordingCommand typeElement = new RecordingCommand(TransactionUtil.getEditingDomain(newElement)) {
@Override
protected void doExecute() {
// create generalization relationship so that the cls specializes Thread
cls.createGeneralization((Classifier) threadType);
// apply Activity stereotype to cls
StereotypeUtil.apply(cls, "robotics::components::Activity");
// give cls a name
int idx = 1;
List<String> allNamesOfExistingActivities = new ArrayList<String>();
for (Classifier c : clsOwner.getNestedClassifiers())
if (c instanceof Class && StereotypeUtil.isApplied(c,"robotics::components::Activity"))
allNamesOfExistingActivities.add(c.getName());
while(allNamesOfExistingActivities.contains("Activity"+idx))
idx = idx+1;
cls.setName("Activity"+idx);
// create a part that instantiates the nested classifier
Property clsPart = clsOwner.createOwnedAttribute("activity"+idx, cls);
// apply ActivityInstance stereotype to the part
StereotypeUtil.apply(clsPart, "robotics::components::ActivityInstance");
}
};
compositeCommand.add(EMFtoGMFCommandWrapper.wrap(typeElement));
return compositeCommand.isEmpty() ? super.getAfterConfigureCommand(request) : compositeCommand;
}
}