Skip to content

Instantly share code, notes, and snippets.

@christiangoudreau
Last active January 24, 2016 22:22
Show Gist options
  • Save christiangoudreau/42e51a2cbb20dd204f4b to your computer and use it in GitHub Desktop.
Save christiangoudreau/42e51a2cbb20dd204f4b to your computer and use it in GitHub Desktop.
Replace widget
/**
* Panel that wraps a GWT simple panel to be replaced by the widgetToAttach. This will make sure that the DIV element
* is being replaced rather than having the widget inserted in it.
* <p/>
* The result of this will be to have a better, cleaner, dom.
*/
public class ReplacePanel implements IsWidget, HasOneWidget {
static class WrongParentTypeException extends RuntimeException {
public WrongParentTypeException(String message) {
super(message);
}
}
private IsWidget widget;
public ReplacePanel() {
widget = new SimplePanel();
}
@Override
public Widget asWidget() {
return widget.asWidget();
}
@Override
public Widget getWidget() {
return widget.asWidget();
}
@Override
public void setWidget(Widget widgetToAttach) {
Widget parentAsWidget = widget.asWidget().getParent();
if (!(parentAsWidget instanceof HTMLPanel)) {
throw new WrongParentTypeException("The parent of ReplacePanel must be of type HTMLPanel");
}
HTMLPanel parent = (HTMLPanel) parentAsWidget;
parent.addAndReplaceElement(widgetToAttach, widget.asWidget().getElement());
widget = widgetToAttach;
}
@Override
public void setWidget(IsWidget widget) {
setWidget(widget.asWidget());
}
}
@jDramaix
Copy link

Just throw the exception inside the setWidget() method

@jDramaix
Copy link

and you don't detach logically the old widget

@christiangoudreau
Copy link
Author

Yes, that is done through the addAndReplace of the parent html panel

@jDramaix
Copy link

you right !

LGTM !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment