| 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.runtime.IAdaptable; |
| 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.util.EcoreUtil; |
| 13 | import org.eclipse.emf.workspace.util.WorkspaceSynchronizer; |
| 14 | import org.eclipse.gmf.runtime.notation.Diagram; |
| 15 | import org.eclipse.gmf.runtime.notation.View; |
| 16 | import org.eclipse.jface.action.Action; |
| 17 | import org.eclipse.jface.action.IMenuManager; |
| 18 | import org.eclipse.jface.viewers.IStructuredSelection; |
| 19 | import org.eclipse.ui.IActionBars; |
| 20 | import org.eclipse.ui.IEditorInput; |
| 21 | import org.eclipse.ui.IWorkbenchPage; |
| 22 | import org.eclipse.ui.PartInitException; |
| 23 | import org.eclipse.ui.navigator.CommonActionProvider; |
| 24 | import org.eclipse.ui.navigator.ICommonActionConstants; |
| 25 | import org.eclipse.ui.navigator.ICommonActionExtensionSite; |
| 26 | import org.eclipse.ui.navigator.ICommonViewerWorkbenchSite; |
| 27 | import org.eclipse.ui.part.FileEditorInput; |
| 28 | |
| 29 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.UsageScenarioEditPart; |
| 30 | import de.uka.ipd.sdq.pcm.gmf.usage.part.Messages; |
| 31 | import de.uka.ipd.sdq.pcm.gmf.usage.part.PalladioComponentModelUsageDiagramEditor; |
| 32 | import de.uka.ipd.sdq.pcm.gmf.usage.part.PalladioComponentModelUsageDiagramEditorPlugin; |
| 33 | import de.uka.ipd.sdq.pcm.gmf.usage.part.PalladioComponentModelVisualIDRegistry; |
| 34 | |
| 35 | /** |
| 36 | * @generated |
| 37 | */ |
| 38 | public class PalladioComponentModelNavigatorActionProvider extends |
| 39 | CommonActionProvider { |
| 40 | |
| 41 | /** |
| 42 | * @generated |
| 43 | */ |
| 44 | private boolean myContribute; |
| 45 | |
| 46 | /** |
| 47 | * @generated |
| 48 | */ |
| 49 | private OpenDiagramAction myOpenDiagramAction; |
| 50 | |
| 51 | /** |
| 52 | * @generated |
| 53 | */ |
| 54 | public void init(ICommonActionExtensionSite aSite) { |
| 55 | super.init(aSite); |
| 56 | if (aSite.getViewSite() instanceof ICommonViewerWorkbenchSite) { |
| 57 | myContribute = true; |
| 58 | makeActions((ICommonViewerWorkbenchSite) aSite.getViewSite()); |
| 59 | } else { |
| 60 | myContribute = false; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @generated |
| 66 | */ |
| 67 | private void makeActions(ICommonViewerWorkbenchSite viewerSite) { |
| 68 | myOpenDiagramAction = new OpenDiagramAction(viewerSite); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @generated |
| 73 | */ |
| 74 | public void fillActionBars(IActionBars actionBars) { |
| 75 | if (!myContribute) { |
| 76 | return; |
| 77 | } |
| 78 | IStructuredSelection selection = (IStructuredSelection) getContext() |
| 79 | .getSelection(); |
| 80 | myOpenDiagramAction.selectionChanged(selection); |
| 81 | if (myOpenDiagramAction.isEnabled()) { |
| 82 | actionBars.setGlobalActionHandler(ICommonActionConstants.OPEN, |
| 83 | myOpenDiagramAction); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @generated |
| 89 | */ |
| 90 | public void fillContextMenu(IMenuManager menu) { |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @generated |
| 95 | */ |
| 96 | private class OpenDiagramAction extends Action { |
| 97 | |
| 98 | /** |
| 99 | * @generated |
| 100 | */ |
| 101 | private Diagram myDiagram; |
| 102 | |
| 103 | /** |
| 104 | * @generated |
| 105 | */ |
| 106 | private ICommonViewerWorkbenchSite myViewerSite; |
| 107 | |
| 108 | /** |
| 109 | * @generated |
| 110 | */ |
| 111 | public OpenDiagramAction(ICommonViewerWorkbenchSite viewerSite) { |
| 112 | super(Messages.NavigatorActionProvider_OpenDiagramActionName); |
| 113 | myViewerSite = viewerSite; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @generated |
| 118 | */ |
| 119 | public final void selectionChanged(IStructuredSelection selection) { |
| 120 | myDiagram = null; |
| 121 | if (selection.size() == 1) { |
| 122 | Object selectedElement = selection.getFirstElement(); |
| 123 | if (selectedElement instanceof PalladioComponentModelNavigatorItem) { |
| 124 | selectedElement = ((PalladioComponentModelNavigatorItem) selectedElement) |
| 125 | .getView(); |
| 126 | } else if (selectedElement instanceof IAdaptable) { |
| 127 | selectedElement = ((IAdaptable) selectedElement) |
| 128 | .getAdapter(View.class); |
| 129 | } |
| 130 | if (selectedElement instanceof Diagram) { |
| 131 | Diagram diagram = (Diagram) selectedElement; |
| 132 | if (UsageScenarioEditPart.MODEL_ID |
| 133 | .equals(PalladioComponentModelVisualIDRegistry |
| 134 | .getModelID(diagram))) { |
| 135 | myDiagram = diagram; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | setEnabled(myDiagram != null); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @generated |
| 144 | */ |
| 145 | public void run() { |
| 146 | if (myDiagram == null || myDiagram.eResource() == null) { |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | IEditorInput editorInput = getEditorInput(); |
| 151 | IWorkbenchPage page = myViewerSite.getPage(); |
| 152 | try { |
| 153 | page.openEditor(editorInput, |
| 154 | PalladioComponentModelUsageDiagramEditor.ID); |
| 155 | } catch (PartInitException e) { |
| 156 | PalladioComponentModelUsageDiagramEditorPlugin.getInstance() |
| 157 | .logError("Exception while openning diagram", e); //$NON-NLS-1$ |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * @generated |
| 163 | */ |
| 164 | private IEditorInput getEditorInput() { |
| 165 | for (Iterator it = myDiagram.eResource().getContents().iterator(); it |
| 166 | .hasNext();) { |
| 167 | EObject nextEObject = (EObject) it.next(); |
| 168 | if (nextEObject == myDiagram) { |
| 169 | return new FileEditorInput(WorkspaceSynchronizer |
| 170 | .getFile(myDiagram.eResource())); |
| 171 | } |
| 172 | if (nextEObject instanceof Diagram) { |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | URI uri = EcoreUtil.getURI(myDiagram); |
| 177 | String editorName = uri.lastSegment() |
| 178 | + "#" + myDiagram.eResource().getContents().indexOf(myDiagram); //$NON-NLS-1$ |
| 179 | IEditorInput editorInput = new URIEditorInput(uri, editorName); |
| 180 | return editorInput; |
| 181 | } |
| 182 | |
| 183 | } |
| 184 | |
| 185 | } |