| 1 | /* |
| 2 | * Copyright 2009, SDQ, IPD, Uni Karlsruhe (TH) |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.pcm.gmf.resource.navigator; |
| 5 | |
| 6 | import java.util.ArrayList; |
| 7 | import java.util.Collection; |
| 8 | import java.util.Collections; |
| 9 | import java.util.HashMap; |
| 10 | import java.util.Iterator; |
| 11 | |
| 12 | import org.eclipse.core.resources.IFile; |
| 13 | import org.eclipse.emf.common.util.URI; |
| 14 | import org.eclipse.emf.ecore.resource.Resource; |
| 15 | import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain; |
| 16 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
| 17 | import org.eclipse.emf.workspace.util.WorkspaceSynchronizer; |
| 18 | import org.eclipse.gmf.runtime.emf.core.GMFEditingDomainFactory; |
| 19 | import org.eclipse.gmf.runtime.notation.Diagram; |
| 20 | import org.eclipse.gmf.runtime.notation.Edge; |
| 21 | import org.eclipse.gmf.runtime.notation.View; |
| 22 | import org.eclipse.jface.viewers.Viewer; |
| 23 | import org.eclipse.ui.IMemento; |
| 24 | import org.eclipse.ui.navigator.ICommonContentExtensionSite; |
| 25 | import org.eclipse.ui.navigator.ICommonContentProvider; |
| 26 | |
| 27 | import de.uka.ipd.sdq.pcm.gmf.resource.edit.parts.CommunicationLinkResourceSpecificationEditPart; |
| 28 | import de.uka.ipd.sdq.pcm.gmf.resource.edit.parts.LinkingResource2EditPart; |
| 29 | import de.uka.ipd.sdq.pcm.gmf.resource.edit.parts.LinkingResourceConnectedResourceContainers_LinkingResourceEditPart; |
| 30 | import de.uka.ipd.sdq.pcm.gmf.resource.edit.parts.LinkingResourceNetworkSwitchCompartmentEditPart; |
| 31 | import de.uka.ipd.sdq.pcm.gmf.resource.edit.parts.ProcessingResourceSpecificationEditPart; |
| 32 | import de.uka.ipd.sdq.pcm.gmf.resource.edit.parts.ResourceContainerEditPart; |
| 33 | import de.uka.ipd.sdq.pcm.gmf.resource.edit.parts.ResourceContainerResourceContainerCompartmentEditPart; |
| 34 | import de.uka.ipd.sdq.pcm.gmf.resource.edit.parts.ResourceEnvironmentEditPart; |
| 35 | import de.uka.ipd.sdq.pcm.gmf.resource.part.Messages; |
| 36 | import de.uka.ipd.sdq.pcm.gmf.resource.part.PalladioComponentModelVisualIDRegistry; |
| 37 | |
| 38 | /** |
| 39 | * @generated |
| 40 | */ |
| 41 | public class PalladioComponentModelNavigatorContentProvider implements |
| 42 | ICommonContentProvider { |
| 43 | |
| 44 | /** |
| 45 | * @generated |
| 46 | */ |
| 47 | private static final Object[] EMPTY_ARRAY = new Object[0]; |
| 48 | |
| 49 | /** |
| 50 | * @generated |
| 51 | */ |
| 52 | private Viewer myViewer; |
| 53 | |
| 54 | /** |
| 55 | * @generated |
| 56 | */ |
| 57 | private AdapterFactoryEditingDomain myEditingDomain; |
| 58 | |
| 59 | /** |
| 60 | * @generated |
| 61 | */ |
| 62 | private WorkspaceSynchronizer myWorkspaceSynchronizer; |
| 63 | |
| 64 | /** |
| 65 | * @generated |
| 66 | */ |
| 67 | private Runnable myViewerRefreshRunnable; |
| 68 | |
| 69 | /** |
| 70 | * @generated |
| 71 | */ |
| 72 | public PalladioComponentModelNavigatorContentProvider() { |
| 73 | TransactionalEditingDomain editingDomain = GMFEditingDomainFactory.INSTANCE |
| 74 | .createEditingDomain(); |
| 75 | myEditingDomain = (AdapterFactoryEditingDomain) editingDomain; |
| 76 | myEditingDomain.setResourceToReadOnlyMap(new HashMap() { |
| 77 | public Object get(Object key) { |
| 78 | if (!containsKey(key)) { |
| 79 | put(key, Boolean.TRUE); |
| 80 | } |
| 81 | return super.get(key); |
| 82 | } |
| 83 | }); |
| 84 | myViewerRefreshRunnable = new Runnable() { |
| 85 | public void run() { |
| 86 | if (myViewer != null) { |
| 87 | myViewer.refresh(); |
| 88 | } |
| 89 | } |
| 90 | }; |
| 91 | myWorkspaceSynchronizer = new WorkspaceSynchronizer(editingDomain, |
| 92 | new WorkspaceSynchronizer.Delegate() { |
| 93 | public void dispose() { |
| 94 | } |
| 95 | |
| 96 | public boolean handleResourceChanged(final Resource resource) { |
| 97 | for (Iterator it = myEditingDomain.getResourceSet() |
| 98 | .getResources().iterator(); it.hasNext();) { |
| 99 | Resource nextResource = (Resource) it.next(); |
| 100 | nextResource.unload(); |
| 101 | } |
| 102 | if (myViewer != null) { |
| 103 | myViewer.getControl().getDisplay().asyncExec( |
| 104 | myViewerRefreshRunnable); |
| 105 | } |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | public boolean handleResourceDeleted(Resource resource) { |
| 110 | for (Iterator it = myEditingDomain.getResourceSet() |
| 111 | .getResources().iterator(); it.hasNext();) { |
| 112 | Resource nextResource = (Resource) it.next(); |
| 113 | nextResource.unload(); |
| 114 | } |
| 115 | if (myViewer != null) { |
| 116 | myViewer.getControl().getDisplay().asyncExec( |
| 117 | myViewerRefreshRunnable); |
| 118 | } |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | public boolean handleResourceMoved(Resource resource, |
| 123 | final URI newURI) { |
| 124 | for (Iterator it = myEditingDomain.getResourceSet() |
| 125 | .getResources().iterator(); it.hasNext();) { |
| 126 | Resource nextResource = (Resource) it.next(); |
| 127 | nextResource.unload(); |
| 128 | } |
| 129 | if (myViewer != null) { |
| 130 | myViewer.getControl().getDisplay().asyncExec( |
| 131 | myViewerRefreshRunnable); |
| 132 | } |
| 133 | return true; |
| 134 | } |
| 135 | }); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @generated |
| 140 | */ |
| 141 | public void dispose() { |
| 142 | myWorkspaceSynchronizer.dispose(); |
| 143 | myWorkspaceSynchronizer = null; |
| 144 | myViewerRefreshRunnable = null; |
| 145 | for (Iterator it = myEditingDomain.getResourceSet().getResources() |
| 146 | .iterator(); it.hasNext();) { |
| 147 | Resource resource = (Resource) it.next(); |
| 148 | resource.unload(); |
| 149 | } |
| 150 | ((TransactionalEditingDomain) myEditingDomain).dispose(); |
| 151 | myEditingDomain = null; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @generated |
| 156 | */ |
| 157 | public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
| 158 | myViewer = viewer; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * @generated |
| 163 | */ |
| 164 | public Object[] getElements(Object inputElement) { |
| 165 | return getChildren(inputElement); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @generated |
| 170 | */ |
| 171 | public void restoreState(IMemento aMemento) { |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * @generated |
| 176 | */ |
| 177 | public void saveState(IMemento aMemento) { |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * @generated |
| 182 | */ |
| 183 | public void init(ICommonContentExtensionSite aConfig) { |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * @generated |
| 188 | */ |
| 189 | public Object[] getChildren(Object parentElement) { |
| 190 | if (parentElement instanceof IFile) { |
| 191 | IFile file = (IFile) parentElement; |
| 192 | URI fileURI = URI.createPlatformResourceURI(file.getFullPath() |
| 193 | .toString(), true); |
| 194 | Resource resource = myEditingDomain.getResourceSet().getResource( |
| 195 | fileURI, true); |
| 196 | Collection result = new ArrayList(); |
| 197 | result.addAll(createNavigatorItems(selectViewsByType(resource |
| 198 | .getContents(), ResourceEnvironmentEditPart.MODEL_ID), |
| 199 | file, false)); |
| 200 | return result.toArray(); |
| 201 | } |
| 202 | |
| 203 | if (parentElement instanceof PalladioComponentModelNavigatorGroup) { |
| 204 | PalladioComponentModelNavigatorGroup group = (PalladioComponentModelNavigatorGroup) parentElement; |
| 205 | return group.getChildren(); |
| 206 | } |
| 207 | |
| 208 | if (parentElement instanceof PalladioComponentModelNavigatorItem) { |
| 209 | PalladioComponentModelNavigatorItem navigatorItem = (PalladioComponentModelNavigatorItem) parentElement; |
| 210 | if (navigatorItem.isLeaf() || !isOwnView(navigatorItem.getView())) { |
| 211 | return EMPTY_ARRAY; |
| 212 | } |
| 213 | return getViewChildren(navigatorItem.getView(), parentElement); |
| 214 | } |
| 215 | |
| 216 | return EMPTY_ARRAY; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * @generated |
| 221 | */ |
| 222 | private Object[] getViewChildren(View view, Object parentElement) { |
| 223 | switch (PalladioComponentModelVisualIDRegistry.getVisualID(view)) { |
| 224 | |
| 225 | case ResourceEnvironmentEditPart.VISUAL_ID: { |
| 226 | Collection result = new ArrayList(); |
| 227 | PalladioComponentModelNavigatorGroup links = new PalladioComponentModelNavigatorGroup( |
| 228 | Messages.NavigatorGroupName_ResourceEnvironment_1000_links, |
| 229 | "icons/linksNavigatorGroup.gif", parentElement); //$NON-NLS-1$ |
| 230 | Collection connectedViews = getChildrenByType(Collections |
| 231 | .singleton(view), PalladioComponentModelVisualIDRegistry |
| 232 | .getType(ResourceContainerEditPart.VISUAL_ID)); |
| 233 | result.addAll(createNavigatorItems(connectedViews, parentElement, |
| 234 | false)); |
| 235 | connectedViews = getChildrenByType(Collections.singleton(view), |
| 236 | PalladioComponentModelVisualIDRegistry |
| 237 | .getType(LinkingResource2EditPart.VISUAL_ID)); |
| 238 | result.addAll(createNavigatorItems(connectedViews, parentElement, |
| 239 | false)); |
| 240 | connectedViews = getDiagramLinksByType( |
| 241 | Collections.singleton(view), |
| 242 | PalladioComponentModelVisualIDRegistry |
| 243 | .getType(LinkingResourceConnectedResourceContainers_LinkingResourceEditPart.VISUAL_ID)); |
| 244 | links |
| 245 | .addChildren(createNavigatorItems(connectedViews, links, |
| 246 | false)); |
| 247 | if (!links.isEmpty()) { |
| 248 | result.add(links); |
| 249 | } |
| 250 | return result.toArray(); |
| 251 | } |
| 252 | |
| 253 | case ResourceContainerEditPart.VISUAL_ID: { |
| 254 | Collection result = new ArrayList(); |
| 255 | PalladioComponentModelNavigatorGroup incominglinks = new PalladioComponentModelNavigatorGroup( |
| 256 | Messages.NavigatorGroupName_ResourceContainer_2001_incominglinks, |
| 257 | "icons/incomingLinksNavigatorGroup.gif", parentElement); //$NON-NLS-1$ |
| 258 | Collection connectedViews = getChildrenByType( |
| 259 | Collections.singleton(view), |
| 260 | PalladioComponentModelVisualIDRegistry |
| 261 | .getType(ResourceContainerResourceContainerCompartmentEditPart.VISUAL_ID)); |
| 262 | connectedViews = getChildrenByType( |
| 263 | connectedViews, |
| 264 | PalladioComponentModelVisualIDRegistry |
| 265 | .getType(ProcessingResourceSpecificationEditPart.VISUAL_ID)); |
| 266 | result.addAll(createNavigatorItems(connectedViews, parentElement, |
| 267 | false)); |
| 268 | connectedViews = getIncomingLinksByType( |
| 269 | Collections.singleton(view), |
| 270 | PalladioComponentModelVisualIDRegistry |
| 271 | .getType(LinkingResourceConnectedResourceContainers_LinkingResourceEditPart.VISUAL_ID)); |
| 272 | incominglinks.addChildren(createNavigatorItems(connectedViews, |
| 273 | incominglinks, true)); |
| 274 | if (!incominglinks.isEmpty()) { |
| 275 | result.add(incominglinks); |
| 276 | } |
| 277 | return result.toArray(); |
| 278 | } |
| 279 | |
| 280 | case LinkingResource2EditPart.VISUAL_ID: { |
| 281 | Collection result = new ArrayList(); |
| 282 | PalladioComponentModelNavigatorGroup outgoinglinks = new PalladioComponentModelNavigatorGroup( |
| 283 | Messages.NavigatorGroupName_LinkingResource_2003_outgoinglinks, |
| 284 | "icons/outgoingLinksNavigatorGroup.gif", parentElement); //$NON-NLS-1$ |
| 285 | Collection connectedViews = getChildrenByType( |
| 286 | Collections.singleton(view), |
| 287 | PalladioComponentModelVisualIDRegistry |
| 288 | .getType(LinkingResourceNetworkSwitchCompartmentEditPart.VISUAL_ID)); |
| 289 | connectedViews = getChildrenByType( |
| 290 | connectedViews, |
| 291 | PalladioComponentModelVisualIDRegistry |
| 292 | .getType(CommunicationLinkResourceSpecificationEditPart.VISUAL_ID)); |
| 293 | result.addAll(createNavigatorItems(connectedViews, parentElement, |
| 294 | false)); |
| 295 | connectedViews = getOutgoingLinksByType( |
| 296 | Collections.singleton(view), |
| 297 | PalladioComponentModelVisualIDRegistry |
| 298 | .getType(LinkingResourceConnectedResourceContainers_LinkingResourceEditPart.VISUAL_ID)); |
| 299 | outgoinglinks.addChildren(createNavigatorItems(connectedViews, |
| 300 | outgoinglinks, true)); |
| 301 | if (!outgoinglinks.isEmpty()) { |
| 302 | result.add(outgoinglinks); |
| 303 | } |
| 304 | return result.toArray(); |
| 305 | } |
| 306 | |
| 307 | case LinkingResourceConnectedResourceContainers_LinkingResourceEditPart.VISUAL_ID: { |
| 308 | Collection result = new ArrayList(); |
| 309 | PalladioComponentModelNavigatorGroup target = new PalladioComponentModelNavigatorGroup( |
| 310 | Messages.NavigatorGroupName_LinkingResourceConnectedResourceContainers_LinkingResource_4002_target, |
| 311 | "icons/linkTargetNavigatorGroup.gif", parentElement); //$NON-NLS-1$ |
| 312 | PalladioComponentModelNavigatorGroup source = new PalladioComponentModelNavigatorGroup( |
| 313 | Messages.NavigatorGroupName_LinkingResourceConnectedResourceContainers_LinkingResource_4002_source, |
| 314 | "icons/linkSourceNavigatorGroup.gif", parentElement); //$NON-NLS-1$ |
| 315 | Collection connectedViews = getLinksTargetByType(Collections |
| 316 | .singleton(view), PalladioComponentModelVisualIDRegistry |
| 317 | .getType(ResourceContainerEditPart.VISUAL_ID)); |
| 318 | target.addChildren(createNavigatorItems(connectedViews, target, |
| 319 | true)); |
| 320 | connectedViews = getLinksSourceByType(Collections.singleton(view), |
| 321 | PalladioComponentModelVisualIDRegistry |
| 322 | .getType(LinkingResource2EditPart.VISUAL_ID)); |
| 323 | source.addChildren(createNavigatorItems(connectedViews, source, |
| 324 | true)); |
| 325 | if (!target.isEmpty()) { |
| 326 | result.add(target); |
| 327 | } |
| 328 | if (!source.isEmpty()) { |
| 329 | result.add(source); |
| 330 | } |
| 331 | return result.toArray(); |
| 332 | } |
| 333 | } |
| 334 | return EMPTY_ARRAY; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * @generated |
| 339 | */ |
| 340 | private Collection getLinksSourceByType(Collection edges, String type) { |
| 341 | Collection result = new ArrayList(); |
| 342 | for (Iterator it = edges.iterator(); it.hasNext();) { |
| 343 | Edge nextEdge = (Edge) it.next(); |
| 344 | View nextEdgeSource = nextEdge.getSource(); |
| 345 | if (type.equals(nextEdgeSource.getType()) |
| 346 | && isOwnView(nextEdgeSource)) { |
| 347 | result.add(nextEdgeSource); |
| 348 | } |
| 349 | } |
| 350 | return result; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * @generated |
| 355 | */ |
| 356 | private Collection getLinksTargetByType(Collection edges, String type) { |
| 357 | Collection result = new ArrayList(); |
| 358 | for (Iterator it = edges.iterator(); it.hasNext();) { |
| 359 | Edge nextEdge = (Edge) it.next(); |
| 360 | View nextEdgeTarget = nextEdge.getTarget(); |
| 361 | if (type.equals(nextEdgeTarget.getType()) |
| 362 | && isOwnView(nextEdgeTarget)) { |
| 363 | result.add(nextEdgeTarget); |
| 364 | } |
| 365 | } |
| 366 | return result; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * @generated |
| 371 | */ |
| 372 | private Collection getOutgoingLinksByType(Collection nodes, String type) { |
| 373 | Collection result = new ArrayList(); |
| 374 | for (Iterator it = nodes.iterator(); it.hasNext();) { |
| 375 | View nextNode = (View) it.next(); |
| 376 | result.addAll(selectViewsByType(nextNode.getSourceEdges(), type)); |
| 377 | } |
| 378 | return result; |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * @generated |
| 383 | */ |
| 384 | private Collection getIncomingLinksByType(Collection nodes, String type) { |
| 385 | Collection result = new ArrayList(); |
| 386 | for (Iterator it = nodes.iterator(); it.hasNext();) { |
| 387 | View nextNode = (View) it.next(); |
| 388 | result.addAll(selectViewsByType(nextNode.getTargetEdges(), type)); |
| 389 | } |
| 390 | return result; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * @generated |
| 395 | */ |
| 396 | private Collection getChildrenByType(Collection nodes, String type) { |
| 397 | Collection result = new ArrayList(); |
| 398 | for (Iterator it = nodes.iterator(); it.hasNext();) { |
| 399 | View nextNode = (View) it.next(); |
| 400 | result.addAll(selectViewsByType(nextNode.getChildren(), type)); |
| 401 | } |
| 402 | return result; |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * @generated |
| 407 | */ |
| 408 | private Collection getDiagramLinksByType(Collection diagrams, String type) { |
| 409 | Collection result = new ArrayList(); |
| 410 | for (Iterator it = diagrams.iterator(); it.hasNext();) { |
| 411 | Diagram nextDiagram = (Diagram) it.next(); |
| 412 | result.addAll(selectViewsByType(nextDiagram.getEdges(), type)); |
| 413 | } |
| 414 | return result; |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * @generated |
| 419 | */ |
| 420 | private Collection selectViewsByType(Collection views, String type) { |
| 421 | Collection result = new ArrayList(); |
| 422 | for (Iterator it = views.iterator(); it.hasNext();) { |
| 423 | View nextView = (View) it.next(); |
| 424 | if (type.equals(nextView.getType()) && isOwnView(nextView)) { |
| 425 | result.add(nextView); |
| 426 | } |
| 427 | } |
| 428 | return result; |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * @generated |
| 433 | */ |
| 434 | private boolean isOwnView(View view) { |
| 435 | return ResourceEnvironmentEditPart.MODEL_ID |
| 436 | .equals(PalladioComponentModelVisualIDRegistry.getModelID(view)); |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * @generated |
| 441 | */ |
| 442 | private Collection createNavigatorItems(Collection views, Object parent, |
| 443 | boolean isLeafs) { |
| 444 | Collection result = new ArrayList(); |
| 445 | for (Iterator it = views.iterator(); it.hasNext();) { |
| 446 | result.add(new PalladioComponentModelNavigatorItem( |
| 447 | (View) it.next(), parent, isLeafs)); |
| 448 | } |
| 449 | return result; |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * @generated |
| 454 | */ |
| 455 | public Object getParent(Object element) { |
| 456 | if (element instanceof PalladioComponentModelAbstractNavigatorItem) { |
| 457 | PalladioComponentModelAbstractNavigatorItem abstractNavigatorItem = (PalladioComponentModelAbstractNavigatorItem) element; |
| 458 | return abstractNavigatorItem.getParent(); |
| 459 | } |
| 460 | return null; |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * @generated |
| 465 | */ |
| 466 | public boolean hasChildren(Object element) { |
| 467 | return element instanceof IFile || getChildren(element).length > 0; |
| 468 | } |
| 469 | |
| 470 | } |