RESOLVED - bug 349439: Unconvenient usage of Graphiti in RCP development
https://bugs.eclipse.org/bugs/show_bug.cgi?id=349439
diff --git a/plugins/org.eclipse.graphiti.ui/META-INF/MANIFEST.MF b/plugins/org.eclipse.graphiti.ui/META-INF/MANIFEST.MF
index 71b2b10..a824349 100644
--- a/plugins/org.eclipse.graphiti.ui/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.graphiti.ui/META-INF/MANIFEST.MF
@@ -11,7 +11,7 @@
  org.eclipse.emf.workspace;bundle-version="[1.3.0,2.0.0)",
  org.eclipse.gef;bundle-version="[3.6.0,4.0.0)",
  org.eclipse.graphiti;bundle-version="0.9.0",
- org.eclipse.ui.ide;bundle-version="[3.5.2,4.0.0)",
+ org.eclipse.ui.ide;bundle-version="[3.5.2,4.0.0)";resolution:=optional,
  org.eclipse.ui.navigator;bundle-version="[3.4.2,4.0.0)",
  org.eclipse.ui.views.properties.tabbed;bundle-version="[3.5.0,4.0.0)",
  org.eclipse.core.expressions;bundle-version="[3.4.101,4.0.0)"
diff --git a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/editor/DiagramEditorFactory.java b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/editor/DiagramEditorFactory.java
index 88b2cd2..9f4858f 100644
--- a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/editor/DiagramEditorFactory.java
+++ b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/editor/DiagramEditorFactory.java
@@ -34,11 +34,11 @@
 import org.eclipse.graphiti.mm.pictograms.Diagram;
 import org.eclipse.graphiti.ui.internal.editor.GFWorkspaceCommandStackImpl;
 import org.eclipse.graphiti.ui.internal.services.GraphitiUiInternal;
+import org.eclipse.graphiti.ui.internal.util.ReflectionUtil;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.IEditorMatchingStrategy;
 import org.eclipse.ui.IEditorReference;
 import org.eclipse.ui.IElementFactory;
-import org.eclipse.ui.IFileEditorInput;
 import org.eclipse.ui.IMemento;
 import org.eclipse.ui.PartInitException;
 
@@ -52,6 +52,7 @@
  */
 public class DiagramEditorFactory implements IElementFactory {
 
+
 	/**
 	 * Creates a new {@link DiagramEditorInput} with a self created
 	 * {@link TransactionalEditingDomain} in case the passed
@@ -69,8 +70,10 @@
 		if (otherInput instanceof DiagramEditorInput) {
 			DiagramEditorInput input = (DiagramEditorInput) otherInput;
 			if (input.getAdapter(TransactionalEditingDomain.class) == null) {
-				// This might happen in case the editor input comes from the navigation history:
-				// there the editing domain is disposed and the diagram can no longer be resolved
+				// This might happen in case the editor input comes from the
+				// navigation history:
+				// there the editing domain is disposed and the diagram can no
+				// longer be resolved
 				// Simply create a new editor input
 				// See Bug 346932
 				TransactionalEditingDomain ed = DiagramEditorFactory.createResourceSetAndEditingDomain();
@@ -78,9 +81,8 @@
 			}
 			return input;
 		}
-		if (otherInput instanceof IFileEditorInput) {
-			final IFileEditorInput fileInput = (IFileEditorInput) otherInput;
-			final IFile file = fileInput.getFile();
+		IFile file = ReflectionUtil.getFileFromEditorInput(otherInput);
+		if (file != null) {
 			final TransactionalEditingDomain domain = createResourceSetAndEditingDomain();
 			URI diagramFileUri = GraphitiUiInternal.getEmfService().getFileURI(file, domain.getResourceSet());
 			if (diagramFileUri != null) {
@@ -113,9 +115,8 @@
 		@Override
 		public boolean matches(IEditorReference editorRef, IEditorInput input) {
 			try {
-				if (input instanceof IFileEditorInput) {
-					final IFileEditorInput fileInput = (IFileEditorInput) input;
-					final IFile file = fileInput.getFile();
+				IFile file = ReflectionUtil.getFileFromEditorInput(input);
+				if (file != null) {
 
 					// Check whether the given file contains a diagram as its
 					// root element. If yes, compare it with the given editor's
diff --git a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/editor/DiagramEditorBehavior.java b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/editor/DiagramEditorBehavior.java
index 06acce7..e2ccef5 100644
--- a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/editor/DiagramEditorBehavior.java
+++ b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/editor/DiagramEditorBehavior.java
@@ -61,6 +61,7 @@
 import org.eclipse.graphiti.ui.internal.services.GraphitiUiInternal;
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
+import org.eclipse.jface.operation.IRunnableWithProgress;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.ui.IEditorInput;
@@ -70,7 +71,6 @@
 import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.IWorkbenchPartSite;
 import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.actions.WorkspaceModifyOperation;
 
 /**
  * @noinstantiate This class is not intended to be instantiated by clients.
@@ -491,10 +491,10 @@
 		final Set<Resource> savedResources = new HashSet<Resource>();
 
 		// Do the work within an operation because this is a long running activity that modifies the workbench.
-		final WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
+		final IRunnableWithProgress operation = new IRunnableWithProgress() {
 			// This is the method that gets invoked when the operation runs.
 			@Override
-			public void execute(IProgressMonitor monitor) {
+			public void run(IProgressMonitor monitor) {
 				// Save the resources to the file system.
 				try {
 					savedResources.addAll(GraphitiUiInternal.getEmfService().save(editingDomain));
diff --git a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/services/impl/UiService.java b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/services/impl/UiService.java
index 5f9a078..e0ff03a 100644
--- a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/services/impl/UiService.java
+++ b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/services/impl/UiService.java
@@ -21,15 +21,11 @@
 import java.util.Map;
 
 import org.eclipse.core.runtime.Assert;
-import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Path;
-import org.eclipse.core.runtime.Status;
 import org.eclipse.gef.GraphicalViewer;
 import org.eclipse.graphiti.internal.util.T;
-import org.eclipse.graphiti.ui.internal.GraphitiUIPlugin;
 import org.eclipse.graphiti.ui.internal.platform.ExtensionManager;
 import org.eclipse.graphiti.ui.internal.services.GraphitiUiInternal;
 import org.eclipse.graphiti.ui.internal.services.IUiService;
@@ -37,6 +33,7 @@
 import org.eclipse.graphiti.ui.internal.util.ui.print.IDiagramsExporter;
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
+import org.eclipse.jface.operation.IRunnableWithProgress;
 import org.eclipse.jface.window.Window;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.SWTException;
@@ -47,7 +44,6 @@
 import org.eclipse.swt.graphics.RGB;
 import org.eclipse.swt.widgets.FileDialog;
 import org.eclipse.swt.widgets.Shell;
-import org.eclipse.ui.actions.WorkspaceModifyOperation;
 
 /**
  * A collection of static helper methods regarding Draw2d.
@@ -168,7 +164,7 @@
 				final String file = filename;
 				final Image im = saveAsImageDialog.getScaledImage();
 				String imageExtension = saveAsImageDialog.getFileExtension();
-				WorkspaceModifyOperation operation;
+				IRunnableWithProgress operation;
 				// if the exporter is non-standard, i.e. registered via
 				// extension point, we need to call the registered exporter.
 				if (diagramExporterTypes.containsKey(imageExtension)) {
@@ -180,7 +176,7 @@
 				} else {
 					int imageFormat = saveAsImageDialog.getImageFormat();
 					byte image[] = createImage(im, imageFormat);
-					operation = getSaveToFileOp(file, image);
+					operation = getSaveToFileOp(shell, file, image);
 				}
 				new ProgressMonitorDialog(shell).run(false, false, operation);
 			} catch (Exception e) {
@@ -193,28 +189,36 @@
 	}
 
 
-	private WorkspaceModifyOperation getExportOp(final String METHOD, final Shell shell, final ExportDiagramDialog saveAsImageDialog,
+	private IRunnableWithProgress getExportOp(final String METHOD, final Shell shell, final ExportDiagramDialog saveAsImageDialog,
 			final String file, final Image im, final IDiagramsExporter exporter) {
-		WorkspaceModifyOperation operation;
-		operation = new WorkspaceModifyOperation() {
+		IRunnableWithProgress operation;
+		operation = new IRunnableWithProgress() {
 			@Override
-			protected void execute(IProgressMonitor monitor) throws CoreException {
+			public void run(IProgressMonitor monitor) {
 				try {
 					exporter.export(im, saveAsImageDialog.getFigure(), file, saveAsImageDialog.getImageScaleFactor());
 				} catch (Exception e) {
-					String message = "Can not export diagram: "; //$NON-NLS-1$
-					MessageDialog.openError(shell, "Can not export diagram", message + e.getMessage()); //$NON-NLS-1$
-					e.printStackTrace();
+					handleException(shell, e);
 				}
 			}
+
 		};
 		return operation;
 	}
 
+
+	private void handleException(final Shell shell, Exception e) {
+		String message = "Can not export diagram: "; //$NON-NLS-1$
+		MessageDialog.openError(shell, "Can not export diagram", message + e.getMessage()); //$NON-NLS-1$
+		e.printStackTrace();
+	}
+
 	/**
-	 * Returns a WorkspeceModifyOperation, which saves the given contents to a
+	 * Returns an IRunnableWithProgress, which saves the given contents to a
 	 * File with the given filename.
 	 * 
+	 * @param shell
+	 * 
 	 * @param filename
 	 *            The name of the file, where to save the contents.
 	 * @param contents
@@ -222,19 +226,16 @@
 	 * @throws Exception
 	 *             On any errors that occur.
 	 */
-	private WorkspaceModifyOperation getSaveToFileOp(final String filename, final byte contents[]) throws Exception {
-		WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
+	private IRunnableWithProgress getSaveToFileOp(final Shell shell, final String filename, final byte contents[]) throws Exception {
+		IRunnableWithProgress operation = new IRunnableWithProgress() {
 			@Override
-			protected void execute(IProgressMonitor monitor) throws CoreException {
+			public void run(IProgressMonitor monitor) {
 				FileOutputStream outputStream = null;
 				try {
 					outputStream = new FileOutputStream(filename);
 					outputStream.write(contents);
 				} catch (Exception e) {
-					// convert exceptions to CoreExceptions
-					Status status = new Status(IStatus.ERROR, GraphitiUIPlugin.PLUGIN_ID, IStatus.ERROR, "Can not save image as file: " //$NON-NLS-1$
-							+ e.getMessage(), e);
-					throw new CoreException(status);
+					handleException(shell, e);
 				} finally {
 					try {
 						outputStream.close();
diff --git a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/services/impl/WorkbenchService.java b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/services/impl/WorkbenchService.java
index bd05ddd..f4c992c 100644
--- a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/services/impl/WorkbenchService.java
+++ b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/services/impl/WorkbenchService.java
@@ -16,6 +16,7 @@
  *******************************************************************************/
 package org.eclipse.graphiti.ui.internal.services.impl;
 
+import org.eclipse.core.runtime.Assert;
 import org.eclipse.emf.transaction.TransactionalEditingDomain;
 import org.eclipse.graphiti.internal.util.T;
 import org.eclipse.graphiti.mm.pictograms.Diagram;
@@ -36,7 +37,6 @@
 import org.eclipse.ui.IWorkbenchWindow;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.ide.IDE;
 import org.eclipse.ui.part.EditorActionBarContributor;
 
 /**
@@ -133,7 +133,8 @@
 		DiagramEditorInput diagramEditorInput = DiagramEditorInput.createEditorInput(diagram, domain, providerId, disposeEditingDomain);
 		IWorkbenchPage workbenchPage = getActivePage();
 		try {
-			IEditorPart editorPart = IDE.openEditor(workbenchPage, diagramEditorInput, editorId);
+			Assert.isNotNull(workbenchPage);
+			IEditorPart editorPart = workbenchPage.openEditor(diagramEditorInput, editorId);
 			if (editorPart instanceof IDiagramEditor) {
 				ret = (IDiagramEditor) editorPart;
 			}
diff --git a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/util/ReflectionUtil.java b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/util/ReflectionUtil.java
new file mode 100644
index 0000000..476128f
--- /dev/null
+++ b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/util/ReflectionUtil.java
@@ -0,0 +1,80 @@
+/*******************************************************************************
+ * <copyright>
+ *
+ * Copyright (c) 2005, 2011 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.ui.internal.util;
+
+import java.lang.reflect.Method;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.graphiti.internal.util.T;
+import org.eclipse.swt.dnd.Transfer;
+
+/**
+ * In this class, we combine all reflection code introduced to make Graphiti run
+ * in a lean RCP setup, that is, for instance without org.eclipse.ui.ide
+ * installed.
+ */
+@SuppressWarnings({ "unchecked", "rawtypes" })
+public class ReflectionUtil {
+
+	private static final String CLASSNAME_RESOURCE_TRANSFER = "org.eclipse.ui.part.ResourceTransfer"; //$NON-NLS-1$
+	private static final String METHODNAME_RESOURCE_TRANSFER = "getInstance"; //$NON-NLS-1$
+
+	private static final String CLASSNAME_FILE_EDITOR_INPUT = "org.eclipse.ui.IFileEditorInput"; //$NON-NLS-1$
+	private static final String METHODNAME_FILE_EDITOR_INPUT = "getFile"; //$NON-NLS-1$
+
+	/**
+	 * IFileEditorInput resides in org.eclipse.ui.ide. We need to support an RCP
+	 * scenario without having this plug-in installed. Therefore ashamed we use
+	 * dynamic class-loading and reflection .
+	 */
+	public static IFile getFileFromEditorInput(Object input) {
+		Class[] genericInterfaces = input.getClass().getInterfaces();
+		for (Class interfaze : genericInterfaces) {
+			if (CLASSNAME_FILE_EDITOR_INPUT.equals(interfaze.getName())) {
+				try {
+					interfaze = Class.forName(CLASSNAME_FILE_EDITOR_INPUT);
+					Method method = interfaze.getMethod(METHODNAME_FILE_EDITOR_INPUT);
+					Object obj = method.invoke(input);
+					if (obj != null && obj instanceof IFile) {
+						return (IFile) obj;
+					}
+				} catch (Exception e) {
+					T.racer().debug(e.getMessage());
+				}
+			}
+		}
+		return null;
+	}
+
+	/**
+	 * ResourceTransfer resides in org.eclipse.ui.ide. We need to support an RCP
+	 * scenario without having this plug-in installed. Therefore ashamed we use
+	 * dynamic class-loading and reflection.
+	 */
+	public static Transfer getResourceTransfer() {
+		Transfer resourceTransfer = null;
+		try {
+			Class<?> forName = Class.forName(CLASSNAME_RESOURCE_TRANSFER);
+			Method method = forName.getMethod(METHODNAME_RESOURCE_TRANSFER);
+			Object obj = method.invoke(forName);
+			if (obj != null && obj instanceof Transfer)
+				resourceTransfer = (Transfer) obj;
+		} catch (Exception e) {
+			T.racer().debug(e.getMessage());
+		}
+		return resourceTransfer;
+	}
+}
diff --git a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/util/clipboard/ModelClipboard.java b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/util/clipboard/ModelClipboard.java
index cebf252..ed33498 100644
--- a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/util/clipboard/ModelClipboard.java
+++ b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/util/clipboard/ModelClipboard.java
@@ -46,6 +46,7 @@
 import org.eclipse.graphiti.ui.internal.Messages;
 import org.eclipse.graphiti.ui.internal.T;
 import org.eclipse.graphiti.ui.internal.services.GraphitiUiInternal;
+import org.eclipse.graphiti.ui.internal.util.ReflectionUtil;
 import org.eclipse.jface.util.LocalSelectionTransfer;
 import org.eclipse.jface.viewers.StructuredSelection;
 import org.eclipse.swt.custom.BusyIndicator;
@@ -54,7 +55,6 @@
 import org.eclipse.swt.dnd.TextTransfer;
 import org.eclipse.swt.dnd.Transfer;
 import org.eclipse.swt.widgets.Display;
-import org.eclipse.ui.part.ResourceTransfer;
 
 /**
  * Provides a clipboard-like storage of EMF-related data based on SWT
@@ -521,7 +521,15 @@
 		result.put(LocalSelectionTransfer.getTransfer(), new StructuredSelection(objects));
 		result.put(UriTransfer.getInstance(), data);
 		result.put(FileTransfer.getInstance(), filePaths.toArray(new String[filePaths.size()]));
-		result.put(ResourceTransfer.getInstance(), files.toArray(new IResource[files.size()]));
+		// Resource Transfer resides in org.eclipse.ui.ide. We need to support
+		// an RCP scenario without having this plug-in installed.
+		try {
+			Transfer resourceTransfer = ReflectionUtil.getResourceTransfer();
+			if (resourceTransfer != null)
+				result.put(resourceTransfer, files.toArray(new IResource[files.size()]));
+		} catch (Exception e) {
+			T.racer().debug(e.getMessage());
+		}
 		result.put(TextTransfer.getInstance(), toExtendedString(objects));
 		return result;
 	}