使用Eclipse向导进行快速开发

豆豆网   技术应用频道   2007年08月18日  【字号: 收藏本文

内容摘要:Eclipse 框架和集成开发环境 (IDE) 的最优秀特性之一是可扩展性。在本文中,您将了解如何快速构建用于自动完成添加新文件过程的向导。由于可以预定义文件的内容,因此向导通过提供一致性和自动化使您可以更好地进行开发。

  现在,您可以运行 Eclipse 插件来查看该插件的内容。

  测试新向导

  在 Eclipse 创建向导所使用的三个类之后,您可以在阅读本文的过程中随时启动另一个 Eclipse 实例来运行和测试插件。要启动插件项目,请在项目上右击,并选择 Run As > Eclipse Application,如图 6 所示。Eclipse 的新实例将启动。

  图 6. 将项目作为 Eclipse 应用程序来运行

  使用Eclipse向导进行快速开发

  现在需要创建包含新文件的临时项目。项目的名称无关紧要 —— 诸如 “temp” 之类的名称即可。当新项目已在工作区中后,请通过选择 File > New > Other 来添加新模板。

  如果一切按预期运行正常,则新类别将列于您为向导定义的类别下的 Select a Wizard 窗口中。我使用了 Example.com 企业模板,如下所示:

  图 7. 使用新模板

  使用Eclipse向导进行快速开发

  当您完成向导的其余部分后,Eclipse 将创建包含定义内容的新文件。如果此简单功能就是模板所需的全部功能,则可以止于此处。但是,可能还需要提示用户提供一些用来整合文件内容的输入。

  自定义向导页面

  初始 NewXHTMLFileWizardPage 的表单中只有两个控件:一个用于容器(项目或文件夹)的名称,而另一个用于创建新文件时使用的名称。createControl() 方法(完整的方法代码如清单 8 所示)负责创建这些控件和将其添加到对话框中。

  清单 8. createControl() 方法  /**
   * @see IDialogPage#createControl(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("&Container:");
    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(container, 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.SINGLE);
    gd = new GridData(GridData.FILL_HORIZONTAL);
    fileText.setLayoutData(gd);
    fileText.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent e) {
        dialogChanged();
      }
    });
    initialize();
    dialogChanged();
    setControl(container);
  }

来源:ibm    作者:Nathan A. Good    责编:豆豆技术应用

正在加载评论...