José
José

Reputation: 3179

Eclipse plug-in on project loaded

I have a plug-in and want to detect when projects are added to workspace, to set some project settings from my plug-in code, Any Ideas.

Specially i want to call setHidden in some resources that are derived files, as this settings seems to not be part of the project, i mean whenever a resources is hidden seems to not persist if i import the project in a new workspace.

Upvotes: 2

Views: 981

Answers (2)

Andrew Eisenberg
Andrew Eisenberg

Reputation: 28737

Ironically, I just wrote something like this yesterday. It is a bit more complicated than you would like. Here is a code snippet for you to play with:

public class ProjectListener implements IResourceChangeListener {

public void resourceChanged(IResourceChangeEvent event) {
    if (event.getType() == IResourceChangeEvent.POST_CHANGE) {
        List<IProject> projects = getProjects(event.getDelta());
        // do something with new projects
    }
}

private List<IProject> getProjects(IResourceDelta delta) {
    final List<IProject> projects = new ArrayList<IProject>();
    try {
        delta.accept(new IResourceDeltaVisitor() {
            public boolean visit(IResourceDelta delta) throws CoreException {
                if (delta.getKind() == IResourceDelta.ADDED && 
                  delta.getResource().getType() == IResource.PROJECT) {
                    IProject project = (IProject) delta.getResource();
                    if (project.isAccessible()) {
                        projects.add(project);
                    }
                }
                // only continue for the workspace root
                return delta.getResource().getType() == IResource.ROOT;
            }
        });
    } catch (CoreException e) {
        // handle error
    }
    return projects;
}

Then, you need to add this ProjectListener to the Workspace, preferably in the start method of your plugin activator:

ResourcesPlugin.getWorkspace().addResourceChangeListener(ProjectListener.LISTENER, IResourceChangeEvent.POST_CHANGE);

And then you want to remove it in the stop method. I literally just wrote this code yesterday. I hope it helps.

Upvotes: 8

Zolt&#225;n Ujhelyi
Zolt&#225;n Ujhelyi

Reputation: 13858

You can define a resourcelistener to the workspace, and look for changes in the resource root. See the following article for details: http://www.eclipse.org/articles/Article-Resource-deltas/resource-deltas.html

Upvotes: 2

Related Questions