]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui.workbench/src/org/simantics/utils/ui/workbench/WorkbenchUtils.java
Sync git svn branch with SVN repository r33269.
[simantics/platform.git] / bundles / org.simantics.utils.ui.workbench / src / org / simantics / utils / ui / workbench / WorkbenchUtils.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.utils.ui.workbench;\r
13 \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
18 \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
45 \r
46 /**\r
47  * Some eclipse workspace utils\r
48  * \r
49  * @author Toni Kalajainen\r
50  */\r
51 public class WorkbenchUtils {\r
52 \r
53     /**\r
54      * Find open viewpart\r
55      * \r
56      * @param primaryID primary view id\r
57      * @param secondaryID secondary view id\r
58      * @return view part if it exists\r
59      */\r
60     public static IViewPart findView(String primaryID, String secondaryID) {\r
61         if (secondaryID==null)\r
62             return _findView(primaryID, false);\r
63         else\r
64             return _findView(primaryID + ":" + secondaryID, false);\r
65     }\r
66 \r
67     /**\r
68      * Find open viewpart\r
69      * \r
70      * @param id full id (example "primaryid:secondaryid")\r
71      * @return <code>IViewPart</code> or null if view wasn't found\r
72      */\r
73     public static IViewPart findView(String id) {\r
74         return _findView(id, false);\r
75     }\r
76 \r
77     /**\r
78      * Finds (and restores) all views that have a specific primary Id\r
79      * \r
80      * @param primaryId primary Id\r
81      * @return all views\r
82      */\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
89     }\r
90 \r
91     /**\r
92      * Find viewpart, opens view if view is closed\r
93      * \r
94      * @param id full id\r
95      * @return <code>IViewPart</code> or null if view wasn't found\r
96      */\r
97     public static IViewPart getView(String id) {\r
98         return _findView(id, true);\r
99     }\r
100 \r
101     public static IWorkbenchWindow getWorkbenchWindow(Shell shell)\r
102     {\r
103         for (IWorkbenchWindow w : PlatformUI.getWorkbench().getWorkbenchWindows())\r
104             if (w.getShell()==shell) return w;\r
105         return null;\r
106     }\r
107 \r
108     /**\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
111      * \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
114      *         a non-UI thread\r
115      */\r
116     public static Shell getActiveWorkbenchWindowShell() {\r
117         IWorkbench wb = PlatformUI.getWorkbench();\r
118         IWorkbenchWindow window = wb.getActiveWorkbenchWindow();\r
119         if (window == null)\r
120             return null;\r
121         return window.getShell();\r
122     }\r
123 \r
124     /**\r
125      * Get the active workbench part in currently active workbench window /\r
126      * active page\r
127      * \r
128      * @return active workbench part of the active workbench window\r
129      */\r
130     public static IWorkbenchPart getActiveWorkbenchPart()\r
131     {\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
139     }\r
140 \r
141 \r
142     /**\r
143      * Get the active workbench part in currently active workbench window /\r
144      * active page\r
145      * \r
146      * @return active workbench part of the active workbench window\r
147      */\r
148     public static IEditorPart getActiveEditor()\r
149     {\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
157     }\r
158 \r
159     /**\r
160      * Get the perspective in current active workbench window / active page\r
161      * @return Current perspective\r
162      */\r
163     public static IPerspectiveDescriptor getCurrentPerspective()\r
164     {\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
172     }\r
173 \r
174     /**\r
175      * Get the perspective in current active workbench window / active page\r
176      * @return Current perspective Id\r
177      */\r
178     public static String getCurrentPerspectiveId()\r
179     {\r
180         IPerspectiveDescriptor p = getCurrentPerspective();\r
181         if (p==null) return null;\r
182         return p.getId();\r
183     }\r
184 \r
185     /**\r
186      * Returns all <code>IViewReference</code>s\r
187      * @return array of all <code>IViewReference</code>s\r
188      */\r
189     public static IViewReference[] getViewReferences() {\r
190         Set<IViewReference> result = new HashSet<IViewReference>();\r
191 \r
192         IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();\r
193         if (windows == null)\r
194             return null;\r
195         for (IWorkbenchWindow wb : windows) {\r
196             if (wb == null)\r
197                 return null;\r
198             IWorkbenchPage pages[] = wb.getPages();\r
199             if (pages == null)\r
200                 return null;\r
201             for (IWorkbenchPage page : pages) {\r
202                 if (page == null)\r
203                     continue;\r
204                 IViewReference refs[] = page.getViewReferences();\r
205                 for (IViewReference ref : refs)\r
206                     result.add(ref);\r
207             }\r
208         }\r
209         return result.toArray(new IViewReference[0]);\r
210     }\r
211 \r
212     /**\r
213      * Returns all <code>IViewReference</code>s for the specified workbench\r
214      * window\r
215      * \r
216      * @return array of all <code>IViewReference</code>s for the specified\r
217      *         window\r
218      */\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
224                 result.add(ref);\r
225         }\r
226         return result.toArray(new IViewReference[0]);\r
227     }\r
228 \r
229     /**\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
234      */\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
239     }\r
240 \r
241     /**\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
246      */\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
252             return null;\r
253         for (IWorkbenchWindow wb : windows) {\r
254             if (wb == null)\r
255                 return null;\r
256             IWorkbenchPage pages[] = wb.getPages();\r
257             if (pages == null)\r
258                 return null;\r
259             for (IWorkbenchPage page : pages) {\r
260                 if (page == null)\r
261                     continue;\r
262 \r
263                 IViewReference vr = page.findViewReference(primaryId, secondaryId);\r
264                 if (vr == null)\r
265                     continue;\r
266 \r
267                 IViewPart vp = vr.getView(restore);\r
268                 if (vp != null)\r
269                     return vp;\r
270             }\r
271         }\r
272         return null;\r
273     }\r
274 \r
275     /**\r
276      * Hides a view\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
279      */\r
280     public static boolean hideView(String id) {\r
281         IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();\r
282         if (windows == null)\r
283             return false;\r
284         for (IWorkbenchWindow wb : windows) {\r
285             if (wb == null)\r
286                 return false;\r
287             IWorkbenchPage pages[] = wb.getPages();\r
288             if (pages == null)\r
289                 return false;\r
290             for (IWorkbenchPage page : pages) {\r
291                 if (page == null)\r
292                     continue;\r
293 \r
294                 IViewReference vr = page.findViewReference(getPrimaryID(id), getSecondaryID(id));\r
295                 if (vr == null)\r
296                     continue;\r
297                 page.hideView(vr);\r
298                 return true;\r
299 \r
300             }\r
301         }\r
302         return false;\r
303     }\r
304 \r
305     /**\r
306      * @param viewPart\r
307      * @return\r
308      */\r
309     public static boolean hideView(IViewPart viewPart) {\r
310         IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();\r
311         if (windows == null)\r
312             return false;\r
313         for (IWorkbenchWindow wb : windows) {\r
314             if (wb == null)\r
315                 return false;\r
316             IWorkbenchPage pages[] = wb.getPages();\r
317             if (pages == null)\r
318                 return false;\r
319             for (IWorkbenchPage page : pages) {\r
320                 if (page == null)\r
321                     continue;\r
322                 page.hideView(viewPart);\r
323                 return true;\r
324             }\r
325         }\r
326         return false;\r
327     }\r
328 \r
329     /**\r
330      * @param view\r
331      * @return\r
332      */\r
333     public static boolean hideView(IWorkbenchWindow window, IViewPart view) {\r
334         for (IWorkbenchPage page : window.getPages()) {\r
335             if (page == null)\r
336                 continue;\r
337             page.hideView(view);\r
338             return true;\r
339         }\r
340         return false;\r
341     }\r
342 \r
343     /**\r
344      * @param view\r
345      * @return\r
346      */\r
347     public static boolean hideView(IWorkbenchWindow window, IViewReference view) {\r
348         for (IWorkbenchPage page : window.getPages()) {\r
349             if (page == null)\r
350                 continue;\r
351             page.hideView(view);\r
352             return true;\r
353         }\r
354         return false;\r
355     }\r
356 \r
357     /**\r
358      * Gets the primary id from view id "prim:secon"\r
359      * \r
360      * @param id full id\r
361      * @return primary id\r
362      */\r
363     public static String getPrimaryID(String id) {\r
364         String splitted[] = id.split(":");\r
365         if (splitted == null || splitted.length < 1)\r
366             return null;\r
367         return splitted[0];\r
368     }\r
369 \r
370     /**\r
371      * Gets the secondary id from view id "prim:secon"\r
372      * \r
373      * @param id full id\r
374      * @return secondary id\r
375      */\r
376     public static String getSecondaryID(String id) {\r
377         String splitted[] = id.split(":");\r
378         if (splitted == null || splitted.length < 2)\r
379             return null;\r
380         return splitted[1];\r
381     }\r
382 \r
383     /**\r
384      * Creates the view\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
388      */\r
389     public static IViewPart showView(String id) throws PartInitException {\r
390         return showView(id, IWorkbenchPage.VIEW_CREATE);\r
391     }\r
392 \r
393     /**\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
398      */\r
399     public static IViewPart activateView(String id) throws PartInitException {\r
400         IViewPart vp = showView(id, IWorkbenchPage.VIEW_ACTIVATE);\r
401         return vp;\r
402     }\r
403 \r
404     /**\r
405      * Shows the view\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
410      */\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
414         if (vp != null) {\r
415             if (mode == IWorkbenchPage.VIEW_CREATE)\r
416                 return vp;\r
417 \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
423 \r
424                 if (mode == IWorkbenchPage.VIEW_VISIBLE) {\r
425                     page.bringToTop(vp);\r
426                 } else if (mode == IWorkbenchPage.VIEW_ACTIVATE) {\r
427                     page.activate(vp);\r
428                 }\r
429             } else {\r
430                 //System.out.println("NOT in UI thread!");\r
431                 final IViewPart fvp = vp;\r
432                 display.asyncExec(new Runnable() {\r
433                     @Override\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
439                         } else {\r
440                             page.activate(fvp);\r
441                         }\r
442                     }\r
443                 });\r
444             }\r
445             return vp;\r
446         }\r
447 \r
448         final String primaryID = getPrimaryID(id);\r
449         final String secondaryID = getSecondaryID(id);\r
450         if (primaryID == null/* || secondaryID == null*/)\r
451             return null;\r
452 \r
453         // Create the view on active page\r
454         final IWorkbenchWindow wb = PlatformUI.getWorkbench().getActiveWorkbenchWindow();\r
455         if (wb == null)\r
456             return null;\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
461             page = pages[0];\r
462         }\r
463 \r
464         vp = page.showView(primaryID, secondaryID, mode);\r
465         return vp;\r
466     }\r
467 \r
468 \r
469     /**\r
470      * IMPORTANT: If you're using multiple workbench windows, use\r
471      * {@link #showPerspective(IWorkbenchWindow, String, IAdaptable)} instead.\r
472      * \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
476      */\r
477     public static void showPerspective(String perspectiveId) throws WorkbenchException {\r
478         showPerspective(PlatformUI.getWorkbench().getActiveWorkbenchWindow(), perspectiveId);\r
479     }\r
480 \r
481     /**\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
485      * \r
486      * <p>\r
487      * IMPORTANT: If you're using multiple workbench windows, use\r
488      * {@link #showPerspective(IWorkbenchWindow, String, IAdaptable)} instead.\r
489      * </p>\r
490      * \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
495      */\r
496     public static void showPerspective(IWorkbenchWindow window, String perspectiveId) throws WorkbenchException {\r
497         PlatformUI.getWorkbench().showPerspective(perspectiveId, window);\r
498     }\r
499 \r
500     /**\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
504      * \r
505      * <p>\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
511      * </p>\r
512      * \r
513      * <p>\r
514      * Example:\r
515      * \r
516      * <pre>\r
517      * WorkbenchUtils.showPerspective(window, myPerspective, window.getActivePage().getInput());\r
518      * </pre>\r
519      * \r
520      * </p>\r
521      * \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
528      */\r
529     public static void showPerspective(IWorkbenchWindow window, String perspectiveId, IAdaptable input) throws WorkbenchException {\r
530         PlatformUI.getWorkbench().showPerspective(perspectiveId, window, input);\r
531     }\r
532 \r
533     /**\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
536      * input.\r
537      * \r
538      * @param perspectiveFilter a filter for <code>IPerspectiveDescriptor</code>s.\r
539      */\r
540     public static void closeFilteredPerspectives(IFilter perspectiveFilter) {\r
541         for (IWorkbenchWindow window : PlatformUI.getWorkbench().getWorkbenchWindows()) {\r
542             closeFilteredPerspectives(window, perspectiveFilter);\r
543         }\r
544     }\r
545 \r
546     /**\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
550      * \r
551      * @param perspectiveFilter a filter for <code>IPerspectiveDescriptor</code>s.\r
552      */\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
558                 }\r
559             }\r
560         }\r
561     }\r
562 \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
568         }\r
569         throw new IllegalArgumentException("Specified IWorkbenchPart is neither IViewPart nor IEditorPart");\r
570     }\r
571 \r
572     /**\r
573      * @param editorId\r
574      * @param input\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
579      *             not found\r
580      * @throws IllegalStateException\r
581      *             if either current workbench window or current workbench page\r
582      *             returns <code>null</code>.\r
583      */\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
588         }\r
589 \r
590         IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();\r
591         if (window == null) {\r
592             throw new IllegalStateException("active workbench window is null");\r
593         }\r
594 \r
595         IWorkbenchPage page = window.getActivePage();\r
596         if (page == null) {\r
597             throw new IllegalStateException("active workbench page is null");\r
598         }\r
599 \r
600         return page.openEditor(input, desc.getId());\r
601     }\r
602 \r
603     /**\r
604      * @param editorId\r
605      * @param input\r
606      * @param perspectiveId\r
607      * @throws Exception\r
608      */\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
612     }\r
613 \r
614     /**\r
615      * Closes an editor part.\r
616      * \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
622      */\r
623     public static boolean closeAllEditors(boolean save) {\r
624         IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();\r
625         if (windows == null)\r
626             return false;\r
627         for (IWorkbenchWindow wb : windows) {\r
628             if (wb == null)\r
629                 return false;\r
630             IWorkbenchPage pages[] = wb.getPages();\r
631             if (pages == null)\r
632                 return false;\r
633             for (IWorkbenchPage page : pages) {\r
634                 if (page == null)\r
635                     continue;\r
636                 return page.closeAllEditors(save);\r
637             }\r
638         }\r
639         return false;\r
640     }\r
641 \r
642     /**\r
643      * Closes an editor part.\r
644      * \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
650      */\r
651     public static boolean closeEditor(IEditorPart editorPart, boolean save) {\r
652         IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();\r
653         if (windows == null)\r
654             return false;\r
655         for (IWorkbenchWindow wb : windows) {\r
656             if (wb == null)\r
657                 return false;\r
658             IWorkbenchPage pages[] = wb.getPages();\r
659             if (pages == null)\r
660                 return false;\r
661             for (IWorkbenchPage page : pages) {\r
662                 if (page == null)\r
663                     continue;\r
664                 return page.closeEditor(editorPart, save);\r
665             }\r
666         }\r
667         return false;\r
668     }\r
669 \r
670     /**\r
671      * Closes all editor parts in the specified workbench window.\r
672      * \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
678      */\r
679     public static boolean closeEditors(IWorkbenchWindow window, boolean save) {\r
680         boolean result = true;\r
681         for (IWorkbenchPage page : window.getPages()) {\r
682             if (page == null)\r
683                 continue;\r
684             result &= page.closeAllEditors(false);\r
685         }\r
686         return result;\r
687     }\r
688 \r
689     /**\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
694      * \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
697      *         action bar\r
698      */\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
708         return null;\r
709     }\r
710 \r
711     /**\r
712      * Try to get {@link IStatusLineManager} from the specified\r
713      * {@link IWorkbenchSite}.\r
714      * \r
715      * @return the status line if available or <code>null</code> if not\r
716      */\r
717     public static IStatusLineManager getStatusLine(IWorkbenchPart part) {\r
718         IActionBars bars = getActionBars(part);\r
719         return bars != null ? bars.getStatusLineManager() : null;\r
720     }\r
721 \r
722     /**\r
723      * Try to get {@link IStatusLineManager} from the specified\r
724      * {@link IWorkbenchSite}.\r
725      * \r
726      * @return the status line if available or <code>null</code> if not\r
727      */\r
728     public static IStatusLineManager getStatusLine(IWorkbenchSite site) {\r
729         IActionBars bars = getActionBars(site);\r
730         return bars != null ? bars.getStatusLineManager() : null;\r
731     }\r
732 \r
733     /**\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
737      *         activated\r
738      * @throws NullPointerException\r
739      *             if partSite is <code>null</code>\r
740      */\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
746             @Override\r
747             public void run() {\r
748                 partSite.getPage().activate(part);\r
749             }\r
750         };\r
751         if (Thread.currentThread() == display.getThread()) {\r
752             activator.run();\r
753             return true;\r
754         } else {\r
755             SWTUtils.asyncExec(display, activator);\r
756             return false;\r
757         }\r
758     }\r
759 \r
760 }\r