blob: df20f8e7219176a47c495cd9e2b1e6b2df40d3c2 [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.Category;
import org.eclipse.opencert.vocabulary.diagram.edit.policies.VocabularyBaseItemSemanticEditPolicy;
/**
* @generated
*/
public class CategorySubCategoriesReorientCommand extends EditElementCommand {
/**
* @generated
*/
private final int reorientDirection;
/**
* @generated
*/
private final EObject referenceOwner;
/**
* @generated
*/
private final EObject oldEnd;
/**
* @generated
*/
private final EObject newEnd;
/**
* @generated
*/
public CategorySubCategoriesReorientCommand(
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 Category) {
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 Category && newEnd instanceof Category)) {
return false;
}
return VocabularyBaseItemSemanticEditPolicy.getLinkConstraints()
.canExistCategorySubCategories_4002(getNewSource(),
getOldTarget());
}
/**
* @generated
*/
protected boolean canReorientTarget() {
if (!(oldEnd instanceof Category && newEnd instanceof Category)) {
return false;
}
return VocabularyBaseItemSemanticEditPolicy.getLinkConstraints()
.canExistCategorySubCategories_4002(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().getSubCategories().remove(getOldTarget());
getNewSource().getSubCategories().add(getOldTarget());
return CommandResult.newOKCommandResult(referenceOwner);
}
/**
* @generated
*/
protected CommandResult reorientTarget() throws ExecutionException {
getOldSource().getSubCategories().remove(getOldTarget());
getOldSource().getSubCategories().add(getNewTarget());
return CommandResult.newOKCommandResult(referenceOwner);
}
/**
* @generated
*/
protected Category getOldSource() {
return (Category) referenceOwner;
}
/**
* @generated
*/
protected Category getNewSource() {
return (Category) newEnd;
}
/**
* @generated
*/
protected Category getOldTarget() {
return (Category) oldEnd;
}
/**
* @generated
*/
protected Category getNewTarget() {
return (Category) newEnd;
}
}