(FWIW, I made a nearly identical post to the old MyGWT forums, but (a) that was just days before they were shutdown; and (b) I didn't include a sample to demonstrate the problem.)
It seems that if you create a container and add some widgets to it, and then you want to remove one or more of those widgets, the removal fails unless the container has been attached to the DOM. Why? Well, it appears that in the AbstractContainer::remove() method (which, in MyGWT 0.5.2 is the Container::remove() method) checks to see that the parent of widget being removed is "this". Well, before being attached, it appears that the parent has not yet been set.
Code:
package sampleApp.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.Window;
import net.mygwt.ui.client.widget.WidgetContainer;
import net.mygwt.ui.client.widget.Viewport;
/**
* This class demonstrates a bug where you can add a child to a MyGWT parent before the parent
* has been attached to the DOM, but attempting to remove that child before the parent is attached
* silently fails.
*/
public class RemoveBeforeAttached
implements EntryPoint {
public void onModuleLoad() {
final Viewport viewport = new Viewport();
viewport.setBounds(0, 0, Window.getClientWidth(), Window.getClientHeight());
// First: Show that we cannot remove a widget when it's added before the parent
// has been attached...
WidgetContainer firstContainer = new WidgetContainer();
firstContainer.add( new HTML( "<b>Testing</b>" ) );
if ( 1 != firstContainer.getWidgetCount() ) {
Window.alert( "firstContainer: Expected only a single widget!" );
}
firstContainer.removeAll();
if ( 0 != firstContainer.getWidgetCount() ) {
Window.alert( "firstContainer: Expected NO MORE widgets!" );
}
// Now, show that if we wait until after being attached, we can remove children.
WidgetContainer secondContainer = new WidgetContainer() {
protected void onAttach() {
super.onAttach();
removeAll();
if ( 0 != getWidgetCount() ) {
Window.alert( "secondContainer: Expected NO MORE widgets!" );
}
else {
Window.alert( "secondContainer: All's good when removed after attached!" );
}
}
};
secondContainer.add( new HTML( "<b>Testing</b>" ) );
if ( 1 != secondContainer.getWidgetCount() ) {
Window.alert( "secondContainer: Expected only a single widget!" );
}
viewport.add( secondContainer );
viewport.layout( true );
}
}
Now, I readily admit that I have not verified this in the 1.0 code base, except by inspection. I'm simply not setup to run GWT 1.5 yet... However, by inspection, it seems that the 1.0 code base still has this bug.
jay