How to move the an eclipse-rcp window in the center of a desktop screen

Using eclipse-rcp HelloWorld Example

To be able to do this we need to override the WorkbenchWindowAdvisor's postWindowCreate method in the ApplicationWorkbenchWindowAdvisor class.
 @Override
    /**
     * ostWindowCreate - called after the window has been created, either from an
  * initial state or from a restored state; used to adjust the window
     */
    public void postWindowCreate() {
     super.postWindowCreate();
     //get the workbench window
     IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
     Display display = configurer.getWindow().getWorkbench().getDisplay();
     //get the desktop window
     Shell shell = configurer.getWindow().getShell();
        Rectangle rectBounds = display.getBounds();
        
        int minWidth = 600;
        int minHeight = 400;
        int x = 400;
        int y = 100;
        
        //note: for multi-monitors negative x value is returned
        if(rectBounds.x > 0) { //single monitor
         // This formulae calculate the shell's Left ant Top
         x = (rectBounds.width - minWidth) / 2;
         y = (rectBounds.height - minHeight) / 2;
        } else { //assumption 2 monitors that's why rectBounds.width is divided by 2
         x = (rectBounds.width / 2 - minWidth) / 2;
         y = (rectBounds.height - minHeight) / 2;
        }

        // based on the above calculations, set the window location
        shell.setBounds(x, y, minWidth, minHeight);
    }
How to move the an eclipse-rcp window in the center of a desktop screen How to move the an eclipse-rcp window in the center of a desktop screen Reviewed by czetsuya on Tuesday, November 03, 2009 Rating: 5

No comments:

Powered by Blogger.