/** | |
* <copyright> | |
* | |
* Copyright (c) 2013 itemis and others. | |
* All rights reserved. This program and the accompanying materials | |
* are made available under the terms of the Eclipse Public License v2.0 | |
* which accompanies this distribution, and is available at | |
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html | |
* | |
* Contributors: | |
* itemis - Initial API and implementation | |
* | |
* </copyright> | |
*/ | |
package org.eclipse.sphinx.examples.hummingbird20.diagram.gmf.navigator; | |
import org.eclipse.core.runtime.IAdaptable; | |
import org.eclipse.emf.common.ui.URIEditorInput; | |
import org.eclipse.emf.common.util.URI; | |
import org.eclipse.emf.ecore.EObject; | |
import org.eclipse.emf.ecore.resource.Resource; | |
import org.eclipse.emf.ecore.util.EcoreUtil; | |
import org.eclipse.emf.workspace.util.WorkspaceSynchronizer; | |
import org.eclipse.gmf.runtime.notation.Diagram; | |
import org.eclipse.gmf.runtime.notation.View; | |
import org.eclipse.jface.action.Action; | |
import org.eclipse.jface.action.IMenuManager; | |
import org.eclipse.jface.viewers.IStructuredSelection; | |
import org.eclipse.sphinx.examples.hummingbird20.diagram.gmf.edit.parts.ApplicationEditPart; | |
import org.eclipse.sphinx.examples.hummingbird20.diagram.gmf.part.Hummingbird20DiagramEditor; | |
import org.eclipse.sphinx.examples.hummingbird20.diagram.gmf.part.Hummingbird20DiagramEditorPlugin; | |
import org.eclipse.sphinx.examples.hummingbird20.diagram.gmf.part.Hummingbird20VisualIDRegistry; | |
import org.eclipse.sphinx.examples.hummingbird20.diagram.gmf.part.Messages; | |
import org.eclipse.ui.IActionBars; | |
import org.eclipse.ui.IEditorInput; | |
import org.eclipse.ui.IWorkbenchPage; | |
import org.eclipse.ui.PartInitException; | |
import org.eclipse.ui.navigator.CommonActionProvider; | |
import org.eclipse.ui.navigator.ICommonActionConstants; | |
import org.eclipse.ui.navigator.ICommonActionExtensionSite; | |
import org.eclipse.ui.navigator.ICommonViewerWorkbenchSite; | |
import org.eclipse.ui.part.FileEditorInput; | |
/** | |
* @generated | |
*/ | |
public class Hummingbird20NavigatorActionProvider extends CommonActionProvider { | |
/** | |
* @generated | |
*/ | |
private boolean myContribute; | |
/** | |
* @generated | |
*/ | |
private OpenDiagramAction myOpenDiagramAction; | |
/** | |
* @generated | |
*/ | |
@Override | |
public void init(ICommonActionExtensionSite aSite) { | |
super.init(aSite); | |
if (aSite.getViewSite() instanceof ICommonViewerWorkbenchSite) { | |
myContribute = true; | |
makeActions((ICommonViewerWorkbenchSite) aSite.getViewSite()); | |
} else { | |
myContribute = false; | |
} | |
} | |
/** | |
* @generated | |
*/ | |
private void makeActions(ICommonViewerWorkbenchSite viewerSite) { | |
myOpenDiagramAction = new OpenDiagramAction(viewerSite); | |
} | |
/** | |
* @generated | |
*/ | |
@Override | |
public void fillActionBars(IActionBars actionBars) { | |
if (!myContribute) { | |
return; | |
} | |
IStructuredSelection selection = (IStructuredSelection) getContext().getSelection(); | |
myOpenDiagramAction.selectionChanged(selection); | |
if (myOpenDiagramAction.isEnabled()) { | |
actionBars.setGlobalActionHandler(ICommonActionConstants.OPEN, myOpenDiagramAction); | |
} | |
} | |
/** | |
* @generated | |
*/ | |
@Override | |
public void fillContextMenu(IMenuManager menu) { | |
} | |
/** | |
* @generated | |
*/ | |
private static class OpenDiagramAction extends Action { | |
/** | |
* @generated | |
*/ | |
private Diagram myDiagram; | |
/** | |
* @generated | |
*/ | |
private ICommonViewerWorkbenchSite myViewerSite; | |
/** | |
* @generated | |
*/ | |
public OpenDiagramAction(ICommonViewerWorkbenchSite viewerSite) { | |
super(Messages.NavigatorActionProvider_OpenDiagramActionName); | |
myViewerSite = viewerSite; | |
} | |
/** | |
* @generated | |
*/ | |
public final void selectionChanged(IStructuredSelection selection) { | |
myDiagram = null; | |
if (selection.size() == 1) { | |
Object selectedElement = selection.getFirstElement(); | |
if (selectedElement instanceof Hummingbird20NavigatorItem) { | |
selectedElement = ((Hummingbird20NavigatorItem) selectedElement).getView(); | |
} else if (selectedElement instanceof IAdaptable) { | |
selectedElement = ((IAdaptable) selectedElement).getAdapter(View.class); | |
} | |
if (selectedElement instanceof Diagram) { | |
Diagram diagram = (Diagram) selectedElement; | |
if (ApplicationEditPart.MODEL_ID.equals(Hummingbird20VisualIDRegistry.getModelID(diagram))) { | |
myDiagram = diagram; | |
} | |
} | |
} | |
setEnabled(myDiagram != null); | |
} | |
/** | |
* @generated | |
*/ | |
@Override | |
public void run() { | |
if (myDiagram == null || myDiagram.eResource() == null) { | |
return; | |
} | |
IEditorInput editorInput = getEditorInput(myDiagram); | |
IWorkbenchPage page = myViewerSite.getPage(); | |
try { | |
page.openEditor(editorInput, Hummingbird20DiagramEditor.ID); | |
} catch (PartInitException e) { | |
Hummingbird20DiagramEditorPlugin.getInstance().logError("Exception while openning diagram", e); //$NON-NLS-1$ | |
} | |
} | |
/** | |
* @generated | |
*/ | |
private static IEditorInput getEditorInput(Diagram diagram) { | |
Resource diagramResource = diagram.eResource(); | |
for (EObject nextEObject : diagramResource.getContents()) { | |
if (nextEObject == diagram) { | |
return new FileEditorInput(WorkspaceSynchronizer.getFile(diagramResource)); | |
} | |
if (nextEObject instanceof Diagram) { | |
break; | |
} | |
} | |
URI uri = EcoreUtil.getURI(diagram); | |
String editorName = uri.lastSegment() + '#' + diagram.eResource().getContents().indexOf(diagram); | |
IEditorInput editorInput = new URIEditorInput(uri, editorName); | |
return editorInput; | |
} | |
} | |
} |