I think that may be the best way to go, although you should extend
pageabledataview instead. Then you'd be in trouble if you wanted to
use a plain dataview, but at least you would just have more
functionality then you need, not less.

My first though was something like the attached file, but now I'm not
so sure. This way the user has to do the looping for the columns
themselves.

On 8/12/05, Igor Vaynberg <[EMAIL PROTECTED]> wrote:
> I don't think that's the right approach. The dataprovider is meant to
> provide the data, and not really know how its displayed. Sure the
> sortabledataprovider is kind of on the fance, but I think sorting is more of
> a data artifact rather then a presentation one. I think the breakage into
> columns should be handled by the display component. The rows container can
> pull the iterator out and just pass that to the columns container...a
> orderedrepeatingview inside a dataview? Just thinking out loud...
> -Igor
package wicket.contrib.dataview;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import wicket.Component;
import wicket.WicketRuntimeException;
import wicket.model.AbstractDetachableModel;
import wicket.model.IDetachable;
import wicket.model.IModel;

public abstract class ColumnedDataProvider implements IDataProvider
{
	private int columns;
	private IDataProvider dataProvider;
	
	public ColumnedDataProvider(int columns, IDataProvider dataProvider)
	{
		this.columns = columns;
		this.dataProvider = dataProvider;
	}
	
	public Iterator iterator(int first, int count)
	{
		Iterator i = dataProvider.iterator(first * columns, count * columns);
		ArrayList rows = new ArrayList();
		
		while (i.hasNext())
		{
			ArrayList row = new ArrayList(columns);
			for (int j = 0; j < columns && i.hasNext(); j++)
			{
				row.add(i.next());
			}
			rows.add(row);
		}
		
		return rows.iterator();
	}

	public int size()
	{
		return dataProvider.size() / columns;
	}

	public IModel model(Object object)
	{
		return new DetachingList((List) object);
	}
	
	private static class DetachingList extends AbstractDetachableModel
	{
		private List list;
		
		public DetachingList(List list)
		{
			this.list = list;
		}
		
		public IModel getNestedModel()
		{
			return null;
		}

		protected void onAttach() {}

		protected void onDetach()
		{
			for (Iterator i = list.iterator(); i.hasNext();)
			{
				Object o = i.next();
				if (o instanceof IDetachable)
				{
					((IDetachable) o).detach();
				}
				else
				{
					throw new WicketRuntimeException("List object must be detachable.");
				}
			}
		}

		protected Object onGetObject(Component component)
		{
			return list;
		}

		protected void onSetObject(Component component, Object object)
		{
			this.list = (List) object;
		}
	}
}

Reply via email to