RootPanel Widget - Attach, detach, attach again

48 views
Skip to first unread message

vmrggg

unread,
Aug 30, 2011, 7:28:53 PM8/30/11
to Google Web Toolkit
In the following 3 steps test I'm trying to get the same behaviour
from both "adRootPanel" and "adDialogBox".

Step 1 - I click on "Attach adPanel" to turn both ON, and both buttons
"Click Here." works as expected;
Step 2 - I click on "Detach adPanel" to turn both OFF;
Step 3 - I click on "Attach adPanel" again to turn both ON again, but
just the dialogBox works as "I" expected.

Looks like a really dumb question, but can someone point me in the
right direction?

Thanks in advance,

Vini


public class ADRootTest implements EntryPoint {

// Attach/Detach RootPanel
RootPanel adRootPanel;

// "Attach/Detach" DialogBox
DialogBox adDialogBox;

private void doAttach() {
// RootPanel Attach
adRootPanel = RootPanel.get( "AREA_BROWSER_2" );
RootPanel.detachOnWindowClose(adRootPanel);
Button bt = new Button("Click Here.");
adRootPanel.add(bt);
bt.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Window.alert("RootPanel - I'm working !");
}
});

// DialogBox "Attach"
adDialogBox = new DialogBox();
adDialogBox.setModal(false);
Button bt2 = new Button("Click Here.");
adDialogBox.add(bt2);
bt2.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Window.alert("DialogBox - I'm working !");
}
});
adDialogBox.show();
}

private void doDetach() {
// RootPanel Detach
adRootPanel.clear();
adRootPanel.removeFromParent();
adRootPanel = null;

// DialogBox "Detach"
adDialogBox.hide();
}

@Override
public void onModuleLoad() {
Button btAttach = new Button("Attach adPanel");
Button btDetach = new Button("Detach adPanel");

RootPanel rp;
rp = RootPanel.get( "AREA_BROWSER_1" );
rp.add(btAttach);
rp.add(btDetach);

btAttach.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
doAttach();
}
});

btDetach.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
doDetach();
}
});

}

}

vmrggg

unread,
Aug 31, 2011, 9:56:12 AM8/31/11
to google-we...@googlegroups.com
Hi!

Sorry. Nobody has time to go through code. I understand that.

Let me try to explain it in another way:

RootPanel rp = RootPanel.get( "DIV_ID" );
rp.add( widget1 );
...
rp.add( widgetN );
RootPanel.detachOnWindowClose( rp );

At this point in time the event handlers of widget1 through widgetN works allright.

Later on I need to get rid of this set of widgets and replace then with another one, so I tried the following:

rp.clear(); // To remove widget1 through widgetN
rp.removeFromParent(); // To "unbind" from "DIV_ID"

Then I start again (in another part of the code) with a new set of widgets, but using the same "DIV_ID" of the host Html page, but the event handlers of this new set of widgets does not work. If I use another element id of the same host page, say "DIV_ID_2", it works.

There is something completely wrong in my undestanding of the GWT about this point?

Thanks,

Vini

Thomas Broyer

unread,
Aug 31, 2011, 11:05:05 AM8/31/11
to google-we...@googlegroups.com


On Wednesday, August 31, 2011 3:56:12 PM UTC+2, vmrggg wrote:
Hi!

Sorry. Nobody has time to go through code. I understand that.

Let me try to explain it in another way:

RootPanel rp = RootPanel.get( "DIV_ID" );
rp.add( widget1 );
...
rp.add( widgetN );
RootPanel.detachOnWindowClose( rp );

detachOnWindowClose is automatically called by RootPanel.get(). It's a method expected to be used in widgets' static wrap(Element) methods. There's little to no reason to call it if you're not writing your own wrap(Element) method for your own widget.
 
At this point in time the event handlers of widget1 through widgetN works allright.

Later on I need to get rid of this set of widgets and replace then with another one, so I tried the following:

rp.clear(); // To remove widget1 through widgetN
rp.removeFromParent(); // To "unbind" from "DIV_ID"

When you removeFromParent(), given that a RootPanel has no parent and is in the "detach on window close" list, it is detached (RootPanel.detachNow(rp) is called).

The issue is that RootPanel caches RootPanel instances, so a subsequent call to RootPanel.get(String) with the same ID will return the exact same RootPanel instance, but won't re-attach it!

Then I start again (in another part of the code) with a new set of widgets, but using the same "DIV_ID" of the host Html page, but the event handlers of this new set of widgets does not work. If I use another element id of the same host page, say "DIV_ID_2", it works.

There is something completely wrong in my undestanding of the GWT about this point?

Just remove your call to removeFromParent(), there's absolutely no reason to do it on a RootPanel. 

vmrggg

unread,
Aug 31, 2011, 11:31:21 AM8/31/11
to google-we...@googlegroups.com
Thanks Thomas,

Works like a charm now.

Seems I was overdoing trying to get it right :)

Best wishes,

Vinicius Reinaldi
Reply all
Reply to author
Forward
0 new messages