1、eclipse插件开发之扩展项目特性实现项目特性插件本节将从一个具体的实例出发,在Eclipse项目中添加用户自己的项目特性,扩展Eclipse的选项,让用户的插件具有一个产品的雏形。项目扩展点用户已经了解了如何在Eclipse中添加视图、编辑器、透视图菜单和工具栏按钮。本节将要介绍如何加入自己的项目相关特性,其中包括向导、项目特性、构建器和属性页。项目特性扩展点如图1所示。图1 项目特性扩展点项目特性扩展点及实现类“nature”为用户添加项目的特有特性,Eclipse中一个项目可以同时具有多个nature特性。例如用户要在Java项目的基础上扩展自己的项目,加入了自己项目的特有特性。这样J
2、DT就能认识用户新建的项目,用户也可以判断此Java项目是否是自己的扩展。一般来说,用户期望用到JDT的强大功能,就要使自己的项目从Java项目扩展过来。在插件中添加nature扩展点,步骤如下。1. 在插件清单文件的Extensions页中添加“org.eclipse.core.resources.natures”扩展点,设置ID属性为“helloworldnature”。2. 在“org.eclipse.core.resources.natures”节点下添加runtime子节点。3. 在runtime节点下添加run节点,设置实现类class为“com.free.project. Hel
3、loWorldNature”。 扩展点在plugin.xml文件中的描述如下。nature扩展点的实现类要实现“IProjectNature”接口,并要实现“IProjectNature”的如下几个方法。setProject(IProject project):在nature类中保存当前项目。getProject():从nature中得到当前项目。configure():当nature被添加到项目中时,通过configure方法配置当前项目。deconfigure():当nature从项目删除时,通过deconfigure方法删除相应配置。nature对应的实现类为“HelloWorldNat
4、ure”,在此实现类中主要功能是通过configure方法配置当前项目的构建器,代码如例程1所示。例程1 HelloWorldNature.javapublic class HelloWorldNature implements IProjectNature private IProject _project;/配置Naturepublic void configure() throws CoreException System.out.println(configure);IProjectDescription projectDesc = _project.getDescription();
5、ICommand buildSpec = projectDesc.getBuildSpec();boolean hasBuilder = false;/遍历项目的构建器for (int i = 0; i buildSpec.length; +i) if (buildSpeci.getBuilderName().equals(com.free.proj.helloproject.helloworldbuilder) hasBuilder = true;System.out.println(true);break;if (hasBuilder = false) System.out.println
6、(false);ICommand newCommand = projectDesc.newCommand();newCommand.setBuilderName(com.free.proj.helloproject.helloworldbuilder); ICommand buildSpecs = new ICommandbuildSpec.length + 1;System.arraycopy(buildSpec, 0, buildSpecs, 1, buildSpec.length);buildSpecs0 = newCommand;projectDesc.setBuildSpec(bui
7、ldSpecs);_project.setDescription(projectDesc, null);public void deconfigure() throws CoreException /获得Nature所关联的项目public IProject getProject() System.out.println(getProject);return _project;/设置Nature所关联的项目public void setProject(IProject project) System.out.println(setProject);_project = project;提示:此
8、外构建器的ID号为“com.free.proj.helloproject.helloworldbuilder”,ID号为插件ID号加构建器ID号,在Eclipse中和产品及项目相关的ID都采用这种方式。构建器扩展点及实现类Builder为项目的构建器,通过构建器能实现项目的增量构建和完全构建。在插件中添加Builder扩展点,步骤如下。1. 在插件清单文件的Extensions页中添加“org.eclipse.core.resources.builders”扩展点,设置ID属性为“helloworldbuilder”,name属性为“hello world, bulder”。2. 在“org.
9、eclipse.core.resources.builders”节点下添加builder子节点。3. 在builder节点下添加run节点,设置实现类class为“com.free.project. HelloWorldBuilder”。 扩展点在plugin.xml文件中的描述如下。builder扩展点的实现类继承于“IncrementalProjectBuilder”类,并实现了两个资源监听器。当项目中资源发生变化的时候调用构建,本例中只打印出相应的数据,并没有实现构建的动作,当用户需要构建时可以替换相应代码,例如编译文件等。代码如例程2所示。例程2 HelloWorldBuilder.j
10、avapublic class HelloWorldBuilder extends IncrementalProjectBuilder /实现资源监听器private class HelloWorldVisitor implements IResourceVisitor public boolean visit(IResource resource) throws CoreException System.out.println(* HelloWorldVisitor.visit() *);switch (resource.getType() case IResource.PROJECT:Sy
11、stem.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 HelloWorldDeltaVisitor implements IResourceDelt
12、aVisitor 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 = Fold
13、er;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:Sy
14、stem.out.println(type + removed: + delta.getResource().getName();break;return true;public HelloWorldBuilder() System.out.println(HelloWorldBuilder.constructor();/开始构建protected IProject build(int kind, Map args, IProgressMonitor monitor)throws CoreException System.out.println(HelloWorldBuilder.build(
15、);/当构建类型不同时,添加不同的资源监听器if (kind = IncrementalProjectBuilder.FULL_BUILD) System.out.println(FULL_BUILD);getProject().accept(new HelloWorldVisitor(); else IResourceDelta delta = getDelta(getProject();if (delta = null) System.out.println(AUTO_BUILD);getProject().accept(new HelloWorldVisitor(); else Syst
16、em.out.println(INCREMENTAL_BUILD);delta.accept(new HelloWorldDeltaVisitor();return null;在用户建立的项目中,如果资源发生改变,builder会调用“HelloWorldDeltaVisitor”类进行增量构建处理,当清除项目时会调用“HelloWorldVisitor”类进行完全构建处理。插件运行后,当用户选择自定义项目时,用户可以打开项目属性窗口看到“hello world, builder”构建器,如图2所示。图2 项目属性窗口工程向导扩展点及实现类用户如果要在项目中添加自定义的资源,采用Eclipse
17、中的向导是最好的方式。项目也是资源的一种,一般来说如果对新建的资源有输入条件的要求,都采用向导的方式。例如新建Java项目,要输入项目名称和构建路径等。在插件中添加newWizard扩展点,步骤如下。1. 在插件清单文件的Extensions页中添加“org.eclipse.ui.newWizards”扩展点。2. 在“org.eclipse.ui.newWizards”节点下添加category子节点,设置ID属性为“com.free.project.wizard.category”,name属性为“HelloWorld Project Category”。3. 在“org.eclipse.
18、ui.newWizards”节点下添加wizard节点,设置实现类class为“com.free.project.HelloWorldWizard”,ID属性为“com.free.project.wizard”。扩展点在plugin.xml文件中的描述如下:newWizards扩展点的实现类继承于“org.eclipse.jface.wizard.Wizard”类,并实现了“org.eclipse.ui.INewWizard”接口。在实现类中,通过addPages方法添加向导页到当前向导,通过performFinish方法完成向导的结束。实现类“HelloWorldWizard”的代码如例程3
19、所示。例程3 HelloWorldWizard.javapublic class HelloWorldWizard extends Wizard implements INewWizard private WizardNewProjectCreationPage _page;public HelloWorldWizard() super();/添加向导页public void addPages() _page = new WizardNewProjectCreationPage(page1);_page.setDescription(Page 1 of Helloworld New Wizar
20、d);_page.setTitle(HelloWorld Wizard);addPage(_page);/完成向导public boolean performFinish() boolean result = false;System.out.println(project page.name = + _page.getProjectName();System.out.println(project page.path = + _page.getLocationPath();IProject project = _page.getProjectHandle();try project.crea
21、te(null);result = true; catch (CoreException e) e.printStackTrace();IProjectDescription projectDesc = null;try project.open(null);projectDesc = project.getDescription(); catch (CoreException e1) e1.printStackTrace();String natureIds = projectDesc.getNatureIds();String newNatureIds = new Stringnature
22、Ids.length + 1;System.arraycopy(natureIds, 0, newNatureIds, 0, natureIds.length);newNatureIdsnatureIds.length = com.free.proj.helloproject.helloworldnature; projectDesc.setNatureIds(newNatureIds);try project.setDescription(projectDesc, null); catch (CoreException e2) e2.printStackTrace();return resu
23、lt;public void init(IWorkbench workbench, IStructuredSelection selection) 本例程中没有实现用户定义的向导页,而是用Eclipse下的“WizardNewProjectCreationPage”页来作为向导页。用户也可以定义向导页,如果有兴趣可以参考多页签编辑器中的例子源代码,多页签编辑器插件已经实现了向导。本例中可以通过新建菜单新建用户定义的项目,向导对话框如图3所示。图3 向导对话框首选项页扩展点及实现类在用户定义的项目中,一般都会有相应的参数配制。例如说Tomcat要配置相应的Web服务器,JDT可以配置代码的风格、
24、JDK和JRE等。在插件中可以添加preferencePages扩展点,实现项目参数的配置,步骤如下。1. 在插件清单文件的Extensions页中添加“org.eclipse.ui.preferencePages”扩展点。2. 在“org.eclipse.ui.preferencePages”节点下添加page子节点,设置节点的ID属性为“com.free.project.page”,name属性为“HelloWorld”,class属性为“com.free.project.Workbench- PreferencePage”。扩展点在plugin.xml文件中的描述如下。preferenc
25、ePages扩展点的实现类继承于“org.eclipse.jface.preference.PreferencePage”类,并实现了“org.eclipse.ui.IWorkbenchPreferencePage”接口。在实现类中,通过createContents方法构建自己的属性页,通过performOk方法完成向导的结束。实现类“WorkbenchPreferencePage”的代码如例程4所示。例程4 WorkbenchPreferencePage.javapublic class WorkbenchPreferencePage extends PreferencePage imple
26、mentsIWorkbenchPreferencePage private Text _greeting;public WorkbenchPreferencePage() super();public WorkbenchPreferencePage(String title) super(title);public WorkbenchPreferencePage(String title, ImageDescriptor image) super(title, image);protected Control createContents(Composite parent) Label lab
27、el = new Label(parent, SWT.CENTER);label.setText(Greeting);_greeting = new Text(parent, SWT.SINGLE | SWT.BORDER);return parent;protected IPreferenceStore doGetPreferenceStore() System.out.println(WorkbenchPreferencePage.doGetPreferenceStore();return HelloWorldPlugin.getDefault().getPreferenceStore()
28、;public void init(IWorkbench workbench) protected void performDefaults() System.out.println(WorkbenchPreferencePage.performDefaults();IPreferenceStore prefStore = getPreferenceStore();prefStore.setValue(HelloWorldPlugin.GREETING, world);HelloWorldPlugin.getDefault().savePluginPreferences();_greeting.setText(prefStore.getString(HelloWorldPlugi
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2