Search code examples
javaeclipseeclipse-plugineclipse-rcp

How to subscribe to an OpenProject event in Eclipse?


I'm developing an Eclipse plugin.

I have been reading how to subscribe get notification when a project is about to be closed, using the interface IResourceChangeListener, and using the PRE_CLOSE event type. The following text has been taken from the Eclipse help:

Notifies listeners that a project is about to be closed. This event can be used to extract and save necessary information from the in-memory representation (e.g., session properties) of a project before it is closed. (When a project is closed, the in-memory representation is disposed). The workspace is locked (no resources can be updated) during this event. The event contains the project that is being closed.

I didn't found how to be notified when a project is about to be opened.


Solution

  • You can create your own IResourceChangeListener and filter the kind of delta by IResourceDelta.OPEN, which only affects to IProjects, and it's fired both when opening and closing a project:

    public void resourceChanged(IResourceChangeEvent event) {
        if (event == null || event.getDelta() == null)
            return;
        event.getDelta().accept(new IResourceDeltaVisitor() {
            public boolean visit(IResourceDelta delta) throws CoreException {
                if (delta.getKind() == IResourceDelta.OPEN)
                    final IResource resource = delta.getResource();
                    if (!(resource instanceof IProject))
                        return;
                   //do your stuff and check the project is opened or closed
            }
    }
    

    Useful link: http://www.eclipse.org/articles/Article-Resource-deltas/resource-deltas.html