[sample code] A simple BeanItemContainer

I just thought I’d share a simple BeanItemContainer, a container that can hold any type of Java Bean (POJO) for display in table or select components (by using setContainerDataSource())


package examplecontainer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import com.itmill.toolkit.data.Container;
import com.itmill.toolkit.data.Item;
import com.itmill.toolkit.data.Property;
import com.itmill.toolkit.data.util.BeanItem;

public class BeanItemContainer<T> implements Container,
        Container.ItemSetChangeNotifier {

    private ArrayList<ItemSetChangeListener> itemSetChangeListeners;

    private final HashMap<T, BeanItem> itemIdToItemMap;

    private final Class<T> type;

    public BeanItemContainer(Class<T> type) {
        this.type = type;
        itemIdToItemMap = new HashMap<T, BeanItem>();
    }

    @SuppressWarnings("unchecked")
    public boolean addContainerProperty(Object propertyId, Class type,
            Object defaultValue) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    public Object addItem() throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    public Item addItem(Object itemId) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    public Item addBeanItem(T itemId) {
        BeanItem item = null;
        item = new BeanItem(itemId);
        itemIdToItemMap.put(itemId, item);
        fireItemSetChange();
        return item;
    }

    public boolean containsId(Object itemId) {
        return itemIdToItemMap.containsKey(itemId);
    }

    public Property getContainerProperty(Object itemId, Object propertyId) {
        return itemIdToItemMap.get(itemId).getItemProperty(propertyId);
    }

    @SuppressWarnings("unchecked")
    public Collection getContainerPropertyIds() {
        return new BeanItem(newT()).getItemPropertyIds();
    }

    public Item getItem(Object itemId) {
        return itemIdToItemMap.get(itemId);
    }

    @SuppressWarnings("unchecked")
    public Collection getItemIds() {
        return itemIdToItemMap.keySet();
    }

    @SuppressWarnings("unchecked")
    public Class getType(Object propertyId) {
        return new BeanItem(newT()).getItemProperty(propertyId).getClass();
    }

    private T newT() {
        try {
            return type.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean removeAllItems() throws UnsupportedOperationException {
        itemIdToItemMap.clear();
        fireItemSetChange();
        return true;
    }

    public boolean removeContainerProperty(Object propertyId)
            throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    public boolean removeItem(Object itemId)
            throws UnsupportedOperationException {
        itemIdToItemMap.remove(itemId);
        fireItemSetChange();
        return false;
    }

    public int size() {
        return itemIdToItemMap.size();
    }

    public void addListener(ItemSetChangeListener listener) {
        if (itemSetChangeListeners == null)
            itemSetChangeListeners = new ArrayList<ItemSetChangeListener>();

        if (!itemSetChangeListeners.contains(listener))
            itemSetChangeListeners.add(listener);
    }

    public void removeListener(ItemSetChangeListener listener) {
        if (itemSetChangeListeners == null
                || !itemSetChangeListeners.contains(listener))
            return;
        itemSetChangeListeners.remove(listener);
    }

    private void fireItemSetChange() {
        if (itemSetChangeListeners == null)
            return;
        for (ItemSetChangeListener listener : itemSetChangeListeners) {
            listener.containerItemSetChange(new ItemSetChangeEvent() {
                public Container getContainer() {
                    return BeanItemContainer.this;
                }
            });
        }
    }

    public void addAll(List<T> itemIds) {
        for (T itemId : itemIds) {
            addBeanItem(itemId);
        }
    }
}

Usage:


BeanItemContainer<MyPojo> container = new BeanItemContainer<MyPojo>(MyPojo.class);
container.addBeanItem(pojo1);
container.addBeanItem(pojo2);
.
.
.
container.addAll(myListOfPojos);

myTable.setContainerDataSource(container);

This could probably be much better, so feel free to reply with a better solution.

Hope this is of some use!