开发Eclipse插件:HelloWorld项目的构建与自定义向导实现
立即解锁
发布时间: 2025-08-18 00:23:42 阅读量: 2 订阅数: 9 

### 开发Eclipse插件:HelloWorld项目的构建与自定义向导实现
在Eclipse开发环境中,我们可以通过一系列操作来实现自定义项目类型的插件,本文将详细介绍如何完成HelloWorld项目的构建、文件修改监听以及自定义向导的实现。
#### 1. ICommand接口与构建器配置
ICommand接口位于`org.eclipse.core.resources`包中。在代码里,会使用`_project`获取与项目关联的当前构建器列表,并手动将新构建器添加到该列表的前面。不过,在添加构建器之前,务必检查其是否已经存在,因为多次向项目添加同一个构建器可能会导致严重的性能问题和难以追踪的错误。
以下是`configure()`方法的代码:
```java
public void configure() throws CoreException {
IProjectDescription projectDesc = _project.getDescription();
ICommand[] buildSpec = projectDesc.getBuildSpec();
boolean hasBuilder = false;
for (int i = 0; i < buildSpec.length; ++i) {
if (buildSpec[i]
.getBuilderName()
.equals("com.triveratech.helloworld.project.helloworldbuilder")) {
hasBuilder = true;
break;
}
}
if (hasBuilder == false) {
ICommand newCommand = projectDesc.newCommand();
newCommand.setBuilderName("com.triveratech.helloworld.project.helloworldbuilder");
ICommand[] buildSpecs = new ICommand[buildSpec.length + 1];
System.arraycopy(buildSpec, 0, buildSpecs, 1, buildSpec.length);
buildSpecs[0] = newCommand;
projectDesc.setBuildSpec(buildSpecs);
_project.setDescription(projectDesc, null);
}
}
```
此方法会检查项目描述中的构建器列表,若指定构建器不存在,则创建新的构建器命令并添加到列表头部,最后更新项目描述。
#### 2. 构建器的主入口与Visitor模式
构建器的主入口是`build()`方法。这里要实现一个`Visitor`对象,并将其传递给`IResourceDelta`对象或`IProject`对象。在本例中,`Visitor`对象将作为构建器的内部类来实现。
Visitor模式是经典的23种设计模式之一,它代表要在另一个对象上执行的操作,并且是从目标对象内部调用的。因此,作为`IResourceVisitor`子类的`Visitor`会被传递给`IResourceDelta`对象或`IProject`对象,由这些对象决定何时调用`IResourceVisitor`对象。
以下是需要注意的几点:
- 构建器必须继承自`IncrementalProjectBuilder`(如`HelloWorldBuilder`)。
- 构建器有一次使用`startupOnIntialize()`进行初始化的机会。
当Eclipse启动且存在相应类型的项目时,构建器会被实例化;否则,直到创建合适类型的项目(如`HelloWorldProject`)时才会实例化。创建`HelloWorldProject`类型的项目时,构建器会被实例化并调用`build()`方法。项目发生任何更改(如添加或更改文件)时,构建器也会被调用。
为了方便调试,可以在构建器的构造函数和`build()`方法中添加打印语句:
```java
System.out.println("HelloWorldBuilder.constructor()");
System.out.println("HelloWorldBuilder.build()");
```
运行运行时工作台并创建名为`TestBuilder`的`HelloWorldProject`,在主机工作台的控制台窗口会看到相应输出。若添加文件,会再次看到`HelloWorldBuilder.build()`的输出。
#### 3. 实现IResourceVisitor和IResourceDeltaVisitor
接下来,实现`IResourceVisitor`对象和`IResourceDeltaVisitor`对象,并将其作为`HelloWorldBuilder`的私有内部类。
```java
private class HelloWorldVisitor implements IResourceVisitor {
public boolean visit(IResource resource) throws CoreException {
System.out.println("HelloWorldVisitor.visit()");
switch (resource.getType()) {
case IResource.PROJECT :
System.out.println("Project added: " + resource.getName());
break;
case IResource.FOLDER :
System.out.println("Folder added: " + resource.getName());
break;
case IResource.FILE :
System.out.println("File added: " + resource.getName());
break;
}
return true; // visit child resources
}
}
private class HelloWorldDeltaVisitor implements IResourceDeltaVisitor {
public boolean visit(IResourceDelta delta) throws CoreException {
System.out.println("HelloWorldDeltaVisitor.visit()");
String type = null;
switch (delta.getResource().getType()) {
case IResource.ROOT :
type = "ROOT";
break;
case IResource.PROJECT :
type = "Project";
break;
case IResource.FOLDER :
type = "Folder";
break;
case IResource.FILE :
type = "File";
break;
}
switch (delta.getKind()) {
case IResourceDelta.ADDED :
System.out.println(type + " added: " + delta.getResource().getName());
break;
case IResourceDelta.CHANGED :
System.out.println(type + " changed: " + delta.getResource().getName());
break;
case IResourceDelta.REMOVED :
System.out.println(type + " removed: " + delta.getResource().getName());
break;
}
return true; // visit child resources
}
}
```
`Hello
0
0
复制全文
相关推荐










