I am trying to get hold of a shell in Eclipse RCP and bring up popups in my application and had gone through several resources/tutorials to achieve the task but without much luck.
Display.getCurrent().getActiveShell();
or
Display.getDefault().getActiveShell();
or
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
or sometimes even
Display.getDefault().getShells()[0];
But different things seem to work in different situations.
Resources online have different views and most things I find say you shouldn't really use the Display
class at all but I am trying to bring up a popup from within an eclipse Job. It's got to the end and failed and the popup needs to be displayed using:
Display.getCurrent().syncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openError(Display.getDefault().getActiveShell(), "Publish", e.getMessage());
}
});
}
The popup is inside a Runnable being executed from a Thread started from an Eclipse Job. It's complex but necessary to provide progress bar updates whilst calling a synchronous method on a server.
It doesn't work and I don't know why. Just getting a null from the getActiveShell() call.
The real question though isn't 'why doesn't my code work' but was is the definitively correct way to get a shell for a popup in Eclipse RCP framework, and especial in situations where you can't access a workbench?
Looking though the Eclipse source there does not seem to be a definitive way. The closest is some variant of
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
Even if this returns null
that is still acceptable to MessageDialog
.
DebugUIPlugin
has this more elaborate version:
public static Shell getShell() {
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window == null) {
IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
if (windows.length > 0) {
return windows[0].getShell();
}
}
else {
return window.getShell();
}
return null;
}
Use of MessageDialog
from a Job
is rare in the Eclipse source. As the Job runs asynchronously a popup from the Job could be rather unexpected.
In Eclipse 4 applications if you can get the IEclipseContext
there should be an IShellProvider
in the context. e4 apps can also use StatusReporter
which does not need a shell.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With