| 1 | /* |
| 2 | *Copyright 2007, SDQ, IPD, University of Karlsruhe |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.pcm.gmf.usage.navigator; |
| 5 | |
| 6 | import java.util.Iterator; |
| 7 | |
| 8 | import org.eclipse.core.resources.IFile; |
| 9 | import org.eclipse.emf.common.ui.URIEditorInput; |
| 10 | import org.eclipse.emf.common.util.URI; |
| 11 | import org.eclipse.emf.ecore.EObject; |
| 12 | import org.eclipse.emf.ecore.resource.Resource; |
| 13 | import org.eclipse.emf.ecore.resource.ResourceSet; |
| 14 | import org.eclipse.emf.ecore.util.EcoreUtil; |
| 15 | import org.eclipse.emf.workspace.util.WorkspaceSynchronizer; |
| 16 | import org.eclipse.gef.EditPart; |
| 17 | import org.eclipse.gef.GraphicalViewer; |
| 18 | import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditor; |
| 19 | import org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocument; |
| 20 | import org.eclipse.gmf.runtime.notation.Diagram; |
| 21 | import org.eclipse.gmf.runtime.notation.View; |
| 22 | import org.eclipse.jface.viewers.IStructuredSelection; |
| 23 | import org.eclipse.jface.viewers.StructuredSelection; |
| 24 | import org.eclipse.ui.IEditorInput; |
| 25 | import org.eclipse.ui.IEditorPart; |
| 26 | import org.eclipse.ui.IWorkbenchPage; |
| 27 | import org.eclipse.ui.navigator.ILinkHelper; |
| 28 | import org.eclipse.ui.part.FileEditorInput; |
| 29 | |
| 30 | import de.uka.ipd.sdq.pcm.gmf.usage.part.PalladioComponentModelUsageDiagramEditorPlugin; |
| 31 | |
| 32 | /** |
| 33 | * @generated |
| 34 | */ |
| 35 | public class PalladioComponentModelNavigatorLinkHelper implements ILinkHelper { |
| 36 | |
| 37 | /** |
| 38 | * @generated |
| 39 | */ |
| 40 | private static IEditorInput getEditorInput(Diagram diagram) { |
| 41 | Resource diagramResource = diagram.eResource(); |
| 42 | for (Iterator it = diagramResource.getContents().iterator(); it |
| 43 | .hasNext();) { |
| 44 | EObject nextEObject = (EObject) it.next(); |
| 45 | if (nextEObject == diagram) { |
| 46 | return new FileEditorInput(WorkspaceSynchronizer |
| 47 | .getFile(diagramResource)); |
| 48 | } |
| 49 | if (nextEObject instanceof Diagram) { |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | URI uri = EcoreUtil.getURI(diagram); |
| 54 | String editorName = uri.lastSegment() |
| 55 | + "#" + diagram.eResource().getContents().indexOf(diagram); //$NON-NLS-1$ |
| 56 | IEditorInput editorInput = new URIEditorInput(uri, editorName); |
| 57 | return editorInput; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @generated |
| 62 | */ |
| 63 | public IStructuredSelection findSelection(IEditorInput anInput) { |
| 64 | IDiagramDocument document = PalladioComponentModelUsageDiagramEditorPlugin |
| 65 | .getInstance().getDocumentProvider() |
| 66 | .getDiagramDocument(anInput); |
| 67 | if (document == null) { |
| 68 | return StructuredSelection.EMPTY; |
| 69 | } |
| 70 | Diagram diagram = document.getDiagram(); |
| 71 | IFile file = WorkspaceSynchronizer.getFile(diagram.eResource()); |
| 72 | if (file != null) { |
| 73 | PalladioComponentModelNavigatorItem item = new PalladioComponentModelNavigatorItem( |
| 74 | diagram, file, false); |
| 75 | return new StructuredSelection(item); |
| 76 | } |
| 77 | return StructuredSelection.EMPTY; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @generated |
| 82 | */ |
| 83 | public void activateEditor(IWorkbenchPage aPage, |
| 84 | IStructuredSelection aSelection) { |
| 85 | if (aSelection == null || aSelection.isEmpty()) { |
| 86 | return; |
| 87 | } |
| 88 | if (false == aSelection.getFirstElement() instanceof PalladioComponentModelAbstractNavigatorItem) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | PalladioComponentModelAbstractNavigatorItem abstractNavigatorItem = (PalladioComponentModelAbstractNavigatorItem) aSelection |
| 93 | .getFirstElement(); |
| 94 | View navigatorView = null; |
| 95 | if (abstractNavigatorItem instanceof PalladioComponentModelNavigatorItem) { |
| 96 | navigatorView = ((PalladioComponentModelNavigatorItem) abstractNavigatorItem) |
| 97 | .getView(); |
| 98 | } else if (abstractNavigatorItem instanceof PalladioComponentModelNavigatorGroup) { |
| 99 | PalladioComponentModelNavigatorGroup navigatorGroup = (PalladioComponentModelNavigatorGroup) abstractNavigatorItem; |
| 100 | if (navigatorGroup.getParent() instanceof PalladioComponentModelNavigatorItem) { |
| 101 | navigatorView = ((PalladioComponentModelNavigatorItem) navigatorGroup |
| 102 | .getParent()).getView(); |
| 103 | } |
| 104 | } |
| 105 | if (navigatorView == null) { |
| 106 | return; |
| 107 | } |
| 108 | IEditorInput editorInput = getEditorInput(navigatorView.getDiagram()); |
| 109 | IEditorPart editor = aPage.findEditor(editorInput); |
| 110 | if (editor == null) { |
| 111 | return; |
| 112 | } |
| 113 | aPage.bringToTop(editor); |
| 114 | if (editor instanceof DiagramEditor) { |
| 115 | DiagramEditor diagramEditor = (DiagramEditor) editor; |
| 116 | ResourceSet diagramEditorResourceSet = diagramEditor |
| 117 | .getEditingDomain().getResourceSet(); |
| 118 | EObject selectedView = diagramEditorResourceSet.getEObject( |
| 119 | EcoreUtil.getURI(navigatorView), true); |
| 120 | if (selectedView == null) { |
| 121 | return; |
| 122 | } |
| 123 | GraphicalViewer graphicalViewer = (GraphicalViewer) diagramEditor |
| 124 | .getAdapter(GraphicalViewer.class); |
| 125 | EditPart selectedEditPart = (EditPart) graphicalViewer |
| 126 | .getEditPartRegistry().get(selectedView); |
| 127 | if (selectedEditPart != null) { |
| 128 | graphicalViewer.select(selectedEditPart); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | } |