简体   繁体   中英

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

I have WizardDialog/Wizard and content is 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.

Bad way is to call:

getShell().dispose;

because SWT throws and Exception:

!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?

You should use "close" method on your wizard dialog object. 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. And when error occurs just call YourCustomErrorHandlerInterface#onError method, which will close the wizard.

Hope this helps.

The Wizard implementation of this IWizard method disposes all the pages controls using DialogPage.dispose.

Subclasses should extend this method if the wizard instance maintains addition SWT resource that need to be disposed.

(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). 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. 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. I hope I was clear.

Maybe setVisible(false) should work.

I think you use cancelPressed() method on WizardDialog for close wizard dialog

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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