Bug: 359227: provide link helper for editor and corresponding digram
node in Project Explorer
diff --git a/examples/org.eclipse.graphiti.examples.common/plugin.xml b/examples/org.eclipse.graphiti.examples.common/plugin.xml
index 5d2d8d9..3ffd05b 100644
--- a/examples/org.eclipse.graphiti.examples.common/plugin.xml
+++ b/examples/org.eclipse.graphiti.examples.common/plugin.xml
Binary files differ
diff --git a/examples/org.eclipse.graphiti.examples.common/src/org/eclipse/graphiti/examples/common/navigator/EditorLinkHelper.java b/examples/org.eclipse.graphiti.examples.common/src/org/eclipse/graphiti/examples/common/navigator/EditorLinkHelper.java
new file mode 100644
index 0000000..4d5ca20
--- /dev/null
+++ b/examples/org.eclipse.graphiti.examples.common/src/org/eclipse/graphiti/examples/common/navigator/EditorLinkHelper.java
@@ -0,0 +1,81 @@
+package org.eclipse.graphiti.examples.common.navigator;

+

+import org.eclipse.core.resources.IFile;

+import org.eclipse.core.resources.IResource;

+import org.eclipse.core.resources.IWorkspaceRoot;

+import org.eclipse.core.resources.ResourcesPlugin;

+import org.eclipse.core.runtime.IPath;

+import org.eclipse.core.runtime.Path;

+import org.eclipse.emf.common.util.URI;

+import org.eclipse.graphiti.ui.editor.DiagramEditorInput;

+import org.eclipse.jface.viewers.IStructuredSelection;

+import org.eclipse.jface.viewers.StructuredSelection;

+import org.eclipse.ui.IEditorInput;

+import org.eclipse.ui.IWorkbenchPage;

+import org.eclipse.ui.navigator.ILinkHelper;

+

+/**

+ * Provides information to the Common Navigator (Project Explorer) on how to

+ * link active editors with selections of diagram nodes.

+ * 

+ * @since 0.9

+ */

+public class EditorLinkHelper implements ILinkHelper {

+

+	/**

+	 * Return a selection that contains the file that the given editor input

+	 * represent. The {@link StructuredSelection} will be compared with nodes in

+	 * the tree. See also extension point

+	 * {@code "org.eclipse.ui.navigator.linkHelper"}.

+	 */

+	public IStructuredSelection findSelection(IEditorInput editorInput) {

+		if (editorInput instanceof DiagramEditorInput) {

+			if (editorInput.exists()) {

+				DiagramEditorInput diagramEditorInput = (DiagramEditorInput) editorInput;

+				final IFile file = getFile(diagramEditorInput.getUri());

+				if (file != null) {

+					return new StructuredSelection(file);

+				}

+			}

+

+		}

+		return StructuredSelection.EMPTY;

+	}

+

+	public void activateEditor(IWorkbenchPage aPage, IStructuredSelection aSelection) {

+		// not needed

+	}

+

+	private IFile getFile(URI uri) {

+		if (uri == null) {

+			return null;

+		}

+

+		final IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();

+

+		// File URIs

+		final String filePath = getWorkspaceFilePath(uri.trimFragment());

+		if (filePath == null) {

+			final IPath location = Path.fromOSString(uri.toString());

+			final IFile file = workspaceRoot.getFileForLocation(location);

+			if (file != null) {

+				return file;

+			}

+			return null;

+		}

+

+		// Platform resource URIs

+		else {

+			final IResource workspaceResource = workspaceRoot.findMember(filePath);

+			return (IFile) workspaceResource;

+		}

+	}

+

+	private String getWorkspaceFilePath(URI uri) {

+		if (uri.isPlatform()) {

+			return uri.toPlatformString(true);

+		}

+		return null;

+	}

+

+}

diff --git a/examples/org.eclipse.graphiti.examples.common/src/org/eclipse/graphiti/examples/common/navigator/nodes/DiagramsNode.java b/examples/org.eclipse.graphiti.examples.common/src/org/eclipse/graphiti/examples/common/navigator/nodes/DiagramsNode.java
deleted file mode 100644
index ec75709..0000000
--- a/examples/org.eclipse.graphiti.examples.common/src/org/eclipse/graphiti/examples/common/navigator/nodes/DiagramsNode.java
+++ /dev/null
@@ -1,98 +0,0 @@
-/*******************************************************************************
- * <copyright>
- *
- * Copyright (c) 2005, 2010 SAP AG.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- *    SAP AG - initial API, implementation and documentation
- *
- * </copyright>
- *
- *******************************************************************************/
-package org.eclipse.graphiti.examples.common.navigator.nodes;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.eclipse.core.resources.IContainer;
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IProject;
-import org.eclipse.core.resources.IResource;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.emf.ecore.resource.Resource;
-import org.eclipse.emf.ecore.resource.ResourceSet;
-import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
-import org.eclipse.graphiti.examples.common.Messages;
-import org.eclipse.graphiti.examples.common.navigator.nodes.base.AbstractInstancesOfTypeContainerNode;
-import org.eclipse.graphiti.mm.pictograms.Diagram;
-
-
-public class DiagramsNode extends AbstractInstancesOfTypeContainerNode {
-
-	private static final String NAME = Messages.DiagramsNode_DiagramNodeTitle;
-
-	public DiagramsNode(Object parent, IProject project) {
-		super(parent, project);
-	}
-
-	@Override
-	protected String getContainerName() {
-		return NAME;
-	}
-
-	public Object[] getChildren() {
-		IProject project = getProject();
-		if (project != null) {
-			ResourceSet rSet = new ResourceSetImpl();
-			return getAllDiagramFiles(project, rSet).toArray();
-		}
-		return null;
-	}
-
-	private List<IFile> getFiles(IContainer folder) {
-		List<IFile> ret = new ArrayList<IFile>();
-		try {
-			IResource[] members = folder.members();
-			for (IResource resource : members) {
-				if (resource instanceof IContainer) {
-					ret.addAll(getFiles((IContainer) resource));
-				} else if (resource instanceof IFile) {
-					IFile file = (IFile) resource;
-					if (file.getName().endsWith(".diagram")) { //$NON-NLS-1$
-						ret.add(file);
-					}
-				}
-			}
-		} catch (CoreException e) {
-			e.printStackTrace();
-		}
-		return ret;
-	}
-
-	private List<IFile> getAllDiagramFiles(IProject project, ResourceSet rSet) {
-		List<IFile> files = getFiles(project);
-
-		List<IFile> ret = new ArrayList<IFile>();
-		for (IFile file : files) {
-			// The following call extracts the diagram from the
-			// given file. For the Tutorial, diagrams always reside
-			// in a file of their own and are the first root object.
-			// This may of course be different in a concrete tool
-			// implementation, so tool builders should use their own
-			// way of retrieval here
-			@SuppressWarnings("restriction")
-			Diagram diagram = org.eclipse.graphiti.ui.internal.services.GraphitiUiInternal.getEmfService().getDiagramFromFile(file, rSet);
-			if (diagram != null) {
-				Resource eResource = diagram.eResource();
-				eResource.unload();
-				rSet.getResources().remove(eResource);
-				ret.add(file);
-			}
-		}
-		return ret;
-	}
-}