Pergunta

I am stuck in getting an absolute position of DialogBox. I know it is the common problem (and strange workaround) for PopupPanel (which is parent to DialogBox) to set it, but what if I want to get it, what is the exact moment when the box attached to DOM? Neither overriding show nor onAttach nor show does not help:

class MyDialog extends DialogBox {

    public MyDialog(. . .) {
        ComplexPanel vert = new VerticalPanel();
        vert.add("Test");
        vert.add(new Button("Close", new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                MyDialog.this.hide();
            }
        }));
        setWidget(vert);

        this.addAttachHandler(new AttachEvent.Handler() {

            @Override
            public void onAttachOrDetach(AttachEvent event) {
                if (event.isAttached()) Log.debug("attach:"+MyDialog.this.getAbsoluteLeft() +";"+
                                                            MyDialog.this.getAbsoluteTop());
            }
        });
    }

    @Override
    protected void onLoad() {
        super.onLoad();
        Log.debug("load:"+this.getAbsoluteLeft() +";"+this.getAbsoluteTop());
    }

    @Override
    public void show() {
        super.show();
        Log.debug("show:"+this.getAbsoluteLeft() +";"+this.getAbsoluteTop());
    }

}

So when I call new MyDialog().show();, all this lines do log 0;0, however dialog is positioned in center of a page. But what I want is the sum of the chain of offsetParent positions. (And they are 0 in these moments even in JavaScript, if use JSNI to check this)

Again, setPopupPositionAndShow allows to set position but not get it :(

Foi útil?

Solução

Finally, I've got this to work:

@Override
public void setPopupPosition(int left, int top) {
    super.setPopupPosition(left, top);
    if (this.isAttached()) Log.debug("setPos:"+this.getAbsoluteLeft() +";"+this.getAbsoluteTop());
}

It gets the proper position and I hope it is the right way to do it and setPopupPosition is called every time. You will even call it manually when using setPopupPositionAndShow.

I think it will be wise to let this question stay at SO "for future generations".


Upd. If you plan to call center(...) or some similar method of your dialog, be aware that setPopupPosition will be called twice or more times (may be first time with 0, 0), even if you'll check if it isAttached(). Add some additional check to ensure that positions are correct in current call.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top