Java停靠另一个对话框上的对话框

时间:2014-12-09 14:58:57

标签: java dialog swt eclipse-rcp jface

我已经同时创建了两个对话框。现在我想将一个对话框停靠到另一个对话框。因此,如果一个人将被移动,另一个人也将被移动。

我试图实现一个侦听器,但它不起作用,因为在对话框关闭后将调用它。

public class DialogOne extends TitleAreaDialog {

    public DialogOne(Shell parentShell) {
        super(parentShell);
    }

    @Override
    public void create() {
        super.create();
        setTitle("Dialog One");
    }

    @Override
    public int open() {
        Display.getCurrent().asyncExec(new Runnable() {

            @Override
            public void run() {
                DialogTwo dialogTwo = new DialogTwo(getShell());
                dialogTwo.open();
                dialogTwo.addControlListener(new ControlAdapter() {

                    @Override
                    public void controlMoved(ControlEvent e) {
                        System.out.println("Moved");

                    }
                });

            }
        });
        return super.open();
    }

    @Override
    protected void setShellStyle(int newShellStyle) {
        super.setShellStyle(SWT.CLOSE | SWT.MODELESS | SWT.BORDER | SWT.TITLE);
        setBlockOnOpen(false);
    }
}

public class DialogTwo extends TitleAreaDialog {

    private Composite composite;

    public DialogTwo(Shell parentShell) {
        super(parentShell);
    }

    @Override
    public void create() {
        super.create();
        setTitle("Dialog Two");
    }

    @Override
    protected void setShellStyle(int newShellStyle) {
        super.setShellStyle(SWT.CLOSE | SWT.MODELESS | SWT.BORDER | SWT.TITLE);
        setBlockOnOpen(false);
    }

    @Override
    protected Control createDialogArea(Composite parent) {
        composite = (Composite) super.createDialogArea(parent);
        return composite;
    }

    public void addControlListener(ControlListener listener) {
        composite.addControlListener(listener);
    }
}

那么我如何才能完成从第二个对话框获取边界并重新定位第二个对话框旁边的第一个边界。当第二个将被移动。

2 个答案:

答案 0 :(得分:1)

我找到了一种方法来满足我的要求。我想,我可以先用dialogTwo.create()创建对话框。所以我可以在打开对话框之前做我想做的事情。在这种情况下我是shell并添加一个控件监听器。

public class DialogOne extends TitleAreaDialog {

  private Shell dialogShell;

  public DialogOne(Shell parentShell) {
    super(parentShell);
  }


  @Override
  public void create() {
    super.create();
    setTitle("Dialog One");
    getShell().addControlListener(new ControlAdapter() {


      @Override
      public void controlMoved(ControlEvent controlevent) {
        moveDialog(DialogOne.this.getShell());

      }
    });
  }


  @Override
  protected Control createDialogArea(Composite parent) {
    Composite composite = (Composite) super.createDialogArea(parent);
    createSecondDialog();
    return composite;
  }

  private void createSecondDialog() {
    final DialogTwo dialogTwo = new DialogTwo(getShell());
    dialogTwo.create();
    dialogShell = dialogTwo.getShell();
    dialogShell.addControlListener(new ControlAdapter() {


      @Override
      public void controlMoved(ControlEvent e) {
        moveDialog(dialogShell);

      }
    });

    Display.getCurrent().asyncExec(new Runnable() {


      @Override
      public void run() {

        dialogTwo.open();
      }
    });

  }

  protected void moveDialog(Shell shell) {
    Rectangle rightBounds;
    int dialogBorder = 10;
    if (shell.equals(getShell())) {
      rightBounds = shell.getBounds();
      dialogShell.setLocation(rightBounds.width + rightBounds.x + dialogBorder, rightBounds.y);
    } else {
      rightBounds = dialogShell.getBounds();
      Rectangle leftBounds = getShell().getBounds();
      getShell().setLocation(
        rightBounds.x - leftBounds.width - dialogBorder,
        rightBounds.y);
    }
  }


  @Override
  protected void setShellStyle(int newShellStyle) {
    super.setShellStyle(SWT.CLOSE | SWT.MODELESS | SWT.BORDER | SWT.TITLE);
    setBlockOnOpen(false);
  }
}

答案 1 :(得分:0)

dialogTwo.open()块的调用。如果在调用open之前添加dialogTwo.setBlockOnOpen(false),则可以获得非阻塞行为。

第二件事是:你应该将一个ControlListener添加到shell而不是它的一个子节点。所以这应该有效:

            DialogTwo dialogTwo = new DialogTwo(getShell());
            dialogTwo.setBlockOnOpen(false);
            dialogTwo.open();
            dialogTwo.getShell().addControlListener(new ControlAdapter() {

                @Override
                public void controlMoved(ControlEvent e) {
                    System.out.println("Moved");

                }
            });