How do I get the workbench window to open a modal dialog in an Eclipse based project?
Asked Answered
P

3

21

In order to open a modal dialog, you need to pass a parent window, and pass the necessary flags for the dialog to be modal of course.

Depending on where you are in the eclipse infrastructure, finding this parent window is not always easy.

How can the parent window be accessed?

Pilatus answered 14/1, 2009 at 14:55 Comment(0)
P
25

From a view or an editor (this part is easy):

this.getSite().getWorkbenchWindow().getShell()

From elsewhere, access a view or editor and same as above.

If you find yourself in a class where you don't have access to a view or editor, you probably don't want to be calling any UI code, but if you really want to shoot yourself in the foot:

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()
Pilatus answered 14/1, 2009 at 15:0 Comment(0)
R
30

The piece of code from the previous answer will work. However, keep in mind you can only open your dialog from the UI thread. If you are opening the dialog from a different thread, e.g. a background process, you need to do something like this:

PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
    public void run() {
        Shell activeShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
    }
});

Otherwise you will get an exception when creating the dialog.

Rienzi answered 14/1, 2009 at 17:7 Comment(1)
Of course, I should have added that. Which is more of the same problem - You have to know where to find that "getWorkbench()" function.Pilatus
P
25

From a view or an editor (this part is easy):

this.getSite().getWorkbenchWindow().getShell()

From elsewhere, access a view or editor and same as above.

If you find yourself in a class where you don't have access to a view or editor, you probably don't want to be calling any UI code, but if you really want to shoot yourself in the foot:

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()
Pilatus answered 14/1, 2009 at 15:0 Comment(0)
E
0

Not quite exactly what you want to do but you may need to use SWT.APPLICATION_MODAL, SWT.DIALOG_TRIM etc. when creating your dialog in order to make it a modal dialog (but perhaps that's not what your question was about).

See this link for more info.

Eyetooth answered 14/1, 2009 at 15:27 Comment(2)
Yes, that too. But my question was more specifically about finding the shell of the workbench window. I did it 6 months ago and had forgotten in the meantime so figured I'd add the titbit of knowledge to stackoverflowPilatus
@Pilatus - My most upvoted answers on StackOverflow are just notes that I wrote for myself that I know that I'll find via Google months or years later when I'll have the exact same question again.Corsair

© 2022 - 2024 — McMap. All rights reserved.