1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.utils.ui.workbench;
14 import java.util.ArrayList;
15 import java.util.HashSet;
16 import java.util.List;
19 import org.eclipse.core.runtime.IAdaptable;
20 import org.eclipse.jface.action.IStatusLineManager;
21 import org.eclipse.jface.viewers.IFilter;
22 import org.eclipse.swt.widgets.Display;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.ui.IActionBars;
25 import org.eclipse.ui.IEditorDescriptor;
26 import org.eclipse.ui.IEditorInput;
27 import org.eclipse.ui.IEditorPart;
28 import org.eclipse.ui.IEditorSite;
29 import org.eclipse.ui.IPerspectiveDescriptor;
30 import org.eclipse.ui.IViewPart;
31 import org.eclipse.ui.IViewReference;
32 import org.eclipse.ui.IViewSite;
33 import org.eclipse.ui.IWorkbench;
34 import org.eclipse.ui.IWorkbenchPage;
35 import org.eclipse.ui.IWorkbenchPart;
36 import org.eclipse.ui.IWorkbenchPartSite;
37 import org.eclipse.ui.IWorkbenchSite;
38 import org.eclipse.ui.IWorkbenchWindow;
39 import org.eclipse.ui.PartInitException;
40 import org.eclipse.ui.PlatformUI;
41 import org.eclipse.ui.WorkbenchException;
42 import org.eclipse.ui.intro.IIntroSite;
43 import org.eclipse.ui.part.IPageSite;
44 import org.simantics.utils.ui.SWTUtils;
47 * Some eclipse workspace utils
49 * @author Toni Kalajainen
51 public class WorkbenchUtils {
56 * @param primaryID primary view id
57 * @param secondaryID secondary view id
58 * @return view part if it exists
60 public static IViewPart findView(String primaryID, String secondaryID) {
61 if (secondaryID==null)
62 return _findView(primaryID, false);
64 return _findView(primaryID + ":" + secondaryID, false);
70 * @param id full id (example "primaryid:secondaryid")
71 * @return <code>IViewPart</code> or null if view wasn't found
73 public static IViewPart findView(String id) {
74 return _findView(id, false);
78 * Finds (and restores) all views that have a specific primary Id
80 * @param primaryId primary Id
83 public static IViewPart[] findAllViews(String primaryId) {
84 List<IViewPart> result = new ArrayList<IViewPart>();
85 for (IViewReference ref : getViewReferences())
86 if (primaryId.equals(ref.getId()))
87 result.add(ref.getView(true));
88 return result.toArray(new IViewPart[0]);
92 * Find viewpart, opens view if view is closed
95 * @return <code>IViewPart</code> or null if view wasn't found
97 public static IViewPart getView(String id) {
98 return _findView(id, true);
101 public static IWorkbenchWindow getWorkbenchWindow(Shell shell)
103 for (IWorkbenchWindow w : PlatformUI.getWorkbench().getWorkbenchWindows())
104 if (w.getShell()==shell) return w;
109 * Try to get the {@link Shell} of the currently active workbench window.
110 * Must be invoked from the SWT UI thread to receive results.
112 * @return Shell of the currently active workbench window or
113 * <code>null</code> if there is no active window or if called from
116 public static Shell getActiveWorkbenchWindowShell() {
117 IWorkbench wb = PlatformUI.getWorkbench();
118 IWorkbenchWindow window = wb.getActiveWorkbenchWindow();
121 return window.getShell();
125 * Get the active workbench part in currently active workbench window /
128 * @return active workbench part of the active workbench window
130 public static IWorkbenchPart getActiveWorkbenchPart()
132 IWorkbench wb = PlatformUI.getWorkbench();
133 if (wb==null) return null;
134 IWorkbenchWindow wbw = wb.getActiveWorkbenchWindow();
135 if (wbw==null) return null;
136 IWorkbenchPage wbp = wbw.getActivePage();
137 if (wbp==null) return null;
138 return wbp.getActivePart();
143 * Get the active workbench part in currently active workbench window /
146 * @return active workbench part of the active workbench window
148 public static IEditorPart getActiveEditor()
150 IWorkbench wb = PlatformUI.getWorkbench();
151 if (wb==null) return null;
152 IWorkbenchWindow wbw = wb.getActiveWorkbenchWindow();
153 if (wbw==null) return null;
154 IWorkbenchPage wbp = wbw.getActivePage();
155 if (wbp==null) return null;
156 return wbp.getActiveEditor();
160 * Get the perspective in current active workbench window / active page
161 * @return Current perspective
163 public static IPerspectiveDescriptor getCurrentPerspective()
165 IWorkbench wb = PlatformUI.getWorkbench();
166 if (wb==null) return null;
167 IWorkbenchWindow wbw = wb.getActiveWorkbenchWindow();
168 if (wbw==null) return null;
169 IWorkbenchPage wbp = wbw.getActivePage();
170 if (wbp==null) return null;
171 return wbp.getPerspective();
175 * Get the perspective in current active workbench window / active page
176 * @return Current perspective Id
178 public static String getCurrentPerspectiveId()
180 IPerspectiveDescriptor p = getCurrentPerspective();
181 if (p==null) return null;
186 * Returns all <code>IViewReference</code>s
187 * @return array of all <code>IViewReference</code>s
189 public static IViewReference[] getViewReferences() {
190 Set<IViewReference> result = new HashSet<IViewReference>();
192 IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
195 for (IWorkbenchWindow wb : windows) {
198 IWorkbenchPage pages[] = wb.getPages();
201 for (IWorkbenchPage page : pages) {
204 IViewReference refs[] = page.getViewReferences();
205 for (IViewReference ref : refs)
209 return result.toArray(new IViewReference[0]);
213 * Returns all <code>IViewReference</code>s for the specified workbench
216 * @return array of all <code>IViewReference</code>s for the specified
219 public static IViewReference[] getViewReferences(IWorkbenchWindow window) {
220 Set<IViewReference> result = new HashSet<IViewReference>();
221 for (IWorkbenchPage page : window.getPages()) {
222 IViewReference refs[] = page.getViewReferences();
223 for (IViewReference ref : refs)
226 return result.toArray(new IViewReference[0]);
230 * Finds a <code>IViewPart</code>. Opens the view if it's closed
231 * @param primaryID primary ID of the view
232 * @param secondaryID secondary ID of the view or null if view has no secondary ID
233 * @return <code>IViewPart</code> if view was found or opened, else returns null
235 public static IViewPart getView(String primaryID, String secondaryID) {
236 if (secondaryID==null)
237 return _findView(primaryID, true);
238 return _findView(primaryID+":"+secondaryID, true);
242 * Finds a <code>IViewPart</code>
243 * @param id id of the View
244 * @param restore set to true if you want to open closed view
245 * @return the <code>IViewPart</code> if view was found or restored, else returns null
247 private static IViewPart _findView(String id, boolean restore) {
248 String primaryId = getPrimaryID(id);
249 String secondaryId = getSecondaryID(id);
250 IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
253 for (IWorkbenchWindow wb : windows) {
256 IWorkbenchPage pages[] = wb.getPages();
259 for (IWorkbenchPage page : pages) {
263 IViewReference vr = page.findViewReference(primaryId, secondaryId);
267 IViewPart vp = vr.getView(restore);
277 * @param id the id of the view
278 * @return true if view was hidden, returns false if view with id wasn't found
280 public static boolean hideView(String id) {
281 IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
284 for (IWorkbenchWindow wb : windows) {
287 IWorkbenchPage pages[] = wb.getPages();
290 for (IWorkbenchPage page : pages) {
294 IViewReference vr = page.findViewReference(getPrimaryID(id), getSecondaryID(id));
309 public static boolean hideView(IViewPart viewPart) {
310 IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
313 for (IWorkbenchWindow wb : windows) {
316 IWorkbenchPage pages[] = wb.getPages();
319 for (IWorkbenchPage page : pages) {
322 page.hideView(viewPart);
333 public static boolean hideView(IWorkbenchWindow window, IViewPart view) {
334 for (IWorkbenchPage page : window.getPages()) {
347 public static boolean hideView(IWorkbenchWindow window, IViewReference view) {
348 for (IWorkbenchPage page : window.getPages()) {
358 * Gets the primary id from view id "prim:secon"
363 public static String getPrimaryID(String id) {
364 String splitted[] = id.split(":");
365 if (splitted == null || splitted.length < 1)
371 * Gets the secondary id from view id "prim:secon"
374 * @return secondary id
376 public static String getSecondaryID(String id) {
377 String splitted[] = id.split(":");
378 if (splitted == null || splitted.length < 2)
385 * @param id the id of the view
386 * @return <code>IViewPart</code> or null if view couldn't be created
387 * @throws PartInitException
389 public static IViewPart showView(String id) throws PartInitException {
390 return showView(id, IWorkbenchPage.VIEW_CREATE);
395 * @param id the id of the view
396 * @return <code>IViewPart</code> or null if view couldn't be activated
397 * @throws PartInitException
399 public static IViewPart activateView(String id) throws PartInitException {
400 IViewPart vp = showView(id, IWorkbenchPage.VIEW_ACTIVATE);
406 * @param id the id of the view
407 * @param mode <code>IWorkbenchPage.VIEW_CREATE</code> or <code>IWorkbenchPage.VIEW_ACTIVATE</code>
408 * @return the <code>IViewPart</code> or null if showing the view failed
409 * @throws PartInitException
411 public static IViewPart showView(String id, final int mode) throws PartInitException {
412 // Is view already created
413 IViewPart vp = findView(id);
415 if (mode == IWorkbenchPage.VIEW_CREATE)
418 Display display = Display.getCurrent();
419 if (Thread.currentThread() == display.getThread()) {
420 // This is the UI-thread
421 //System.out.println("In UI thread");
422 IWorkbenchPage page = vp.getViewSite().getPage();
424 if (mode == IWorkbenchPage.VIEW_VISIBLE) {
426 } else if (mode == IWorkbenchPage.VIEW_ACTIVATE) {
430 //System.out.println("NOT in UI thread!");
431 final IViewPart fvp = vp;
432 display.asyncExec(new Runnable() {
435 final IWorkbenchWindow wb = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
436 IWorkbenchPage page = wb.getActivePage();
437 if (mode == IWorkbenchPage.VIEW_VISIBLE) {
438 page.bringToTop(fvp);
448 final String primaryID = getPrimaryID(id);
449 final String secondaryID = getSecondaryID(id);
450 if (primaryID == null/* || secondaryID == null*/)
453 // Create the view on active page
454 final IWorkbenchWindow wb = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
457 IWorkbenchPage page = wb.getActivePage();
459 IWorkbenchPage pages[] = wb.getPages();
460 if (pages==null || pages.length == 0) return null;
464 vp = page.showView(primaryID, secondaryID, mode);
470 * IMPORTANT: If you're using multiple workbench windows, use
471 * {@link #showPerspective(IWorkbenchWindow, String, IAdaptable)} instead.
473 * @param perspectiveId the ID of the perspective to activate in the
474 * currently active workbench window
475 * @throws WorkbenchException if perspective activation fails
477 public static void showPerspective(String perspectiveId) throws WorkbenchException {
478 showPerspective(PlatformUI.getWorkbench().getActiveWorkbenchWindow(), perspectiveId);
482 * Opens the specified perspective in the specified workbench window
483 * according to the description of
484 * {@link IWorkbench#showPerspective(String, IWorkbenchWindow)}.
487 * IMPORTANT: If you're using multiple workbench windows, use
488 * {@link #showPerspective(IWorkbenchWindow, String, IAdaptable)} instead.
491 * @param window the window in which to show the specified perspective
492 * @param perspectiveId the ID of the perspective to activate in the
493 * currently active workbench window
494 * @throws WorkbenchException if perspective activation fails
496 public static void showPerspective(IWorkbenchWindow window, String perspectiveId) throws WorkbenchException {
497 PlatformUI.getWorkbench().showPerspective(perspectiveId, window);
501 * Opens the specified perspective in the specified workbench window with
502 * the specified page input according to the description of
503 * {@link IWorkbench#showPerspective(String, IWorkbenchWindow, IAdaptable)}.
506 * This is the only <code>showPerspective</code> that will force the
507 * specified perspective to open in exactly the specified window. The other
508 * methods cannot guarantee this. See
509 * {@link IWorkbench#showPerspective(String, IWorkbenchWindow, IAdaptable)}
510 * for an explanation why.
517 * WorkbenchUtils.showPerspective(window, myPerspective, window.getActivePage().getInput());
522 * @param window the window in which to show the specified perspective
523 * @param perspectiveId the ID of the perspective to activate in the
524 * currently active workbench window
525 * @param the page input, or <code>null</code> if there is no current input.
526 * This is used to seed the input for the page's views
527 * @throws WorkbenchException if perspective activation fails
529 public static void showPerspective(IWorkbenchWindow window, String perspectiveId, IAdaptable input) throws WorkbenchException {
530 PlatformUI.getWorkbench().showPerspective(perspectiveId, window, input);
534 * Close all perspectives in all open workbench windows that do not pass the
535 * specified filter. The filter will get IPerspectiveDescriptor instances as
538 * @param perspectiveFilter a filter for <code>IPerspectiveDescriptor</code>s.
540 public static void closeFilteredPerspectives(IFilter perspectiveFilter) {
541 for (IWorkbenchWindow window : PlatformUI.getWorkbench().getWorkbenchWindows()) {
542 closeFilteredPerspectives(window, perspectiveFilter);
547 * Close all perspectives in the specified workbench window that do not pass
548 * the specified filter. The filter will get IPerspectiveDescriptor
549 * instances as input.
551 * @param perspectiveFilter a filter for <code>IPerspectiveDescriptor</code>s.
553 public static void closeFilteredPerspectives(IWorkbenchWindow window, IFilter perspectiveFilter) {
554 for (IWorkbenchPage page : window.getPages()) {
555 for (IPerspectiveDescriptor desc : page.getOpenPerspectives()) {
556 if (!perspectiveFilter.select(desc)) {
557 page.closePerspective(desc, true, false);
563 public static IActionBars getActionBars(IWorkbenchPart part) {
564 if (part instanceof IViewPart) {
565 return ((IViewPart) part).getViewSite().getActionBars();
566 } else if (part instanceof IEditorPart) {
567 return ((IEditorPart) part).getEditorSite().getActionBars();
569 throw new IllegalArgumentException("Specified IWorkbenchPart is neither IViewPart nor IEditorPart");
575 * @throws PartInitException
576 * if editor opening fails
577 * @throws IllegalArgumentException
578 * if an editor (IEditorDescription) with the specified id is
580 * @throws IllegalStateException
581 * if either current workbench window or current workbench page
582 * returns <code>null</code>.
584 public static IEditorPart openEditor(String editorId, IEditorInput input) throws PartInitException {
585 IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().findEditor(editorId);
587 throw new IllegalArgumentException("editor with id '" + editorId + "' not found");
590 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
591 if (window == null) {
592 throw new IllegalStateException("active workbench window is null");
595 IWorkbenchPage page = window.getActivePage();
597 throw new IllegalStateException("active workbench page is null");
600 return page.openEditor(input, desc.getId());
606 * @param perspectiveId
609 public static void openEditorInPerspective(String editorId, IEditorInput input, String perspectiveId) throws Exception {
610 WorkbenchUtils.showPerspective(perspectiveId);
611 openEditor(editorId, input);
615 * Closes an editor part.
618 * editor part instance to close
619 * @param save <code>true</code> to save changes before closing,
620 * <code>false</code> to discard any changes
621 * @return <code>true</code> if the part was closed successfully
623 public static boolean closeAllEditors(boolean save) {
624 IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
627 for (IWorkbenchWindow wb : windows) {
630 IWorkbenchPage pages[] = wb.getPages();
633 for (IWorkbenchPage page : pages) {
636 return page.closeAllEditors(save);
643 * Closes an editor part.
646 * editor part instance to close
647 * @param save <code>true</code> to save changes before closing,
648 * <code>false</code> to discard any changes
649 * @return <code>true</code> if the part was closed successfully
651 public static boolean closeEditor(IEditorPart editorPart, boolean save) {
652 IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
655 for (IWorkbenchWindow wb : windows) {
658 IWorkbenchPage pages[] = wb.getPages();
661 for (IWorkbenchPage page : pages) {
664 return page.closeEditor(editorPart, save);
671 * Closes all editor parts in the specified workbench window.
673 * @param window editor part instance to close
674 * @param save <code>true</code> to save changes before closing,
675 * <code>false</code> to discard any changes
676 * @return <code>true</code> if all editor parts were closed properly,
677 * <code>false</code> otherwise
679 public static boolean closeEditors(IWorkbenchWindow window, boolean save) {
680 boolean result = true;
681 for (IWorkbenchPage page : window.getPages()) {
684 result &= page.closeAllEditors(false);
690 * Try to get {@link IActionBars} from the specified {@link IWorkbenchSite}.
691 * There are four alternatives to what the site can be: {@link IViewSite},
692 * {@link IPageSite}, {@link IEditorSite} or {@link IIntroSite}. All of them
693 * have a method for retrieving {@link IActionBars}.
695 * @return the action bars of the specified workbench site or
696 * <code>null</code> if the site is unrecognized or does not have an
699 public static IActionBars getActionBars(IWorkbenchSite site) {
700 if (site instanceof IViewSite)
701 return ((IViewSite) site).getActionBars();
702 else if (site instanceof IPageSite)
703 return ((IPageSite) site).getActionBars();
704 else if (site instanceof IEditorSite)
705 return ((IEditorSite) site).getActionBars();
706 else if (site instanceof IIntroSite)
707 return ((IIntroSite) site).getActionBars();
712 * Try to get {@link IStatusLineManager} from the specified
713 * {@link IWorkbenchSite}.
715 * @return the status line if available or <code>null</code> if not
717 public static IStatusLineManager getStatusLine(IWorkbenchPart part) {
718 IActionBars bars = getActionBars(part);
719 return bars != null ? bars.getStatusLineManager() : null;
723 * Try to get {@link IStatusLineManager} from the specified
724 * {@link IWorkbenchSite}.
726 * @return the status line if available or <code>null</code> if not
728 public static IStatusLineManager getStatusLine(IWorkbenchSite site) {
729 IActionBars bars = getActionBars(site);
730 return bars != null ? bars.getStatusLineManager() : null;
734 * @param partSite site of workbench part to activate
735 * @return <code>false</code> if the activation was scheduled
736 * asynchronously, <code>true</code> if the part was synchronously
738 * @throws NullPointerException
739 * if partSite is <code>null</code>
741 public static boolean activatePart(final IWorkbenchPartSite partSite) {
742 final IWorkbenchPart part = partSite.getPart();
743 IWorkbench workbench = partSite.getWorkbenchWindow().getWorkbench();
744 Display display = workbench.getDisplay();
745 Runnable activator = new Runnable() {
748 partSite.getPage().activate(part);
751 if (Thread.currentThread() == display.getThread()) {
755 SWTUtils.asyncExec(display, activator);