blob: 8013d2f45df55f23c430fa5e01b00cfc5f3d4c5c [file] [log] [blame]
package org.eclipse.uml2.diagram.deploy.edit.commands;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.emf.type.core.commands.CreateElementCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest;
import org.eclipse.uml2.diagram.deploy.communication.CommunicationPathOperations;
import org.eclipse.uml2.diagram.deploy.edit.policies.UMLBaseItemSemanticEditPolicy;
import org.eclipse.uml2.uml.AggregationKind;
import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.CommunicationPath;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.UMLFactory;
import org.eclipse.uml2.uml.UMLPackage;
/**
* @generated
*/
public class CommunicationPathCreateCommand extends CreateElementCommand {
/**
* @generated
*/
private final EObject source;
/**
* @generated
*/
private final EObject target;
/**
* @generated
*/
private Package container;
/**
* @generated
*/
public CommunicationPathCreateCommand(CreateRelationshipRequest request, EObject source, EObject target) {
super(request);
this.source = source;
this.target = target;
if (request.getContainmentFeature() == null) {
setContainmentFeature(UMLPackage.eINSTANCE.getPackage_PackagedElement());
}
// Find container element for the new link.
// Climb up by containment hierarchy starting from the source
// and return the first element that is instance of the container class.
for (EObject element = source; element != null; element = element.eContainer()) {
if (element instanceof Package) {
container = (Package) element;
super.setElementToEdit(container);
break;
}
}
}
/**
* @generated
*/
public boolean canExecute() {
if (source == null && target == null) {
return false;
}
if (source != null && false == source instanceof Type) {
return false;
}
if (target != null && false == target instanceof Type) {
return false;
}
if (getSource() == null) {
return true; // link creation is in progress; source is not defined yet
}
// target may be null here but it's possible to check constraint
if (getContainer() == null) {
return false;
}
return UMLBaseItemSemanticEditPolicy.LinkConstraints.canCreateCommunicationPath_4004(getContainer(), getSource(), getTarget());
}
/**
* @generated NOT
*/
protected EObject doDefaultElementCreation() {
Type sourceType = (Type) getSource();
Type targetType = (Type) getTarget();
Association path = CommunicationPathOperations.createCommunicationPath(//
targetType, false, AggregationKind.NONE_LITERAL, "src", 1, 1, //
sourceType, true, AggregationKind.NONE_LITERAL, "dst", 1, 1);
return path;
}
/**
* @generated
*/
protected EClass getEClassToEdit() {
return UMLPackage.eINSTANCE.getPackage();
}
/**
* @generated
*/
protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
if (!canExecute()) {
throw new ExecutionException("Invalid arguments in create link command"); //$NON-NLS-1$
}
return super.doExecuteWithResult(monitor, info);
}
/**
* @generated
*/
protected ConfigureRequest createConfigureRequest() {
ConfigureRequest request = super.createConfigureRequest();
request.setParameter(CreateRelationshipRequest.SOURCE, getSource());
request.setParameter(CreateRelationshipRequest.TARGET, getTarget());
return request;
}
/**
* @generated
*/
protected void setElementToEdit(EObject element) {
throw new UnsupportedOperationException();
}
/**
* @generated
*/
protected Type getSource() {
return (Type) source;
}
/**
* @generated
*/
protected Type getTarget() {
return (Type) target;
}
/**
* @generated
*/
public Package getContainer() {
return container;
}
}