| 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.EClass; |
| 9 | import org.eclipse.emf.ecore.EObject; |
| 10 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
| 11 | import org.eclipse.gef.Request; |
| 12 | import org.eclipse.gef.commands.Command; |
| 13 | import org.eclipse.gef.commands.UnexecutableCommand; |
| 14 | import org.eclipse.gef.requests.ReconnectRequest; |
| 15 | import org.eclipse.gmf.runtime.common.core.command.ICommand; |
| 16 | import org.eclipse.gmf.runtime.common.core.command.ICompositeCommand; |
| 17 | import org.eclipse.gmf.runtime.diagram.core.commands.DeleteCommand; |
| 18 | import org.eclipse.gmf.runtime.diagram.ui.commands.CommandProxy; |
| 19 | import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy; |
| 20 | import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; |
| 21 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.SemanticEditPolicy; |
| 22 | import org.eclipse.gmf.runtime.emf.commands.core.command.CompositeTransactionalCommand; |
| 23 | import org.eclipse.gmf.runtime.emf.type.core.IElementType; |
| 24 | import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest; |
| 25 | import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest; |
| 26 | import org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest; |
| 27 | import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest; |
| 28 | import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyReferenceRequest; |
| 29 | import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyRequest; |
| 30 | import org.eclipse.gmf.runtime.emf.type.core.requests.DuplicateElementsRequest; |
| 31 | import org.eclipse.gmf.runtime.emf.type.core.requests.GetEditContextRequest; |
| 32 | import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest; |
| 33 | import org.eclipse.gmf.runtime.emf.type.core.requests.MoveRequest; |
| 34 | import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientReferenceRelationshipRequest; |
| 35 | import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientRelationshipRequest; |
| 36 | import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest; |
| 37 | import org.eclipse.gmf.runtime.notation.View; |
| 38 | |
| 39 | import de.uka.ipd.sdq.pcm.core.composition.ComposedStructure; |
| 40 | import de.uka.ipd.sdq.pcm.gmf.composite.edit.helpers.PalladioComponentModelBaseEditHelper; |
| 41 | import de.uka.ipd.sdq.pcm.gmf.composite.part.PalladioComponentModelVisualIDRegistry; |
| 42 | import de.uka.ipd.sdq.pcm.gmf.composite.providers.PalladioComponentModelElementTypes; |
| 43 | import de.uka.ipd.sdq.pcm.repository.InfrastructureProvidedRole; |
| 44 | import de.uka.ipd.sdq.pcm.repository.InfrastructureRequiredRole; |
| 45 | import de.uka.ipd.sdq.pcm.repository.OperationProvidedRole; |
| 46 | import de.uka.ipd.sdq.pcm.repository.OperationRequiredRole; |
| 47 | import de.uka.ipd.sdq.pcm.repository.SinkRole; |
| 48 | import de.uka.ipd.sdq.pcm.repository.SourceRole; |
| 49 | |
| 50 | /** |
| 51 | * @generated |
| 52 | */ |
| 53 | public class PalladioComponentModelBaseItemSemanticEditPolicy extends |
| 54 | SemanticEditPolicy { |
| 55 | |
| 56 | /** |
| 57 | * Extended request data key to hold editpart visual id. |
| 58 | * |
| 59 | * @generated |
| 60 | */ |
| 61 | public static final String VISUAL_ID_KEY = "visual_id"; //$NON-NLS-1$ |
| 62 | |
| 63 | /** |
| 64 | * @generated |
| 65 | */ |
| 66 | private final IElementType myElementType; |
| 67 | |
| 68 | /** |
| 69 | * @generated |
| 70 | */ |
| 71 | protected PalladioComponentModelBaseItemSemanticEditPolicy( |
| 72 | IElementType elementType) { |
| 73 | myElementType = elementType; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Add visual id of edited editpart to extended data of the request |
| 78 | * so command switch can decide what kind of diagram element is being edited. |
| 79 | * It is done in those cases when it's not possible to deduce diagram |
| 80 | * element kind from domain element. |
| 81 | * |
| 82 | * @generated |
| 83 | */ |
| 84 | public Command getCommand(Request request) { |
| 85 | if (request instanceof ReconnectRequest) { |
| 86 | Object view = ((ReconnectRequest) request).getConnectionEditPart() |
| 87 | .getModel(); |
| 88 | if (view instanceof View) { |
| 89 | Integer id = new Integer(PalladioComponentModelVisualIDRegistry |
| 90 | .getVisualID((View) view)); |
| 91 | request.getExtendedData().put(VISUAL_ID_KEY, id); |
| 92 | } |
| 93 | } |
| 94 | return super.getCommand(request); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns visual id from request parameters. |
| 99 | * |
| 100 | * @generated |
| 101 | */ |
| 102 | protected int getVisualID(IEditCommandRequest request) { |
| 103 | Object id = request.getParameter(VISUAL_ID_KEY); |
| 104 | return id instanceof Integer ? ((Integer) id).intValue() : -1; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @generated |
| 109 | */ |
| 110 | protected Command getSemanticCommand(IEditCommandRequest request) { |
| 111 | IEditCommandRequest completedRequest = completeRequest(request); |
| 112 | Command semanticCommand = getSemanticCommandSwitch(completedRequest); |
| 113 | semanticCommand = getEditHelperCommand(completedRequest, |
| 114 | semanticCommand); |
| 115 | if (completedRequest instanceof DestroyRequest) { |
| 116 | DestroyRequest destroyRequest = (DestroyRequest) completedRequest; |
| 117 | return shouldProceed(destroyRequest) ? addDeleteViewCommand( |
| 118 | semanticCommand, destroyRequest) : null; |
| 119 | } |
| 120 | return semanticCommand; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @generated |
| 125 | */ |
| 126 | protected Command addDeleteViewCommand(Command mainCommand, |
| 127 | DestroyRequest completedRequest) { |
| 128 | Command deleteViewCommand = getGEFWrapper(new DeleteCommand( |
| 129 | getEditingDomain(), (View) getHost().getModel())); |
| 130 | return mainCommand == null ? deleteViewCommand : mainCommand |
| 131 | .chain(deleteViewCommand); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @generated |
| 136 | */ |
| 137 | private Command getEditHelperCommand(IEditCommandRequest request, |
| 138 | Command editPolicyCommand) { |
| 139 | if (editPolicyCommand != null) { |
| 140 | ICommand command = editPolicyCommand instanceof ICommandProxy ? ((ICommandProxy) editPolicyCommand) |
| 141 | .getICommand() |
| 142 | : new CommandProxy(editPolicyCommand); |
| 143 | request.setParameter( |
| 144 | PalladioComponentModelBaseEditHelper.EDIT_POLICY_COMMAND, |
| 145 | command); |
| 146 | } |
| 147 | IElementType requestContextElementType = getContextElementType(request); |
| 148 | request.setParameter( |
| 149 | PalladioComponentModelBaseEditHelper.CONTEXT_ELEMENT_TYPE, |
| 150 | requestContextElementType); |
| 151 | ICommand command = requestContextElementType.getEditCommand(request); |
| 152 | request.setParameter( |
| 153 | PalladioComponentModelBaseEditHelper.EDIT_POLICY_COMMAND, null); |
| 154 | request |
| 155 | .setParameter( |
| 156 | PalladioComponentModelBaseEditHelper.CONTEXT_ELEMENT_TYPE, |
| 157 | null); |
| 158 | if (command != null) { |
| 159 | if (!(command instanceof CompositeTransactionalCommand)) { |
| 160 | command = new CompositeTransactionalCommand(getEditingDomain(), |
| 161 | command.getLabel()).compose(command); |
| 162 | } |
| 163 | return new ICommandProxy(command); |
| 164 | } |
| 165 | return editPolicyCommand; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @generated |
| 170 | */ |
| 171 | private IElementType getContextElementType(IEditCommandRequest request) { |
| 172 | IElementType requestContextElementType = PalladioComponentModelElementTypes |
| 173 | .getElementType(getVisualID(request)); |
| 174 | return requestContextElementType != null ? requestContextElementType |
| 175 | : myElementType; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * @generated |
| 180 | */ |
| 181 | protected Command getSemanticCommandSwitch(IEditCommandRequest req) { |
| 182 | if (req instanceof CreateRelationshipRequest) { |
| 183 | return getCreateRelationshipCommand((CreateRelationshipRequest) req); |
| 184 | } else if (req instanceof CreateElementRequest) { |
| 185 | return getCreateCommand((CreateElementRequest) req); |
| 186 | } else if (req instanceof ConfigureRequest) { |
| 187 | return getConfigureCommand((ConfigureRequest) req); |
| 188 | } else if (req instanceof DestroyElementRequest) { |
| 189 | return getDestroyElementCommand((DestroyElementRequest) req); |
| 190 | } else if (req instanceof DestroyReferenceRequest) { |
| 191 | return getDestroyReferenceCommand((DestroyReferenceRequest) req); |
| 192 | } else if (req instanceof DuplicateElementsRequest) { |
| 193 | return getDuplicateCommand((DuplicateElementsRequest) req); |
| 194 | } else if (req instanceof GetEditContextRequest) { |
| 195 | return getEditContextCommand((GetEditContextRequest) req); |
| 196 | } else if (req instanceof MoveRequest) { |
| 197 | return getMoveCommand((MoveRequest) req); |
| 198 | } else if (req instanceof ReorientReferenceRelationshipRequest) { |
| 199 | return getReorientReferenceRelationshipCommand((ReorientReferenceRelationshipRequest) req); |
| 200 | } else if (req instanceof ReorientRelationshipRequest) { |
| 201 | return getReorientRelationshipCommand((ReorientRelationshipRequest) req); |
| 202 | } else if (req instanceof SetRequest) { |
| 203 | return getSetCommand((SetRequest) req); |
| 204 | } |
| 205 | return null; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * @generated |
| 210 | */ |
| 211 | protected Command getConfigureCommand(ConfigureRequest req) { |
| 212 | return null; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * @generated |
| 217 | */ |
| 218 | protected Command getCreateRelationshipCommand(CreateRelationshipRequest req) { |
| 219 | return null; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * @generated |
| 224 | */ |
| 225 | protected Command getCreateCommand(CreateElementRequest req) { |
| 226 | return null; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * @generated |
| 231 | */ |
| 232 | protected Command getSetCommand(SetRequest req) { |
| 233 | return null; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * @generated |
| 238 | */ |
| 239 | protected Command getEditContextCommand(GetEditContextRequest req) { |
| 240 | return null; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * @generated |
| 245 | */ |
| 246 | protected Command getDestroyElementCommand(DestroyElementRequest req) { |
| 247 | return null; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * @generated |
| 252 | */ |
| 253 | protected Command getDestroyReferenceCommand(DestroyReferenceRequest req) { |
| 254 | return null; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * @generated |
| 259 | */ |
| 260 | protected Command getDuplicateCommand(DuplicateElementsRequest req) { |
| 261 | return null; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * @generated |
| 266 | */ |
| 267 | protected Command getMoveCommand(MoveRequest req) { |
| 268 | return null; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * @generated |
| 273 | */ |
| 274 | protected Command getReorientReferenceRelationshipCommand( |
| 275 | ReorientReferenceRelationshipRequest req) { |
| 276 | return UnexecutableCommand.INSTANCE; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * @generated |
| 281 | */ |
| 282 | protected Command getReorientRelationshipCommand( |
| 283 | ReorientRelationshipRequest req) { |
| 284 | return UnexecutableCommand.INSTANCE; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * @generated |
| 289 | */ |
| 290 | protected final Command getGEFWrapper(ICommand cmd) { |
| 291 | return new ICommandProxy(cmd); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Returns editing domain from the host edit part. |
| 296 | * @generated |
| 297 | */ |
| 298 | protected TransactionalEditingDomain getEditingDomain() { |
| 299 | return ((IGraphicalEditPart) getHost()).getEditingDomain(); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Clean all shortcuts to the host element from the same diagram |
| 304 | * @generated |
| 305 | */ |
| 306 | protected void addDestroyShortcutsCommand(ICompositeCommand cmd, View view) { |
| 307 | assert view.getEAnnotation("Shortcut") == null; //$NON-NLS-1$ |
| 308 | for (Iterator it = view.getDiagram().getChildren().iterator(); it |
| 309 | .hasNext();) { |
| 310 | View nextView = (View) it.next(); |
| 311 | if (nextView.getEAnnotation("Shortcut") == null || !nextView.isSetElement() || nextView.getElement() != view.getElement()) { //$NON-NLS-1$ |
| 312 | continue; |
| 313 | } |
| 314 | cmd.add(new DeleteCommand(getEditingDomain(), nextView)); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Finds container element for the new relationship of the specified type. |
| 320 | * Default implementation goes up by containment hierarchy starting from |
| 321 | * the specified element and returns the first element that is instance of |
| 322 | * the specified container class. |
| 323 | * |
| 324 | * @generated not |
| 325 | */ |
| 326 | protected EObject getRelationshipContainer(Object uelement, |
| 327 | EClass containerClass, IElementType relationshipType) { |
| 328 | if (uelement instanceof EObject) { |
| 329 | if (uelement instanceof OperationProvidedRole |
| 330 | || uelement instanceof OperationRequiredRole |
| 331 | || uelement instanceof InfrastructureProvidedRole |
| 332 | || uelement instanceof InfrastructureRequiredRole |
| 333 | || uelement instanceof SinkRole |
| 334 | || uelement instanceof SourceRole) { |
| 335 | uelement = ((View) getHost().getParent().getModel()) |
| 336 | .getElement(); |
| 337 | } |
| 338 | EObject element = (EObject) uelement; |
| 339 | for (; element != null; element = element.eContainer()) { |
| 340 | if (containerClass.isSuperTypeOf(element.eClass())) { |
| 341 | return element; |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | return null; |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * @generated |
| 350 | */ |
| 351 | public static class LinkConstraints { |
| 352 | |
| 353 | /** |
| 354 | * @generated |
| 355 | */ |
| 356 | public static boolean canCreateAssemblyConnector_4004( |
| 357 | ComposedStructure container, OperationRequiredRole source, |
| 358 | OperationProvidedRole target) { |
| 359 | return canExistAssemblyConnector_4004(container, source, target); |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * @generated |
| 364 | */ |
| 365 | public static boolean canCreateAssemblyEventConnector_4007( |
| 366 | ComposedStructure container, SourceRole source, SinkRole target) { |
| 367 | return canExistAssemblyEventConnector_4007(container, source, |
| 368 | target); |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * @generated |
| 373 | */ |
| 374 | public static boolean canCreateRequiredDelegationConnector_4005( |
| 375 | ComposedStructure container, OperationRequiredRole source, |
| 376 | OperationRequiredRole target) { |
| 377 | return canExistRequiredDelegationConnector_4005(container, source, |
| 378 | target); |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * @generated |
| 383 | */ |
| 384 | public static boolean canCreateProvidedDelegationConnector_4006( |
| 385 | ComposedStructure container, OperationProvidedRole source, |
| 386 | OperationProvidedRole target) { |
| 387 | return canExistProvidedDelegationConnector_4006(container, source, |
| 388 | target); |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * @generated |
| 393 | */ |
| 394 | public static boolean canCreateAssemblyInfrastructureConnector_4008( |
| 395 | ComposedStructure container, InfrastructureRequiredRole source, |
| 396 | InfrastructureProvidedRole target) { |
| 397 | return canExistAssemblyInfrastructureConnector_4008(container, |
| 398 | source, target); |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * @generated |
| 403 | */ |
| 404 | public static boolean canExistAssemblyConnector_4004( |
| 405 | ComposedStructure container, OperationRequiredRole source, |
| 406 | OperationProvidedRole target) { |
| 407 | return true; |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * @generated |
| 412 | */ |
| 413 | public static boolean canExistAssemblyEventConnector_4007( |
| 414 | ComposedStructure container, SourceRole source, SinkRole target) { |
| 415 | return true; |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * @generated |
| 420 | */ |
| 421 | public static boolean canExistRequiredDelegationConnector_4005( |
| 422 | ComposedStructure container, OperationRequiredRole source, |
| 423 | OperationRequiredRole target) { |
| 424 | return true; |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * @generated |
| 429 | */ |
| 430 | public static boolean canExistProvidedDelegationConnector_4006( |
| 431 | ComposedStructure container, OperationProvidedRole source, |
| 432 | OperationProvidedRole target) { |
| 433 | return true; |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * @generated |
| 438 | */ |
| 439 | public static boolean canExistAssemblyInfrastructureConnector_4008( |
| 440 | ComposedStructure container, InfrastructureRequiredRole source, |
| 441 | InfrastructureProvidedRole target) { |
| 442 | return true; |
| 443 | } |
| 444 | |
| 445 | } |
| 446 | |
| 447 | } |