Skip to content

Instantly share code, notes, and snippets.

@rdwallis
Forked from christiangoudreau/gist:42e51a2cbb20dd204f4b
Last active January 24, 2016 22:22
Show Gist options
  • Save rdwallis/27813ab85dd96c69b09c to your computer and use it in GitHub Desktop.
Save rdwallis/27813ab85dd96c69b09c to your computer and use it in GitHub Desktop.
/**
* Simple panel that wraps a GWT simple panel to be replaced by the widgetToAttach we set while updating the references.
* <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() {
SimplePanel simplePanel = new com.google.gwt.user.client.ui.SimplePanel();
widget = simplePanel;
simplePanel.addAttachHandler(new Handler() {
@Override
public void onAttachOrDetach(AttachEvent event) {
Widget parentAsWidget = getParent();
if (!(parentAsWidget instanceof HTMLPanel)) {
throw new WrongParentTypeException("The parent of ReplacePanel must be of type HTMLPanel");
}
}
});
}
@Override
public Widget asWidget() {
return widget.asWidget();
}
@Override
public Widget getWidget() {
return widget.asWidget();
}
@Override
public void setWidget(Widget widgetToAttach) {
Widget parentAsWidget = getParent();
if (parentAsWidget != null && parentAsWidget instanceof HTMLPanel) {
HTMLPanel parent = (HTMLPanel) parentAsWidget;
parent.addAndReplaceElement(widgetToAttach, widget.asWidget().getElement());
widget = widgetToAttach;
}
}
@Override
public void setWidget(IsWidget widget) {
setWidget(widget.asWidget());
}
private Widget getParent() {
return widget.asWidget().getParent();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment