blob: 69bd895b7c4cce592f8ada6724c808221c39ef16 [file] [log] [blame]
package org.eclipse.uml2.diagram.csd.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.csd.edit.helpers.AssociationEditHelper;
import org.eclipse.uml2.diagram.csd.edit.policies.UMLBaseItemSemanticEditPolicy;
import org.eclipse.uml2.uml.AggregationKind;
import org.eclipse.uml2.uml.Association;
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 AssociationCreateCommand extends CreateElementCommand {
/**
* @generated
*/
private final EObject source;
/**
* @generated
*/
private final EObject target;
/**
* @generated
*/
private Package container;
/**
* @generated
*/
public AssociationCreateCommand(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.canCreateAssociation_4011(getContainer(), getSource(), getTarget());
}
/**
* @generated NOT
*/
protected EObject doDefaultElementCreation() {
Type sourceType = (Type) getSource();
Type targetType = (Type) getTarget();
//due to association end conventiontions (see AssociationEndConvention)
//we need to have member end of type SourceType to be the first one created
//thus, we are calling UML2 createAssociation() in opposite order
boolean setNavigability = getCreateRequest().getParameter(AssociationEditHelper.PARAMETER_SET_TARGET_NAVIGABILITY) != null;
Association result = targetType.createAssociation(//
false, AggregationKind.NONE_LITERAL, "src", 1, 1, //
sourceType, setNavigability, AggregationKind.NONE_LITERAL, "dst", 1, 1);
return result;
}
/**
* @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;
}
}