/*
* License (BSD Style License):
* Copyright (c) 2011
* Software Engineering
* Department of Computer Science
* Technische Universität Darmstadt
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the Software Engineering Group or Technische
* Universität Darmstadt nor the names of its contributors may be used to
* endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package de.tud.cs.st.vespucci.vespucci_model.diagram.navigator;
import java.util.Iterator;
import org.eclipse.core.resources.IFile;
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;
/**
* @generated
*/
public class VespucciNavigatorLinkHelper 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 = de.tud.cs.st.vespucci.vespucci_model.diagram.part.VespucciDiagramEditorPlugin.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) {
de.tud.cs.st.vespucci.vespucci_model.diagram.navigator.VespucciNavigatorItem item = new de.tud.cs.st.vespucci.vespucci_model.diagram.navigator.VespucciNavigatorItem(
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 de.tud.cs.st.vespucci.vespucci_model.diagram.navigator.VespucciAbstractNavigatorItem) {
return;
}
de.tud.cs.st.vespucci.vespucci_model.diagram.navigator.VespucciAbstractNavigatorItem abstractNavigatorItem = (de.tud.cs.st.vespucci.vespucci_model.diagram.navigator.VespucciAbstractNavigatorItem) aSelection
.getFirstElement();
View navigatorView = null;
if (abstractNavigatorItem instanceof de.tud.cs.st.vespucci.vespucci_model.diagram.navigator.VespucciNavigatorItem) {
navigatorView = ((de.tud.cs.st.vespucci.vespucci_model.diagram.navigator.VespucciNavigatorItem) abstractNavigatorItem)
.getView();
} else if (abstractNavigatorItem instanceof de.tud.cs.st.vespucci.vespucci_model.diagram.navigator.VespucciNavigatorGroup) {
de.tud.cs.st.vespucci.vespucci_model.diagram.navigator.VespucciNavigatorGroup navigatorGroup = (de.tud.cs.st.vespucci.vespucci_model.diagram.navigator.VespucciNavigatorGroup) abstractNavigatorItem;
if (navigatorGroup.getParent() instanceof de.tud.cs.st.vespucci.vespucci_model.diagram.navigator.VespucciNavigatorItem) {
navigatorView = ((de.tud.cs.st.vespucci.vespucci_model.diagram.navigator.VespucciNavigatorItem) navigatorGroup
.getParent()).getView();
}
}
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);
}
}
}
}