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.
/**
* Copyright 2014 Richard Wallis.
* All rights reserved.
*/
package com.wallissoftware.wave.client.widgets;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.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 extends SimplePanel {
static class WrongParentTypeException extends RuntimeException {
public WrongParentTypeException(final String message) {
super(message);
}
}
private IsWidget widget;
@Override
public Widget asWidget() {
return widget != null ? widget.asWidget() : super.asWidget();
}
@Override
public Widget getWidget() {
return widget != null ? widget.asWidget() : super.getWidget();
}
@Override
public void setWidget(final IsWidget widget) {
setWidget(widget.asWidget());
}
@Override
public void setWidget(final Widget widgetToAttach) {
final Widget parentAsWidget = asWidget().getParent();
if (!(parentAsWidget instanceof HTMLPanel)) {
throw new WrongParentTypeException("The parent of ReplacePanel must be of type HTMLPanel");
}
final HTMLPanel parent = (HTMLPanel) parentAsWidget;
parent.addAndReplaceElement(widgetToAttach, asWidget().getElement());
widget = widgetToAttach;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment