如何在Eclipse插件中监听用户出口?

时间:2023-01-14 15:10:15

I am writing an Eclipse plugin for a college project and need to be able to run code when the user exits and I can't find the correct Listener to enable me do this. An example of similar code is shown below where I listen for successfully completed save events and call a method when this occurs.

我正在为一个大学项目编写一个Eclipse插件,并且需要能够在用户退出时运行代码,而我找不到正确的Listener来让我这样做。下面显示了类似代码的示例,其中我监听成功完成的保存事件并在发生这种情况时调用方法。

public class ExecutionListener implements IExecutionListener{

private DataCollector dataCollector;

public ExecutionListener(DataCollector dataCollector)
{
    this.dataCollector = dataCollector;
}

public void postExecuteSuccess(String action, Object arg1) 
{
    if (action.equals("org.eclipse.ui.file.save")) {
        dataCollector.writeDatabase();
    }
}

So what I want is a Listener which will allow me to listen for exit events and call a method to run my code when this happens. I suppose I wont be able to ensure the exit is successfully completed before running the code but a 'pre-exit' Listener would work just fine. Also if someone does know of the correct Listener could they also tell me the commandId I will need for the exit event (e.g. the commandId for the save event in the above example is "org.eclipse.ui.file.save").

所以我想要的是一个监听器,它允许我监听退出事件并调用一个方法来运行我的代码。我想在运行代码之前我无法确保退出成功完成,但“退出前”监听器可以正常工作。此外,如果有人确实知道正确的监听器,他们也可以告诉我退出事件所需的commandId(例如,上例中的save事件的commandId是“org.eclipse.ui.file.save”)。

Thanks, Jacob

EDIT: To reply to javamonkey79's question:

编辑:回复javamonkey79的问题:

I add the listener like this:

我像这样添加监听器:

/* Adds a listener to listen for file save events if needed. */
if (executionListener == null) {
    ICommandService service = (ICommandService) Activator.getDefault().getWorkbench().
    getService(ICommandService.class);
    executionListener = new ExecutionListener();
    service.addExecutionListener(executionListener);
}

1 个解决方案

#1


The Activator class of your plugin contains a stop() method. The Activator is the class in your plugin that extends the Plugin class and that is referenced in the Manifest.MF at the "Bundle-Activator" tag. The OSGi documentation contains a description on the plugin lifecycle.

插件的Activator类包含stop()方法。 Activator是插件中的类,它扩展了Plugin类,并在Manifest.MF中的“Bundle-Activator”标记中引用。 OSGi文档包含有关插件生命周期的说明。

When the workspace is closed, all of the plugins are stopped. You can then add any clean-up code that you require in this section.

当工作区关闭时,所有插件都会停止。然后,您可以在此部分中添加所需的任何清理代码。

public void stop(BundleContext context) throws Exception {
    plugin = null;
            // Code to clean up here...

    super.stop(context);
}

The API describes when this method is called. Interesting snippet from the API:

API描述何时调用此方法。来自API的有趣片段:

Note 1: If a plug-in has been automatically started, this method will be automatically invoked by the platform when the platform is shut down.

注意1:如果已自动启动插件,平台关闭时平台将自动调用此方法。

The advantages of using this method instead of using a listener on the UI, is that you know that it will be called regardless of how the user leaves the workspace.

使用此方法而不是在UI上使用侦听器的优点是,您知道无论用户如何离开工作区,都将调用它。

#1


The Activator class of your plugin contains a stop() method. The Activator is the class in your plugin that extends the Plugin class and that is referenced in the Manifest.MF at the "Bundle-Activator" tag. The OSGi documentation contains a description on the plugin lifecycle.

插件的Activator类包含stop()方法。 Activator是插件中的类,它扩展了Plugin类,并在Manifest.MF中的“Bundle-Activator”标记中引用。 OSGi文档包含有关插件生命周期的说明。

When the workspace is closed, all of the plugins are stopped. You can then add any clean-up code that you require in this section.

当工作区关闭时,所有插件都会停止。然后,您可以在此部分中添加所需的任何清理代码。

public void stop(BundleContext context) throws Exception {
    plugin = null;
            // Code to clean up here...

    super.stop(context);
}

The API describes when this method is called. Interesting snippet from the API:

API描述何时调用此方法。来自API的有趣片段:

Note 1: If a plug-in has been automatically started, this method will be automatically invoked by the platform when the platform is shut down.

注意1:如果已自动启动插件,平台关闭时平台将自动调用此方法。

The advantages of using this method instead of using a listener on the UI, is that you know that it will be called regardless of how the user leaves the workspace.

使用此方法而不是在UI上使用侦听器的优点是,您知道无论用户如何离开工作区,都将调用它。