To use it we need to overwrite:
protected abstract Widget createInnerWidget();
Yes, it is a hack. And this hack can become invalid if GWT
implementation internals changed.
public abstract class LasyComposite extends Composite
{
private Element dummyDiv;
public LasyComposite()
{
dummyDiv = DOM.createDiv();
setElement(dummyDiv);
}
public Element getElement()
{
return dummyDiv != null ? dummyDiv : super.getElement();
}
protected abstract Widget createInnerWidget();
public void setVisible(boolean visible)
{
if (visible)
if (dummyDiv != null)
if (getParent() != null)
initWidget(createInnerWidget());
super.setVisible(visible);
}
protected void onAttach()
{
if (dummyDiv == null)
super.onAttach();
}
protected void onDetach()
{
if (dummyDiv == null)
super.onDetach();
}
protected void initWidget(Widget widget)
{
super.initWidget(widget);
Element parentElement = DOM.getParent(dummyDiv);
if (parentElement != null)
{
DOM.insertChild(parentElement, widget.getElement(),
DOM.getChildIndex(parentElement, dummyDiv));
DOM.removeChild(parentElement, dummyDiv);
}
dummyDiv = null;
Widget parentWidget = getParent ();
if (parentWidget != null && parentWidget.isAttached())
onAttach();
}
}
You don't need to hack it that badly, especially for the tab panel, you can do it so it shouldn't break when gwt is updated.
For each tab in the tab panel, use a class that has a constructor which does pretty much nothing and add it when you create the tab panel, Then in the tab onBeforeTabSelected(), call the create method
For a StackPanel you have to subclass it and override the showStack(int) method.
For a DeckPanel subclass and override showWidget(int).
You get the idea.
You could build your own structure using the same principle. You can also abstract most of the code and hide it away. The 'skeleton' download on my site uses it to lazy load menu items (if you want to look at a working example), and there's a link to a test site (CB2000) which has 3 levels of colourful lazy loading.
To save you going there, all you need is a class something like this:
class MyContent extends VerticalPanel // or whatever you like
{
boolean created = false;
MyContent()
{
add(new HTML("This is done on creation"));
}
create()
{
add(new HTML("This is done every time"));
add(new HTML("this class is displayed"));
if(created)return;
add(new HTML("This content is only created"));
add(new HTML("the first time through or if"));
add(new HTML("the boolean is reset by the"));
add(new HTML("program (button, timer...)");
add(new HTML("Most of your code will be here"));
created = true;
}
}