/******************************************************************************* | |
* Copyright (c) 2016 Fundación Tecnalia Research & Innovation. | |
* | |
* 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 | |
* | |
* SPDX-License-Identifier: EPL-2.0 | |
* | |
* Contributors: | |
* Huascar Espinoza - initial API and implementation | |
* Alejandra Ruíz - initial API and implementation | |
* Idoya Del Río - initial API and implementation | |
* Mari Carmen Palacios - initial API and implementation | |
* Angel López - initial API and implementation | |
*******************************************************************************/ | |
/* | |
* | |
*/ | |
package org.eclipse.opencert.apm.baseline.baseline.diagram.navigator; | |
import org.eclipse.core.resources.IFile; | |
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.resource.ResourceSet; | |
import org.eclipse.emf.ecore.util.EcoreUtil; | |
import org.eclipse.emf.workspace.util.WorkspaceSynchronizer; | |
import org.eclipse.gef.EditPart; | |
import org.eclipse.gef.GraphicalViewer; | |
import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditor; | |
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocument; | |
import org.eclipse.gmf.runtime.notation.Diagram; | |
import org.eclipse.gmf.runtime.notation.View; | |
import org.eclipse.jface.viewers.IStructuredSelection; | |
import org.eclipse.jface.viewers.StructuredSelection; | |
import org.eclipse.ui.IEditorInput; | |
import org.eclipse.ui.IEditorPart; | |
import org.eclipse.ui.IWorkbenchPage; | |
import org.eclipse.ui.navigator.ILinkHelper; | |
import org.eclipse.ui.part.FileEditorInput; | |
import org.eclipse.opencert.apm.baseline.baseline.diagram.part.BaselineDiagramEditorPlugin; | |
/** | |
* @generated | |
*/ | |
public class BaselineNavigatorLinkHelper implements ILinkHelper { | |
/** | |
* @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; | |
} | |
/** | |
* @generated | |
*/ | |
public IStructuredSelection findSelection(IEditorInput anInput) { | |
IDiagramDocument document = BaselineDiagramEditorPlugin.getInstance() | |
.getDocumentProvider().getDiagramDocument(anInput); | |
if (document == null) { | |
return StructuredSelection.EMPTY; | |
} | |
Diagram diagram = document.getDiagram(); | |
if (diagram == null || diagram.eResource() == null) { | |
return StructuredSelection.EMPTY; | |
} | |
IFile file = WorkspaceSynchronizer.getFile(diagram.eResource()); | |
if (file != null) { | |
BaselineNavigatorItem item = new BaselineNavigatorItem(diagram, | |
file, false); | |
return new StructuredSelection(item); | |
} | |
return StructuredSelection.EMPTY; | |
} | |
/** | |
* @generated | |
*/ | |
public void activateEditor(IWorkbenchPage aPage, | |
IStructuredSelection aSelection) { | |
if (aSelection == null || aSelection.isEmpty()) { | |
return; | |
} | |
if (false == aSelection.getFirstElement() instanceof BaselineAbstractNavigatorItem) { | |
return; | |
} | |
BaselineAbstractNavigatorItem abstractNavigatorItem = (BaselineAbstractNavigatorItem) aSelection | |
.getFirstElement(); | |
View navigatorView = null; | |
if (abstractNavigatorItem instanceof BaselineNavigatorItem) { | |
navigatorView = ((BaselineNavigatorItem) abstractNavigatorItem) | |
.getView(); | |
} else if (abstractNavigatorItem instanceof BaselineNavigatorGroup) { | |
BaselineNavigatorGroup navigatorGroup = (BaselineNavigatorGroup) abstractNavigatorItem; | |
if (navigatorGroup.getParent() instanceof BaselineNavigatorItem) { | |
navigatorView = ((BaselineNavigatorItem) navigatorGroup | |
.getParent()).getView(); | |
} else if (navigatorGroup.getParent() instanceof IAdaptable) { | |
navigatorView = (View) ((IAdaptable) navigatorGroup.getParent()) | |
.getAdapter(View.class); | |
} | |
} | |
if (navigatorView == null) { | |
return; | |
} | |
IEditorInput editorInput = getEditorInput(navigatorView.getDiagram()); | |
IEditorPart editor = aPage.findEditor(editorInput); | |
if (editor == null) { | |
return; | |
} | |
aPage.bringToTop(editor); | |
if (editor instanceof DiagramEditor) { | |
DiagramEditor diagramEditor = (DiagramEditor) editor; | |
ResourceSet diagramEditorResourceSet = diagramEditor | |
.getEditingDomain().getResourceSet(); | |
EObject selectedView = diagramEditorResourceSet.getEObject( | |
EcoreUtil.getURI(navigatorView), true); | |
if (selectedView == null) { | |
return; | |
} | |
GraphicalViewer graphicalViewer = (GraphicalViewer) diagramEditor | |
.getAdapter(GraphicalViewer.class); | |
EditPart selectedEditPart = (EditPart) graphicalViewer | |
.getEditPartRegistry().get(selectedView); | |
if (selectedEditPart != null) { | |
graphicalViewer.select(selectedEditPart); | |
} | |
} | |
} | |
} |