插件开发的一个工具类PlatformUtils
????? 在RCP开发中常用的一些工具方法:
?
package com.unutrip.plugin.dev.commons;import org.eclipse.swt.widgets.Display;import org.eclipse.ui.IEditorInput;import org.eclipse.ui.IEditorPart;import org.eclipse.ui.IViewPart;import org.eclipse.ui.IViewReference;import org.eclipse.ui.IWorkbench;import org.eclipse.ui.IWorkbenchPage;import org.eclipse.ui.IWorkbenchWindow;import org.eclipse.ui.PartInitException;import org.eclipse.ui.PlatformUI;/** * 工作台的工具类 * @author longgangbai * */public class PlatformUtils { /** * 查找当前工作区 * @return */ public static IWorkbenchWindow getWorkbenchWindow(){ return PlatformUI.getWorkbench().getActiveWorkbenchWindow(); } /** * 查找当前页面 * @return */ public static IWorkbenchPage getIWorkbenchPage(){ return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); } /** * 隐藏视图 * @param viewID */ public static void hideView(String viewID){ IWorkbenchPage page =getIWorkbenchPage(); IViewReference f = page.findViewReference(viewID); PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(f); } /** * 针对view,根据其ID获得引用 * 打开视图 * @param viewID */ public static void showView(String viewID){ IWorkbenchPage page = getIWorkbenchPage(); try { if(page!=null) { page.showView(viewID); } } catch (PartInitException e) { e.printStackTrace(); } } /** * 假设Editor具体的实现类为MyFileEditor,则 * 注意:这里根据指定的input(IEditorInput)得到Editor的引用(Editor已实现EditorPart) * 之所以不能根据ID得到是因为一个ID对应一个Editor,而一个Editor一般对应几个input并以选项 * 卡的形式来表现这几个input * @param input */ public static IEditorPart findEditor(IEditorInput input){ IWorkbenchPage page = getIWorkbenchPage(); if(page!=null) { return page.findEditor(input); } return null; } /** * 关闭当前页面 * @param closeAll */ public static void closePage(boolean closeAll){ if(closeAll){ PlatformUI.getWorkbench().close(); }else{ PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().close(); } } /** * */ public static void hidePropertiesView() { hideView("org.eclipse.ui.views.PropertySheet"); } /** * * @param theViewId */ public static void syncExecHideView(final String theViewId) { invokeOnDisplayThread(new Runnable() { public void run() { try { IWorkbenchPage activePage = getIWorkbenchPage(); IViewPart iviewpart = activePage.findView(theViewId); activePage.hideView(iviewpart); } catch (Exception ex) { ex.printStackTrace(); } } }); } public static void invokeOnDisplayThread(Runnable runnable) { IWorkbench workbench = PlatformUI.getWorkbench(); IWorkbenchWindow windows[] = workbench.getWorkbenchWindows(); if (windows != null && windows.length > 0) { Display display = windows[0].getShell().getDisplay(); display.syncExec(runnable); } else { runnable.run(); } }}?
?
在eclipse中获得当前所有打开的editor实例列EditorPart[]?parts?=?PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditors();?IEditorReference[]?parts?=?PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();?