blob: 6045f15f372661b528de758566e71dd001c3f6e9 [file] [log] [blame]
package org.eclipse.uml2.diagram.clazz.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.clazz.edit.policies.UMLBaseItemSemanticEditPolicy;
import org.eclipse.uml2.uml.InstanceSpecification;
import org.eclipse.uml2.uml.InstanceValue;
import org.eclipse.uml2.uml.Slot;
import org.eclipse.uml2.uml.UMLFactory;
import org.eclipse.uml2.uml.UMLPackage;
/**
* @generated
*/
public class AssociationInstanceCreateCommand extends CreateElementCommand {
/**
* @generated
*/
private final EObject source;
/**
* @generated
*/
private final EObject target;
/**
* @generated
*/
private InstanceSpecification container;
/**
* @generated
*/
public AssociationInstanceCreateCommand(CreateRelationshipRequest request, EObject source, EObject target) {
super(request);
this.source = source;
this.target = target;
if (request.getContainmentFeature() == null) {
setContainmentFeature(UMLPackage.eINSTANCE.getInstanceSpecification_Slot());
}
// 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 InstanceSpecification) {
container = (InstanceSpecification) element;
super.setElementToEdit(container);
break;
}
}
}
/**
* @generated
*/
public boolean canExecute() {
if (source == null && target == null) {
return false;
}
if (source != null && false == source instanceof InstanceSpecification) {
return false;
}
if (target != null && false == target instanceof InstanceSpecification) {
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.canCreateSlot_4015(getContainer(), getSource(), getTarget());
}
/**
* @generated NOT
*/
protected EObject doDefaultElementCreation() {
Slot slot = UMLFactory.eINSTANCE.createSlot();
slot.setOwningInstance(getSource());
InstanceValue value = UMLFactory.eINSTANCE.createInstanceValue();
value.setInstance(getTarget());
slot.getValues().add(value);
return slot;
}
/**
* @generated
*/
protected EClass getEClassToEdit() {
return UMLPackage.eINSTANCE.getInstanceSpecification();
}
/**
* @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 InstanceSpecification getSource() {
return (InstanceSpecification) source;
}
/**
* @generated
*/
protected InstanceSpecification getTarget() {
return (InstanceSpecification) target;
}
/**
* @generated
*/
public InstanceSpecification getContainer() {
return container;
}
}