/*
* License (BSD Style License):
* Copyright (c) 2011
* Software Engineering
* Department of Computer Science
* Technische Universität Darmstadt
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the Software Engineering Group or Technische
* Universität Darmstadt nor the names of its contributors may be used to
* endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package de.tud.cs.st.vespucci.vespucci_model.diagram.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.EObject;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
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.commands.EditElementCommand;
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.CreateRelationshipRequest;
/**
* @generated
*/
public class OutgoingCreateCommand extends EditElementCommand {
/**
* @generated
*/
private final EObject source;
/**
* @generated
*/
private final EObject target;
/**
* @generated
*/
private final de.tud.cs.st.vespucci.vespucci_model.Shape container;
/**
* @generated
*/
public OutgoingCreateCommand(CreateRelationshipRequest request, EObject source, EObject target) {
super(request.getLabel(), null, request);
this.source = source;
this.target = target;
container = deduceContainer(source, target);
}
/**
* @generated
*/
public boolean canExecute() {
if (source == null && target == null) {
return false;
}
if (source != null && false == source instanceof de.tud.cs.st.vespucci.vespucci_model.Shape) {
return false;
}
if (target != null && false == target instanceof de.tud.cs.st.vespucci.vespucci_model.Shape) {
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 de.tud.cs.st.vespucci.vespucci_model.diagram.edit.policies.VespucciBaseItemSemanticEditPolicy.getLinkConstraints()
.canCreateOutgoing_4003(getContainer(), getSource(), getTarget());
}
/**
* @generated NOT
* @author Artem Vovk
* @return Returns result of execution.
* @throws ExecutionException
*/
protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
if (!canExecute()) {
throw new ExecutionException("Invalid arguments in create link command");
}
de.tud.cs.st.vespucci.vespucci_model.Outgoing newElement = de.tud.cs.st.vespucci.vespucci_model.Vespucci_modelFactory.eINSTANCE
.createOutgoing();
getContainer().getTargetConnections().add(newElement);
newElement.setSource(getSource());
newElement.setTarget(getTarget());
doConfigure(newElement, monitor, info);
((CreateElementRequest) getRequest()).setNewElement(newElement);
return CommandResult.newOKCommandResult(newElement);
}
/**
* @generated
*/
protected void doConfigure(de.tud.cs.st.vespucci.vespucci_model.Outgoing newElement, IProgressMonitor monitor, IAdaptable info)
throws ExecutionException {
IElementType elementType = ((CreateElementRequest) getRequest()).getElementType();
ConfigureRequest configureRequest = new ConfigureRequest(getEditingDomain(), newElement, elementType);
configureRequest.setClientContext(((CreateElementRequest) getRequest()).getClientContext());
configureRequest.addParameters(getRequest().getParameters());
configureRequest.setParameter(CreateRelationshipRequest.SOURCE, getSource());
configureRequest.setParameter(CreateRelationshipRequest.TARGET, getTarget());
ICommand configureCommand = elementType.getEditCommand(configureRequest);
if (configureCommand != null && configureCommand.canExecute()) {
configureCommand.execute(monitor, info);
}
}
/**
* @generated
*/
protected void setElementToEdit(EObject element) {
throw new UnsupportedOperationException();
}
/**
* @generated
*/
protected de.tud.cs.st.vespucci.vespucci_model.Shape getSource() {
return (de.tud.cs.st.vespucci.vespucci_model.Shape) source;
}
/**
* @generated
*/
protected de.tud.cs.st.vespucci.vespucci_model.Shape getTarget() {
return (de.tud.cs.st.vespucci.vespucci_model.Shape) target;
}
/**
* @generated
*/
public de.tud.cs.st.vespucci.vespucci_model.Shape getContainer() {
return container;
}
/**
* Default approach is to traverse ancestors of the source to find instance of container.
* Modify with appropriate logic.
* @generated
*/
private static de.tud.cs.st.vespucci.vespucci_model.Shape deduceContainer(EObject source, EObject target) {
// 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 de.tud.cs.st.vespucci.vespucci_model.Shape) {
return (de.tud.cs.st.vespucci.vespucci_model.Shape) element;
}
}
return null;
}
}