/*
* GoalDecompositionANDCreateCommand.java
*
* This file is part of the STS-Tool project.
* Copyright (c) 2011-2012 "University of Trento - DISI" All rights reserved.
*
* Is strictly forbidden to remove this copyright notice from this source code.
*
* Disclaimer of Warranty:
* STS-Tool (this software) is provided "as-is" and without warranty of any kind,
* express, implied or otherwise, including without limitation, any warranty of
* merchantability or fitness for a particular purpose.
* In no event shall the copyright holder 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.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License version 3
* as published by the Free Software Foundation with the addition of the
* following permission added to Section 15 as permitted in Section 7(a):
* FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
* "University of Trento - DISI","University of Trento - DISI" DISCLAIMS THE
* WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, see http://www.gnu.org/licenses or write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA, 02110-1301 USA, or download the license from the following URL:
* http://www.sts-tool.eu/License.php
*
* For more information, please contact STS-Tool group at this
* address: ststool@disi.unitn.it
*
*/
package eu.aniketos.wp1.ststool.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;
import eu.aniketos.wp1.ststool.Goal;
import eu.aniketos.wp1.ststool.GoalDecompositionAND;
import eu.aniketos.wp1.ststool.StstoolFactory;
import eu.aniketos.wp1.ststool.diagram.edit.policies.StsToolBaseItemSemanticEditPolicy;
/**
* @generated
*/
public class GoalDecompositionANDCreateCommand extends EditElementCommand {
/**
* @generated
*/
private final EObject source;
/**
* @generated
*/
private final EObject target;
/**
* @generated
*/
private final Goal container;
/**
* @generated
*/
public GoalDecompositionANDCreateCommand(CreateRelationshipRequest request, EObject source, EObject target) {
super(request.getLabel(), null, request);
this.source = source;
this.target = target;
container = deduceContainer(source, target);
}
/**
* @generated
*/
@Override
public boolean canExecute(){
if (source == null && target == null) { return false; }
if (source != null && false == source instanceof Goal) { return false; }
if (target != null && false == target instanceof Goal) { 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 StsToolBaseItemSemanticEditPolicy.LinkConstraints.canCreateGoalDecompositionAND_4007(getContainer(), getSource(), getTarget());
}
/**
* @generated NOT
*/
@Override
protected CommandResult doExecuteWithResult(IProgressMonitor monitor,IAdaptable info) throws ExecutionException{
if (!canExecute()) { throw new ExecutionException("Invalid arguments in create link command"); //$NON-NLS-1$
}
GoalDecompositionAND newElement = StstoolFactory.eINSTANCE.createGoalDecompositionAND();
getContainer().getOutgoingDecompositions().add(newElement);
newElement.setSource(getSource());
newElement.setTarget(getTarget());
doConfigure(newElement, monitor, info);
/*if(getSource().isCapability()){
getSource().setCapability(false);
getTarget().setCapability(true);
}
try {
List<EObject> objectsToDestroy=new ArrayList<EObject>();
for(IncompatibleDuties id:getSource().getIncompatibleDutiesOut()){
if(id.getTarget()==getTarget() || isG1childofG2((Goal)id.getTarget(),getTarget())){
objectsToDestroy.add(id);
}
}
for(IncompatibleDuties id:getSource().getIncompatibleDutiesIn()){
if(isG1childofG2((Goal)id.getSource(),getTarget())){
objectsToDestroy.add(id);
}
}
for(IncompatibleDuties id:getTarget().getIncompatibleDutiesOut()){
if(id.getTarget()==getSource() || isG1childofG2(getSource(),(Goal)id.getTarget())){
objectsToDestroy.add(id);
}
}
for(IncompatibleDuties id:getTarget().getIncompatibleDutiesIn()){
if(isG1childofG2(getSource(),(Goal)id.getSource())){
objectsToDestroy.add(id);
}
}
for(CompatibleDuties cd:getSource().getCompatibleDutiesOut()){
if(cd.getTarget()==getTarget() || isG1childofG2((Goal)cd.getTarget(),getTarget())){
objectsToDestroy.add(cd);
}
}
for(CompatibleDuties cd:getSource().getCompatibleDutiesIn()){
if(isG1childofG2((Goal)cd.getSource(),getTarget())){
objectsToDestroy.add(cd);
}
}
for(CompatibleDuties cd:getTarget().getCompatibleDutiesOut()){
if(cd.getTarget()==getSource() || isG1childofG2(getSource(),(Goal)cd.getTarget())){
objectsToDestroy.add(cd);
}
}
for(CompatibleDuties cd:getTarget().getCompatibleDutiesIn()){
if(isG1childofG2(getSource(),(Goal)cd.getSource())){
objectsToDestroy.add(cd);
}
}
if(objectsToDestroy.size()>0){
try {
CustomStsToolDiagramDocumentEditor editor= (CustomStsToolDiagramDocumentEditor)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
CompoundCommand cc=new CompoundCommand();
for(EObject eobj:objectsToDestroy){
for (Object editPart : editor.getDiagramGraphicalViewer().getEditPartRegistry().values()) {
if (((IAdaptable) editPart).getAdapter(EObject.class) == eobj) {
IGraphicalEditPart ep= (IGraphicalEditPart) editPart;
GroupRequest req=new GroupRequest("delete");
Command c = ep.getCommand(req);
if(c!=null)cc.add(c);
}
}
}
if(!cc.isEmpty())cc.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {}*/
((CreateElementRequest) getRequest()).setNewElement(newElement);
return CommandResult.newOKCommandResult(newElement);
}
/**
* @generated
*/
protected void doConfigure(GoalDecompositionAND 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
*/
@Override
protected void setElementToEdit(EObject element){
throw new UnsupportedOperationException();
}
/**
* @generated
*/
protected Goal getSource(){
return (Goal) source;
}
/**
* @generated
*/
protected Goal getTarget(){
return (Goal) target;
}
/**
* @generated
*/
public Goal getContainer(){
return container;
}
/**
* Default approach is to traverse ancestors of the source to find instance of container. Modify with appropriate logic.
*
* @generated
*/
private static Goal 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 Goal) { return (Goal) element; }
}
return null;
}
}