JFace Wizard 自定义 “Next” 按钮事件

来源:互联网 发布:win7公用网络是黑色的 编辑:程序博客网 时间:2024/05/29 08:15

JFace 的 Wizard是很常用的UI,我们也很需要在点击Next的时候做些动作,Wizard 本身没有给我们提供一个很容易发现的接口,虽然有个 getNextPage() 方法,但是很难用 嘿嘿

 

得自己动动手啦。

 

首先呢,写个类来继承WizardDialog,并覆盖他的buttonPressed方法,这样就能在点击Next的时候做动作了。

Code Snippet:

 

public class CreditSCWizardDialog extends WizardDialog {

    public CreditSCWizardDialog(Shell parentShell, IWizard newWizard) {
        super(parentShell, newWizard);
    }
    
    @Override
    protected void buttonPressed(int buttonId) {
        if (buttonId == IDialogConstants.NEXT_ID){

            // 点击Next的时候 执行actionWhenNextButtonPressed()方法

            // 我这里这个方法有个boolean的返回值。

            if (actionWhenNextButtonPressed()) {
                super.buttonPressed(buttonId);
            }
        }
        else {
            super.buttonPressed(buttonId);
        }
    }
    
    protected boolean actionWhenNextButtonPressed() {

       // 这里填写要做的动作

      // 我想很多情况下,你的Wizard会有很多页面对吧

      // 而你呢,不同的页面又想有不同的动作,怎么办?

      }
}

 

其实呢,很简单了。

再写一个类继承继承WizardPage,在这个类里面弄个抽象方法,

比如这个抽象方法叫做:nextButtonClick().

 

public abstract class CreditSCWizardPage extends WizardPage {

    protected CreditSCWizardPage(String pageName) {
        super(pageName);
    }

    protected abstract boolean nextButtonClick();

}

 

嗯,我想你看明白了对吧,这里有最关键的一步,we are almost there.

 

修改刚才的那个WizardDialog类中的actionWhenNextButtonPressed()方法。

protected boolean actionWhenNextButtonPressed() {

        IWizardPage currentPage = getWizard().getContainer().getCurrentPage();
        return ((CreditSCWizardPage) currentPage).nextButtonClick();

}


这样呢,你的每个WizardPage实现中都去实现自己的nextButtonClick()方法就行啦