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