| 1 | /* |
| 2 | * Copyright 2007, SDQ, IPD, Uni Karlsruhe (TH) |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.pcm.gmf.composite.edit.policies; |
| 5 | |
| 6 | import java.util.Iterator; |
| 7 | |
| 8 | import org.eclipse.emf.ecore.EAnnotation; |
| 9 | import org.eclipse.emf.ecore.EObject; |
| 10 | import org.eclipse.gef.commands.Command; |
| 11 | import org.eclipse.gef.commands.UnexecutableCommand; |
| 12 | import org.eclipse.gmf.runtime.diagram.core.commands.DeleteCommand; |
| 13 | import org.eclipse.gmf.runtime.emf.commands.core.command.CompositeTransactionalCommand; |
| 14 | import org.eclipse.gmf.runtime.emf.type.core.commands.DestroyElementCommand; |
| 15 | import org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest; |
| 16 | import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest; |
| 17 | import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientRelationshipRequest; |
| 18 | import org.eclipse.gmf.runtime.notation.Edge; |
| 19 | import org.eclipse.gmf.runtime.notation.View; |
| 20 | |
| 21 | import de.uka.ipd.sdq.pcm.core.composition.ComposedStructure; |
| 22 | import de.uka.ipd.sdq.pcm.core.composition.CompositionPackage; |
| 23 | import de.uka.ipd.sdq.pcm.gmf.composite.edit.commands.AssemblyConnectorCreateCommand; |
| 24 | import de.uka.ipd.sdq.pcm.gmf.composite.edit.commands.AssemblyConnectorReorientCommand; |
| 25 | import de.uka.ipd.sdq.pcm.gmf.composite.edit.commands.RequiredDelegationConnectorCreateCommand; |
| 26 | import de.uka.ipd.sdq.pcm.gmf.composite.edit.commands.RequiredDelegationConnectorReorientCommand; |
| 27 | import de.uka.ipd.sdq.pcm.gmf.composite.edit.parts.AssemblyConnectorEditPart; |
| 28 | import de.uka.ipd.sdq.pcm.gmf.composite.edit.parts.RequiredDelegationConnectorEditPart; |
| 29 | import de.uka.ipd.sdq.pcm.gmf.composite.part.PalladioComponentModelVisualIDRegistry; |
| 30 | import de.uka.ipd.sdq.pcm.gmf.composite.providers.PalladioComponentModelElementTypes; |
| 31 | import de.uka.ipd.sdq.pcm.repository.OperationRequiredRole; |
| 32 | |
| 33 | /** |
| 34 | * Semantic edit policy for inner assembly context operation required roles. |
| 35 | * |
| 36 | * @generated |
| 37 | */ |
| 38 | public class OperationRequiredRoleItemSemanticEditPolicy extends |
| 39 | PalladioComponentModelBaseItemSemanticEditPolicy { |
| 40 | |
| 41 | /** |
| 42 | * @generated |
| 43 | */ |
| 44 | public OperationRequiredRoleItemSemanticEditPolicy() { |
| 45 | super(PalladioComponentModelElementTypes.OperationRequiredRole_3008); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @generated |
| 50 | */ |
| 51 | protected Command getDestroyElementCommand(DestroyElementRequest req) { |
| 52 | View view = (View) getHost().getModel(); |
| 53 | CompositeTransactionalCommand cmd = new CompositeTransactionalCommand( |
| 54 | getEditingDomain(), null); |
| 55 | cmd.setTransactionNestingEnabled(false); |
| 56 | for (Iterator it = view.getTargetEdges().iterator(); it.hasNext();) { |
| 57 | Edge incomingLink = (Edge) it.next(); |
| 58 | if (PalladioComponentModelVisualIDRegistry |
| 59 | .getVisualID(incomingLink) == RequiredDelegationConnectorEditPart.VISUAL_ID) { |
| 60 | DestroyElementRequest r = new DestroyElementRequest( |
| 61 | incomingLink.getElement(), false); |
| 62 | cmd.add(new DestroyElementCommand(r)); |
| 63 | cmd.add(new DeleteCommand(getEditingDomain(), incomingLink)); |
| 64 | continue; |
| 65 | } |
| 66 | } |
| 67 | for (Iterator it = view.getSourceEdges().iterator(); it.hasNext();) { |
| 68 | Edge outgoingLink = (Edge) it.next(); |
| 69 | if (PalladioComponentModelVisualIDRegistry |
| 70 | .getVisualID(outgoingLink) == AssemblyConnectorEditPart.VISUAL_ID) { |
| 71 | DestroyElementRequest r = new DestroyElementRequest( |
| 72 | outgoingLink.getElement(), false); |
| 73 | cmd.add(new DestroyElementCommand(r)); |
| 74 | cmd.add(new DeleteCommand(getEditingDomain(), outgoingLink)); |
| 75 | continue; |
| 76 | } |
| 77 | if (PalladioComponentModelVisualIDRegistry |
| 78 | .getVisualID(outgoingLink) == RequiredDelegationConnectorEditPart.VISUAL_ID) { |
| 79 | DestroyElementRequest r = new DestroyElementRequest( |
| 80 | outgoingLink.getElement(), false); |
| 81 | cmd.add(new DestroyElementCommand(r)); |
| 82 | cmd.add(new DeleteCommand(getEditingDomain(), outgoingLink)); |
| 83 | continue; |
| 84 | } |
| 85 | } |
| 86 | EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$ |
| 87 | if (annotation == null) { |
| 88 | // there are indirectly referenced children, need extra commands: false |
| 89 | addDestroyShortcutsCommand(cmd, view); |
| 90 | // delete host element |
| 91 | cmd.add(new DestroyElementCommand(req)); |
| 92 | } else { |
| 93 | cmd.add(new DeleteCommand(getEditingDomain(), view)); |
| 94 | } |
| 95 | return getGEFWrapper(cmd.reduce()); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Get the create relationship command or null / UnexecutableCommand instance |
| 100 | * if this is not an valid end for the connector creation. |
| 101 | * |
| 102 | * @param req The request object to create the command |
| 103 | * @return The prepared creation command object |
| 104 | * |
| 105 | * @generated not |
| 106 | */ |
| 107 | protected Command getCreateRelationshipCommand(CreateRelationshipRequest req) { |
| 108 | if (PalladioComponentModelElementTypes.AssemblyConnector_4004 == req |
| 109 | .getElementType()) { |
| 110 | return req.getTarget() == null ? getStartCreateRelationshipCommandAssemblyConnector(req) |
| 111 | : null; |
| 112 | } |
| 113 | if (PalladioComponentModelElementTypes.RequiredDelegationConnector_4005 == req |
| 114 | .getElementType()) { |
| 115 | return req.getTarget() == null ? getStartCreateRelationshipCommandRequiredDelegationConnector(req) |
| 116 | : null; |
| 117 | } |
| 118 | return super.getCreateRelationshipCommand(req); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @generated |
| 123 | */ |
| 124 | protected Command getStartCreateRelationshipCommand( |
| 125 | CreateRelationshipRequest req) { |
| 126 | if (PalladioComponentModelElementTypes.AssemblyConnector_4004 == req |
| 127 | .getElementType()) { |
| 128 | return getGEFWrapper(new AssemblyConnectorCreateCommand(req, req |
| 129 | .getSource(), req.getTarget())); |
| 130 | } |
| 131 | if (PalladioComponentModelElementTypes.RequiredDelegationConnector_4005 == req |
| 132 | .getElementType()) { |
| 133 | return getGEFWrapper(new RequiredDelegationConnectorCreateCommand( |
| 134 | req, req.getSource(), req.getTarget())); |
| 135 | } |
| 136 | return null; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Helper to create an instance of an outgoing assembly connector. |
| 141 | * |
| 142 | * @param req The request describing the command to be created. |
| 143 | * @return The prepared command to create an AssemblyConnector |
| 144 | * |
| 145 | * @generated not |
| 146 | */ |
| 147 | protected Command getStartCreateRelationshipCommandAssemblyConnector( |
| 148 | CreateRelationshipRequest req) { |
| 149 | EObject sourceEObject = req.getSource(); |
| 150 | if (false == sourceEObject instanceof OperationRequiredRole) { |
| 151 | return UnexecutableCommand.INSTANCE; |
| 152 | } |
| 153 | OperationRequiredRole source = (OperationRequiredRole) sourceEObject; |
| 154 | ComposedStructure container = (ComposedStructure) getRelationshipContainer( |
| 155 | source, CompositionPackage.eINSTANCE.getComposedStructure(), |
| 156 | req.getElementType()); |
| 157 | if (container == null) { |
| 158 | return UnexecutableCommand.INSTANCE; |
| 159 | } |
| 160 | if (!PalladioComponentModelBaseItemSemanticEditPolicy.LinkConstraints |
| 161 | .canCreateAssemblyConnector_4004(container, source, null)) { |
| 162 | return UnexecutableCommand.INSTANCE; |
| 163 | } |
| 164 | req.setParameter("REQ_CHILD_CONTEXT", ((View) getHost().getParent() |
| 165 | .getModel()).getElement()); |
| 166 | return new Command() { |
| 167 | }; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Get a command to create a required delegation connector. |
| 172 | * |
| 173 | * @param req The request object to build up the command. |
| 174 | * @return The prepared command. |
| 175 | * |
| 176 | * @generated not |
| 177 | */ |
| 178 | protected Command getStartCreateRelationshipCommandRequiredDelegationConnector( |
| 179 | CreateRelationshipRequest req) { |
| 180 | EObject sourceEObject = req.getSource(); |
| 181 | if (false == sourceEObject instanceof OperationRequiredRole) { |
| 182 | return UnexecutableCommand.INSTANCE; |
| 183 | } |
| 184 | OperationRequiredRole source = (OperationRequiredRole) sourceEObject; |
| 185 | ComposedStructure container = (ComposedStructure) getRelationshipContainer( |
| 186 | source, CompositionPackage.eINSTANCE.getComposedStructure(), |
| 187 | req.getElementType()); |
| 188 | if (container == null) { |
| 189 | return UnexecutableCommand.INSTANCE; |
| 190 | } |
| 191 | req.setParameter("CHILD_CONTEXT", ((View) getHost().getParent() |
| 192 | .getModel()).getElement()); |
| 193 | if (!PalladioComponentModelBaseItemSemanticEditPolicy.LinkConstraints |
| 194 | .canCreateRequiredDelegationConnector_4005(container, source, |
| 195 | null)) { |
| 196 | return UnexecutableCommand.INSTANCE; |
| 197 | } |
| 198 | return new Command() { |
| 199 | }; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @generated |
| 204 | */ |
| 205 | protected Command getCompleteCreateRelationshipCommand( |
| 206 | CreateRelationshipRequest req) { |
| 207 | if (PalladioComponentModelElementTypes.AssemblyConnector_4004 == req |
| 208 | .getElementType()) { |
| 209 | return null; |
| 210 | } |
| 211 | if (PalladioComponentModelElementTypes.RequiredDelegationConnector_4005 == req |
| 212 | .getElementType()) { |
| 213 | return getGEFWrapper(new RequiredDelegationConnectorCreateCommand( |
| 214 | req, req.getSource(), req.getTarget())); |
| 215 | } |
| 216 | return null; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Returns command to reorient EClass based link. New link target or source |
| 221 | * should be the domain model element associated with this node. |
| 222 | * |
| 223 | * @generated |
| 224 | */ |
| 225 | protected Command getReorientRelationshipCommand( |
| 226 | ReorientRelationshipRequest req) { |
| 227 | switch (getVisualID(req)) { |
| 228 | case AssemblyConnectorEditPart.VISUAL_ID: |
| 229 | return getGEFWrapper(new AssemblyConnectorReorientCommand(req)); |
| 230 | case RequiredDelegationConnectorEditPart.VISUAL_ID: |
| 231 | return getGEFWrapper(new RequiredDelegationConnectorReorientCommand( |
| 232 | req)); |
| 233 | } |
| 234 | return super.getReorientRelationshipCommand(req); |
| 235 | } |
| 236 | |
| 237 | } |