Reputation: 627
I have a project with an incremental builder. The builder writes into a model representations of all resources and their changes, given that the project has a certain nature. This runs pretty well. But the incremental builder is not called if a whole project is deleted from the workspace. What is the best way to create an event handler for that?
I know that I could create an IResourceChangeListener
and attach it to all projects with my nature. But than I would have to start my plugin with the start of the IDE and that is rather messy.
So, what is the best way to catch "Project deleted" events?
Upvotes: 3
Views: 960
Reputation: 3957
You can use an IResourceChangeListener to receive notifications about changes in the workspace. The IResourceChangelistener API is very versatile and can give you change info about many different kinds of changes. Here's an example of how you can use it specifically to detect project deletion.
public class ProjectDeletionListenerManager implements IResourceChangeListener {
public interface ProjectDeletionListener {
void projectAboutToBeDeleted(IProject project);
}
private IWorkspace workspace;
private ProjectDeletionListener listener;
public ProjectDeletionListenerManager(ProjectDeletionListener listener) {
this.workspace = ResourcesPlugin.getWorkspace();
this.listener = listener;
this.workspace.addResourceChangeListener(this,
IResourceChangeEvent.PRE_DELETE);
}
@Override
public void resourceChanged(IResourceChangeEvent event) {
IResource rsrc = event.getResource();
if (rsrc instanceof IProject) {
listener.projectAboutToBeDeleted((IProject) rsrc);
}
}
public void dispose() {
if (listener!=null) {
workspace.removeResourceChangeListener(this);
listener = null;
}
}
}
Note: code snippet based on this code.
Upvotes: 7
Reputation: 111142
IResourceChangeListener
is the way to do this. You don't attach it to projects, it always gets called for all changes.
You can use the org.eclipse.ui.startup
extension point to get your plug-in started during Eclipse startup.
Upvotes: 2