How to listen for user output in my Eclipse plugin?

I am writing an Eclipse plugin for a college project and should be able to run the code when the user logs out, and I cannot find the correct listener so that I can do this. An example of similar code is shown below, where I listen to successfully executed save events and the method is called when this happens.

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, I want a listener that will allow me to listen for exit events and call a method to run my code when this happens. I suppose I wonโ€™t be able to ensure that the exit is completed successfully before running the code, but the pre-exit listener will work fine. In addition, if someone knows about the correct listener, they can also tell me the commandId that I will need for the exit event (for example, commandId for the save event in the above example is "org.eclipse.ui.file.save").

Thanks Jacob

EDIT: To answer the javamonkey79 question:

I add a listener as follows:

 /* 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); } 
+4
source share
1 answer

Your plugin's Activator class contains the stop () method. An activator is a class in your plugin that extends the Plugin class and refers to Manifest.MF in the "Bundle-Activator" tag. The OSGi documentation describes the life cycle of the plugin.

When the workspace is closed, all plugins stop. Then you can add any cleanup code that you need 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 . An interesting snippet from the API:

Note 1: if the plug-in was automatically launched, this method will automatically call the platform when the platform is closed down.

The advantage of using this method instead of using a listener in the user interface is that you know that it will be called regardless of how the user leaves the workspace.

+5
source

All Articles