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