Friday, February 4, 2011

Wicket: NavigatorLabel for GridView

It seems that the NavigatorLabel do not support GridView. So I make it based on the Original NavigatorLabel. It's very simple.

/**
 * Label that provides Showing x to y of z message given for a DataTable. The message can be
 * overridden using the <code>NavigatorLabel</code> property key, the default message is used is of
 * the format <code>Showing ${from} to ${to} of ${of}</code>. The message can also be configured
 * pragmatically by setting it as the model object of the label.
 * 
 * @author Igor Vaynberg (ivaynberg)
 * 
 */
public class GridNavigatorLabel extends Label
{
    private static final long serialVersionUID = 1L;

    // TODO Factor this interface out and let dataview/datatable implement it
    private static interface PageableComponent extends IClusterable
    {
        /**
         * @return total number of rows across all pages
         */
        int getRowCount();

        /**
         * @return current page
         */
        int getCurrentPage();

        /**
         * @return rows per page
         */
        int getRowsPerPage();

        int getColumnsPerRow();
    }

    /**
     * @param id
     *            component id
     * @param table
     *            table
     */
    public GridNavigatorLabel(final String id, final GridView<?> table)
    {
        this(id, new PageableComponent()
        {

            /**
             * 
             */
            private static final long serialVersionUID = 1L;

            public int getCurrentPage()
            {
                return table.getCurrentPage();
            }

            public int getRowCount()
            {
                return table.getRowCount();
            }

            public int getRowsPerPage()
            {
                return table.getRows();
            }
   
            public int getColumnsPerRow()
            {
                return table.getColumns();
            }

        });

    }

    private GridNavigatorLabel(final String id, final PageableComponent table)
    {
        super(id);
        setDefaultModel(new StringResourceModel("NavigatorLabel", this,
            new Model<LabelModelObject>(new LabelModelObject(table)),
            "Showing ${from} to ${to} of ${of}"));
    }

    private class LabelModelObject implements IClusterable
    {
        private static final long serialVersionUID = 1L;
        private final PageableComponent table;

        /**
         * Construct.
         * 
         * @param table
         */
        public LabelModelObject(PageableComponent table)
        {
            this.table = table;
        }

        /**
         * @return "z" in "Showing x to y of z"
         */
        public int getOf()
        {
            return table.getRowCount();
        }

        /**
         * @return "x" in "Showing x to y of z"
         */
        public int getFrom()
        {
            if (getOf() == 0)
            {
                return 0;
            }
            return (table.getCurrentPage() * table.getRowsPerPage() * table.getColumnsPerRow()) + 1;
        }

        /**
         * @return "y" in "Showing x to y of z"
         */
        public int getTo()
        {
            if (getOf() == 0)
            {
                return 0;
            }
            return Math.min(getOf(), getFrom() + table.getRowsPerPage()* table.getColumnsPerRow() - 1);
        }

    }
}

No comments:

Post a Comment