performFinish() 方法将在调用 createProject() 创建文件和文件夹之后调用两个静态方法更新当前透视图并选择在 IDE 中新创建的项目。
createProject() 方法
清单 20 中所示的 createProject() 方法将创建并打开新项目。然后,方法将向项目中添加两个新文件和两个新文件夹。文件都是由名为 addFileToProject() 的私有方法添加的,该方法的作用是保持 createProject() 相对整洁。
清单 20. createProject() 方法 /** * This creates the project in the workspace. * * @param description * @param projectHandle * @param monitor * @throws CoreException * @throws OperationCanceledException */ void createProject(IProjectDescription description, IProject proj, IProgressMonitor monitor) throws CoreException, OperationCanceledException { try { monitor.beginTask("", 2000); proj.create(description, new SubProgressMonitor(monitor, 1000)); if (monitor.isCanceled()) { throw new OperationCanceledException(); } proj.open(IResource.BACKGROUND_REFRESH, new SubProgressMonitor( monitor, 1000)); /* * Okay, now we have the project and we can do more things with it * before updating the perspective. */ IContainer container = (IContainer) proj; /* Add an XHTML file */ addFileToProject(container, new Path("index.html"), NewXHTMLFileWizard.openContentStream("Welcome to " + proj.getName()), monitor); /* Add the style folder and the site.css file to it */ final IFolder styleFolder = container.getFolder(new Path("styles")); styleFolder.create(true, true, monitor); InputStream resourceStream = this.getClass().getResourceAsStream( "templates/site-css-template.resource"); addFileToProject(container, new Path(styleFolder.getName() + Path.SEPARATOR + "style.css"), resourceStream, monitor); resourceStream.close(); /* * Add the images folder, which is an official Exmample.com standard * for static web projects. */ IFolder imageFolder = container.getFolder(new Path("images")); imageFolder.create(true, true, monitor); } catch (IOException ioe) { IStatus status = new Status(IStatus.ERROR, "ExampleWizard", IStatus.OK, ioe.getLocalizedMessage(), null); throw new CoreException(status); } finally { monitor.done(); } }
addFileToProject() 方法
您可能会发现 addFileToProject() 方法中的大部分代码基本上与清单 14 中所示的 doFinish() 方法中代码相同。方法的签名差别很大:它已被修改为接受参数以使其在 createProject() 方法的上下文内具有更好的可重用性。
清单 21. addFileToProject() 方法 /** * Adds a new file to the project. * * @param container * @param path * @param contentStream * @param monitor * @throws CoreException */ private void addFileToProject(IContainer container, Path path, InputStream contentStream, IProgressMonitor monitor) throws CoreException { final IFile file = container.getFile(path); if (file.exists()) { file.setContents(contentStream, true, true, monitor); } else { file.create(contentStream, true, monitor); } }
如果文件已存在,则文件的内容将被设为传递到方法中的 contentStream 的内容。如果文件尚不存在,则以文件中的内容创建文件。
完整的 NewSiteProjectWizard 类包含在本文的下载中。在添加了此处的方法实现及 INewWizard 和 IExecutableExtension 接口的实现后,您可以如先前所示把项目作为 Eclipse 应用程序来运行。这一次,除了可以用 NewXHTMLFileWizard 创建新文件之外,还可以创建新项目。
故障检修
当部署 wizard .jar 文件(此处称为 NewFileWizard_1.0.0.jar)时,将其放入 plugin 文件夹中。转到 plugin.xml 文件,在 Eclipse 中双击该文件,导航到 Overview 附签并单击右边的 Export Wizard 链接,从而创建 .jar 文件。然后将 .jar 移到 Eclipse plugin 文件夹中,不要对它解包。
如果启动 Eclipse 时遇到 “Plugin does not have a valid identifier” 或 “Plugin does not have a valid version” 错误,请尝试在命令行使用 -clean 参数启动 Eclipse。
如果遇到错误,可以在调试时把项目作为 Eclipse 插件来运行。我喜欢使用 Eclipse IDE 中的 Debug Perspective。选择 Run > Debug Last Launched 在逐步浏览代码时开始运行 Eclipse 中的插件。
您很有可能需要在 performFinish() 方法的第一行中设置一个断点,因为那是操作的起点。一旦您单击了向导中的 Finish,调试器就应当停在断点处(只要错误不出现在断点之前)。
结束语
Eclipse IDE 最优秀的特性之一是通过创建用于添加创建新文件的新向导的插件轻松地提供扩展功能。使用企业专有的向导来创建文件将使企业可以快速一致地开发应用程序。
(编辑:aniston)
|