Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 804
Daniel Peñalba Avatar asked Oct 05 '11 16:10

Daniel Peñalba


People also ask

How do I open a closed project in Eclipse?

To reopen a closed project, in the Package Explorer view, select the closed project and click on the Project menu and select Open Project. Once the project is open its content can be edited using the Eclipse user interface.

How do I open an already imported project in Eclipse?

You need to use "File"->"Import"->"General"->"Import Existing Project" to be able to use your existing project.


3 Answers

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

like image 142
Tate Avatar answered Oct 04 '22 20:10

Tate


I know this question has been long answered, but I want to update it with a working code snippet, just in case anybody will need it. I tested it on Eclipse Luna, Indigo, and Kepler.

public void resourceChanged(final IResourceChangeEvent event) {
    if (event == null || event.getDelta() == null) {
        return;
    }

    try {
        event.getDelta().accept(new IResourceDeltaVisitor() {
            public boolean visit(final IResourceDelta delta) throws CoreException {
                IResource resource = delta.getResource();
                if (((resource.getType() & IResource.PROJECT) != 0)
                        && resource.getProject().isOpen()
                        && delta.getKind() == IResourceDelta.CHANGED
                        && ((delta.getFlags() & IResourceDelta.OPEN) != 0)) {

                    IProject project = (IProject)resource;
                    projectOpened(project);
                }
                return true;
            }
        });
    } catch (CoreException e) {
        e.printStackTrace();
    }
}
like image 33
AMilassin Avatar answered Oct 04 '22 18:10

AMilassin


IResourceChangeEvent is POST_CHANGE and related IResourceDelta kind is IResourceDelta.CHANGED and flags include IResourceDelta.OPEN.

like image 43
Martti Käärik Avatar answered Oct 04 '22 19:10

Martti Käärik