'Eclipse RCP Center Dialog on Active Monitor

I am trying to center a dialog in my Eclipse RCP application, and I tried different methods to center the dialog.

Is there an easy/accepted way to center the dialog respecting the current location of the primary shell (the workbench app If I am not mistaken)? I can center the dialog on the primary monitor, but I am unable to center the dialog respecting the main app window.

This is a problem on multi-monitor setups. I start the app and it is displayed on the primary monitor. If I move the app to the second screen, I am failing to center the dialog not on the primary monitor but on the second screen where the app is.

---- Update 1 ----

At the moment every dialog is created with a new Shell:

Shell shell = new Shell(Display.getCurrent(), SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
        PortOverviewDialog portOverviewDialog = new PortOverviewDialog(shell);
        portOverviewDialog.show();

I think this is the main problem. How can I avoid this by getting the primary Shell?

---- Update 2 ----

For me the code Shell shell = HandlerUtil.getActiveShell(event); gets the parent Shell. And I am setting the Dialog to "parent center" with the help of the following code:

Rectangle parentSize = getParent().getBounds();
Rectangle shellSize = shell.getBounds();
int locationX = (parentSize.width - shellSize.width)/2+parentSize.x;
int locationY = (parentSize.height - shellSize.height)/2+parentSize.y;
shell.setLocation(new Point(locationX, locationY));


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source