blob: 0092ec3eb3902234d2ceba12d2c66c7d91137753 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2016 KPIT Technologies.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Jan Mauersberger- initial API and implementation
* Sascha Baumgart- initial API and implementation
*******************************************************************************/
/*
*
*/
package org.eclipse.opencert.vocabulary.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.emf.type.core.commands.EditElementCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientReferenceRelationshipRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientRelationshipRequest;
import org.eclipse.opencert.vocabulary.Term;
import org.eclipse.opencert.vocabulary.diagram.edit.policies.VocabularyBaseItemSemanticEditPolicy;
/**
* @generated
*/
public class TermIsAReorientCommand extends EditElementCommand {
/**
* @generated
*/
private final int reorientDirection;
/**
* @generated
*/
private final EObject referenceOwner;
/**
* @generated
*/
private final EObject oldEnd;
/**
* @generated
*/
private final EObject newEnd;
/**
* @generated
*/
public TermIsAReorientCommand(ReorientReferenceRelationshipRequest request) {
super(request.getLabel(), null, request);
reorientDirection = request.getDirection();
referenceOwner = request.getReferenceOwner();
oldEnd = request.getOldRelationshipEnd();
newEnd = request.getNewRelationshipEnd();
}
/**
* @generated
*/
public boolean canExecute() {
if (false == referenceOwner instanceof Term) {
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 Term && newEnd instanceof Term)) {
return false;
}
return VocabularyBaseItemSemanticEditPolicy.getLinkConstraints()
.canExistTermIsA_4004(getNewSource(), getOldTarget());
}
/**
* @generated
*/
protected boolean canReorientTarget() {
if (!(oldEnd instanceof Term && newEnd instanceof Term)) {
return false;
}
return VocabularyBaseItemSemanticEditPolicy.getLinkConstraints()
.canExistTermIsA_4004(getOldSource(), 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().getIsA().remove(getOldTarget());
getNewSource().getIsA().add(getOldTarget());
return CommandResult.newOKCommandResult(referenceOwner);
}
/**
* @generated
*/
protected CommandResult reorientTarget() throws ExecutionException {
getOldSource().getIsA().remove(getOldTarget());
getOldSource().getIsA().add(getNewTarget());
return CommandResult.newOKCommandResult(referenceOwner);
}
/**
* @generated
*/
protected Term getOldSource() {
return (Term) referenceOwner;
}
/**
* @generated
*/
protected Term getNewSource() {
return (Term) newEnd;
}
/**
* @generated
*/
protected Term getOldTarget() {
return (Term) oldEnd;
}
/**
* @generated
*/
protected Term getNewTarget() {
return (Term) newEnd;
}
}