-
-
Save christiangoudreau/42e51a2cbb20dd204f4b to your computer and use it in GitHub Desktop.
Replace widget
This file contains 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
/** | |
* 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
commented
Aug 11, 2014
- You should throws a exception if the parent is not an HTMLPanel.
- Also you should remove the old widget from the parent at the end
public void onAttachOrDetach(AttachEvent event) {
Widget parentAsWidget = getParent();
if (!(parentAsWidget instanceof HTMLPanel)) {
throw new WrongParentTypeException("The parent of ReplacePanel must be of type HTMLPanel");
}
}
This code is called when the widget is detached and the parent will be null and the exception will be thrown.
Just throw the exception inside the setWidget() method
and you don't detach logically the old widget
Yes, that is done through the addAndReplace of the parent html panel
you right !
LGTM !
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment