import java.util.Set;
import com.itmill.toolkit.data.Container;
-import com.itmill.toolkit.data.Item;
import com.itmill.toolkit.data.Property;
import com.itmill.toolkit.data.Container.Filterable;
import com.itmill.toolkit.data.Container.Indexed;
*
* @param type
* the class of beans to be used with this containers.
+ * @throws IllegalArgumentException
+ * If the type is null
*/
public BeanItemContainer(Class<BT> type) {
+ if (type == null) {
+ throw new IllegalArgumentException(
+ "The type passed to BeanItemContainer must not be null");
+ }
this.type = type;
model = BeanItem.getPropertyDescriptors(type);
}
/**
- * Constructs BeanItemContainer with given collection of beans in it.
+ * Constructs BeanItemContainer with given collection of beans in it. The
+ * collection must not be empty or an IllegalArgument is thrown.
*
- * @param list
+ * @param collection
* non empty {@link Collection} of beans.
+ * @throws IllegalArgumentException
+ * If the collection is null or empty.
*/
- public BeanItemContainer(Collection<BT> list) {
- type = (Class<BT>) list.iterator().next().getClass();
+ public BeanItemContainer(Collection<BT> collection)
+ throws IllegalArgumentException {
+ if (collection == null || collection.isEmpty()) {
+ throw new IllegalArgumentException(
+ "The collection passed to BeanItemContainer must not be null or empty");
+ }
+
+ type = (Class<BT>) collection.iterator().next().getClass();
model = BeanItem.getPropertyDescriptors(type);
int i = 0;
- for (BT bt : list) {
+ for (BT bt : collection) {
addItemAt(i++, bt);
}
}
*
* @see com.itmill.toolkit.data.Container.Indexed#addItemAt(int, Object)
*/
- public Item addItemAt(int index, Object newItemId)
+ public BeanItem addItemAt(int index, Object newItemId)
throws UnsupportedOperationException {
if (index < 0 || index > size()) {
return null;
* Id of the new item to be added.
* @return Returns new item or null if the operation fails.
*/
- private Item addItemAtInternalIndex(int index, Object newItemId) {
+ private BeanItem addItemAtInternalIndex(int index, Object newItemId) {
// Make sure that the Item has not been created yet
if (allItems.contains(newItemId)) {
return null;
* @see com.itmill.toolkit.data.Container.Ordered#addItemAfter(Object,
* Object)
*/
- public Item addItemAfter(Object previousItemId, Object newItemId)
+ public BeanItem addItemAfter(Object previousItemId, Object newItemId)
throws UnsupportedOperationException {
// only add if the previous item is visible
if (list.contains(previousItemId)) {
*
* @see com.itmill.toolkit.data.Container#addItem(Object)
*/
- public Item addItem(Object itemId) throws UnsupportedOperationException {
+ public BeanItem addItem(Object itemId) throws UnsupportedOperationException {
if (list.size() > 0) {
// add immediately after last visible item
int lastIndex = allItems.indexOf(lastItemId());
return beanToItem.get(itemId).getItemProperty(propertyId);
}
- @SuppressWarnings("unchecked")
- public Collection getContainerPropertyIds() {
+ public Collection<String> getContainerPropertyIds() {
return model.keySet();
}
- public Item getItem(Object itemId) {
+ public BeanItem getItem(Object itemId) {
return beanToItem.get(itemId);
}
- @SuppressWarnings("unchecked")
- public Collection getItemIds() {
- return (Collection) list.clone();
+ public Collection<BT> getItemIds() {
+ return (Collection<BT>) list.clone();
}
public Class<?> getType(Object propertyId) {
--- /dev/null
+package com.itmill.toolkit.tests.components.beanitemcontainer;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.itmill.toolkit.data.util.BeanItemContainer;
+import com.itmill.toolkit.tests.components.TestBase;
+import com.itmill.toolkit.ui.Table;
+
+public class TestBeanItemContainerUsage extends TestBase {
+
+ @Override
+ protected String getDescription() {
+ return "A test for the BeanItemContainer. The table should contain three persons and show their first and last names and their age.";
+ }
+
+ @Override
+ protected Integer getTicketNumber() {
+ return 1061;
+ }
+
+ @Override
+ protected void setup() {
+ Table t = new Table("Table containing Persons");
+ t.setPageLength(5);
+ t.setWidth("100%");
+ List<Person> persons = new ArrayList<Person>();
+ persons.add(new Person("Jones", "Birchman", 35));
+ persons.add(new Person("Marc", "Smith", 30));
+ persons.add(new Person("Greg", "Sandman", 75));
+
+ BeanItemContainer<Person> bic = new BeanItemContainer<Person>(persons);
+ t.setContainerDataSource(bic);
+
+ addComponent(t);
+ }
+
+ public static class Person {
+ private String firstName;
+ private String lastName;
+ private int age;
+
+ public String getFirstName() {
+ return firstName;
+ }
+
+ public void setFirstName(String firstName) {
+ this.firstName = firstName;
+ }
+
+ public String getLastName() {
+ return lastName;
+ }
+
+ public void setLastName(String lastName) {
+ this.lastName = lastName;
+ }
+
+ public int getAge() {
+ return age;
+ }
+
+ public void setAge(int age) {
+ this.age = age;
+ }
+
+ public Person(String firstName, String lastName, int age) {
+ super();
+ this.firstName = firstName;
+ this.lastName = lastName;
+ this.age = age;
+ }
+
+ }
+}