你好,欢迎来到电脑编程技巧与维护杂志社! 杂志社简介广告服务读者反馈编程社区  
合订本订阅
 
 
您的位置:技术专栏 / Linux开发
使用Eclipse向导进行快速开发(5)
 
必须先在文件的顶部声明新控件和其他控件,然后才可以将新控件添加到此方法中。

  清单 9. 声明新控件public class NewXHTMLFileWizardPage extends WizardPage {
/* Newly added for the page title */
  private Text titleText;
  // the rest of the class...  
}

  现在添加文本的 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(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();
      }
    });
   
      
    /* Need to add empty label so the next two controls
     * are pushed to the next line in the grid. */
    label = new Label(container, 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.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent e) {
        dialogChanged();
      }
    });
        
    /* Finished adding the custom control */
    
    initialize();
    dialogChanged();
    setControl(container);
  }

在将代码添加到 NewXHTMLFileWizard 类中之前,请通过执行先前概述的步骤测试到目前为止的更改。

  验证用户输入

  用户在新向导的 Title 字段中输入的文本将用作新 HTML 文件的 <title> 元素中的文本。出于本文的目的,该值是必需的,因此需要添加检查输入的代码以确保用户输入的内容是没有问题的。

  对 dialogChanged() 方法的修改如下所示:

  清单 12. 检验 dialogChanged() 中的输入  /**
   * Ensures that both text fields are set.
   */
  private void dialogChanged() {
    IResource container = ResourcesPlugin.getWorkspace().getRoot()
        .findMember(new Path(getContainerName()));
    String fileName = getFileName();
      
    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 (!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");
      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 be specified");
      return;
    }
        
    
    updateStatus(null);
  }

(编辑:aniston)

  推荐精品文章

·2024年12月目录 
·2024年11月目录 
·2024年10月目录 
·2024年9月目录 
·2024年8月目录 
·2024年7月目录 
·2024年6月目录 
·2024年5月目录 
·2024年4月目录 
·2024年3月目录 
·2024年2月目录 
·2024年1月目录
·2023年12月目录
·2023年11月目录

  联系方式
TEL:010-82561037
Fax: 010-82561614
QQ: 100164630
Mail:gaojian@comprg.com.cn

  友情链接
 
Copyright 2001-2010, www.comprg.com.cn, All Rights Reserved
京ICP备14022230号-1,电话/传真:010-82561037 82561614 ,Mail:gaojian@comprg.com.cn
地址:北京市海淀区远大路20号宝蓝大厦E座704,邮编:100089