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