激活零件时,eclipse e4 RCP应用程序中屏幕上的中心对话框

时间:2015-10-27 06:56:19

标签: eclipse-rcp jface e4

当我的Eclipse RCP应用程序中的Part部分被激活时,我正在打开一个对话框。 在Part的@PostConstruct方法中,我正在向EPartService注册IPartListner。 以下代码显示:

 partService.addPartListener(new IPartListener() {

        @Override
        public void partActivated(MPart part) {
            if(part.getElementId().equals("left_part_id")) {
                SignInDialog dialog = new SignInDialog(shell, display, eventBroker);
                dialog.open();
            }
        }
    });

在扩展JFace对话框的SignInDialog中,我这样做:

@Override
protected void configureShell(Shell newShell) {
    Monitor monitor = display.getPrimaryMonitor();
    Rectangle monitorRect = monitor.getBounds();
    int x = monitorRect.x + (monitorRect.width - 600) / 2;
    int y = monitorRect.y + (monitorRect.height - 360) / 2;
    newShell.setLocation(x, y);
    newShell.setText("Sign In");
    super.configureShell(newShell);
}

@Override
protected Point getInitialSize() {
    return new Point(600, 360);
}

请注意,左侧部分始终可见并通过Application.e4xmi进行捆绑。我的问题是,只要部件被激活,对话框就会显示在显示器的LowerRight角落。如果通过单击按钮打开相同的对话框,则会在中心正确显示。任何帮助将不胜感激,提前谢谢!

1 个答案:

答案 0 :(得分:3)

使用getInitialLocation方法设置对话框的位置。 configureShell中设置的尺寸被默认getInitialLocation

覆盖
@Override
protected Point getInitialLocation(final Point initialSize)
{
  Display display = getShell().getDisplay();
  Monitor monitor = display.getPrimaryMonitor();
  Rectangle monitorRect = monitor.getBounds();
  int x = monitorRect.x + (monitorRect.width - 600) / 2;
  int y = monitorRect.y + (monitorRect.height - 360) / 2;

  return new Point(x, y);
}
相关问题