| 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.Collection; |
| 7 | import java.util.Collections; |
| 8 | import java.util.HashMap; |
| 9 | import java.util.HashSet; |
| 10 | import java.util.Iterator; |
| 11 | import java.util.LinkedList; |
| 12 | import java.util.List; |
| 13 | import java.util.Map; |
| 14 | import java.util.Set; |
| 15 | |
| 16 | import org.eclipse.core.runtime.IAdaptable; |
| 17 | import org.eclipse.emf.ecore.EObject; |
| 18 | import org.eclipse.gef.EditPart; |
| 19 | import org.eclipse.gef.commands.Command; |
| 20 | import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil; |
| 21 | import org.eclipse.gmf.runtime.diagram.ui.commands.DeferredLayoutCommand; |
| 22 | import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy; |
| 23 | import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; |
| 24 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.CanonicalConnectionEditPolicy; |
| 25 | import org.eclipse.gmf.runtime.diagram.ui.requests.CreateConnectionViewRequest; |
| 26 | import org.eclipse.gmf.runtime.diagram.ui.requests.RequestConstants; |
| 27 | import org.eclipse.gmf.runtime.notation.Diagram; |
| 28 | import org.eclipse.gmf.runtime.notation.Edge; |
| 29 | import org.eclipse.gmf.runtime.notation.View; |
| 30 | |
| 31 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.BranchEditPart; |
| 32 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.BranchTransitionEditPart; |
| 33 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.ClosedWorkloadEditPart; |
| 34 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.DelayEditPart; |
| 35 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.EntryLevelSystemCallEditPart; |
| 36 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.LoopEditPart; |
| 37 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.OpenWorkloadEditPart; |
| 38 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.ScenarioBehaviour2EditPart; |
| 39 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.ScenarioBehaviour3EditPart; |
| 40 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.ScenarioBehaviourEditPart; |
| 41 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.StartEditPart; |
| 42 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.StopEditPart; |
| 43 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.UsageScenario2EditPart; |
| 44 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.UsageScenarioEditPart; |
| 45 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.VariableUsageEditPart; |
| 46 | import de.uka.ipd.sdq.pcm.gmf.usage.part.PalladioComponentModelDiagramUpdater; |
| 47 | import de.uka.ipd.sdq.pcm.gmf.usage.part.PalladioComponentModelLinkDescriptor; |
| 48 | import de.uka.ipd.sdq.pcm.gmf.usage.part.PalladioComponentModelNodeDescriptor; |
| 49 | import de.uka.ipd.sdq.pcm.gmf.usage.part.PalladioComponentModelVisualIDRegistry; |
| 50 | import de.uka.ipd.sdq.pcm.usagemodel.UsagemodelPackage; |
| 51 | |
| 52 | /** |
| 53 | * @generated |
| 54 | */ |
| 55 | public class UsageScenarioCanonicalEditPolicy extends |
| 56 | CanonicalConnectionEditPolicy { |
| 57 | |
| 58 | /** |
| 59 | * @generated |
| 60 | */ |
| 61 | Set myFeaturesToSynchronize; |
| 62 | |
| 63 | /** |
| 64 | * @generated |
| 65 | */ |
| 66 | protected List getSemanticChildrenList() { |
| 67 | View viewObject = (View) getHost().getModel(); |
| 68 | List result = new LinkedList(); |
| 69 | for (Iterator it = PalladioComponentModelDiagramUpdater |
| 70 | .getUsageModel_1000SemanticChildren(viewObject).iterator(); it |
| 71 | .hasNext();) { |
| 72 | result.add(((PalladioComponentModelNodeDescriptor) it.next()) |
| 73 | .getModelElement()); |
| 74 | } |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @generated |
| 80 | */ |
| 81 | protected boolean shouldDeleteView(View view) { |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @generated |
| 87 | */ |
| 88 | protected boolean isOrphaned(Collection semanticChildren, final View view) { |
| 89 | int visualID = PalladioComponentModelVisualIDRegistry.getVisualID(view); |
| 90 | switch (visualID) { |
| 91 | case UsageScenario2EditPart.VISUAL_ID: |
| 92 | if (!semanticChildren.contains(view.getElement())) { |
| 93 | return true; |
| 94 | } |
| 95 | } |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @generated |
| 101 | */ |
| 102 | protected String getDefaultFactoryHint() { |
| 103 | return null; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @generated |
| 108 | */ |
| 109 | protected Set getFeaturesToSynchronize() { |
| 110 | if (myFeaturesToSynchronize == null) { |
| 111 | myFeaturesToSynchronize = new HashSet(); |
| 112 | myFeaturesToSynchronize.add(UsagemodelPackage.eINSTANCE |
| 113 | .getUsageModel_UsageScenario_UsageModel()); |
| 114 | } |
| 115 | return myFeaturesToSynchronize; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @generated |
| 120 | */ |
| 121 | protected List getSemanticConnectionsList() { |
| 122 | return Collections.EMPTY_LIST; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @generated |
| 127 | */ |
| 128 | protected EObject getSourceElement(EObject relationship) { |
| 129 | return null; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @generated |
| 134 | */ |
| 135 | protected EObject getTargetElement(EObject relationship) { |
| 136 | return null; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @generated |
| 141 | */ |
| 142 | protected boolean shouldIncludeConnection(Edge connector, |
| 143 | Collection children) { |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @generated |
| 149 | */ |
| 150 | protected void refreshSemantic() { |
| 151 | List createdViews = new LinkedList(); |
| 152 | createdViews.addAll(refreshSemanticChildren()); |
| 153 | List createdConnectionViews = new LinkedList(); |
| 154 | createdConnectionViews.addAll(refreshSemanticConnections()); |
| 155 | createdConnectionViews.addAll(refreshConnections()); |
| 156 | |
| 157 | if (createdViews.size() > 1) { |
| 158 | // perform a layout of the container |
| 159 | DeferredLayoutCommand layoutCmd = new DeferredLayoutCommand(host() |
| 160 | .getEditingDomain(), createdViews, host()); |
| 161 | executeCommand(new ICommandProxy(layoutCmd)); |
| 162 | } |
| 163 | |
| 164 | createdViews.addAll(createdConnectionViews); |
| 165 | makeViewsImmutable(createdViews); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @generated |
| 170 | */ |
| 171 | private Collection refreshConnections() { |
| 172 | Map domain2NotationMap = new HashMap(); |
| 173 | Collection linkDescriptors = collectAllLinks(getDiagram(), |
| 174 | domain2NotationMap); |
| 175 | Collection existingLinks = new LinkedList(getDiagram().getEdges()); |
| 176 | for (Iterator linksIterator = existingLinks.iterator(); linksIterator |
| 177 | .hasNext();) { |
| 178 | Edge nextDiagramLink = (Edge) linksIterator.next(); |
| 179 | int diagramLinkVisualID = PalladioComponentModelVisualIDRegistry |
| 180 | .getVisualID(nextDiagramLink); |
| 181 | if (diagramLinkVisualID == -1) { |
| 182 | if (nextDiagramLink.getSource() != null |
| 183 | && nextDiagramLink.getTarget() != null) { |
| 184 | linksIterator.remove(); |
| 185 | } |
| 186 | continue; |
| 187 | } |
| 188 | EObject diagramLinkObject = nextDiagramLink.getElement(); |
| 189 | EObject diagramLinkSrc = nextDiagramLink.getSource().getElement(); |
| 190 | EObject diagramLinkDst = nextDiagramLink.getTarget().getElement(); |
| 191 | for (Iterator linkDescriptorsIterator = linkDescriptors.iterator(); linkDescriptorsIterator |
| 192 | .hasNext();) { |
| 193 | PalladioComponentModelLinkDescriptor nextLinkDescriptor = (PalladioComponentModelLinkDescriptor) linkDescriptorsIterator |
| 194 | .next(); |
| 195 | if (diagramLinkObject == nextLinkDescriptor.getModelElement() |
| 196 | && diagramLinkSrc == nextLinkDescriptor.getSource() |
| 197 | && diagramLinkDst == nextLinkDescriptor |
| 198 | .getDestination() |
| 199 | && diagramLinkVisualID == nextLinkDescriptor |
| 200 | .getVisualID()) { |
| 201 | linksIterator.remove(); |
| 202 | linkDescriptorsIterator.remove(); |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | deleteViews(existingLinks.iterator()); |
| 208 | return createConnections(linkDescriptors, domain2NotationMap); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * @generated |
| 213 | */ |
| 214 | private Collection collectAllLinks(View view, Map domain2NotationMap) { |
| 215 | if (!UsageScenarioEditPart.MODEL_ID |
| 216 | .equals(PalladioComponentModelVisualIDRegistry.getModelID(view))) { |
| 217 | return Collections.EMPTY_LIST; |
| 218 | } |
| 219 | Collection result = new LinkedList(); |
| 220 | switch (PalladioComponentModelVisualIDRegistry.getVisualID(view)) { |
| 221 | case UsageScenarioEditPart.VISUAL_ID: { |
| 222 | if (!domain2NotationMap.containsKey(view.getElement())) { |
| 223 | result.addAll(PalladioComponentModelDiagramUpdater |
| 224 | .getUsageModel_1000ContainedLinks(view)); |
| 225 | } |
| 226 | if (!domain2NotationMap.containsKey(view.getElement()) |
| 227 | || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$ |
| 228 | domain2NotationMap.put(view.getElement(), view); |
| 229 | } |
| 230 | break; |
| 231 | } |
| 232 | case UsageScenario2EditPart.VISUAL_ID: { |
| 233 | if (!domain2NotationMap.containsKey(view.getElement())) { |
| 234 | result.addAll(PalladioComponentModelDiagramUpdater |
| 235 | .getUsageScenario_2004ContainedLinks(view)); |
| 236 | } |
| 237 | if (!domain2NotationMap.containsKey(view.getElement()) |
| 238 | || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$ |
| 239 | domain2NotationMap.put(view.getElement(), view); |
| 240 | } |
| 241 | break; |
| 242 | } |
| 243 | case ScenarioBehaviourEditPart.VISUAL_ID: { |
| 244 | if (!domain2NotationMap.containsKey(view.getElement())) { |
| 245 | result.addAll(PalladioComponentModelDiagramUpdater |
| 246 | .getScenarioBehaviour_3014ContainedLinks(view)); |
| 247 | } |
| 248 | if (!domain2NotationMap.containsKey(view.getElement()) |
| 249 | || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$ |
| 250 | domain2NotationMap.put(view.getElement(), view); |
| 251 | } |
| 252 | break; |
| 253 | } |
| 254 | case StartEditPart.VISUAL_ID: { |
| 255 | if (!domain2NotationMap.containsKey(view.getElement())) { |
| 256 | result.addAll(PalladioComponentModelDiagramUpdater |
| 257 | .getStart_3001ContainedLinks(view)); |
| 258 | } |
| 259 | if (!domain2NotationMap.containsKey(view.getElement()) |
| 260 | || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$ |
| 261 | domain2NotationMap.put(view.getElement(), view); |
| 262 | } |
| 263 | break; |
| 264 | } |
| 265 | case StopEditPart.VISUAL_ID: { |
| 266 | if (!domain2NotationMap.containsKey(view.getElement())) { |
| 267 | result.addAll(PalladioComponentModelDiagramUpdater |
| 268 | .getStop_3002ContainedLinks(view)); |
| 269 | } |
| 270 | if (!domain2NotationMap.containsKey(view.getElement()) |
| 271 | || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$ |
| 272 | domain2NotationMap.put(view.getElement(), view); |
| 273 | } |
| 274 | break; |
| 275 | } |
| 276 | case EntryLevelSystemCallEditPart.VISUAL_ID: { |
| 277 | if (!domain2NotationMap.containsKey(view.getElement())) { |
| 278 | result.addAll(PalladioComponentModelDiagramUpdater |
| 279 | .getEntryLevelSystemCall_3003ContainedLinks(view)); |
| 280 | } |
| 281 | if (!domain2NotationMap.containsKey(view.getElement()) |
| 282 | || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$ |
| 283 | domain2NotationMap.put(view.getElement(), view); |
| 284 | } |
| 285 | break; |
| 286 | } |
| 287 | case VariableUsageEditPart.VISUAL_ID: { |
| 288 | if (!domain2NotationMap.containsKey(view.getElement())) { |
| 289 | result.addAll(PalladioComponentModelDiagramUpdater |
| 290 | .getVariableUsage_3012ContainedLinks(view)); |
| 291 | } |
| 292 | if (!domain2NotationMap.containsKey(view.getElement()) |
| 293 | || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$ |
| 294 | domain2NotationMap.put(view.getElement(), view); |
| 295 | } |
| 296 | break; |
| 297 | } |
| 298 | case LoopEditPart.VISUAL_ID: { |
| 299 | if (!domain2NotationMap.containsKey(view.getElement())) { |
| 300 | result.addAll(PalladioComponentModelDiagramUpdater |
| 301 | .getLoop_3005ContainedLinks(view)); |
| 302 | } |
| 303 | if (!domain2NotationMap.containsKey(view.getElement()) |
| 304 | || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$ |
| 305 | domain2NotationMap.put(view.getElement(), view); |
| 306 | } |
| 307 | break; |
| 308 | } |
| 309 | case ScenarioBehaviour2EditPart.VISUAL_ID: { |
| 310 | if (!domain2NotationMap.containsKey(view.getElement())) { |
| 311 | result.addAll(PalladioComponentModelDiagramUpdater |
| 312 | .getScenarioBehaviour_3007ContainedLinks(view)); |
| 313 | } |
| 314 | if (!domain2NotationMap.containsKey(view.getElement()) |
| 315 | || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$ |
| 316 | domain2NotationMap.put(view.getElement(), view); |
| 317 | } |
| 318 | break; |
| 319 | } |
| 320 | case BranchEditPart.VISUAL_ID: { |
| 321 | if (!domain2NotationMap.containsKey(view.getElement())) { |
| 322 | result.addAll(PalladioComponentModelDiagramUpdater |
| 323 | .getBranch_3008ContainedLinks(view)); |
| 324 | } |
| 325 | if (!domain2NotationMap.containsKey(view.getElement()) |
| 326 | || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$ |
| 327 | domain2NotationMap.put(view.getElement(), view); |
| 328 | } |
| 329 | break; |
| 330 | } |
| 331 | case BranchTransitionEditPart.VISUAL_ID: { |
| 332 | if (!domain2NotationMap.containsKey(view.getElement())) { |
| 333 | result.addAll(PalladioComponentModelDiagramUpdater |
| 334 | .getBranchTransition_3009ContainedLinks(view)); |
| 335 | } |
| 336 | if (!domain2NotationMap.containsKey(view.getElement()) |
| 337 | || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$ |
| 338 | domain2NotationMap.put(view.getElement(), view); |
| 339 | } |
| 340 | break; |
| 341 | } |
| 342 | case ScenarioBehaviour3EditPart.VISUAL_ID: { |
| 343 | if (!domain2NotationMap.containsKey(view.getElement())) { |
| 344 | result.addAll(PalladioComponentModelDiagramUpdater |
| 345 | .getScenarioBehaviour_3010ContainedLinks(view)); |
| 346 | } |
| 347 | if (!domain2NotationMap.containsKey(view.getElement()) |
| 348 | || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$ |
| 349 | domain2NotationMap.put(view.getElement(), view); |
| 350 | } |
| 351 | break; |
| 352 | } |
| 353 | case DelayEditPart.VISUAL_ID: { |
| 354 | if (!domain2NotationMap.containsKey(view.getElement())) { |
| 355 | result.addAll(PalladioComponentModelDiagramUpdater |
| 356 | .getDelay_3017ContainedLinks(view)); |
| 357 | } |
| 358 | if (!domain2NotationMap.containsKey(view.getElement()) |
| 359 | || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$ |
| 360 | domain2NotationMap.put(view.getElement(), view); |
| 361 | } |
| 362 | break; |
| 363 | } |
| 364 | case ClosedWorkloadEditPart.VISUAL_ID: { |
| 365 | if (!domain2NotationMap.containsKey(view.getElement())) { |
| 366 | result.addAll(PalladioComponentModelDiagramUpdater |
| 367 | .getClosedWorkload_3015ContainedLinks(view)); |
| 368 | } |
| 369 | if (!domain2NotationMap.containsKey(view.getElement()) |
| 370 | || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$ |
| 371 | domain2NotationMap.put(view.getElement(), view); |
| 372 | } |
| 373 | break; |
| 374 | } |
| 375 | case OpenWorkloadEditPart.VISUAL_ID: { |
| 376 | if (!domain2NotationMap.containsKey(view.getElement())) { |
| 377 | result.addAll(PalladioComponentModelDiagramUpdater |
| 378 | .getOpenWorkload_3016ContainedLinks(view)); |
| 379 | } |
| 380 | if (!domain2NotationMap.containsKey(view.getElement()) |
| 381 | || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$ |
| 382 | domain2NotationMap.put(view.getElement(), view); |
| 383 | } |
| 384 | break; |
| 385 | } |
| 386 | } |
| 387 | for (Iterator children = view.getChildren().iterator(); children |
| 388 | .hasNext();) { |
| 389 | result.addAll(collectAllLinks((View) children.next(), |
| 390 | domain2NotationMap)); |
| 391 | } |
| 392 | for (Iterator edges = view.getSourceEdges().iterator(); edges.hasNext();) { |
| 393 | result.addAll(collectAllLinks((View) edges.next(), |
| 394 | domain2NotationMap)); |
| 395 | } |
| 396 | return result; |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * @generated |
| 401 | */ |
| 402 | private Collection createConnections(Collection linkDescriptors, |
| 403 | Map domain2NotationMap) { |
| 404 | List adapters = new LinkedList(); |
| 405 | for (Iterator linkDescriptorsIterator = linkDescriptors.iterator(); linkDescriptorsIterator |
| 406 | .hasNext();) { |
| 407 | final PalladioComponentModelLinkDescriptor nextLinkDescriptor = (PalladioComponentModelLinkDescriptor) linkDescriptorsIterator |
| 408 | .next(); |
| 409 | EditPart sourceEditPart = getEditPart(nextLinkDescriptor |
| 410 | .getSource(), domain2NotationMap); |
| 411 | EditPart targetEditPart = getEditPart(nextLinkDescriptor |
| 412 | .getDestination(), domain2NotationMap); |
| 413 | if (sourceEditPart == null || targetEditPart == null) { |
| 414 | continue; |
| 415 | } |
| 416 | CreateConnectionViewRequest.ConnectionViewDescriptor descriptor = new CreateConnectionViewRequest.ConnectionViewDescriptor( |
| 417 | nextLinkDescriptor.getSemanticAdapter(), String |
| 418 | .valueOf(nextLinkDescriptor.getVisualID()), |
| 419 | ViewUtil.APPEND, false, ((IGraphicalEditPart) getHost()) |
| 420 | .getDiagramPreferencesHint()); |
| 421 | CreateConnectionViewRequest ccr = new CreateConnectionViewRequest( |
| 422 | descriptor); |
| 423 | ccr.setType(RequestConstants.REQ_CONNECTION_START); |
| 424 | ccr.setSourceEditPart(sourceEditPart); |
| 425 | sourceEditPart.getCommand(ccr); |
| 426 | ccr.setTargetEditPart(targetEditPart); |
| 427 | ccr.setType(RequestConstants.REQ_CONNECTION_END); |
| 428 | Command cmd = targetEditPart.getCommand(ccr); |
| 429 | if (cmd != null && cmd.canExecute()) { |
| 430 | executeCommand(cmd); |
| 431 | IAdaptable viewAdapter = (IAdaptable) ccr.getNewObject(); |
| 432 | if (viewAdapter != null) { |
| 433 | adapters.add(viewAdapter); |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | return adapters; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * @generated |
| 442 | */ |
| 443 | private EditPart getEditPart(EObject domainModelElement, |
| 444 | Map domain2NotationMap) { |
| 445 | View view = (View) domain2NotationMap.get(domainModelElement); |
| 446 | if (view != null) { |
| 447 | return (EditPart) getHost().getViewer().getEditPartRegistry().get( |
| 448 | view); |
| 449 | } |
| 450 | return null; |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * @generated |
| 455 | */ |
| 456 | private Diagram getDiagram() { |
| 457 | return ((View) getHost().getModel()).getDiagram(); |
| 458 | } |
| 459 | |
| 460 | } |