使用Eclipse向导进行快速开发插件_第1页
使用Eclipse向导进行快速开发插件_第2页
使用Eclipse向导进行快速开发插件_第3页
使用Eclipse向导进行快速开发插件_第4页
使用Eclipse向导进行快速开发插件_第5页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、使用eclipse向导进行快速开发插件入门 本文将演示如何使用向导把新文件添加到已有 eclipse 项目中。当内置模板功能不足时,eclipse 向导是定义可重复文件类型模板的优秀方法。阅读完本文后,您应当能够在 eclipse 中实现自己的向导以创建新文件。为了发挥本文的最大功效,您必须熟悉如何构建 java 编程语言类,并且还应当熟悉继承和使用接口。您应当能够启动 eclipse,但是本文假定您并不精通 eclipse。eclipse 向导概览我喜欢 eclipse ide 的很多特性。其中之一就是可扩展性。通过添加提供功能的插件(包括自动创建类、接口、项目、其他资源的向导),可以轻松地

2、自定义 ide。此特性对于大型企业来说非常重要,在这类企业中可以基于 eclipse ide 流线化地构建和分发插件,使许多人都可以自动利用功能。在任何企业中,让团队以相同的样式构建应用程序可以提供许多优点。如果应用程序是以一致的样式构建的,则应用程序将更易于维护。一致性可以帮助减少错误。此外,由于应用程序是以相同的样式构建的,因此团队成员可以更轻松地完成一个又一个项目。能够为 eclipse 框架创建自定义向导使企业可以构建企业专有的向导,这些向导将给团队提供使用一致的最佳实践创建应用程序的机会。创建新向导本文中构建的自定义向导位于 eclipse 的插件项目中。开始创建自定义向导十分简单,

3、这要感谢为您开始编写代码提供前期帮助的其他向导。在接下来的步骤中,将使用 plug-in project 向导创建插件的开头部分。要构建新插件项目,请执行以下步骤:选择 file > new > project 以选择项目向导。在 plug-in development 下选择 plug-in project,如下所示:单击 next 前进到下一步。图 1. 选择插件项目添加项目名称(在本文中,该名称为 examplewizard(不是那么有创造性),如图 2 所示)。如果没有特殊原因,请使用默认位置。单击 next。图 2. 新插件项目在 plug-in version 中输入版

4、本号,并添加插件名称和插件提供者的名称(可能是您,也可能是协作的团队)。请一定要更新 activator 的包名称,它默认为小写版本的项目名称。最好使用符合您公司标准的包名称:例如 com.example.eclipse.wizards。当您填写完下图中所示的信息后,单击 next。图 3. 选择插件项目选择 custom plug-in wizard,因为使用此选项可以让您对所包括的组件进行微调。如果您以前从未创建过新插件项目,那么现在最好看看其他模板的描述以了解可用信息。单击 next。在 template selection 窗口中,单击 deselect all 取消选中所有选项。然后

5、,选择 new file wizard,如下所示。单击 next。图 4. 选择模板eclipse 向导将给您提示一些关于正在创建的新向导的信息(参见图 5)。确保更新包名称,理想情况下将其更新为 activator 所使用的相同名称 (com.example.eclipse.wizards)。将 wizard category name 更新为新向导文件夹的名称。该值的使用方法与 图 1 中的 plug-in development 类别相同。wizard class name 是从 wizard 继承的类的 java 类名,该类将实现 inewwizard 接口。wizard page c

6、lass name 将扩展 wizardpage 类。图 5. new wizard options 窗口单击 finish。eclipse 将给新项目添加必要的类和库。虽然还没完成,但是已经有了很好的开端并且准备好开始在向导背后添加一些实现。wizard 类和 inewwizard 接口现在项目中有三个类:newxhtmlfilewizard、newxhtmlfilewizardpage 和 activator。下面的部分将处理 newxhtmlfilewizard 类。该类如清单 1 所示,不过没有显示方法中的所有代码。清单 1. newxhtmlfilewizard 类public cl

7、ass newxhtmlfilewizard extends wizard implements inewwizard private newxhtmlfilewizardpage page;private iselection selection;public newxhtmlfilewizard() / snipped.public void addpages() / snipped.public boolean performfinish() / snipped.private void dofinish(/ snipped.private inputstream opencontent

8、stream() / snipped.private void throwcoreexception(string message) throws coreexception / snipped.public void init(iworkbench workbench, istructuredselection selection) / snipped.实现 inewwizard 接口必须使用最后一个方法 init()。接下来,本文将介绍此方法以及此模板中自动包括的其余方法。addpages() 方法addpages() 方法将把页面添加到向导中。清单 2 中所示的方法将把单个页面添加到向导

9、 newxhtmlfilewizardpage 中。清单 2. addpages() 方法将把页面添加到向导中/* * adding the page to the wizard. */public void addpages() page = new newxhtmlfilewizardpage(selection);/ you can add more pages here.addpage(page);newxhtmlfilewizardpage 类包含为用户提供指定页面名称功能的控件。您可以稍后把控件添加到页面中,使最终用户可以输入更多信息。performfinish() 方法当用户单击

10、向导中的 finish 按钮时将调用 performfinish() 方法。在执行一些检查之后,它将使用 irunnablewithprogress 接口调用 dofinish() 方法。使用此接口意味着在执行 dofinish() 方法时(在本例中需要花很长时间运行)不必编写显示进度条的所有 ui 元素。下面完整地列出了该方法。清单 3. performfinish() 方法/* * this method is called when 'finish' button is pressed in * the wizard. we will create an operatio

11、n and run it * using wizard as execution context. */public boolean performfinish() final string containername = page.getcontainername();final string filename = page.getfilename();irunnablewithprogress op = new irunnablewithprogress() public void run(iprogressmonitor monitor) throws invocationtargete

12、xception try dofinish(containername, filename, monitor); catch (coreexception e) throw new invocationtargetexception(e); finally monitor.done();try getcontainer().run(true, false, op); catch (interruptedexception e) return false; catch (invocationtargetexception e) throwable realexception = e.gettar

13、getexception();messagedialog.openerror(getshell(), "error", realexception.getmessage();return false;return true;dofinish() 方法 如下所示,dofinish() 方法将创建新文件并通过 ide 中的编辑器打开新文件。将调用 opencontentstream() 方法以获得给新文件填充内容的输入流。清单 4. 初始的 dofinish() 方法/* * the worker method. it will find the container, crea

14、te the * file if missing or just replace its contents, and open * the editor on the newly created file. */private void dofinish(string containername,string filename,iprogressmonitor monitor)throws coreexception / create a sample filemonitor.begintask("creating " + filename, 2);iworkspacero

15、ot root = resourcesplugin.getworkspace().getroot();iresource resource = root.findmember(new path(containername);if (!resource.exists() | !(resource instanceof icontainer) throwcoreexception("container "" + containername + "" does not exist.");icontainer container = (ico

16、ntainer) resource;final ifile file = container.getfile(new path(filename);try inputstream stream = opencontentstream();if (file.exists() file.setcontents(stream, true, true, monitor); else file.create(stream, true, monitor);stream.close(); catch (ioexception e) monitor.worked(1);monitor.settaskname(

17、"opening file for editing.");getshell().getdisplay().asyncexec(new runnable() public void run() iworkbenchpage page =platformui.getworkbench().getactiveworkbenchwindow().getactivepage();try ide.openeditor(page, file, true); catch (partinitexception e) );monitor.worked(1);opencontentstream(

18、) 方法如下所示,opencontentstream() 方法将返回包含生成的静态字符串作为模板一部分的 bytearrayinputstream。对于本文,字符串将被替换为模板文件的内容。此方法中的代码是首先必须更改的,这样才能在创建时允许把更多有用的内容添加到新文件中。清单 5. opencontentstream() 方法/* * initialize file contents with a sample text. */private inputstream opencontentstream() string contents ="this is the initial

19、file contents for *.html " +"file that should be word-sorted in the preview " +"page of the multi-page editor"return new bytearrayinputstream(contents.getbytes();添加基本内容新文件的内容不使用静态字符串值,您可以使用 getresourceasstream() 方法把文件的内容载入到 inputstream 中,并且 dofinish() 方法可以用它来填充新文件。请做出如下所示的修改

20、。清单 6. 从资源获得输入流/* * initialize the file contents to contents of the * given resource. */private inputstream opencontentstream() return this.getclass().getresourceasstream("templates/index-xhtml-template.resource");index-xhtml-template.resource 文件中是有效的可扩展超文本标记语言(extensible hypertext markup

21、language,xhtml)v1.0 strict web 页面。它有针对一组模拟企业样式表和 javascript 文件的一些基本标记和点。该文件列于清单 7 中。此文件与 newxhtmlfilewizard 类在同一个包中,因此在本文中此文件位于 com.example.eclipse.wizards 包中。如果需要将文件放在其他包中,则可以像访问目录内的文件一样访问它(即,com.example.resources 是 /com/example/resources)。清单 7. index-xhtml-template.resource 文件<!doctype html pub

22、lic "-/w3c/dtd xhtml 1.0 strict/en" "/tr/xhtml1/dtd/xhtml1-strict.dtd"><html xmlns="/1999/xhtml"><head><title>this is an e web page</title><link rel="stylesheet" href=" type="text/css"

23、; /><script type="text/javascript" language="javascript" src="</script></head><body><div id="search"><form id="searchform" name="searchform" action="<input type="text" name="searchtext"

24、; /> <input type="button"value="search e" /></form></div><div id="mainmenu"><p class="menu">main menu</p><ul class="mainmenu"><li><a href="<li><a href=" 1</a></li>&l

25、t;/ul></div><!- put the body of your page here -><div id="body"></div></body></html>现在,您可以运行 eclipse 插件来查看该插件的内容。测试新向导在 eclipse 创建向导所使用的三个类之后,您可以在阅读本文的过程中随时启动另一个 eclipse 实例来运行和测试插件。要启动插件项目,请在项目上右击,并选择 run as > eclipse application,如图 6 所示。eclipse 的新

26、实例将启动。图 6. 将项目作为 eclipse 应用程序来运行现在需要创建包含新文件的临时项目。项目的名称无关紧要 诸如 “temp” 之类的名称即可。当新项目已在工作区中后,请通过选择 file > new > other 来添加新模板。如果一切按预期运行正常,则新类别将列于您为向导定义的类别下的 select a wizard 窗口中。我使用了 e 企业模板,如下所示:图 7. 使用新模板当您完成向导的其余部分后,eclipse 将创建包含定义内容的新文件。如果此简单功能就是模板所需的全部功能,则可以止于此处。但是,可能还需要提示用户提供一些用来整合文件内容的输入。自定义向导

27、页面初始 newxhtmlfilewizardpage 的表单中只有两个控件:一个用于容器(项目或文件夹)的名称,而另一个用于创建新文件时使用的名称。createcontrol() 方法(完整的方法代码如清单 8 所示)负责创建这些控件和将其添加到对话框中。清单 8. createcontrol() 方法/* * see idialogpage#createcontrol(composite) */public void createcontrol(composite parent) composite container = new composite(parent, swt.null);g

28、ridlayout layout = new gridlayout();container.setlayout(layout);layout.numcolumns = 3;layout.verticalspacing = 9;label label = new label(container, swt.null);label.settext("&container:");containertext = new text(container, swt.border | swt.single);griddata gd = new griddata(griddata.fi

29、ll_horizontal);containertext.setlayoutdata(gd);containertext.addmodifylistener(new modifylistener() public void modifytext(modifyevent e) dialogchanged(););button button = new button(container, swt.push);button.settext("browse.");button.addselectionlistener(new selectionadapter() public vo

30、id widgetselected(selectionevent e) handlebrowse(););label = new label(container, swt.null);label.settext("&file name:");filetext = new text(container, swt.border | swt.single);gd = new griddata(griddata.fill_horizontal);filetext.setlayoutdata(gd);filetext.addmodifylistener(new modifyl

31、istener() public void modifytext(modifyevent e) dialogchanged(););initialize();dialogchanged();setcontrol(container);必须先在文件的顶部声明新控件和其他控件,然后才可以将新控件添加到此方法中。 清单 9. 声明新控件public class newxhtmlfilewizardpage extends wizardpage /* newly added for the page title */private text titletext;/ the rest of the cl

32、ass.现在添加文本的 getter。newxhtmlfilewizard 类将在构建新文件时使用此 getter。gettitle() 方法如下所示:清单 10. gettitle() 方法/* * gets the html title for the new file */public string gettitle() return titletext.gettext();修改 createcontrol() 方法以添加标题的新输入控件和标签。新代码如下所示:清单 11. 修改后的 createcontrol() 方法/* * see idialogpage#createcontrol

33、(composite) */public void createcontrol(composite parent) composite container = new composite(parent, swt.null);gridlayout layout = new gridlayout();container.setlayout(layout);layout.numcolumns = 3;layout.verticalspacing = 9;label label = new label(container, swt.null);label.settext("&cont

34、ainer:");containertext = new text(container, swt.border | swt.single);griddata gd = new griddata(griddata.fill_horizontal);containertext.setlayoutdata(gd);containertext.addmodifylistener(new modifylistener() public void modifytext(modifyevent e) dialogchanged(););button button = new button(cont

35、ainer, swt.push);button.settext("browse.");button.addselectionlistener(new selectionadapter() public void widgetselected(selectionevent e) handlebrowse(););label = new label(container, swt.null);label.settext("&file name:");filetext = new text(container, swt.border | swt.sing

36、le);gd = new griddata(griddata.fill_horizontal);filetext.setlayoutdata(gd);filetext.addmodifylistener(new modifylistener() public void modifytext(modifyevent e) dialogchanged(););/* need to add empty label so the next two controls * are pushed to the next line in the grid. */label = new label(contai

37、ner, swt.null);label.settext("");/* adding the custom control here */label = new label(container, swt.null);label.settext("&title:");titletext = new text(container, swt.border | swt.single);gd = new griddata(griddata.fill_horizontal);titletext.setlayoutdata(gd);titletext.addm

38、odifylistener(new modifylistener() public void modifytext(modifyevent e) dialogchanged(););/* finished adding the custom control */initialize();dialogchanged();setcontrol(container);在将代码添加到 newxhtmlfilewizard 类中之前,请通过执行先前概述的步骤测试到目前为止的更改。验证用户输入用户在新向导的 title 字段中输入的文本将用作新 html 文件的 <title> 元素中的文本。

39、出于本文的目的,该值是必需的,因此需要添加检查输入的代码以确保用户输入的内容是没有问题的。对 dialogchanged() 方法的修改如下所示:清单 12. 检验 dialogchanged() 中的输入/* * ensures that both text fields are set. */private void dialogchanged() iresource container = resourcesplugin.getworkspace().getroot().findmember(new path(getcontainername();string filename = ge

40、tfilename(); string titletext = gettitle();if (getcontainername().length() = 0) updatestatus("file container must be specified");return;if (container = null| (container.gettype() & (iresource.project | iresource.folder) = 0) updatestatus("file container must exist");return;if

41、 (!container.isaccessible() updatestatus("project must be writable");return;if (filename.length() = 0) updatestatus("file name must be specified");return;if (filename.replace('', '/').indexof('/', 1) > 0) updatestatus("file name must be valid"

42、);return;int dotloc = filename.lastindexof('.');if (dotloc != -1) string ext = filename.substring(dotloc + 1);if (ext.equalsignorecase("html") = false) updatestatus("file extension must be "html"");return; if (titletext.length() =0 )updatestatus("title must

43、 be specified");return;updatestatus(null);完成这些更改后,如果不输入 title 值,向导将提供错误消息。此外,finish 按钮将被禁用,直至为 title 指定了值为止。通过使用先前概述的步骤运行插件项目来检验此功能。添加自定义内容向导页面 newxhtmlfilewizardpage 现在将捕捉用户输入的 html 标题的值,但是它尚未把该值合并到文件中。要开始将该值添加到文件中,首先需要编辑 index-xhtml-template.resource 文件使其包含该值的占位符。您可以将 <title> 元素更改为 <

44、title>$title</title>,这样可以更轻松地包含占位符。修改 performfinish() 方法以从向导页面获得标题并将标题传递给 dofinish() 方法以及其余值。清单 13. 最终的 performfinish() 方法/* * this method is called when 'finish' button is pressed in the wizard. we * will create an operation and run it using wizard as execution context. */public bo

45、olean performfinish() final string containername = page.getcontainername();final string filename = page.getfilename();final string title = page.gettitle();irunnablewithprogress op = new irunnablewithprogress() public void run(iprogressmonitor monitor)throws invocationtargetexception try dofinish(con

46、tainername, filename, title, monitor); catch (coreexception e) throw new invocationtargetexception(e); finally monitor.done();try getcontainer().run(true, false, op); catch (interruptedexception e) return false; catch (invocationtargetexception e) throwable realexception = e.gettargetexception();mes

47、sagedialog.openerror(getshell(), "error", realexception.getmessage();return false;return true;接下来,略微修改 dofinish() 方法使其接受标题作为参数并将其传递给 opencontentstream() 方法。清单 14. 接受标题作为参数的最终 dofinish() 方法/* * the worker method. it will find the container, create the file if missing * or just replace its c

48、ontents, and open the editor on the newly created * file. */private void dofinish(string containername, string filename, string title,iprogressmonitor monitor) throws coreexception monitor.begintask("creating " + filename, 2);iworkspaceroot root = resourcesplugin.getworkspace().getroot();i

49、resource resource = root.findmember(new path(containername);if (!resource.exists() | !(resource instanceof icontainer) throwcoreexception("container "" + containername+ "" does not exist.");icontainer container = (icontainer) resource;final ifile file = container.getfil

50、e(new path(filename);try inputstream stream = opencontentstream(title);try if (file.exists() file.setcontents(stream, true, true, monitor); else file.create(stream, true, monitor); finally stream.close(); catch (ioexception e) monitor.worked(1);monitor.settaskname("opening file for editing.&quo

51、t;);getshell().getdisplay().asyncexec(new runnable() public void run() iworkbenchpage page = platformui.getworkbench().getactiveworkbenchwindow().getactivepage();try ide.openeditor(page, file, true); catch (partinitexception e) );monitor.worked(1);最后,需要修改 opencontentstream() 方法的很大一部分才能够将文件中的 $title

52、值替换为用户提供的值(参见清单 15)。在配有大量不同值的模板中,您可以使用更精确的解决方案,例如扩展 filterinputstream 并替换一整组不同值的新类。 清单 15. 最终的 opencontentstream() 方法/* * initialize the file contents to contents of the given resource. */private inputstream opencontentstream(string title)throws coreexception final string newline = "" / syste

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论