Thursday, August 2, 2012

GWT Tricks #3

I love when a simple and elegant solution can be easily integrated into my software.  That doesn't always happen, but I truly believe in the KISS principle, especially when architecting and building complex UI applications that have lots of moving parts.

It was brought to my attention that my empty GWT tables were displaying "1-1 of 0" for my SimplePager display.  This was not my code, but was coming from Googles GWT pager implementation.  A web search showed this was an issue that a lot of folks were complaining about.

To be fair, there's no data in the table, and it clearly says "0" items.   Some web searches showed solutions about overriding GWT code and reimplementing the counter mechanisms.  That led me on my way to a solution.  If my table was empty, I want to display my own text, something like "0 of 0", otherwise, I want to let GWT do the calculation.

The solution was to create my own class that extends the GWT SimplePager and then override the createText() method that displays the counter information:



public class MyPager extends SimplePager
{
    public MyPager(final TextLocation center, final Resources pagerResources, final boolean showFastForwardButton,
                       final int fastForwardRows, final boolean showLastPageButton)
    {
        super(center, pagerResources, showFastForwardButton, fastForwardRows, showLastPageButton);
    }


    // Bug with pager that says 1-1 of 0 when the list is empty. fix it to say 0 of 0.
    @Override
    protected String createText()
    {
        final HasRows display = getDisplay();
        final int dataSize = display.getRowCount();

        if (dataSize == 0)
        {
            return "0 of 0";
        }
        else
        {
            return super.createText();
        }
    }
}

Then just use this new pager for my table:

MyPager pager = new MyPager(SimplePager.TextLocation.CENTER, pagerResources, false, 0, true);