| 1 | /* |
| 2 | * Copyright 2007, SDQ, IPD, University of Karlsruhe |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.pcm.gmf.usage.edit.policies; |
| 5 | |
| 6 | import java.util.Iterator; |
| 7 | |
| 8 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
| 9 | import org.eclipse.gef.Request; |
| 10 | import org.eclipse.gef.commands.Command; |
| 11 | import org.eclipse.gef.commands.UnexecutableCommand; |
| 12 | import org.eclipse.gef.requests.ReconnectRequest; |
| 13 | import org.eclipse.gmf.runtime.common.core.command.ICommand; |
| 14 | import org.eclipse.gmf.runtime.common.core.command.ICompositeCommand; |
| 15 | import org.eclipse.gmf.runtime.diagram.core.commands.DeleteCommand; |
| 16 | import org.eclipse.gmf.runtime.diagram.ui.commands.CommandProxy; |
| 17 | import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy; |
| 18 | import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; |
| 19 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.SemanticEditPolicy; |
| 20 | import org.eclipse.gmf.runtime.emf.commands.core.command.CompositeTransactionalCommand; |
| 21 | import org.eclipse.gmf.runtime.emf.type.core.IElementType; |
| 22 | import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest; |
| 23 | import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest; |
| 24 | import org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest; |
| 25 | import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest; |
| 26 | import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyReferenceRequest; |
| 27 | import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyRequest; |
| 28 | import org.eclipse.gmf.runtime.emf.type.core.requests.DuplicateElementsRequest; |
| 29 | import org.eclipse.gmf.runtime.emf.type.core.requests.GetEditContextRequest; |
| 30 | import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest; |
| 31 | import org.eclipse.gmf.runtime.emf.type.core.requests.MoveRequest; |
| 32 | import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientReferenceRelationshipRequest; |
| 33 | import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientRelationshipRequest; |
| 34 | import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest; |
| 35 | import org.eclipse.gmf.runtime.notation.View; |
| 36 | |
| 37 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.helpers.PalladioComponentModelBaseEditHelper; |
| 38 | import de.uka.ipd.sdq.pcm.gmf.usage.part.PalladioComponentModelVisualIDRegistry; |
| 39 | import de.uka.ipd.sdq.pcm.gmf.usage.providers.PalladioComponentModelElementTypes; |
| 40 | import de.uka.ipd.sdq.pcm.usagemodel.AbstractUserAction; |
| 41 | |
| 42 | /** |
| 43 | * @generated |
| 44 | */ |
| 45 | public class PalladioComponentModelBaseItemSemanticEditPolicy extends |
| 46 | SemanticEditPolicy { |
| 47 | |
| 48 | /** |
| 49 | * Extended request data key to hold editpart visual id. |
| 50 | * |
| 51 | * @generated |
| 52 | */ |
| 53 | public static final String VISUAL_ID_KEY = "visual_id"; //$NON-NLS-1$ |
| 54 | |
| 55 | /** |
| 56 | * @generated |
| 57 | */ |
| 58 | private final IElementType myElementType; |
| 59 | |
| 60 | /** |
| 61 | * @generated |
| 62 | */ |
| 63 | protected PalladioComponentModelBaseItemSemanticEditPolicy( |
| 64 | IElementType elementType) { |
| 65 | myElementType = elementType; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Add visual id of edited editpart to extended data of the request |
| 70 | * so command switch can decide what kind of diagram element is being edited. |
| 71 | * It is done in those cases when it's not possible to deduce diagram |
| 72 | * element kind from domain element. |
| 73 | * |
| 74 | * @generated |
| 75 | */ |
| 76 | public Command getCommand(Request request) { |
| 77 | if (request instanceof ReconnectRequest) { |
| 78 | Object view = ((ReconnectRequest) request).getConnectionEditPart() |
| 79 | .getModel(); |
| 80 | if (view instanceof View) { |
| 81 | Integer id = new Integer(PalladioComponentModelVisualIDRegistry |
| 82 | .getVisualID((View) view)); |
| 83 | request.getExtendedData().put(VISUAL_ID_KEY, id); |
| 84 | } |
| 85 | } |
| 86 | return super.getCommand(request); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns visual id from request parameters. |
| 91 | * |
| 92 | * @generated |
| 93 | */ |
| 94 | protected int getVisualID(IEditCommandRequest request) { |
| 95 | Object id = request.getParameter(VISUAL_ID_KEY); |
| 96 | return id instanceof Integer ? ((Integer) id).intValue() : -1; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @generated |
| 101 | */ |
| 102 | protected Command getSemanticCommand(IEditCommandRequest request) { |
| 103 | IEditCommandRequest completedRequest = completeRequest(request); |
| 104 | Command semanticCommand = getSemanticCommandSwitch(completedRequest); |
| 105 | semanticCommand = getEditHelperCommand(completedRequest, |
| 106 | semanticCommand); |
| 107 | if (completedRequest instanceof DestroyRequest) { |
| 108 | DestroyRequest destroyRequest = (DestroyRequest) completedRequest; |
| 109 | return shouldProceed(destroyRequest) ? addDeleteViewCommand( |
| 110 | semanticCommand, destroyRequest) : null; |
| 111 | } |
| 112 | return semanticCommand; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @generated |
| 117 | */ |
| 118 | protected Command addDeleteViewCommand(Command mainCommand, |
| 119 | DestroyRequest completedRequest) { |
| 120 | Command deleteViewCommand = getGEFWrapper(new DeleteCommand( |
| 121 | getEditingDomain(), (View) getHost().getModel())); |
| 122 | return mainCommand == null ? deleteViewCommand : mainCommand |
| 123 | .chain(deleteViewCommand); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @generated |
| 128 | */ |
| 129 | private Command getEditHelperCommand(IEditCommandRequest request, |
| 130 | Command editPolicyCommand) { |
| 131 | if (editPolicyCommand != null) { |
| 132 | ICommand command = editPolicyCommand instanceof ICommandProxy ? ((ICommandProxy) editPolicyCommand) |
| 133 | .getICommand() |
| 134 | : new CommandProxy(editPolicyCommand); |
| 135 | request.setParameter( |
| 136 | PalladioComponentModelBaseEditHelper.EDIT_POLICY_COMMAND, |
| 137 | command); |
| 138 | } |
| 139 | IElementType requestContextElementType = getContextElementType(request); |
| 140 | request.setParameter( |
| 141 | PalladioComponentModelBaseEditHelper.CONTEXT_ELEMENT_TYPE, |
| 142 | requestContextElementType); |
| 143 | ICommand command = requestContextElementType.getEditCommand(request); |
| 144 | request.setParameter( |
| 145 | PalladioComponentModelBaseEditHelper.EDIT_POLICY_COMMAND, null); |
| 146 | request |
| 147 | .setParameter( |
| 148 | PalladioComponentModelBaseEditHelper.CONTEXT_ELEMENT_TYPE, |
| 149 | null); |
| 150 | if (command != null) { |
| 151 | if (!(command instanceof CompositeTransactionalCommand)) { |
| 152 | command = new CompositeTransactionalCommand(getEditingDomain(), |
| 153 | command.getLabel()).compose(command); |
| 154 | } |
| 155 | return new ICommandProxy(command); |
| 156 | } |
| 157 | return editPolicyCommand; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * @generated |
| 162 | */ |
| 163 | private IElementType getContextElementType(IEditCommandRequest request) { |
| 164 | IElementType requestContextElementType = PalladioComponentModelElementTypes |
| 165 | .getElementType(getVisualID(request)); |
| 166 | return requestContextElementType != null ? requestContextElementType |
| 167 | : myElementType; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * @generated |
| 172 | */ |
| 173 | protected Command getSemanticCommandSwitch(IEditCommandRequest req) { |
| 174 | if (req instanceof CreateRelationshipRequest) { |
| 175 | return getCreateRelationshipCommand((CreateRelationshipRequest) req); |
| 176 | } else if (req instanceof CreateElementRequest) { |
| 177 | return getCreateCommand((CreateElementRequest) req); |
| 178 | } else if (req instanceof ConfigureRequest) { |
| 179 | return getConfigureCommand((ConfigureRequest) req); |
| 180 | } else if (req instanceof DestroyElementRequest) { |
| 181 | return getDestroyElementCommand((DestroyElementRequest) req); |
| 182 | } else if (req instanceof DestroyReferenceRequest) { |
| 183 | return getDestroyReferenceCommand((DestroyReferenceRequest) req); |
| 184 | } else if (req instanceof DuplicateElementsRequest) { |
| 185 | return getDuplicateCommand((DuplicateElementsRequest) req); |
| 186 | } else if (req instanceof GetEditContextRequest) { |
| 187 | return getEditContextCommand((GetEditContextRequest) req); |
| 188 | } else if (req instanceof MoveRequest) { |
| 189 | return getMoveCommand((MoveRequest) req); |
| 190 | } else if (req instanceof ReorientReferenceRelationshipRequest) { |
| 191 | return getReorientReferenceRelationshipCommand((ReorientReferenceRelationshipRequest) req); |
| 192 | } else if (req instanceof ReorientRelationshipRequest) { |
| 193 | return getReorientRelationshipCommand((ReorientRelationshipRequest) req); |
| 194 | } else if (req instanceof SetRequest) { |
| 195 | return getSetCommand((SetRequest) req); |
| 196 | } |
| 197 | return null; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * @generated |
| 202 | */ |
| 203 | protected Command getConfigureCommand(ConfigureRequest req) { |
| 204 | return null; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * @generated |
| 209 | */ |
| 210 | protected Command getCreateRelationshipCommand(CreateRelationshipRequest req) { |
| 211 | return null; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * @generated |
| 216 | */ |
| 217 | protected Command getCreateCommand(CreateElementRequest req) { |
| 218 | return null; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * @generated |
| 223 | */ |
| 224 | protected Command getSetCommand(SetRequest req) { |
| 225 | return null; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * @generated |
| 230 | */ |
| 231 | protected Command getEditContextCommand(GetEditContextRequest req) { |
| 232 | return null; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * @generated |
| 237 | */ |
| 238 | protected Command getDestroyElementCommand(DestroyElementRequest req) { |
| 239 | return null; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * @generated |
| 244 | */ |
| 245 | protected Command getDestroyReferenceCommand(DestroyReferenceRequest req) { |
| 246 | return null; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * @generated |
| 251 | */ |
| 252 | protected Command getDuplicateCommand(DuplicateElementsRequest req) { |
| 253 | return null; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * @generated |
| 258 | */ |
| 259 | protected Command getMoveCommand(MoveRequest req) { |
| 260 | return null; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * @generated |
| 265 | */ |
| 266 | protected Command getReorientReferenceRelationshipCommand( |
| 267 | ReorientReferenceRelationshipRequest req) { |
| 268 | return UnexecutableCommand.INSTANCE; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * @generated |
| 273 | */ |
| 274 | protected Command getReorientRelationshipCommand( |
| 275 | ReorientRelationshipRequest req) { |
| 276 | return UnexecutableCommand.INSTANCE; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * @generated |
| 281 | */ |
| 282 | protected final Command getGEFWrapper(ICommand cmd) { |
| 283 | return new ICommandProxy(cmd); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Returns editing domain from the host edit part. |
| 288 | * |
| 289 | * @generated |
| 290 | */ |
| 291 | protected TransactionalEditingDomain getEditingDomain() { |
| 292 | return ((IGraphicalEditPart) getHost()).getEditingDomain(); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Clean all shortcuts to the host element from the same diagram |
| 297 | * @generated |
| 298 | */ |
| 299 | protected void addDestroyShortcutsCommand(ICompositeCommand cmd, View view) { |
| 300 | assert view.getEAnnotation("Shortcut") == null; //$NON-NLS-1$ |
| 301 | for (Iterator it = view.getDiagram().getChildren().iterator(); it |
| 302 | .hasNext();) { |
| 303 | View nextView = (View) it.next(); |
| 304 | if (nextView.getEAnnotation("Shortcut") == null || !nextView.isSetElement() || nextView.getElement() != view.getElement()) { //$NON-NLS-1$ |
| 305 | continue; |
| 306 | } |
| 307 | cmd.add(new DeleteCommand(getEditingDomain(), nextView)); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * @generated |
| 313 | */ |
| 314 | public static class LinkConstraints { |
| 315 | |
| 316 | /** |
| 317 | * @generated |
| 318 | */ |
| 319 | public static boolean canCreateAbstractUserActionSuccessor_4002( |
| 320 | AbstractUserAction source, AbstractUserAction target) { |
| 321 | if (source != null) { |
| 322 | if (source.getSuccessor() != null) { |
| 323 | return false; |
| 324 | } |
| 325 | } |
| 326 | if (target != null && (target.getPredecessor() != null)) { |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | return canExistAbstractUserActionSuccessor_4002(source, target); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * @generated |
| 335 | */ |
| 336 | public static boolean canExistAbstractUserActionSuccessor_4002( |
| 337 | AbstractUserAction source, AbstractUserAction target) { |
| 338 | return true; |
| 339 | } |
| 340 | |
| 341 | } |
| 342 | |
| 343 | } |