简体   繁体   English

Eclipse RCP / JFace向导 - 如何以编程方式强制从wizardPage强制关闭wizardDialog

[英]Eclipse RCP / JFace Wizard - How to programmatically force close wizardDialog from wizardPage

I have WizardDialog/Wizard and content is WizardPage. 我有WizardDialog /向导,内容是WizardPage。 Lets say I am doing something inside Page, and when some error occurs I popup with MessageBox and after clicking on OK I want to force close wizardDialog. 让我说我在页面内做一些事情,当发生一些错误时,我弹出MessageBox,点击确定后我想强制关闭wizardDialog。

Bad way is to call: 糟糕的方式是打电话:

getShell().dispose;

because SWT throws and Exception: 因为SWT抛出和异常:

!ENTRY org.eclipse.ui 4 0 2013-08-20 14:15:13.353
!MESSAGE Unhandled event loop exception
!STACK 0
org.eclipse.swt.SWTException: Widget is disposed

Instead it When I call: 相反它当我打电话:

getWizard().performCancel();

It does nothing. 它什么都不做。

How to force close Wizard without SWT Exception? 如何强制关闭向导没有SWT异常?

You should use "close" method on your wizard dialog object. 您应该在向导对话框对象上使用“close”方法。 To call it from a wizard page, I would suggest you to make a callback interface and pass it to the page. 要从向导页面调用它,我建议您创建一个回调接口并将其传递给页面。 Something like that: 像这样的东西:

final YourWizard wizard = new YourWizard ();
WizardDialog wizardDialog = new WizardDialog(shell, wizard);

wizard.setErrorhandler(new YourCustomErrorHandlerInterface() {

        @Override
        public void onError() {
            wizardDialog.close();
        }
});
wizardDialog .open();

After, when wizard page is created you pass YourCustomErrorHandlerInterface to it. 之后,当创建向导页面时,您将YourCustomErrorHandlerInterface传递给它。 And when error occurs just call YourCustomErrorHandlerInterface#onError method, which will close the wizard. 当发生错误时,只需调用YourCustomErrorHandlerInterface#onError方法,这将关闭向导。

Hope this helps. 希望这可以帮助。

The Wizard implementation of this IWizard method disposes all the pages controls using DialogPage.dispose. 此IWizard方法的向导实现使用DialogPage.dispose处理所有页面控件。

Subclasses should extend this method if the wizard instance maintains addition SWT resource that need to be disposed. 如果向导实例维护需要处理的附加SWT资源,则子类应扩展此方法。

(Javadoc) (Javadoc中)

So, when you dispose the dialog, the pages after current page are not visible (I think) and loaded, that's why Wizard.close() matters (same for Wizard.getShell().close() I think). 因此,当您处理对话框时,当前页面之后的页面不可见(我认为)并且已加载,这就是Wizard.close()Wizard.close()对于Wizard.getShell().close()我认为是相同的)。 The method performCancel should be implemented by MyWizard to define what should be done after user click on cancel but it is not defined in Wizard. 方法performCancel应由MyWizard实现,以定义用户单击取消后应该执行的操作,但未在向导中定义。 It is called by the Wizard after the users clickon cancel. 用户单击取消后,向导调用它。 For example : 例如 :

void close(){
     dosmthng
     performCancel();
     dispose();
}

In fact this is the equivalent for performFinish(), but with the button cancel. 实际上这相当于performFinish(),但是取消了按钮。 I hope I was clear. 我希望我很清楚。

Maybe setVisible(false) should work. 也许setVisible(false)应该有效。

I think you use cancelPressed() method on WizardDialog for close wizard dialog 我认为你在WizardDialog上使用cancelPressed()方法来关闭向导对话框

BaseWizard baseWizard=new BaseWizard();
BaseWizardDialog baseWizardDialog=new BaseWizardDialog(getShell(),baseWizard);
baseWizard.setBaseWizardDialog(baseWizardDialog);
baseWizardDialog.open();




public class BaseWizard extends Wizard  {
private BaseWizardDialog baseWizardDialog=null;
private BaseWizardPage baseWizardPage;
public BaseWizard()
    {
    super();
    setWindowTitle("My Wizard");
        baseWizardPage=new BaseWizardPage();

    }
public void setBaseWizardDialog(BaseWizardDialog baseWizardDialog) {
        this.baseWizardDialog = baseWizardDialog;
        baseWizardPage.setBaseWizardDialog(this.baseWizardDialog);
    }

    public BaseWizardDialog getBaseWizardDialog() {
        return baseWizardDialog;
    }
}

public class BaseWizardPage extends WizardPage {

public void createControl(Composite parent) {
private BaseWizardDialog baseWizardDialog=null;
public void setBaseWizardDialog(BaseWizardDialog baseWizardDialog) {
        this.baseWizardDialog = baseWizardDialog;
    }

    public BaseWizardDialog getBaseWizardDialog() {
        return baseWizardDialog;
    }

create first control and when you want to close the dialog simply write cancel pressed 创建第一个控件,当您想关闭对话框时,只需按下取消按钮

if(ConditiontoClose==true)
getBaseWizardDialog().cancelPressed();

}

}

就我而言,唯一有效的选择是:

getShell().setVisible(false);

This worked for me. 这对我有用。

 // fatal error situation detected on a wizard page
 MessageDialog.openError(getShell(), "Error", "Wizard cannot continue and will now exit.");
 getWizard().getContainer().getShell().close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM