Search code examples
javaeclipseeclipse-plugineclipse-rcpmessagedialog

Stop IWizard for Warning Message


I am doing Eclipse plug-in development... When the user clicks finish, I am making a service call that will return three options.

1. Everything is okay to proceed
2. Give user warning
3. Stop Finish completely

So 1 and 3 are easy.

Here is what I am doing for 3:

IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.OK, message, e);
throw new CoreException(status);

So this will just throw an exception and popup a message dialog with the message that I am passing to it.

I am stuck on number 2...

I have tried the following:

MessageDialog dialog = new MessageDialog(null, theTitle, Dialog.getImage(Dialog.DLG_IMG_WARNING), dialogMessage, MessageDialog.WARNING, new String[]{"Yes", "No"}, 0);

int returnCode = dialog.open();
if(returnCode == 0){
    //proceed with finish
}
else if(returnCode == 1){
    //stop the finsih
    return;
}

Here is the message I am receiving when doing this:

null argument:The dialog should be created in UI thread

Is it possible to give the user a warning message after he clicks finish?


Solution

  • All UI code must be created and run in the UI thread. So if you want to display a dialog from some other thread and wait for the result use Display.syncExec:

    final int [] returnCode = new int[1];
    
    Display.getDefault().syncExec(new Runnable()
         {
            @Override
            public void run()
            { 
               MessageDialog dialog = ....
    
               returnCode[0] = dialog.open();
            }
         });
    
    if (returnCode[0] == 0)
    .....