How would I use GWT to create an infinite scroll panel?

gwt

Solution

I was able to confirm the following works in GWT 1.5 at least:

final VerticalPanel vp = new VerticalPanel();
for (int i = 0; i < 40; i++) {
    vp.add(new HTML("Oh oh"));
}
final HTML f = new HTML("END");
vp.add(f);

final ScrollPanel panel = new ScrollPanel(vp);
panel.setHeight("20em");
panel.addScrollListener(new ScrollListener() {

    HTML end = f;
    public void onScroll(Widget widget, int scrollLeft, int scrollTop) {

        int finalPos = end.getAbsoluteTop() + end.getOffsetHeight(); 
        int panelPos = panel.getAbsoluteTop() + panel.getOffsetHeight();
        if (finalPos == panelPos) {
            end = new HTML("MORE !!");
            vp.add(end);
        }

    }
});

Note the interesting bits are the calculations of the positions.

Problem

By infinite scrolling, I mean I initially load a few child widgets, and as the user scrolls down lazily load more to fill it out. Any ideas appreciated. (ideally would like something for GWT 1.6 and 1.5)

Original source