插件启动时访问工作台 [英] Access Workbench at Plug-in Start

查看:676
本文介绍了插件启动时访问工作台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在插件开始时访问 IWorkbench ,以附加 IPartListener



最简单的解决方案是在插件的 start()方法中添加侦听器: p>

  public class MyPlugin extends AbstractUIPlugin {

private IPartListener partListener = new MyPartListener();

public void start(BundleContext context)throws PluginException {
super.start(context);

Display.getDefault()。asyncExec(() - >
PlatformUI.getWorkbench()。getActiveWorkbenchWindow()。getActivePage()
.addPartListener(this.partListener)) ;
}
}

说明: code>显示#asyncExec 运行下一次UI线程是空闲的,这是在工作台创建之后(因为它已经在创建它的过程中,当一个



代码运行良好一段时间,但在发布后,其他使用它的项目有时会失败:


导致:java.lang.NullPointerException
在org.acme.project.MyPlugin.lambda $ 0(MyPlugin.java:43)
在org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
在org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)


显然在这种情况下,工作台尚未创建。我将代码移动到 IStartup 实现的 earlyStartup()中,现在它适用于所有曾经投诉的项目



由于插件的开始和工作台的创建不是确定性的,我的问题是:这是正确的地方吗?或者这是另一个NullPointerException(或任何其他异常)等待发生?

解决方案

JavaDoc for IStartup 中的earlyStartup 方法明确表示,它在工作台初始化后进行运行,并描述做的事情非常相似,所以这应该是可以的。 p>

Javadoc:


在工作台初始化后,将在单独的线程中调用。 / p>

请注意,大多数工作台方法必须在UI线程中调用,因为
可以访问SWT。例如,要获取当前的工作台
窗口,请使用:




  final IWorkbench workbench = PlatformUI.getWorkbench(); 
workbench.getDisplay()。asyncExec(new Runnable(){
public void run(){
IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
if(window!= null ){
// do something
}
}
});


I want to access the IWorkbench at the start of a plug-in to attach a IPartListener.

The simplest solution was to add the listener in the start() method of the plug-in:

public class MyPlugin extends AbstractUIPlugin {

private IPartListener partListener = new MyPartListener();

public void start(BundleContext context) throws PluginException {
        super.start(context);

        Display.getDefault().asyncExec(() -> 
            PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
            .addPartListener(this.partListener));
}
}

The explanation: Display#asyncExec runs the next time the UI thread is free, which is after the workbench was created (since it's already in the process of creating it when an AbstractUIPlugin is started).

The code worked well for a while, but after the release other projects using it sometimes failed with:

Caused by: java.lang.NullPointerException at org.acme.project.MyPlugin.lambda$0(MyPlugin.java:43) at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35) at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)

Evidently in this case the workbench wasn't created yet. I moved the code into the earlyStartup() of an IStartup implementation and now it works for all projects that had complained before.

Since the start of the plug-ins and creation of the workbench is not deterministic, my question is: Is this the right place? Or is this another NullPointerException (or any other exception) waiting to happen?

解决方案

The JavaDoc for the earlyStartup method in the IStartup class explicitly says it is run after the workbench initializes and describes doing something very similar, so this should be OK.

Javadoc:

Will be called in a separate thread after the workbench initializes.

Note that most workbench methods must be called in the UI thread since they may access SWT. For example, to obtain the current workbench window, use:

 final IWorkbench workbench = PlatformUI.getWorkbench();
 workbench.getDisplay().asyncExec(new Runnable() {
   public void run() {
     IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
     if (window != null) {
       // do something
     }
   }
 });

这篇关于插件启动时访问工作台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆