Monday, March 03, 2008

Eclipse : Adding nature to a project

In Eclipse plugin development terminology adding a nature to a project is like tagging the project with a specific tag . Generally natures are used to install various builders for the project. I will talk about builders in a later post . Lets look at some code and see how how natures are added to projects:

IProjectDescription projectDescription = project.getDescription();

description is a description of the project . It gives a lot of information including the entire list of
nature ids and build commands.

String[] ids = projectDescription.getNatureIds();
String[] newIds = new String[ids.length + 1];
System.arraycopy(ids,0,newIds,0,ids.length);
newIds[ids.length] = YOUR_NATURE_ID;
projectDescription.setNatureIds(newIds);
project.setDescription(projectDescription,null);

the remove nature id code is kind of similar .

To actually implement your nature , you need to contribute to the extension point
org.eclipse.core.resources.natures

Basically that boils down to adding something like this in your plugin.xml

id="yourNatureID"
name="Your Nature Name">







"id" here represents the id of the nature .
"name" represents the name of the nature .
"class" represents the class implements the IProjectNature .

the requires-nature here represents the nature id that is required for this nature to be successfully installed .

Next , finally the class that implements the IProjectNature interface.

public class YourProjectNature implements IProjectNature {
IProject project;
public YourProjectTestNature() {
}
public IProject getProject() {
return project;
}
public void setProject(IProject project) {
this.project= project;
}
}

No comments: