| 1 | /* |
| 2 | * Copyright 2007, IPD, SDQ, University of Karlsruhe |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.pcm.gmf.repository.edit.commands; |
| 5 | |
| 6 | import org.eclipse.core.commands.ExecutionException; |
| 7 | import org.eclipse.core.runtime.IAdaptable; |
| 8 | import org.eclipse.core.runtime.IProgressMonitor; |
| 9 | import org.eclipse.emf.ecore.EObject; |
| 10 | import org.eclipse.gmf.runtime.common.core.command.CommandResult; |
| 11 | import org.eclipse.gmf.runtime.emf.type.core.commands.EditElementCommand; |
| 12 | import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientRelationshipRequest; |
| 13 | |
| 14 | import de.uka.ipd.sdq.pcm.core.entity.InterfaceRequiringEntity; |
| 15 | import de.uka.ipd.sdq.pcm.gmf.repository.edit.policies.PalladioComponentModelBaseItemSemanticEditPolicy; |
| 16 | import de.uka.ipd.sdq.pcm.repository.EventGroup; |
| 17 | import de.uka.ipd.sdq.pcm.repository.SourceRole; |
| 18 | |
| 19 | /** |
| 20 | * @generated |
| 21 | */ |
| 22 | public class SourceRoleReorientCommand extends EditElementCommand { |
| 23 | |
| 24 | /** |
| 25 | * @generated |
| 26 | */ |
| 27 | private final int reorientDirection; |
| 28 | |
| 29 | /** |
| 30 | * @generated |
| 31 | */ |
| 32 | private final EObject oldEnd; |
| 33 | |
| 34 | /** |
| 35 | * @generated |
| 36 | */ |
| 37 | private final EObject newEnd; |
| 38 | |
| 39 | /** |
| 40 | * @generated |
| 41 | */ |
| 42 | public SourceRoleReorientCommand(ReorientRelationshipRequest request) { |
| 43 | super(request.getLabel(), request.getRelationship(), request); |
| 44 | reorientDirection = request.getDirection(); |
| 45 | oldEnd = request.getOldRelationshipEnd(); |
| 46 | newEnd = request.getNewRelationshipEnd(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @generated |
| 51 | */ |
| 52 | public boolean canExecute() { |
| 53 | if (false == getElementToEdit() instanceof SourceRole) { |
| 54 | return false; |
| 55 | } |
| 56 | if (reorientDirection == ReorientRelationshipRequest.REORIENT_SOURCE) { |
| 57 | return canReorientSource(); |
| 58 | } |
| 59 | if (reorientDirection == ReorientRelationshipRequest.REORIENT_TARGET) { |
| 60 | return canReorientTarget(); |
| 61 | } |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @generated |
| 67 | */ |
| 68 | protected boolean canReorientSource() { |
| 69 | if (!(oldEnd instanceof InterfaceRequiringEntity && newEnd instanceof InterfaceRequiringEntity)) { |
| 70 | return false; |
| 71 | } |
| 72 | EventGroup target = getLink().getEventGroup__SourceRole(); |
| 73 | return PalladioComponentModelBaseItemSemanticEditPolicy.LinkConstraints |
| 74 | .canExistSourceRole_4110(getNewSource(), target); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @generated |
| 79 | */ |
| 80 | protected boolean canReorientTarget() { |
| 81 | if (!(oldEnd instanceof EventGroup && newEnd instanceof EventGroup)) { |
| 82 | return false; |
| 83 | } |
| 84 | if (!(getLink().eContainer() instanceof InterfaceRequiringEntity)) { |
| 85 | return false; |
| 86 | } |
| 87 | InterfaceRequiringEntity source = (InterfaceRequiringEntity) getLink() |
| 88 | .eContainer(); |
| 89 | return PalladioComponentModelBaseItemSemanticEditPolicy.LinkConstraints |
| 90 | .canExistSourceRole_4110(source, getNewTarget()); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @generated |
| 95 | */ |
| 96 | protected CommandResult doExecuteWithResult(IProgressMonitor monitor, |
| 97 | IAdaptable info) throws ExecutionException { |
| 98 | if (!canExecute()) { |
| 99 | throw new ExecutionException( |
| 100 | "Invalid arguments in reorient link command"); //$NON-NLS-1$ |
| 101 | } |
| 102 | if (reorientDirection == ReorientRelationshipRequest.REORIENT_SOURCE) { |
| 103 | return reorientSource(); |
| 104 | } |
| 105 | if (reorientDirection == ReorientRelationshipRequest.REORIENT_TARGET) { |
| 106 | return reorientTarget(); |
| 107 | } |
| 108 | throw new IllegalStateException(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @generated |
| 113 | */ |
| 114 | protected CommandResult reorientSource() throws ExecutionException { |
| 115 | getOldSource().getRequiredRoles_InterfaceRequiringEntity().remove( |
| 116 | getLink()); |
| 117 | getNewSource().getRequiredRoles_InterfaceRequiringEntity().add( |
| 118 | getLink()); |
| 119 | return CommandResult.newOKCommandResult(getLink()); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @generated |
| 124 | */ |
| 125 | protected CommandResult reorientTarget() throws ExecutionException { |
| 126 | getLink().setEventGroup__SourceRole(getNewTarget()); |
| 127 | return CommandResult.newOKCommandResult(getLink()); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @generated |
| 132 | */ |
| 133 | protected SourceRole getLink() { |
| 134 | return (SourceRole) getElementToEdit(); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * @generated |
| 139 | */ |
| 140 | protected InterfaceRequiringEntity getOldSource() { |
| 141 | return (InterfaceRequiringEntity) oldEnd; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @generated |
| 146 | */ |
| 147 | protected InterfaceRequiringEntity getNewSource() { |
| 148 | return (InterfaceRequiringEntity) newEnd; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @generated |
| 153 | */ |
| 154 | protected EventGroup getOldTarget() { |
| 155 | return (EventGroup) oldEnd; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @generated |
| 160 | */ |
| 161 | protected EventGroup getNewTarget() { |
| 162 | return (EventGroup) newEnd; |
| 163 | } |
| 164 | } |