eclipse插件开发之扩展项目特性.docx
- 文档编号:8381587
- 上传时间:2023-01-30
- 格式:DOCX
- 页数:13
- 大小:162.31KB
eclipse插件开发之扩展项目特性.docx
《eclipse插件开发之扩展项目特性.docx》由会员分享,可在线阅读,更多相关《eclipse插件开发之扩展项目特性.docx(13页珍藏版)》请在冰豆网上搜索。
eclipse插件开发之扩展项目特性
实现项目特性插件
本节将从一个具体的实例出发,在Eclipse项目中添加用户自己的项目特性,扩展Eclipse的选项,让用户的插件具有一个产品的雏形。
项目扩展点
用户已经了解了如何在Eclipse中添加视图、编辑器、透视图菜单和工具栏按钮。
本节将要介绍如何加入自己的项目相关特性,其中包括向导、项目特性、构建器和属性页。
项目特性扩展点如图1所示。
图1项目特性扩展点
项目特性扩展点及实现类
“nature”为用户添加项目的特有特性,Eclipse中一个项目可以同时具有多个nature特性。
例如用户要在Java项目的基础上扩展自己的项目,加入了自己项目的特有特性。
这样JDT就能认识用户新建的项目,用户也可以判断此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.HelloWorldNature”。
扩展点在plugin.xml文件中的描述如下。
id="helloworldnature" point="org.eclipse.core.resources.natures">
nature扩展点的实现类要实现“IProjectNature”接口,并要实现“IProjectNature”的如下几个方法。
setProject(IProjectproject):
在nature类中保存当前项目。
getProject():
从nature中得到当前项目。
configure():
当nature被添加到项目中时,通过configure方法配置当前项目。
deconfigure():
当nature从项目删除时,通过deconfigure方法删除相应配置。
nature对应的实现类为“HelloWorldNature”,在此实现类中主要功能是通过configure方法配置当前项目的构建器,代码如例程1所示。
例程1HelloWorldNature.java
publicclassHelloWorldNatureimplementsIProjectNature{
privateIProject_project;
//配置Nature
publicvoidconfigure()throwsCoreException{
System.out.println("configure");
IProjectDescriptionprojectDesc=_project.getDescription();
ICommand[]buildSpec=projectDesc.getBuildSpec();
booleanhasBuilder=false;
//遍历项目的构建器
for(inti=0;i if(buildSpec[i] .getBuilderName() .equals("com.free.proj.helloproject.helloworldbuilder")){ hasBuilder=true; System.out.println("true"); break; } } if(hasBuilder==false){ System.out.println("false"); ICommandnewCommand=projectDesc.newCommand(); newCommand.setBuilderName("com.free.proj.helloproject.helloworldbuilder"); ICommand[]buildSpecs=newICommand[buildSpec.length+1]; System.arraycopy(buildSpec,0,buildSpecs,1,buildSpec.length); buildSpecs[0]=newCommand; projectDesc.setBuildSpec(buildSpecs); _project.setDescription(projectDesc,null); } } publicvoiddeconfigure()throwsCoreException{ } //获得Nature所关联的项目 publicIProjectgetProject(){ System.out.println("getProject"); return_project; } //设置Nature所关联的项目 publicvoidsetProject(IProjectproject){ System.out.println("setProject"); _project=project; } } 提示: 此外构建器的ID号为“com.free.proj.helloproject.helloworldbuilder”,ID号为插件ID号加构建器ID号,在Eclipse中和产品及项目相关的ID都采用这种方式。 构建器扩展点及实现类 Builder为项目的构建器,通过构建器能实现项目的增量构建和完全构建。 在插件中添加Builder扩展点,步骤如下。 1.在插件清单文件的Extensions页中添加“org.eclipse.core.resources.builders”扩展点,设置ID属性为“helloworldbuilder”,name属性为“helloworld,bulder”。 2.在“org.eclipse.core.resources.builders”节点下添加builder子节点。 3.在builder节点下添加run节点,设置实现类class为“com.free.project.HelloWorldBuilder”。 扩展点在plugin.xml文件中的描述如下。 id="helloworldbuilder"name="helloworld,bulder" point="org.eclipse.core.resources.builders"> builder扩展点的实现类继承于“IncrementalProjectBuilder”类,并实现了两个资源监听器。 当项目中资源发生变化的时候调用构建,本例中只打印出相应的数据,并没有实现构建的动作,当用户需要构建时可以替换相应代码,例如编译文件等。 代码如例程2所示。 例程2HelloWorldBuilder.java publicclassHelloWorldBuilderextendsIncrementalProjectBuilder{ //实现资源监听器 privateclassHelloWorldVisitorimplementsIResourceVisitor{ publicbooleanvisit(IResourceresource)throwsCoreException{ System.out.println("****HelloWorldVisitor.visit()****"); switch(resource.getType()){ caseIResource.PROJECT: System.out.println("Projectadded: "+resource.getName()); break; caseIResource.FOLDER: System.out.println("Folderadded: "+resource.getName()); break; caseIResource.FILE: System.out.println("Fileadded: "+resource.getName()); break; } returntrue; } } //实现资源监听器 privateclassHelloWorldDeltaVisitorimplementsIResourceDeltaVisitor{ publicbooleanvisit(IResourceDeltadelta)throwsCoreException{ System.out.println("****HelloWorldDeltaVisitor.visit()****"); Stringtype=null; switch(delta.getResource().getType()){ caseIResource.ROOT: type="ROOT"; break; caseIResource.PROJECT: type="Project"; break; caseIResource.FOLDER: type="Folder"; break; caseIResource.FILE: type="File"; break; } switch(delta.getKind()){ caseIResourceDelta.ADDED: System.out.println(type+"added: "+delta.getResource().getName()); break; caseIResourceDelta.CHANGED: System.out.println(type+"changed: " +delta.getResource().getName()); break; caseIResourceDelta.REMOVED: System.out.println(type+"removed: " +delta.getResource().getName()); break; } returntrue; } } publicHelloWorldBuilder(){ System.out.println("HelloWorldBuilder.constructor()"); } //开始构建 protectedIProject[]build(intkind,Mapargs,IProgressMonitormonitor) throwsCoreException{ System.out.println("HelloWorldBuilder.build()"); //当构建类型不同时,添加不同的资源监听器 if(kind==IncrementalProjectBuilder.FULL_BUILD){ System.out.println("FULL_BUILD"); getProject().accept(newHelloWorldVisitor()); }else{ IResourceDeltadelta=getDelta(getProject()); if(delta==null){ System.out.println("AUTO_BUILD"); getProject().accept(newHelloWorldVisitor()); }else{ System.out.println("INCREMENTAL_BUILD"); delta.accept(newHelloWorldDeltaVisitor()); } } returnnull; } } 在用户建立的项目中,如果资源发生改变,builder会调用“HelloWorldDeltaVisitor”类进行增量构建处理,当清除项目时会调用“HelloWorldVisitor”类进行完全构建处理。 插件运行后,当用户选择自定义项目时,用户可以打开项目属性窗口看到“helloworld,builder”构建器,如图2所示。 图2项目属性窗口 工程向导扩展点及实现类 用户如果要在项目中添加自定义的资源,采用Eclipse中的向导是最好的方式。 项目也是资源的一种,一般来说如果对新建的资源有输入条件的要求,都采用向导的方式。 例如新建Java项目,要输入项目名称和构建路径等。 在插件中添加newWizard扩展点,步骤如下。 1.在插件清单文件的Extensions页中添加“org.eclipse.ui.newWizards”扩展点。 2.在“org.eclipse.ui.newWizards”节点下添加category子节点,设置ID属性为“com.free.project.wizard.category”,name属性为“HelloWorldProjectCategory”。 3.在“org.eclipse.ui.newWizards”节点下添加wizard节点,设置实现类class为“com.free.project.HelloWorldWizard”,ID属性为“com.free.project.wizard”。 扩展点在plugin.xml文件中的描述如下: point="org.eclipse.ui.newWizards"> name="HelloWorldProjectCategory" id="com.free.project.wizard.category"/> class="com.free.project.HelloWorldWizard" category="com.free.project.wizard.category" name="HelloWorldProjectWizard" id="com.free.project.wizard"/> newWizards扩展点的实现类继承于“org.eclipse.jface.wizard.Wizard”类,并实现了“org.eclipse.ui.INewWizard”接口。 在实现类中,通过addPages方法添加向导页到当前向导,通过performFinish方法完成向导的结束。 实现类“HelloWorldWizard”的代码如例程3所示。 例程3HelloWorldWizard.java publicclassHelloWorldWizardextendsWizardimplementsINewWizard{ privateWizardNewProjectCreationPage_page; publicHelloWorldWizard(){ super(); } //添加向导页 publicvoidaddPages(){ _page=newWizardNewProjectCreationPage("page1"); _page.setDescription("Page1ofHelloworldNewWizard"); _page.setTitle("HelloWorldWizard"); addPage(_page); } //完成向导 publicbooleanperformFinish(){ booleanresult=false; System.out.println("projectpage.name="+_page.getProjectName()); System.out.println("projectpage.path="+_page.getLocationPath()); IProjectproject=_page.getProjectHandle(); try{ project.create(null); result=true; }catch(CoreExceptione){ e.printStackTrace(); } IProjectDescriptionprojectDesc=null; try{ project.open(null); projectDesc=project.getDescription(); }catch(CoreExceptione1){ e1.printStackTrace(); } String[]natureIds=projectDesc.getNatureIds(); String[]newNatureIds=newString[natureIds.length+1]; System.arraycopy(natureIds,0,newNatureIds,0,natureIds.length); newNatureIds[natureIds.length]="com.free.proj.helloproject.helloworldnature"; projectDesc.setNatureIds(newNatureIds); try{ project.setDescription(projectDesc,null); }catch(CoreExceptione2){ e2.printStackTrace(); } returnresult; } publicvoidinit(IWorkbenchworkbench,IStructuredSelectionselection){ } } 本例程中没有实现用户定义的向导页,而是用Eclipse下的“WizardNewProjectCreationPage”页来作为向导页。 用户也可以定义向导页,如果有兴趣可以参考多页签编辑器中的例子源代码,多页签编辑器插件已经实现了向导。 本例中可以通过新建菜单新建用户定义的项目,向导对话框如图3所示。 图3向导对话框 首选项页扩展点及实现类 在用户定义的项目中,一般都会有相应的参数配制。 例如说Tomcat要配置相应的Web服务器,JDT可以配置代码的风格、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文件中的描述如下。 point="org.eclipse.ui.preferencePages"> class="com.free.project.WorkbenchPreferencePage" name="HelloWorld" id="com.free.project.page"/> preferencePages扩展点的实现类继承于“org.eclipse.jface.preference.PreferencePage”类,并实现了“org.eclipse.ui.IWorkbenchPreferencePage”接口。 在实现类中,通过createContents方法构建自己的属性页,通过performOk方法完成向导的结束。 实现类“WorkbenchPreferencePage”的代码如例程4所示。 例程4WorkbenchPreferencePage.java publicclassWorkbenchPreferencePageextendsPreferencePageimplements IWorkbenchPreferencePage{ privateText_greeting; publicWorkbenchPreferencePage(){ super(); } publicWorkbenchPreferencePage(Stringtitle){ super(title); } publicWorkbenchPreferencePage(Stringtitle,ImageDescriptorimage){ super(title,image); } protectedControlcreateContents(Compositeparent){ Labellabel=newLabel(parent,SWT.CENTER); label.setText("Greeting"); _greeting=newText(parent,SWT.SINGLE|SWT.BORDER); returnparent; } protectedIPreferenceStoredoGetPreferenceStore(){ System.out.println("WorkbenchPreferencePage.doGetPreferenceStore()"); returnHelloWorldPlugin.getDefault().getPreferenceStore(); } publicvoidinit(IWorkbenchworkbench){ } protectedvoidperformDefaults(){ System.out.println("WorkbenchPreferencePage.perform
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- eclipse 插件 开发 扩展 项目 特性
![提示](https://static.bdocx.com/images/bang_tan.gif)