Question

In Eclipse PDE, when looking for resource changes (using IResourceChangeEvent), how do I find out exactly when a project is created? Do I have access to the project (as an IProject) in this context?

Was it helpful?

Solution

Several parts to this answer:

When resources change, your listener gets an event. From that event you can get a delta describing the resources that were changes. You implement a visitor (passed to the delta with the accept() method) that actually implements your response to the change and controls how far down the delta you want to traverse by answering a boolean from its visit() method. You probably won't traverse too much since any new projects will be at the top of the delta. Once you've recognized that you're visiting a delta node representing a new project (you use flags() and getKind() to see whether or not it's a new project) you can take whatever action you'd like.

The other part of this answer is to warn you that often resource changes are batched, usually for performance reasons, so you might get notified of a new project being created after many files have been created into that project.

OTHER TIPS

To augment the other answer that referenced resource change listeners...

Reliably detecting project creation is actually rather difficult. The issue is what you define as project creation. To the Eclipse resources layer, project creation is when a blank project is created (no metadata or files of any sort), but in a typical scenario you want to know when a particular kind of project is created.

This means that you cannot assume that when your code receives a project creation event, that you will be able to ask questions about that project. Project creation event may be delivered together with subsequent file events in a single batch or the events may trickle in separately.

Another consideration is that to Eclipse project creation is a number of different scenarios. It could be user running a project creation wizard, or importing existing project from disk, or importing from Git, or any number of other ways that a third party plugin may cause a project to be added to the workspace. Each of those scenarios will have slightly different way in which events are batched or trickle in.

The only solution is to look at file events as opposed to project events. Figure out what questions your listener needs to ask of the project, then figure out what files contain the answers and track those files. For instance, if you need to check for a nature and a classpath entry, monitor .project and .classpath files. Don't assume that the project is always in consistent state. It may not be if you are looking at it part way through a Git import.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top