Bug 300899 - Endless OpenDoc events
diff --git a/bundles/org.eclipse.ui.ide.application/META-INF/MANIFEST.MF b/bundles/org.eclipse.ui.ide.application/META-INF/MANIFEST.MF
index f374c29..b5791d7 100644
--- a/bundles/org.eclipse.ui.ide.application/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.ui.ide.application/META-INF/MANIFEST.MF
@@ -7,7 +7,7 @@
 Bundle-Localization: plugin
 Bundle-ClassPath: e4-ide-application.jar,
  .
-Require-Bundle: org.eclipse.ui.ide;bundle-version="[3.5.0,4.0.0)",
+Require-Bundle: org.eclipse.ui.ide;bundle-version="[3.6.0,4.0.0)",
  org.eclipse.core.runtime;bundle-version="[3.2.0,4.0.0)",
  org.eclipse.core.resources;bundle-version="[3.2.0,4.0.0)",
  org.eclipse.ui;bundle-version="[3.5.0,4.0.0)",
diff --git a/bundles/org.eclipse.ui.ide.application/src/org/eclipse/ui/internal/ide/application/DelayedEventsProcessor.java b/bundles/org.eclipse.ui.ide.application/src/org/eclipse/ui/internal/ide/application/DelayedEventsProcessor.java
index 4680c0b..315c9ea 100644
--- a/bundles/org.eclipse.ui.ide.application/src/org/eclipse/ui/internal/ide/application/DelayedEventsProcessor.java
+++ b/bundles/org.eclipse.ui.ide.application/src/org/eclipse/ui/internal/ide/application/DelayedEventsProcessor.java
@@ -16,7 +16,10 @@
 import org.eclipse.core.filesystem.EFS;
 import org.eclipse.core.filesystem.IFileInfo;
 import org.eclipse.core.filesystem.IFileStore;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.Status;
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.osgi.util.NLS;
 import org.eclipse.swt.SWT;
@@ -92,12 +95,19 @@
 				IFileInfo fetchInfo = fileStore.fetchInfo();
 				if (!fetchInfo.isDirectory() && fetchInfo.exists()) {
 					IWorkbenchPage page = window.getActivePage();
+					if (page == null) {
+						String msg = NLS.bind(IDEWorkbenchMessages.OpenDelayedFileAction_message_noWindow, path);
+						MessageDialog.open(MessageDialog.ERROR, window.getShell(),
+								IDEWorkbenchMessages.OpenDelayedFileAction_title,
+								msg, SWT.SHEET);
+					}
 					try {
-						IDE.openEditorOnFileStore(page, fileStore);
+						IDE.openInternalEditorOnFileStore(page, fileStore);
 					} catch (PartInitException e) {
 						String msg = NLS.bind(IDEWorkbenchMessages.OpenDelayedFileAction_message_errorOnOpen,
 										fileStore.getName());
-						IDEWorkbenchPlugin.log(msg, e.getStatus());
+						CoreException eLog = new PartInitException(e.getMessage());
+						IDEWorkbenchPlugin.log(msg, new Status(IStatus.ERROR, IDEApplication.PLUGIN_ID, msg, eLog));
 						MessageDialog.open(MessageDialog.ERROR, window.getShell(),
 								IDEWorkbenchMessages.OpenDelayedFileAction_title,
 								msg, SWT.SHEET);
diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/IDE.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/IDE.java
index 7b8fff6..b3500c4 100644
--- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/IDE.java
+++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/IDE.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2003, 2009 IBM Corporation and others.
+ * Copyright (c) 2003, 2010 IBM Corporation and others.
  * 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
@@ -1152,6 +1152,84 @@
     }
 
 	/**
+	 * Opens an internal editor on the given IFileStore object.
+	 * <p>
+	 * Unlike the other <code>openEditor</code> methods, this one can be used to
+	 * open files that reside outside the workspace resource set.
+	 * </p>
+	 * <p>
+	 * If the page already has an editor open on the target object then that
+	 * editor is brought to front; otherwise, a new editor is opened.
+	 * </p>
+	 * 
+	 * @param page
+	 *            the page in which the editor will be opened
+	 * @param fileStore
+	 *            the IFileStore representing the file to open
+	 * @return an open editor or <code>null</code> if an external editor was
+	 *         opened
+	 * @exception PartInitException
+	 *                if no internal editor can be found or if the editor could
+	 *                not be initialized
+	 * @see org.eclipse.ui.IWorkbenchPage#openEditor(IEditorInput, String)
+	 * @since 3.6
+	 */
+	public static IEditorPart openInternalEditorOnFileStore(IWorkbenchPage page, IFileStore fileStore) throws PartInitException {
+		if (page == null)
+			throw new IllegalArgumentException();
+		if (fileStore == null)
+			throw new IllegalArgumentException();
+
+		IEditorInput input = getEditorInput(fileStore);
+		String name = fileStore.fetchInfo().getName();
+		if (name == null)
+			throw new IllegalArgumentException();
+
+		IContentType[] contentTypes = null;
+		InputStream is = null;
+		try {
+			is = fileStore.openInputStream(EFS.NONE, null);
+			contentTypes = Platform.getContentTypeManager().findContentTypesFor(is, name);
+		} catch (CoreException ex) {
+			// it's OK, ignore
+		} catch (IOException ex) {
+			// it's OK, ignore
+		} finally {
+			if (is != null) {
+				try {
+					is.close();
+				} catch (IOException e) {
+					// nothing good can be done here, ignore
+				}
+			}
+		}
+
+		IEditorRegistry editorReg = PlatformUI.getWorkbench().getEditorRegistry();
+		if (contentTypes != null) {
+			for(int i = 0 ; i < contentTypes.length; i++) {
+				IEditorDescriptor editorDesc = editorReg.getDefaultEditor(name, contentTypes[i]);
+				if ((editorDesc != null) && (editorDesc.isInternal()))
+					return page.openEditor(input, editorDesc.getId());
+			}
+		}
+
+		// no content types are available, use file name associations
+		IEditorDescriptor[] editors = editorReg.getEditors(name);
+		if (editors != null) {
+			for(int i = 0 ; i < editors.length; i++) {
+				if ((editors[i] != null) && (editors[i].isInternal()))
+					return page.openEditor(input, editors[i].getId());
+			}
+		}
+
+		// fallback to the default text editor
+		IEditorDescriptor textEditor = editorReg.findEditor(IDEWorkbenchPlugin.DEFAULT_TEXT_EDITOR_ID);
+		if (textEditor == null)
+			throw new PartInitException(IDEWorkbenchMessages.IDE_noFileEditorFound);
+		return page.openEditor(input, textEditor.getId());
+	}
+
+	/**
 	 * Save all dirty editors in the workbench whose editor input is a child
 	 * resource of one of the <code>IResource</code>'s provided. Opens a
 	 * dialog to prompt the user if <code>confirm</code> is true. Return true
diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/IDEWorkbenchMessages.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/IDEWorkbenchMessages.java
index 2723dd9..35667ab 100644
--- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/IDEWorkbenchMessages.java
+++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/IDEWorkbenchMessages.java
@@ -907,6 +907,7 @@
 	public static String OpenDelayedFileAction_title;
 	public static String OpenDelayedFileAction_message_errorOnOpen;
 	public static String OpenDelayedFileAction_message_fileNotFound;
+	public static String OpenDelayedFileAction_message_noWindow;
 
 	static {
 		// load message values from bundle file
diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/messages.properties b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/messages.properties
index 85b40ad..c11feff 100644
--- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/messages.properties
+++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/messages.properties
@@ -921,4 +921,5 @@
 OpenDelayedFileAction_title = Open File
 OpenDelayedFileAction_message_errorOnOpen = The file ''{0}'' could not be opened.\nSee log for details.
 OpenDelayedFileAction_message_fileNotFound= The file ''{0}'' could not be found.
+OpenDelayedFileAction_message_noWindow= The file ''{0}'' could not be opened.\nPlease make sure there is at least one open perspective.