您的位置:首页 > 编程语言 > Java开发

在插件开发中如何获取到活动的工作区页面?

2017-08-10 00:00 323 查看
摘要: 在插件开发中如何获取到活动的工作区页面?

在插件开发中如何获取到活动的工作区页面?

第一种方法

IWorkbench wb = PlatformUI.getWorkbench();
IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
IWorkbenchPage page = win.getActiveWorkbenchPage();

// on new versions it may need to be changed to:
IWorkbenchPage page = win.getActivePage();


缺点:返回null当活动窗口不存在的时候

其他的方式

在一个Part里的时候

`1IWorkbenchPage page = getSite().getPage();`
`// Workbench sites are IServiceLocator.`

在一个命令句柄里的时候

The HandlerUtil class provides a number of helper methods to obtain the active window, editor, part, etc. from the ExecutionEvent provided to the handler.

A special case is a handler that implements IElementUpdater as the updateElements() method is provided an UIElement rather than an ExecutionEvent. But the UIElement does provide an IServiceLocator.

在一个Action里的时候

From an action defined in a workbench action set, you can access the window from the init method:

class MyAction implements IWorkbenchWindowActionDelegate {
private IWorkbenchWindow window;
...
public void init(IWorkbenchWindow win) {
this.window = win;
}
}

Similarly, actions contributed to the popupMenus extension point always have an initialization method that sets the current part before the action's run method is called. All wizard extension points also have an IWorkbenchWizard init method that supplies the wizard with the current workbench window before the wizard is launched. In short, if you look carefully, you can almost always get at the current window or page, no matter where you are in the Eclipse UI.

从IEclipseContext or IServiceLocator对象中获取

Service locators are similar to Eclipse Contexts (IEclipseContext):

IServiceLocator locator = …;
IWorkbenchWindow window = locator.get(IWorkbenchWindow.class);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  eclipse 插件开发
相关文章推荐