在Eclipse启动时关闭所有以前打开的编辑器

时间:2011-08-02 11:29:38

标签: eclipse-plugin

我设计了一个Eclipse插件。 Eclipse的一个特性是,如果使用一些开放式编辑器关闭工作台,那么下次启动Eclipse时,在上一个会话中打开的所有编辑器都将保持打开状态。

我的要求是在Eclipse启动时根据之前的会话关闭所有打开的编辑器。在其中一个设计的插件类中,我添加了以下代码

PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
for (IWorkbenchWindow iWorkbenchWindow : windows) {
  if (iWorkbenchWindow.getActivePage() != null) {
    break;
  }
  iWorkbenchWindow.close();
}

此代码在启动时关闭了编辑器。但它也引发了一个例外:

"Unable to read workbench state. Workbench UI layout will be reset.
java.lang.NullPointerException". 

请你告诉我哪里错了。

2 个答案:

答案 0 :(得分:1)

请参阅http://www.eclipse.org/forums/index.php/m/703373/

我引用

On 27.07.2011 12:43, newbie wrote:
> Hi,
> I have created an eclipse plugin.. From the eclipse workbench, i want 
> to close all the open editors on some change event in a combo box 
> located on the tools panel.. I want to perform an exactly similar 
> action which is achieved by right click an editor and selec "Close 
> All" action. Can you please help me with the java code or eclipse 
> api's to achieve that?

org.eclipse.ui.IWorkbenchPage.closeAllEditors(boolean)

Dani
>
> Thanks

答案 1 :(得分:0)

我没有对此进行测试,但我猜你在Eclipse启动序列中过早地执行了代码...

如果您正在使用RCP应用程序,则应尝试将代码添加到ApplicationWorkbenchAdvisor.postStartup() ...但您不是......

对于IDE来说,似乎要困难得多,因为没有钩子(AFAICS)允许您在IDE完全加载后执行代码。

或者为监视IWorkbenchWindows个事件的所有partOpened添加一个监听器,并在此关闭编辑器......