Forked from christiangoudreau/gist:42e51a2cbb20dd204f4b
Last active
January 24, 2016 22:22
-
-
Save rdwallis/27813ab85dd96c69b09c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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