| 1 | /* |
| 2 | * Copyright 2009, SDQ, IPD, Uni Karlsruhe (TH) |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.pcm.gmf.resource.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.resource.edit.parts.CommunicationLinkResourceSpecificationEditPart; |
| 32 | import de.uka.ipd.sdq.pcm.gmf.resource.edit.parts.LinkingResource2EditPart; |
| 33 | import de.uka.ipd.sdq.pcm.gmf.resource.edit.parts.ProcessingResourceSpecificationEditPart; |
| 34 | import de.uka.ipd.sdq.pcm.gmf.resource.edit.parts.ResourceContainerEditPart; |
| 35 | import de.uka.ipd.sdq.pcm.gmf.resource.edit.parts.ResourceEnvironmentEditPart; |
| 36 | import de.uka.ipd.sdq.pcm.gmf.resource.part.PalladioComponentModelDiagramUpdater; |
| 37 | import de.uka.ipd.sdq.pcm.gmf.resource.part.PalladioComponentModelLinkDescriptor; |
| 38 | import de.uka.ipd.sdq.pcm.gmf.resource.part.PalladioComponentModelNodeDescriptor; |
| 39 | import de.uka.ipd.sdq.pcm.gmf.resource.part.PalladioComponentModelVisualIDRegistry; |
| 40 | import de.uka.ipd.sdq.pcm.resourceenvironment.ResourceenvironmentPackage; |
| 41 | |
| 42 | /** |
| 43 | * @generated |
| 44 | */ |
| 45 | public class ResourceEnvironmentCanonicalEditPolicy extends |
| 46 | CanonicalConnectionEditPolicy { |
| 47 | |
| 48 | /** |
| 49 | * @generated |
| 50 | */ |
| 51 | Set myFeaturesToSynchronize; |
| 52 | |
| 53 | /** |
| 54 | * @generated |
| 55 | */ |
| 56 | protected List getSemanticChildrenList() { |
| 57 | View viewObject = (View) getHost().getModel(); |
| 58 | List result = new LinkedList(); |
| 59 | for (Iterator it = PalladioComponentModelDiagramUpdater |
| 60 | .getResourceEnvironment_1000SemanticChildren(viewObject) |
| 61 | .iterator(); it.hasNext();) { |
| 62 | result.add(((PalladioComponentModelNodeDescriptor) it.next()) |
| 63 | .getModelElement()); |
| 64 | } |
| 65 | return result; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @generated |
| 70 | */ |
| 71 | protected boolean shouldDeleteView(View view) { |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @generated |
| 77 | */ |
| 78 | protected boolean isOrphaned(Collection semanticChildren, final View view) { |
| 79 | int visualID = PalladioComponentModelVisualIDRegistry.getVisualID(view); |
| 80 | switch (visualID) { |
| 81 | case ResourceContainerEditPart.VISUAL_ID: |
| 82 | case LinkingResource2EditPart.VISUAL_ID: |
| 83 | if (!semanticChildren.contains(view.getElement())) { |
| 84 | return true; |
| 85 | } |
| 86 | } |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @generated |
| 92 | */ |
| 93 | protected String getDefaultFactoryHint() { |
| 94 | return null; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @generated |
| 99 | */ |
| 100 | protected Set getFeaturesToSynchronize() { |
| 101 | if (myFeaturesToSynchronize == null) { |
| 102 | myFeaturesToSynchronize = new HashSet(); |
| 103 | myFeaturesToSynchronize |
| 104 | .add(ResourceenvironmentPackage.eINSTANCE |
| 105 | .getResourceEnvironment_ResourceContainer_ResourceEnvironment()); |
| 106 | myFeaturesToSynchronize |
| 107 | .add(ResourceenvironmentPackage.eINSTANCE |
| 108 | .getResourceEnvironment_LinkingResources__ResourceEnvironment()); |
| 109 | } |
| 110 | return myFeaturesToSynchronize; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @generated |
| 115 | */ |
| 116 | protected List getSemanticConnectionsList() { |
| 117 | return Collections.EMPTY_LIST; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @generated |
| 122 | */ |
| 123 | protected EObject getSourceElement(EObject relationship) { |
| 124 | return null; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @generated |
| 129 | */ |
| 130 | protected EObject getTargetElement(EObject relationship) { |
| 131 | return null; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @generated |
| 136 | */ |
| 137 | protected boolean shouldIncludeConnection(Edge connector, |
| 138 | Collection children) { |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @generated |
| 144 | */ |
| 145 | protected void refreshSemantic() { |
| 146 | List createdViews = new LinkedList(); |
| 147 | createdViews.addAll(refreshSemanticChildren()); |
| 148 | List createdConnectionViews = new LinkedList(); |
| 149 | createdConnectionViews.addAll(refreshSemanticConnections()); |
| 150 | createdConnectionViews.addAll(refreshConnections()); |
| 151 | |
| 152 | if (createdViews.size() > 1) { |
| 153 | // perform a layout of the container |
| 154 | DeferredLayoutCommand layoutCmd = new DeferredLayoutCommand(host() |
| 155 | .getEditingDomain(), createdViews, host()); |
| 156 | executeCommand(new ICommandProxy(layoutCmd)); |
| 157 | } |
| 158 | |
| 159 | createdViews.addAll(createdConnectionViews); |
| 160 | makeViewsImmutable(createdViews); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * @generated |
| 165 | */ |
| 166 | private Diagram getDiagram() { |
| 167 | return ((View) getHost().getModel()).getDiagram(); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * @generated |
| 172 | */ |
| 173 | private Collection refreshConnections() { |
| 174 | Map domain2NotationMap = new HashMap(); |
| 175 | Collection linkDescriptors = collectAllLinks(getDiagram(), |
| 176 | domain2NotationMap); |
| 177 | Collection existingLinks = new LinkedList(getDiagram().getEdges()); |
| 178 | for (Iterator linksIterator = existingLinks.iterator(); linksIterator |
| 179 | .hasNext();) { |
| 180 | Edge nextDiagramLink = (Edge) linksIterator.next(); |
| 181 | int diagramLinkVisualID = PalladioComponentModelVisualIDRegistry |
| 182 | .getVisualID(nextDiagramLink); |
| 183 | if (diagramLinkVisualID == -1) { |
| 184 | if (nextDiagramLink.getSource() != null |
| 185 | && nextDiagramLink.getTarget() != null) { |
| 186 | linksIterator.remove(); |
| 187 | } |
| 188 | continue; |
| 189 | } |
| 190 | EObject diagramLinkObject = nextDiagramLink.getElement(); |
| 191 | EObject diagramLinkSrc = nextDiagramLink.getSource().getElement(); |
| 192 | EObject diagramLinkDst = nextDiagramLink.getTarget().getElement(); |
| 193 | for (Iterator linkDescriptorsIterator = linkDescriptors.iterator(); linkDescriptorsIterator |
| 194 | .hasNext();) { |
| 195 | PalladioComponentModelLinkDescriptor nextLinkDescriptor = (PalladioComponentModelLinkDescriptor) linkDescriptorsIterator |
| 196 | .next(); |
| 197 | if (diagramLinkObject == nextLinkDescriptor.getModelElement() |
| 198 | && diagramLinkSrc == nextLinkDescriptor.getSource() |
| 199 | && diagramLinkDst == nextLinkDescriptor |
| 200 | .getDestination() |
| 201 | && diagramLinkVisualID == nextLinkDescriptor |
| 202 | .getVisualID()) { |
| 203 | linksIterator.remove(); |
| 204 | linkDescriptorsIterator.remove(); |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | deleteViews(existingLinks.iterator()); |
| 210 | return createConnections(linkDescriptors, domain2NotationMap); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * @generated |
| 215 | */ |
| 216 | private Collection collectAllLinks(View view, Map domain2NotationMap) { |
| 217 | if (!ResourceEnvironmentEditPart.MODEL_ID |
| 218 | .equals(PalladioComponentModelVisualIDRegistry.getModelID(view))) { |
| 219 | return Collections.EMPTY_LIST; |
| 220 | } |
| 221 | Collection result = new LinkedList(); |
| 222 | switch (PalladioComponentModelVisualIDRegistry.getVisualID(view)) { |
| 223 | case ResourceEnvironmentEditPart.VISUAL_ID: { |
| 224 | if (!domain2NotationMap.containsKey(view.getElement())) { |
| 225 | result.addAll(PalladioComponentModelDiagramUpdater |
| 226 | .getResourceEnvironment_1000ContainedLinks(view)); |
| 227 | } |
| 228 | if (!domain2NotationMap.containsKey(view.getElement()) |
| 229 | || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$ |
| 230 | domain2NotationMap.put(view.getElement(), view); |
| 231 | } |
| 232 | break; |
| 233 | } |
| 234 | case ResourceContainerEditPart.VISUAL_ID: { |
| 235 | if (!domain2NotationMap.containsKey(view.getElement())) { |
| 236 | result.addAll(PalladioComponentModelDiagramUpdater |
| 237 | .getResourceContainer_2001ContainedLinks(view)); |
| 238 | } |
| 239 | if (!domain2NotationMap.containsKey(view.getElement()) |
| 240 | || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$ |
| 241 | domain2NotationMap.put(view.getElement(), view); |
| 242 | } |
| 243 | break; |
| 244 | } |
| 245 | case LinkingResource2EditPart.VISUAL_ID: { |
| 246 | if (!domain2NotationMap.containsKey(view.getElement())) { |
| 247 | result.addAll(PalladioComponentModelDiagramUpdater |
| 248 | .getLinkingResource_2003ContainedLinks(view)); |
| 249 | } |
| 250 | if (!domain2NotationMap.containsKey(view.getElement()) |
| 251 | || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$ |
| 252 | domain2NotationMap.put(view.getElement(), view); |
| 253 | } |
| 254 | break; |
| 255 | } |
| 256 | case ProcessingResourceSpecificationEditPart.VISUAL_ID: { |
| 257 | if (!domain2NotationMap.containsKey(view.getElement())) { |
| 258 | result |
| 259 | .addAll(PalladioComponentModelDiagramUpdater |
| 260 | .getProcessingResourceSpecification_3001ContainedLinks(view)); |
| 261 | } |
| 262 | if (!domain2NotationMap.containsKey(view.getElement()) |
| 263 | || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$ |
| 264 | domain2NotationMap.put(view.getElement(), view); |
| 265 | } |
| 266 | break; |
| 267 | } |
| 268 | case CommunicationLinkResourceSpecificationEditPart.VISUAL_ID: { |
| 269 | if (!domain2NotationMap.containsKey(view.getElement())) { |
| 270 | result |
| 271 | .addAll(PalladioComponentModelDiagramUpdater |
| 272 | .getCommunicationLinkResourceSpecification_3002ContainedLinks(view)); |
| 273 | } |
| 274 | if (!domain2NotationMap.containsKey(view.getElement()) |
| 275 | || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$ |
| 276 | domain2NotationMap.put(view.getElement(), view); |
| 277 | } |
| 278 | break; |
| 279 | } |
| 280 | } |
| 281 | for (Iterator children = view.getChildren().iterator(); children |
| 282 | .hasNext();) { |
| 283 | result.addAll(collectAllLinks((View) children.next(), |
| 284 | domain2NotationMap)); |
| 285 | } |
| 286 | for (Iterator edges = view.getSourceEdges().iterator(); edges.hasNext();) { |
| 287 | result.addAll(collectAllLinks((View) edges.next(), |
| 288 | domain2NotationMap)); |
| 289 | } |
| 290 | return result; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * @generated |
| 295 | */ |
| 296 | private Collection createConnections(Collection linkDescriptors, |
| 297 | Map domain2NotationMap) { |
| 298 | List adapters = new LinkedList(); |
| 299 | for (Iterator linkDescriptorsIterator = linkDescriptors.iterator(); linkDescriptorsIterator |
| 300 | .hasNext();) { |
| 301 | final PalladioComponentModelLinkDescriptor nextLinkDescriptor = (PalladioComponentModelLinkDescriptor) linkDescriptorsIterator |
| 302 | .next(); |
| 303 | EditPart sourceEditPart = getEditPart(nextLinkDescriptor |
| 304 | .getSource(), domain2NotationMap); |
| 305 | EditPart targetEditPart = getEditPart(nextLinkDescriptor |
| 306 | .getDestination(), domain2NotationMap); |
| 307 | if (sourceEditPart == null || targetEditPart == null) { |
| 308 | continue; |
| 309 | } |
| 310 | CreateConnectionViewRequest.ConnectionViewDescriptor descriptor = new CreateConnectionViewRequest.ConnectionViewDescriptor( |
| 311 | nextLinkDescriptor.getSemanticAdapter(), String |
| 312 | .valueOf(nextLinkDescriptor.getVisualID()), |
| 313 | ViewUtil.APPEND, false, ((IGraphicalEditPart) getHost()) |
| 314 | .getDiagramPreferencesHint()); |
| 315 | CreateConnectionViewRequest ccr = new CreateConnectionViewRequest( |
| 316 | descriptor); |
| 317 | ccr.setType(RequestConstants.REQ_CONNECTION_START); |
| 318 | ccr.setSourceEditPart(sourceEditPart); |
| 319 | sourceEditPart.getCommand(ccr); |
| 320 | ccr.setTargetEditPart(targetEditPart); |
| 321 | ccr.setType(RequestConstants.REQ_CONNECTION_END); |
| 322 | Command cmd = targetEditPart.getCommand(ccr); |
| 323 | if (cmd != null && cmd.canExecute()) { |
| 324 | executeCommand(cmd); |
| 325 | IAdaptable viewAdapter = (IAdaptable) ccr.getNewObject(); |
| 326 | if (viewAdapter != null) { |
| 327 | adapters.add(viewAdapter); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | return adapters; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * @generated |
| 336 | */ |
| 337 | private EditPart getEditPart(EObject domainModelElement, |
| 338 | Map domain2NotationMap) { |
| 339 | View view = (View) domain2NotationMap.get(domainModelElement); |
| 340 | if (view != null) { |
| 341 | return (EditPart) getHost().getViewer().getEditPartRegistry().get( |
| 342 | view); |
| 343 | } |
| 344 | return null; |
| 345 | } |
| 346 | |
| 347 | } |