Eclipse插件开发系列--nature

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Eclipse插件开发系列--nature:

很久没有写插件开发的体会了.

网上很多的插件开发的文档,但是大多数都是插件XML介绍,还有就是hello world.这种书不看当然不对,但是看多了.也没什么大用了.写的都是差不多的..

哎,看了好几本这样的书,真的是浪费时间了.

项目的nature

如果有插件要创建项目,或者是在已有项目上增加JA V A特性,这个其实就是在.project 里填加nature

先来分析一下.project文件.

tomcatplugin

org.eclipse.jdt.core.javabuilder

org.eclipse.pde.ManifestBuilder

org.eclipse.pde.SchemaBuilder

org.eclipse.pde.PluginNature

org.eclipse.jdt.core.javanature

其实,这就是一个XML文件,如果说拿着XML文件读入,然后,在nature里加点内容,其实也不难..难的是.eclipse不是一般的XML文件,他完全不能按常理出牌..

不然的话,eclipse插件开发,就不用叫插件开发了.直接说JDK开发得了..

但是这个文档里面的结构还是相当的有用.

因为…..

IProject proj = project.getProject();

这个就是获得了当前的Project.

IProjectDescription description = proj.getDescription();

这个就是上面文件里的 了….至少这里是XML文件,还是eclipse项目的特性,然后写入文件,这个你根本不用去管.反正你一跟踪源码就什么都有了

Eclipse.core.resource 的Jar里.

String[] prevNatures= description.getNatureIds();

这个一看就知道是获得了

org.eclipse.pde.PluginNature

org.eclipse.jdt.core.javanature

这一段了..

那么,就可以提供一个完整的函数.

public static void addNatureToProject(IProject project, String natureId) throws CoreException {

IProject proj = project.getProject(); // Needed if project is a IJavaProject

IProjectDescription description = proj.getDescription();

String[] prevNatures= description.getNatureIds();

int natureIndex = -1;

for (int i=0; i

if(prevNatures[i].equals(natureId)) {

natureIndex = i;

i = prevNatures.length;

}

}

// Add nature only if it is not already there

if(natureIndex == -1) {

String[] newNatures= new String[prevNatures.length + 1];

System.arraycopy(prevNatures, 0, newNatures, 0,

prevNatures.length);

newNatures[prevNatures.length]= natureId;

description.setNatureIds(newNatures);

proj.setDescription(description, null);

}

}

Eclipse的内部函数还是挺友好的.返回的类型基本上不需要再强制转换.

而直接返回了String[]

不过这也给增加一个元素带来一点点的麻烦.

/**

* Remove a Nature to a Project

*/

public static void removeNatureToProject(IProject project, String natureId) throws CoreException {

IProject proj = project.getProject(); // Needed if project is a IJavaProject

IProjectDescription description = proj.getDescription();

String[] prevNatures= description.getNatureIds();

int natureIndex = -1;

for (int i=0; i

if(prevNatures[i].equals(natureId)) {

natureIndex = i;

i = prevNatures.length;

}

}

// Remove nature only if it exists...

if(natureIndex != -1) {

String[] newNatures= new String[prevNatures.length - 1];

System.arraycopy(prevNatures, 0, newNatures, 0, natureIndex);

System.arraycopy(prevNatures, natureIndex+1, newNatures, natureIndex, prevNatures.length - (natureIndex+1));

description.setNatureIds(newNatures);

proj.setDescription(description, null);

}

}

同样,后面的这个函数就是把natureId 的特性删除了.

这个也不需要多解释了.

相关文档
最新文档