summaryrefslogtreecommitdiffstats
path: root/src/com
diff options
context:
space:
mode:
authorHenri Sara <henri.sara@itmill.com>2010-12-01 15:14:18 +0000
committerHenri Sara <henri.sara@itmill.com>2010-12-01 15:14:18 +0000
commit461448b37291541cfc6ae9246d382e4dcba651e8 (patch)
tree565916ee1fb190eef5847453533552a62b55d7b0 /src/com
parent477667763afb1a69e22c708425605326bcbc0620 (diff)
downloadvaadin-framework-461448b37291541cfc6ae9246d382e4dcba651e8.tar.gz
vaadin-framework-461448b37291541cfc6ae9246d382e4dcba651e8.zip
#6074 bean property based item id resolver for BeanContainer
svn changeset:16256/svn branch:6.5
Diffstat (limited to 'src/com')
-rw-r--r--src/com/vaadin/data/util/AbstractBeanContainer.java70
-rw-r--r--src/com/vaadin/data/util/BeanContainer.java14
2 files changed, 82 insertions, 2 deletions
diff --git a/src/com/vaadin/data/util/AbstractBeanContainer.java b/src/com/vaadin/data/util/AbstractBeanContainer.java
index 4708af2a08..499f8593fa 100644
--- a/src/com/vaadin/data/util/AbstractBeanContainer.java
+++ b/src/com/vaadin/data/util/AbstractBeanContainer.java
@@ -6,6 +6,8 @@ package com.vaadin.data.util;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.Serializable;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
@@ -85,6 +87,62 @@ public abstract class AbstractBeanContainer<IDTYPE, BT> implements Indexed,
}
/**
+ * A item identifier resolver that returns the value of a bean property.
+ *
+ * The bean must have a getter for the property, and the getter must return
+ * an object of type IDTYPE.
+ */
+ protected class PropertyBasedBeanIdResolver implements
+ BeanIdResolver<IDTYPE, BT> {
+
+ private final Object propertyId;
+ private transient Method getMethod;
+
+ public PropertyBasedBeanIdResolver(Object propertyId) {
+ if (propertyId == null) {
+ throw new IllegalArgumentException(
+ "Property identifier must not be null");
+ }
+ this.propertyId = propertyId;
+ if (getGetter() == null) {
+ throw new IllegalArgumentException(
+ "Missing accessor for property " + propertyId);
+ }
+ }
+
+ private Method getGetter() {
+ if (getMethod == null) {
+ try {
+ String propertyName = propertyId.toString();
+ if (Character.isLowerCase(propertyName.charAt(0))) {
+ final char[] buf = propertyName.toCharArray();
+ buf[0] = Character.toUpperCase(buf[0]);
+ propertyName = new String(buf);
+ }
+
+ getMethod = getBeanType().getMethod("get" + propertyName,
+ new Class[] {});
+ } catch (NoSuchMethodException ignored) {
+ throw new IllegalArgumentException();
+ }
+ }
+ return getMethod;
+ }
+
+ @SuppressWarnings("unchecked")
+ public IDTYPE getIdForBean(BT bean) throws IllegalArgumentException {
+ try {
+ return (IDTYPE) getGetter().invoke(bean);
+ } catch (IllegalAccessException e) {
+ throw new IllegalArgumentException(e);
+ } catch (InvocationTargetException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ }
+
+ /**
* The resolver that finds the item ID for a bean, or null not to use
* automatic resolving.
*
@@ -1020,4 +1078,16 @@ public abstract class AbstractBeanContainer<IDTYPE, BT> implements Indexed,
return beanIdResolver;
}
+ /**
+ * Create an item identifier resolver using a named bean property.
+ *
+ * @param propertyId
+ * property identifier, which must map to a getter in BT
+ * @return created resolver
+ */
+ protected BeanIdResolver<IDTYPE, BT> createBeanPropertyResolver(
+ Object propertyId) {
+ return new PropertyBasedBeanIdResolver(propertyId);
+ }
+
}
diff --git a/src/com/vaadin/data/util/BeanContainer.java b/src/com/vaadin/data/util/BeanContainer.java
index 911e696229..dbfc197674 100644
--- a/src/com/vaadin/data/util/BeanContainer.java
+++ b/src/com/vaadin/data/util/BeanContainer.java
@@ -117,9 +117,19 @@ public class BeanContainer<IDTYPE, BT> extends
// automatic item id resolution
+ /**
+ * Sets the bean id resolver to use a property of the beans as the
+ * identifier.
+ *
+ * @param propertyId
+ * the identifier of the property to use to find item identifiers
+ */
+ public void setBeanIdProperty(Object propertyId) {
+ setIdResolver(createBeanPropertyResolver(propertyId));
+ }
+
@Override
- public void setIdResolver(
- com.vaadin.data.util.AbstractBeanContainer.BeanIdResolver<IDTYPE, BT> beanIdResolver) {
+ public void setIdResolver(BeanIdResolver<IDTYPE, BT> beanIdResolver) {
super.setIdResolver(beanIdResolver);
}