Howto create a new project nature in Eclipse
26/09/2009 1 Comment
This post is a translation of an older post of mine that I’ve originally written in French.
This post shall be the first of a series about Eclipse as a platform instead of an IDE.
The style will be minimalistic, in the sense that I’ll go directly into the how to do the things, with no (or few) explanations.
In this post, I’ll show how to create a new project nature that you can associate later with a new or an existing project.
1. Creating the nature’s class
In the plug-in that’ll define the nature, edit the manifest to add the following dependencies : org.eclipse.core.resources (which defines the IProjectNature interface and the org.eclipse.core.resources.natures extension point) and org.eclipse.core.runtime (which defined CoreException) :
Require-Bundle: org.eclipse.core.resources,
org.eclipse.core.runtime
Create a class implementing org.eclipse.core.resources.IProjectNature :
package jawher.eclipse.howto.nature;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectNature;
import org.eclipse.core.runtime.CoreException;
public class DummyNature implements IProjectNature {
public static final String NATURE_ID = "jawher.eclipse.howto.nature.dummyNature";
private IProject project;
public void configure() throws CoreException {
}
public void deconfigure() throws CoreException {
}
public IProject getProject() {
return project;
}
public void setProject(IProject project) {
this.project = project;
}
}
Note the public field NATURE_ID which contains the nature id. This will come in handy when you’ll need this id to associate the nature we’re creating with a project.
2. Declaring the nature in plugin.xml
Edit the plug-in’s plugin.xml to add the following :
<extension id="dummyNature" name="Ma nature de projets" point="org.eclipse.core.resources.natures">
<runtime>
<run class="jawher.eclipse.howto.nature.DummyNature">
</run>
</runtime>
</extension>
Warning
Do not use the whole nature’s id as a value for the id attribute. Instead, use only the part that comes after the plugin id.
The nature’s id is computed as follows :
<bundle-symbolic-name> . <nature-id-as-per-plugin.xml>
In my case, I’ve used “jawher.eclipse.howto.nature” as the bundle’s symbolic name (id), and since I’ve specified “dummyNature” as the nature id in plugin.xml, and thus, the absolute nature id is “jawher.eclipse.howto.nature.dummyNature”
3. Associate an overlay image with the nature
This step is optional and is used to associate an icon with the nature. Eclipse will draw such an icon as an overlay on top of a project’s icon.
You’ll need to add “org.eclipse.ui.ide” which declares the required extension point “org.eclipse.ui.ide.projectNatureImages” to the required bundles section of the plugin’s manifest.
Now, add the following to plugin.xml :
<extension point="org.eclipse.ui.ide.projectNatureImages">
<img icon="icons/dummy-nature.png" id="jawher.eclipse.howto.nature.dummyNature.img" natureid="jawher.eclipse.howto.nature.dummyNature">
</extension>
This assumes that you have an icon named “dummy-nature.png” in a folder “icons”. The icon should be a small one, generally 8×8 pixels.
Warning
Don’t forget to include the icons folder in the binary build !
4. Source code
The source code is available as a zipped eclipse project.
Thx for the short guide!