add utility to open an editor
authordadubrow
Wed, 14 Jul 2010 07:31:25 -0500
changeset 1612 ead82fb31f27
parent 1611 6bd46a890989
child 1614 b1456c010578
add utility to open an editor
core/com.nokia.cpp.utils.ui/src/com/nokia/cpp/internal/api/utils/ui/WorkbenchUtils.java
--- a/core/com.nokia.cpp.utils.ui/src/com/nokia/cpp/internal/api/utils/ui/WorkbenchUtils.java	Tue Jul 13 15:35:51 2010 -0500
+++ b/core/com.nokia.cpp.utils.ui/src/com/nokia/cpp/internal/api/utils/ui/WorkbenchUtils.java	Wed Jul 14 07:31:25 2010 -0500
@@ -195,6 +195,48 @@
 	}
 
 	/**
+	 * Open and provide an editor with the given input and id.
+	 * @param input
+	 * @param editorId
+	 * @return IEditorPart
+	 * @throws PartInitException
+	 */
+	public static IEditorPart openEditor(final IEditorInput input, final String editorId) throws PartInitException {		
+		final IEditorPart[] parts = { null } ;
+		final PartInitException ex[] = { null };
+		Display.getDefault().syncExec(new Runnable() {
+
+			public void run() {
+				IWorkbench workbench = PlatformUI.getWorkbench();
+				IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
+				if (workbenchWindow == null) {
+					if (workbench.getWorkbenchWindowCount() == 0)
+						return;
+					workbenchWindow = workbench.getWorkbenchWindows()[0];
+				}
+				IWorkbenchPage page = workbenchWindow.getActivePage();
+				if (page == null) {
+					if (workbenchWindow.getPages().length == 0)
+						return;
+					page = workbenchWindow.getPages()[0];
+				}
+				IEditorPart editor;
+				try {
+					editor = page.openEditor(input, editorId);
+				} catch (PartInitException e) {
+					ex[0] = e;
+					return;
+				}
+				parts[0] = editor;				
+			}
+			
+		});
+		if (ex[0] != null)
+			throw ex[0];
+		return parts[0];
+	}
+
+	/**
 	 * Open an external browser and go to the given URL.
 	 * Note this function must be called in UI thread.
 	 *