/*****************************************************************************
* Copyright (c) 2010 CEA LIST.
*
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Patrick Tessier (CEA LIST) Patrick.tessier@cea.fr - Initial API and implementation
*/
package org.eclipse.papyrus.uml.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.EObject;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.emf.type.core.commands.EditElementCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientRelationshipRequest;
import org.eclipse.papyrus.uml.diagram.clazz.edit.policies.UMLBaseItemSemanticEditPolicy;
import org.eclipse.uml2.uml.Namespace;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PackageImport;
/**
* @generated
*/
public class PackageImportReorientCommand extends EditElementCommand {
/**
* @generated
*/
private final int reorientDirection;
/**
* @generated
*/
private final EObject oldEnd;
/**
* @generated
*/
private final EObject newEnd;
/**
* @generated
*/
public PackageImportReorientCommand(ReorientRelationshipRequest request) {
super(request.getLabel(), request.getRelationship(), request);
reorientDirection = request.getDirection();
oldEnd = request.getOldRelationshipEnd();
newEnd = request.getNewRelationshipEnd();
}
/**
* @generated
*/
public boolean canExecute() {
if(false == getElementToEdit() instanceof PackageImport) {
return false;
}
if(reorientDirection == ReorientRelationshipRequest.REORIENT_SOURCE) {
return canReorientSource();
}
if(reorientDirection == ReorientRelationshipRequest.REORIENT_TARGET) {
return canReorientTarget();
}
return false;
}
/**
* @generated
*/
protected boolean canReorientSource() {
if(!(oldEnd instanceof Namespace && newEnd instanceof Namespace)) {
return false;
}
Package target = getLink().getImportedPackage();
return UMLBaseItemSemanticEditPolicy.getLinkConstraints().canExistPackageImport_4010(getLink(), getNewSource(), target);
}
/**
* @generated
*/
protected boolean canReorientTarget() {
if(!(oldEnd instanceof Package && newEnd instanceof Package)) {
return false;
}
if(!(getLink().eContainer() instanceof Namespace)) {
return false;
}
Namespace source = (Namespace)getLink().eContainer();
return UMLBaseItemSemanticEditPolicy.getLinkConstraints().canExistPackageImport_4010(getLink(), source, getNewTarget());
}
/**
* @generated
*/
protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
if(!canExecute()) {
throw new ExecutionException("Invalid arguments in reorient link command"); //$NON-NLS-1$
}
if(reorientDirection == ReorientRelationshipRequest.REORIENT_SOURCE) {
return reorientSource();
}
if(reorientDirection == ReorientRelationshipRequest.REORIENT_TARGET) {
return reorientTarget();
}
throw new IllegalStateException();
}
/**
* @generated
*/
protected CommandResult reorientSource() throws ExecutionException {
getOldSource().getPackageImports().remove(getLink());
getNewSource().getPackageImports().add(getLink());
return CommandResult.newOKCommandResult(getLink());
}
/**
* @generated
*/
protected CommandResult reorientTarget() throws ExecutionException {
getLink().setImportedPackage(getNewTarget());
return CommandResult.newOKCommandResult(getLink());
}
/**
* @generated
*/
protected PackageImport getLink() {
return (PackageImport)getElementToEdit();
}
/**
* @generated
*/
protected Namespace getOldSource() {
return (Namespace)oldEnd;
}
/**
* @generated
*/
protected Namespace getNewSource() {
return (Namespace)newEnd;
}
/**
* @generated
*/
protected Package getOldTarget() {
return (Package)oldEnd;
}
/**
* @generated
*/
protected Package getNewTarget() {
return (Package)newEnd;
}
}