]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils.ui.workbench/src/org/simantics/utils/ui/workbench/WorkbenchUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.ui.workbench / src / org / simantics / utils / ui / workbench / WorkbenchUtils.java
diff --git a/bundles/org.simantics.utils.ui.workbench/src/org/simantics/utils/ui/workbench/WorkbenchUtils.java b/bundles/org.simantics.utils.ui.workbench/src/org/simantics/utils/ui/workbench/WorkbenchUtils.java
new file mode 100644 (file)
index 0000000..6c492dd
--- /dev/null
@@ -0,0 +1,760 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.utils.ui.workbench;\r
+\r
+import java.util.ArrayList;\r
+import java.util.HashSet;\r
+import java.util.List;\r
+import java.util.Set;\r
+\r
+import org.eclipse.core.runtime.IAdaptable;\r
+import org.eclipse.jface.action.IStatusLineManager;\r
+import org.eclipse.jface.viewers.IFilter;\r
+import org.eclipse.swt.widgets.Display;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.ui.IActionBars;\r
+import org.eclipse.ui.IEditorDescriptor;\r
+import org.eclipse.ui.IEditorInput;\r
+import org.eclipse.ui.IEditorPart;\r
+import org.eclipse.ui.IEditorSite;\r
+import org.eclipse.ui.IPerspectiveDescriptor;\r
+import org.eclipse.ui.IViewPart;\r
+import org.eclipse.ui.IViewReference;\r
+import org.eclipse.ui.IViewSite;\r
+import org.eclipse.ui.IWorkbench;\r
+import org.eclipse.ui.IWorkbenchPage;\r
+import org.eclipse.ui.IWorkbenchPart;\r
+import org.eclipse.ui.IWorkbenchPartSite;\r
+import org.eclipse.ui.IWorkbenchSite;\r
+import org.eclipse.ui.IWorkbenchWindow;\r
+import org.eclipse.ui.PartInitException;\r
+import org.eclipse.ui.PlatformUI;\r
+import org.eclipse.ui.WorkbenchException;\r
+import org.eclipse.ui.intro.IIntroSite;\r
+import org.eclipse.ui.part.IPageSite;\r
+import org.simantics.utils.ui.SWTUtils;\r
+\r
+/**\r
+ * Some eclipse workspace utils\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class WorkbenchUtils {\r
+\r
+    /**\r
+     * Find open viewpart\r
+     * \r
+     * @param primaryID primary view id\r
+     * @param secondaryID secondary view id\r
+     * @return view part if it exists\r
+     */\r
+    public static IViewPart findView(String primaryID, String secondaryID) {\r
+        if (secondaryID==null)\r
+            return _findView(primaryID, false);\r
+        else\r
+            return _findView(primaryID + ":" + secondaryID, false);\r
+    }\r
+\r
+    /**\r
+     * Find open viewpart\r
+     * \r
+     * @param id full id (example "primaryid:secondaryid")\r
+     * @return <code>IViewPart</code> or null if view wasn't found\r
+     */\r
+    public static IViewPart findView(String id) {\r
+        return _findView(id, false);\r
+    }\r
+\r
+    /**\r
+     * Finds (and restores) all views that have a specific primary Id\r
+     * \r
+     * @param primaryId primary Id\r
+     * @return all views\r
+     */\r
+    public static IViewPart[] findAllViews(String primaryId) {\r
+        List<IViewPart> result = new ArrayList<IViewPart>();\r
+        for (IViewReference ref : getViewReferences())\r
+            if (primaryId.equals(ref.getId()))\r
+                result.add(ref.getView(true));\r
+        return result.toArray(new IViewPart[0]);\r
+    }\r
+\r
+    /**\r
+     * Find viewpart, opens view if view is closed\r
+     * \r
+     * @param id full id\r
+     * @return <code>IViewPart</code> or null if view wasn't found\r
+     */\r
+    public static IViewPart getView(String id) {\r
+        return _findView(id, true);\r
+    }\r
+\r
+    public static IWorkbenchWindow getWorkbenchWindow(Shell shell)\r
+    {\r
+        for (IWorkbenchWindow w : PlatformUI.getWorkbench().getWorkbenchWindows())\r
+            if (w.getShell()==shell) return w;\r
+        return null;\r
+    }\r
+\r
+    /**\r
+     * Try to get the {@link Shell} of the currently active workbench window.\r
+     * Must be invoked from the SWT UI thread to receive results.\r
+     * \r
+     * @return Shell of the currently active workbench window or\r
+     *         <code>null</code> if there is no active window or if called from\r
+     *         a non-UI thread\r
+     */\r
+    public static Shell getActiveWorkbenchWindowShell() {\r
+        IWorkbench wb = PlatformUI.getWorkbench();\r
+        IWorkbenchWindow window = wb.getActiveWorkbenchWindow();\r
+        if (window == null)\r
+            return null;\r
+        return window.getShell();\r
+    }\r
+\r
+    /**\r
+     * Get the active workbench part in currently active workbench window /\r
+     * active page\r
+     * \r
+     * @return active workbench part of the active workbench window\r
+     */\r
+    public static IWorkbenchPart getActiveWorkbenchPart()\r
+    {\r
+        IWorkbench wb = PlatformUI.getWorkbench();\r
+        if (wb==null) return null;\r
+        IWorkbenchWindow wbw = wb.getActiveWorkbenchWindow();\r
+        if (wbw==null) return null;\r
+        IWorkbenchPage wbp = wbw.getActivePage();\r
+        if (wbp==null) return null;\r
+        return wbp.getActivePart();\r
+    }\r
+\r
+\r
+    /**\r
+     * Get the active workbench part in currently active workbench window /\r
+     * active page\r
+     * \r
+     * @return active workbench part of the active workbench window\r
+     */\r
+    public static IEditorPart getActiveEditor()\r
+    {\r
+        IWorkbench wb = PlatformUI.getWorkbench();\r
+        if (wb==null) return null;\r
+        IWorkbenchWindow wbw = wb.getActiveWorkbenchWindow();\r
+        if (wbw==null) return null;\r
+        IWorkbenchPage wbp = wbw.getActivePage();\r
+        if (wbp==null) return null;\r
+        return wbp.getActiveEditor();\r
+    }\r
+\r
+    /**\r
+     * Get the perspective in current active workbench window / active page\r
+     * @return Current perspective\r
+     */\r
+    public static IPerspectiveDescriptor getCurrentPerspective()\r
+    {\r
+        IWorkbench wb = PlatformUI.getWorkbench();\r
+        if (wb==null) return null;\r
+        IWorkbenchWindow wbw = wb.getActiveWorkbenchWindow();\r
+        if (wbw==null) return null;\r
+        IWorkbenchPage wbp = wbw.getActivePage();\r
+        if (wbp==null) return null;\r
+        return wbp.getPerspective();\r
+    }\r
+\r
+    /**\r
+     * Get the perspective in current active workbench window / active page\r
+     * @return Current perspective Id\r
+     */\r
+    public static String getCurrentPerspectiveId()\r
+    {\r
+        IPerspectiveDescriptor p = getCurrentPerspective();\r
+        if (p==null) return null;\r
+        return p.getId();\r
+    }\r
+\r
+    /**\r
+     * Returns all <code>IViewReference</code>s\r
+     * @return array of all <code>IViewReference</code>s\r
+     */\r
+    public static IViewReference[] getViewReferences() {\r
+        Set<IViewReference> result = new HashSet<IViewReference>();\r
+\r
+        IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();\r
+        if (windows == null)\r
+            return null;\r
+        for (IWorkbenchWindow wb : windows) {\r
+            if (wb == null)\r
+                return null;\r
+            IWorkbenchPage pages[] = wb.getPages();\r
+            if (pages == null)\r
+                return null;\r
+            for (IWorkbenchPage page : pages) {\r
+                if (page == null)\r
+                    continue;\r
+                IViewReference refs[] = page.getViewReferences();\r
+                for (IViewReference ref : refs)\r
+                    result.add(ref);\r
+            }\r
+        }\r
+        return result.toArray(new IViewReference[0]);\r
+    }\r
+\r
+    /**\r
+     * Returns all <code>IViewReference</code>s for the specified workbench\r
+     * window\r
+     * \r
+     * @return array of all <code>IViewReference</code>s for the specified\r
+     *         window\r
+     */\r
+    public static IViewReference[] getViewReferences(IWorkbenchWindow window) {\r
+        Set<IViewReference> result = new HashSet<IViewReference>();\r
+        for (IWorkbenchPage page : window.getPages()) {\r
+            IViewReference refs[] = page.getViewReferences();\r
+            for (IViewReference ref : refs)\r
+                result.add(ref);\r
+        }\r
+        return result.toArray(new IViewReference[0]);\r
+    }\r
+\r
+    /**\r
+     * Finds a <code>IViewPart</code>. Opens the view if it's closed\r
+     * @param primaryID primary ID of the view\r
+     * @param secondaryID secondary ID of the view or null if view has no secondary ID\r
+     * @return <code>IViewPart</code> if view was found or opened, else returns null\r
+     */\r
+    public static IViewPart getView(String primaryID, String secondaryID) {\r
+        if (secondaryID==null)\r
+            return _findView(primaryID, true);\r
+        return _findView(primaryID+":"+secondaryID, true);\r
+    }\r
+\r
+    /**\r
+     * Finds a <code>IViewPart</code>\r
+     * @param id id of the View\r
+     * @param restore set to true if you want to open closed view\r
+     * @return the <code>IViewPart</code> if view was found or restored, else returns null\r
+     */\r
+    private static IViewPart _findView(String id, boolean restore) {\r
+        String primaryId = getPrimaryID(id);\r
+        String secondaryId = getSecondaryID(id);\r
+        IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();\r
+        if (windows == null)\r
+            return null;\r
+        for (IWorkbenchWindow wb : windows) {\r
+            if (wb == null)\r
+                return null;\r
+            IWorkbenchPage pages[] = wb.getPages();\r
+            if (pages == null)\r
+                return null;\r
+            for (IWorkbenchPage page : pages) {\r
+                if (page == null)\r
+                    continue;\r
+\r
+                IViewReference vr = page.findViewReference(primaryId, secondaryId);\r
+                if (vr == null)\r
+                    continue;\r
+\r
+                IViewPart vp = vr.getView(restore);\r
+                if (vp != null)\r
+                    return vp;\r
+            }\r
+        }\r
+        return null;\r
+    }\r
+\r
+    /**\r
+     * Hides a view\r
+     * @param id the id of the view\r
+     * @return true if view was hidden, returns false if view with id wasn't found\r
+     */\r
+    public static boolean hideView(String id) {\r
+        IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();\r
+        if (windows == null)\r
+            return false;\r
+        for (IWorkbenchWindow wb : windows) {\r
+            if (wb == null)\r
+                return false;\r
+            IWorkbenchPage pages[] = wb.getPages();\r
+            if (pages == null)\r
+                return false;\r
+            for (IWorkbenchPage page : pages) {\r
+                if (page == null)\r
+                    continue;\r
+\r
+                IViewReference vr = page.findViewReference(getPrimaryID(id), getSecondaryID(id));\r
+                if (vr == null)\r
+                    continue;\r
+                page.hideView(vr);\r
+                return true;\r
+\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+\r
+    /**\r
+     * @param viewPart\r
+     * @return\r
+     */\r
+    public static boolean hideView(IViewPart viewPart) {\r
+        IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();\r
+        if (windows == null)\r
+            return false;\r
+        for (IWorkbenchWindow wb : windows) {\r
+            if (wb == null)\r
+                return false;\r
+            IWorkbenchPage pages[] = wb.getPages();\r
+            if (pages == null)\r
+                return false;\r
+            for (IWorkbenchPage page : pages) {\r
+                if (page == null)\r
+                    continue;\r
+                page.hideView(viewPart);\r
+                return true;\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+\r
+    /**\r
+     * @param view\r
+     * @return\r
+     */\r
+    public static boolean hideView(IWorkbenchWindow window, IViewPart view) {\r
+        for (IWorkbenchPage page : window.getPages()) {\r
+            if (page == null)\r
+                continue;\r
+            page.hideView(view);\r
+            return true;\r
+        }\r
+        return false;\r
+    }\r
+\r
+    /**\r
+     * @param view\r
+     * @return\r
+     */\r
+    public static boolean hideView(IWorkbenchWindow window, IViewReference view) {\r
+        for (IWorkbenchPage page : window.getPages()) {\r
+            if (page == null)\r
+                continue;\r
+            page.hideView(view);\r
+            return true;\r
+        }\r
+        return false;\r
+    }\r
+\r
+    /**\r
+     * Gets the primary id from view id "prim:secon"\r
+     * \r
+     * @param id full id\r
+     * @return primary id\r
+     */\r
+    public static String getPrimaryID(String id) {\r
+        String splitted[] = id.split(":");\r
+        if (splitted == null || splitted.length < 1)\r
+            return null;\r
+        return splitted[0];\r
+    }\r
+\r
+    /**\r
+     * Gets the secondary id from view id "prim:secon"\r
+     * \r
+     * @param id full id\r
+     * @return secondary id\r
+     */\r
+    public static String getSecondaryID(String id) {\r
+        String splitted[] = id.split(":");\r
+        if (splitted == null || splitted.length < 2)\r
+            return null;\r
+        return splitted[1];\r
+    }\r
+\r
+    /**\r
+     * Creates the view\r
+     * @param id the id of the view\r
+     * @return <code>IViewPart</code> or null if view couldn't be created\r
+     * @throws PartInitException\r
+     */\r
+    public static IViewPart showView(String id) throws PartInitException {\r
+        return showView(id, IWorkbenchPage.VIEW_CREATE);\r
+    }\r
+\r
+    /**\r
+     * Activates the view\r
+     * @param id the id of the view\r
+     * @return <code>IViewPart</code> or null if view couldn't be activated\r
+     * @throws PartInitException\r
+     */\r
+    public static IViewPart activateView(String id) throws PartInitException {\r
+        IViewPart vp = showView(id, IWorkbenchPage.VIEW_ACTIVATE);\r
+        return vp;\r
+    }\r
+\r
+    /**\r
+     * Shows the view\r
+     * @param id the id of the view\r
+     * @param mode <code>IWorkbenchPage.VIEW_CREATE</code> or <code>IWorkbenchPage.VIEW_ACTIVATE</code>\r
+     * @return the <code>IViewPart</code> or null if showing the view failed\r
+     * @throws PartInitException\r
+     */\r
+    public static IViewPart showView(String id, final int mode) throws PartInitException {\r
+        // Is view already created\r
+        IViewPart vp = findView(id);\r
+        if (vp != null) {\r
+            if (mode == IWorkbenchPage.VIEW_CREATE)\r
+                return vp;\r
+\r
+            Display display = Display.getCurrent();\r
+            if (Thread.currentThread() == display.getThread()) {\r
+                // This is the UI-thread\r
+                //System.out.println("In UI thread");\r
+                IWorkbenchPage page = vp.getViewSite().getPage();\r
+\r
+                if (mode == IWorkbenchPage.VIEW_VISIBLE) {\r
+                    page.bringToTop(vp);\r
+                } else if (mode == IWorkbenchPage.VIEW_ACTIVATE) {\r
+                    page.activate(vp);\r
+                }\r
+            } else {\r
+                //System.out.println("NOT in UI thread!");\r
+                final IViewPart fvp = vp;\r
+                display.asyncExec(new Runnable() {\r
+                    @Override\r
+                    public void run() {\r
+                        final IWorkbenchWindow wb = PlatformUI.getWorkbench().getActiveWorkbenchWindow();\r
+                        IWorkbenchPage page = wb.getActivePage();\r
+                        if (mode == IWorkbenchPage.VIEW_VISIBLE) {\r
+                            page.bringToTop(fvp);\r
+                        } else {\r
+                            page.activate(fvp);\r
+                        }\r
+                    }\r
+                });\r
+            }\r
+            return vp;\r
+        }\r
+\r
+        final String primaryID = getPrimaryID(id);\r
+        final String secondaryID = getSecondaryID(id);\r
+        if (primaryID == null/* || secondaryID == null*/)\r
+            return null;\r
+\r
+        // Create the view on active page\r
+        final IWorkbenchWindow wb = PlatformUI.getWorkbench().getActiveWorkbenchWindow();\r
+        if (wb == null)\r
+            return null;\r
+        IWorkbenchPage page = wb.getActivePage();\r
+        if (page == null) {\r
+            IWorkbenchPage pages[] = wb.getPages();\r
+            if (pages==null || pages.length == 0) return null;\r
+            page = pages[0];\r
+        }\r
+\r
+        vp = page.showView(primaryID, secondaryID, mode);\r
+        return vp;\r
+    }\r
+\r
+\r
+    /**\r
+     * IMPORTANT: If you're using multiple workbench windows, use\r
+     * {@link #showPerspective(IWorkbenchWindow, String, IAdaptable)} instead.\r
+     * \r
+     * @param perspectiveId the ID of the perspective to activate in the\r
+     *        currently active workbench window\r
+     * @throws WorkbenchException if perspective activation fails\r
+     */\r
+    public static void showPerspective(String perspectiveId) throws WorkbenchException {\r
+        showPerspective(PlatformUI.getWorkbench().getActiveWorkbenchWindow(), perspectiveId);\r
+    }\r
+\r
+    /**\r
+     * Opens the specified perspective in the specified workbench window\r
+     * according to the description of\r
+     * {@link IWorkbench#showPerspective(String, IWorkbenchWindow)}.\r
+     * \r
+     * <p>\r
+     * IMPORTANT: If you're using multiple workbench windows, use\r
+     * {@link #showPerspective(IWorkbenchWindow, String, IAdaptable)} instead.\r
+     * </p>\r
+     * \r
+     * @param window the window in which to show the specified perspective\r
+     * @param perspectiveId the ID of the perspective to activate in the\r
+     *        currently active workbench window\r
+     * @throws WorkbenchException if perspective activation fails\r
+     */\r
+    public static void showPerspective(IWorkbenchWindow window, String perspectiveId) throws WorkbenchException {\r
+        PlatformUI.getWorkbench().showPerspective(perspectiveId, window);\r
+    }\r
+\r
+    /**\r
+     * Opens the specified perspective in the specified workbench window with\r
+     * the specified page input according to the description of\r
+     * {@link IWorkbench#showPerspective(String, IWorkbenchWindow, IAdaptable)}.\r
+     * \r
+     * <p>\r
+     * This is the only <code>showPerspective</code> that will force the\r
+     * specified perspective to open in exactly the specified window. The other\r
+     * methods cannot guarantee this. See\r
+     * {@link IWorkbench#showPerspective(String, IWorkbenchWindow, IAdaptable)}\r
+     * for an explanation why.\r
+     * </p>\r
+     * \r
+     * <p>\r
+     * Example:\r
+     * \r
+     * <pre>\r
+     * WorkbenchUtils.showPerspective(window, myPerspective, window.getActivePage().getInput());\r
+     * </pre>\r
+     * \r
+     * </p>\r
+     * \r
+     * @param window the window in which to show the specified perspective\r
+     * @param perspectiveId the ID of the perspective to activate in the\r
+     *        currently active workbench window\r
+     * @param the page input, or <code>null</code> if there is no current input.\r
+     *        This is used to seed the input for the page's views\r
+     * @throws WorkbenchException if perspective activation fails\r
+     */\r
+    public static void showPerspective(IWorkbenchWindow window, String perspectiveId, IAdaptable input) throws WorkbenchException {\r
+        PlatformUI.getWorkbench().showPerspective(perspectiveId, window, input);\r
+    }\r
+\r
+    /**\r
+     * Close all perspectives in all open workbench windows that do not pass the\r
+     * specified filter. The filter will get IPerspectiveDescriptor instances as\r
+     * input.\r
+     * \r
+     * @param perspectiveFilter a filter for <code>IPerspectiveDescriptor</code>s.\r
+     */\r
+    public static void closeFilteredPerspectives(IFilter perspectiveFilter) {\r
+        for (IWorkbenchWindow window : PlatformUI.getWorkbench().getWorkbenchWindows()) {\r
+            closeFilteredPerspectives(window, perspectiveFilter);\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Close all perspectives in the specified workbench window that do not pass\r
+     * the specified filter. The filter will get IPerspectiveDescriptor\r
+     * instances as input.\r
+     * \r
+     * @param perspectiveFilter a filter for <code>IPerspectiveDescriptor</code>s.\r
+     */\r
+    public static void closeFilteredPerspectives(IWorkbenchWindow window, IFilter perspectiveFilter) {\r
+        for (IWorkbenchPage page : window.getPages()) {\r
+            for (IPerspectiveDescriptor desc : page.getOpenPerspectives()) {\r
+                if (!perspectiveFilter.select(desc)) {\r
+                    page.closePerspective(desc, true, false);\r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
+    public static IActionBars getActionBars(IWorkbenchPart part) {\r
+        if (part instanceof IViewPart) {\r
+            return ((IViewPart) part).getViewSite().getActionBars();\r
+        } else if (part instanceof IEditorPart) {\r
+            return ((IEditorPart) part).getEditorSite().getActionBars();\r
+        }\r
+        throw new IllegalArgumentException("Specified IWorkbenchPart is neither IViewPart nor IEditorPart");\r
+    }\r
+\r
+    /**\r
+     * @param editorId\r
+     * @param input\r
+     * @throws PartInitException\r
+     *             if editor opening fails\r
+     * @throws IllegalArgumentException\r
+     *             if an editor (IEditorDescription) with the specified id is\r
+     *             not found\r
+     * @throws IllegalStateException\r
+     *             if either current workbench window or current workbench page\r
+     *             returns <code>null</code>.\r
+     */\r
+    public static IEditorPart openEditor(String editorId, IEditorInput input) throws PartInitException {\r
+        IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().findEditor(editorId);\r
+        if (desc == null) {\r
+            throw new IllegalArgumentException("editor with id '" + editorId + "' not found");\r
+        }\r
+\r
+        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();\r
+        if (window == null) {\r
+            throw new IllegalStateException("active workbench window is null");\r
+        }\r
+\r
+        IWorkbenchPage page = window.getActivePage();\r
+        if (page == null) {\r
+            throw new IllegalStateException("active workbench page is null");\r
+        }\r
+\r
+        return page.openEditor(input, desc.getId());\r
+    }\r
+\r
+    /**\r
+     * @param editorId\r
+     * @param input\r
+     * @param perspectiveId\r
+     * @throws Exception\r
+     */\r
+    public static void openEditorInPerspective(String editorId, IEditorInput input, String perspectiveId) throws Exception {\r
+        WorkbenchUtils.showPerspective(perspectiveId);\r
+        openEditor(editorId, input);\r
+    }\r
+\r
+    /**\r
+     * Closes an editor part.\r
+     * \r
+     * @param editorPart\r
+     *            editor part instance to close\r
+     * @param save <code>true</code> to save changes before closing,\r
+     *         <code>false</code> to discard any changes\r
+     * @return <code>true</code> if the part was closed successfully\r
+     */\r
+    public static boolean closeAllEditors(boolean save) {\r
+        IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();\r
+        if (windows == null)\r
+            return false;\r
+        for (IWorkbenchWindow wb : windows) {\r
+            if (wb == null)\r
+                return false;\r
+            IWorkbenchPage pages[] = wb.getPages();\r
+            if (pages == null)\r
+                return false;\r
+            for (IWorkbenchPage page : pages) {\r
+                if (page == null)\r
+                    continue;\r
+                return page.closeAllEditors(save);\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+\r
+    /**\r
+     * Closes an editor part.\r
+     * \r
+     * @param editorPart\r
+     *            editor part instance to close\r
+     * @param save <code>true</code> to save changes before closing,\r
+     *         <code>false</code> to discard any changes\r
+     * @return <code>true</code> if the part was closed successfully\r
+     */\r
+    public static boolean closeEditor(IEditorPart editorPart, boolean save) {\r
+        IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();\r
+        if (windows == null)\r
+            return false;\r
+        for (IWorkbenchWindow wb : windows) {\r
+            if (wb == null)\r
+                return false;\r
+            IWorkbenchPage pages[] = wb.getPages();\r
+            if (pages == null)\r
+                return false;\r
+            for (IWorkbenchPage page : pages) {\r
+                if (page == null)\r
+                    continue;\r
+                return page.closeEditor(editorPart, save);\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+\r
+    /**\r
+     * Closes all editor parts in the specified workbench window.\r
+     * \r
+     * @param window editor part instance to close\r
+     * @param save <code>true</code> to save changes before closing,\r
+     *        <code>false</code> to discard any changes\r
+     * @return <code>true</code> if all editor parts were closed properly,\r
+     *         <code>false</code> otherwise\r
+     */\r
+    public static boolean closeEditors(IWorkbenchWindow window, boolean save) {\r
+        boolean result = true;\r
+        for (IWorkbenchPage page : window.getPages()) {\r
+            if (page == null)\r
+                continue;\r
+            result &= page.closeAllEditors(false);\r
+        }\r
+        return result;\r
+    }\r
+\r
+    /**\r
+     * Try to get {@link IActionBars} from the specified {@link IWorkbenchSite}.\r
+     * There are four alternatives to what the site can be: {@link IViewSite},\r
+     * {@link IPageSite}, {@link IEditorSite} or {@link IIntroSite}. All of them\r
+     * have a method for retrieving {@link IActionBars}.\r
+     * \r
+     * @return the action bars of the specified workbench site or\r
+     *         <code>null</code> if the site is unrecognized or does not have an\r
+     *         action bar\r
+     */\r
+    public static IActionBars getActionBars(IWorkbenchSite site) {\r
+        if (site instanceof IViewSite)\r
+            return ((IViewSite) site).getActionBars();\r
+        else if (site instanceof IPageSite)\r
+            return ((IPageSite) site).getActionBars();\r
+        else if (site instanceof IEditorSite)\r
+            return ((IEditorSite) site).getActionBars();\r
+        else if (site instanceof IIntroSite)\r
+            return ((IIntroSite) site).getActionBars();\r
+        return null;\r
+    }\r
+\r
+    /**\r
+     * Try to get {@link IStatusLineManager} from the specified\r
+     * {@link IWorkbenchSite}.\r
+     * \r
+     * @return the status line if available or <code>null</code> if not\r
+     */\r
+    public static IStatusLineManager getStatusLine(IWorkbenchPart part) {\r
+        IActionBars bars = getActionBars(part);\r
+        return bars != null ? bars.getStatusLineManager() : null;\r
+    }\r
+\r
+    /**\r
+     * Try to get {@link IStatusLineManager} from the specified\r
+     * {@link IWorkbenchSite}.\r
+     * \r
+     * @return the status line if available or <code>null</code> if not\r
+     */\r
+    public static IStatusLineManager getStatusLine(IWorkbenchSite site) {\r
+        IActionBars bars = getActionBars(site);\r
+        return bars != null ? bars.getStatusLineManager() : null;\r
+    }\r
+\r
+    /**\r
+     * @param partSite site of workbench part to activate\r
+     * @return <code>false</code> if the activation was scheduled\r
+     *         asynchronously, <code>true</code> if the part was synchronously\r
+     *         activated\r
+     * @throws NullPointerException\r
+     *             if partSite is <code>null</code>\r
+     */\r
+    public static boolean activatePart(final IWorkbenchPartSite partSite) {\r
+        final IWorkbenchPart part = partSite.getPart();\r
+        IWorkbench workbench = partSite.getWorkbenchWindow().getWorkbench();\r
+        Display display = workbench.getDisplay();\r
+        Runnable activator = new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                partSite.getPage().activate(part);\r
+            }\r
+        };\r
+        if (Thread.currentThread() == display.getThread()) {\r
+            activator.run();\r
+            return true;\r
+        } else {\r
+            SWTUtils.asyncExec(display, activator);\r
+            return false;\r
+        }\r
+    }\r
+\r
+}\r