Eclipse插件开发Nature篇

  • 介绍
本文将介绍如何创建自己的项目特性(nature)。什么是项目特性呢?比如你新建一个Java工程,这个项目的图标右上角就会有个J的标志;如果是个Maven工程呢,在左上角还有有一个M标志。简单来说,项目特性就是用来对不同项目的区分。Nature会关联Builder,那什么是Builder呢? 当你保存Java源文件的同时,就会触发编译动作。这个事情就是Builder来做的。所以说,你的Java工程到底能不能成功编译,责任都落在了Nature和Builder的配合上。比如:你导入了Maven工程,却始终无法生成Maven的依赖,所以也无法编译,这时候你就可以看看你的工程上有没有关联Maven对应的Nature。
  • 步骤
在plugin.xml文件中添加如下配置: [codesyntax lang="xml"]
<extension
 point="org.eclipse.core.resources.natures">
 <runtime>
 	<run class="org.suren.littlebird.SuRenProjectNature" />
 </runtime>
</extension>
<extension id="suren.builder" name="SuRenBuilder"
 point="org.eclipse.core.resources.builders">
 <builder>
 	<run class="org.suren.littlebird.builder.SuRenBuilder" />
 </builder>
</extension>
[/codesyntax] 实现org.eclipse.core.resources.IProjectNature接口中的方法 [codesyntax lang="java"]
package org.suren.littlebird.nature;

import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IProjectNature;
import org.eclipse.core.runtime.CoreException;

/**
 * @author suren
 * @date 2015-4-2
 *
 * http://surenpi.com
 */
public class SuRenProjectNature implements IProjectNature
{
	private static final String SUREN_BUILDER_ID = "suren.builder";

	/**
	 * 工程对象
	 */
	private IProject project;

	/**
	 * 当nature被添加到工程中时调用,配置当前工程
	 */
	@Override
	public void configure() throws CoreException
	{
		if(project == null)
		{
			System.err.println("can not get project object when nature configure.");

			return;
		}

		IProjectDescription projectDesc = project.getDescription();
		ICommand[] buildSpec = projectDesc.getBuildSpec();

		boolean builderSetup = false;
		for(ICommand cmd : buildSpec)
		{
			if(cmd.getBuilderName().equals(SUREN_BUILDER_ID))
			{
				builderSetup = true;
				break;
			}
		}

		if(!builderSetup)
		{
			ICommand newCmd = projectDesc.newCommand();
			newCmd.setBuilderName(SUREN_BUILDER_ID);

			ICommand[] newBuildSpec = new ICommand[buildSpec.length + 1];
			newBuildSpec[0] = newCmd;

			System.arraycopy(buildSpec, 0, newBuildSpec, 1, buildSpec.length);
			projectDesc.setBuildSpec(newBuildSpec);
			project.setDescription(projectDesc, null);
		}
	}

	/**
	 * 当nature从当前工程中取消时,删除配置
	 */
	@Override
	public void deconfigure() throws CoreException
	{
		if(project == null)
		{
			System.err.println("can not get project object when nature deconfigure.");

			return;
		}

		IProjectDescription projectDesc = project.getDescription();
		ICommand[] buildSpec = projectDesc.getBuildSpec();

		int prepareToRemove = -1;
		for(int i = 0; i < buildSpec.length; i++)
		{
			ICommand cmd = buildSpec[i];
			if(cmd.getBuilderName().equals(SUREN_BUILDER_ID))
			{
				prepareToRemove = i;
				break;
			}
		}

		if(prepareToRemove != -1)
		{
			ICommand[] newBuildSpec = new ICommand[buildSpec.length - 1];

			System.arraycopy(buildSpec, 0, newBuildSpec, 0, prepareToRemove + 1);
			System.arraycopy(buildSpec, prepareToRemove + 1,
					newBuildSpec, prepareToRemove, buildSpec.length - prepareToRemove - 1);
			projectDesc.setBuildSpec(newBuildSpec);
			project.setDescription(projectDesc, null);
		}
	}

	/**
	 * 从nature中获取当前工程
	 */
	@Override
	public IProject getProject()
	{
		return this.project;
	}

	/**
	 * 在nature中保存当工程
	 */
	@Override
	public void setProject(IProject project)
	{
		this.project = project;
	}

}
[/codesyntax] 继承org.eclipse.core.resources.IncrementalProjectBuilder [codesyntax lang="java"]
package org.suren.littlebird.builder;

import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;

/**
 * @author suren
 * @date 2015-4-2
 *
 * http://surenpi.com
 */
public class SuRenBuilder extends IncrementalProjectBuilder
{

	@Override
	protected IProject[] build(int kind, Map<String, String> args,
			IProgressMonitor pro) throws CoreException
	{
		System.out.println("prepare to suren build.");

		switch(kind)
		{
			case IncrementalProjectBuilder.FULL_BUILD:
				System.out.println("full suren build.");
				getProject().accept(new SuRenVistor());

				break;
			default:
				IResourceDelta delta = getDelta(getProject());
				if(delta == null)
				{
					getProject().accept(new SuRenVistor());
				}
				else
				{
					delta.accept(new SuRenDeltaVisitor());
				}

				break;
		}

		System.out.println("suren build done.");

		return null;
	}

	private class SuRenVistor implements IResourceVisitor
	{

		@Override
		public boolean visit(IResource resource) throws CoreException
		{
			System.out.println("SuRenVistor");

			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;
		}
	}

	private class SuRenDeltaVisitor implements IResourceDeltaVisitor
	{

		@Override
		public boolean visit(IResourceDelta delta) throws CoreException
		{
			IResource resource = delta.getResource();
			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;
			}

			switch(delta.getKind())
			{
				case IResourceDelta.ADDED:
					System.out.println("ADDED : " + resource.getName());
					break;
				case IResourceDelta.CHANGED:
					System.out.println("CHANGED : " + resource.getName());
					break;
				case IResourceDelta.REMOVED:
					System.out.println("REMOVED : " + resource.getName());
					break;
			}

			return true;
		}
	}

}
[/codesyntax]
  • 工程特性图标
[codesyntax lang="xml"]
<extension
	point="org.eclipse.ui.ide.projectNatureImages">
	<image
		id="suren.nature.image"
		icon="icons/suren.gif"
		natureId="suren.nature">
	</image>
</extension>
[/codesyntax] 图标尺寸:7*6 在plugin.xml中加入上面的代码后,工程图标的右上角就会显示你的图标。
  • 参考
还没看过“Eclipse插件开发HelloWorld篇”?那赶紧先看看吧。 不知道Nature和Builder的实际作用?嗯,你要是再整一个“向导”出来就明白了。 http://blog.csdn.net/soszou/article/details/8030111

你可能感兴趣的:(java,eclipse,开发,插件,nature)