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