On many project I just need to show a list of beans on an AJAX default
datatable... There is already a ListDataProvider... but this component
require a Sortable version... and I always end up rolling out something
like the class bellow. Would it make sense to include a similar class on
wicket code base?

/**
 *  Sortable version of ListDataProvider.

 */

public class SortableListDataProvider<T extends Serializable, S>
extends SortableDataProvider<T, S> {

    private static final long serialVersionUID = 1L;

    /** reference to the list used as dataprovider for the dataview */
    private final List<T> list;

    /**
     * Constructs an empty provider. Useful for lazy loading together
with {@linkplain #getData()}
     */
    public SortableListDataProvider()
    {
        this(Collections.<T> emptyList());
    }

    /**
     *
     * @param list
     *            the list used as dataprovider for the dataview
     */
    public SortableListDataProvider(List<T> list)
    {
        if (list == null)
        {
            throw new IllegalArgumentException("argument [list] cannot
be null");
        }

        this.list = list;
    }

    /**
     * Subclass to lazy load the list
     *
     * @return The list
     */
    protected List<T> getData()
    {
        return list;
    }

    @Override
    public Iterator<? extends T> iterator(final long first, final long count)
    {
        List<T> list = getData();

        long toIndex = first + count;
        if (toIndex > list.size())
        {
            toIndex = list.size();
        }
        return list.subList((int)first, (int)toIndex).listIterator();
    }

    /**
     * @see IDataProvider#size()
     */
    @Override
    public long size()
    {
        return getData().size();
    }

    /**
     * @see IDataProvider#model(Object)
     */
    @Override
    public IModel<T> model(T object)
    {
        return new Model<T>(object);
    }

    /**
     * @see org.apache.wicket.model.IDetachable#detach()
     */
    @Override
    public void detach()
    {
    }
}

-- 
Regards - Ernesto Reinaldo Barreiro

Reply via email to