cipher
cipher

Reputation: 139

Using SWT inside eclipse plugin

I'm trying to use SWT to create dialogs inside an eclipse plugin. I can create a Shell and a Display object and the code compiles with no problems but I can not see any kind of dialogs when I'm debugging the plugin, though the same code runs perfectly when used in a standalone application.

Here is an example code I wrote inside the run method of the SampleAction that implements IWorkbenchWindowActionDelegate

Display display = new Display();
Shell shell = new Shell(display);
shell.open();


while(!shell.isDisposed())
{
    if(!display.readAndDispatch())
    {
        display.sleep();
    }
}

display.dispose();

I also tried removing the readAndDispatch and wrote this code and it still doesn't work .. I see nothing

    Display display = new Display();
    Shell shell = new Shell(display);
    shell.open();
    MessageDialog dialog = new MessageDialog(shell, "My Title", null,
            "My message", MessageDialog.ERROR, new String[] { "First",
          "Second", "Third" }, 0);
    int result = dialog.open();

Upvotes: 0

Views: 1163

Answers (2)

Eugene
Eugene

Reputation: 9474

Note that using a new shell as a parent of the modal dialog can potentially cause inconvenience for the user as the dialog may get behind the main workbench window so the use can neither see the dialog nor do anything in the main workbench window. The solution is to pass window shell as a parent to the dialog. As your action is a IWorkbenchWindowActionDelegate you should record the window passed to init method and then use window.getShell() to obtain the parent shell.

Note that you should not be creating a display or running an event queue (that's readAndDispatch) when writing a plugin (unless you want a modeless dialog - which is a separate topic).

Upvotes: 1

Baz
Baz

Reputation: 36894

This will do the job. No need to open a new shell beforehand. Just pass the new Shell to the MessageDialog.

MessageDialog dialog = new MessageDialog(new Shell(), "My Title", null,
        "My message", MessageDialog.ERROR, new String[] { "First",
      "Second", "Third" }, 0);
int result = dialog.open();

You can also use the parentShell of the GUI part you are currently in and pass it to the dialog.

Upvotes: 3

Related Questions