summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJoonas Lehtinen <joonas.lehtinen@itmill.com>2008-05-19 10:03:35 +0000
committerJoonas Lehtinen <joonas.lehtinen@itmill.com>2008-05-19 10:03:35 +0000
commite23ca8ec3702d1ef69d0f5115b417b976c1d05cf (patch)
tree125108c919e7c9e4797fc50c58715fb401ebed29 /src
parent425232ea7cb87e87b9c43e38e68347f973a70979 (diff)
downloadvaadin-framework-e23ca8ec3702d1ef69d0f5115b417b976c1d05cf.tar.gz
vaadin-framework-e23ca8ec3702d1ef69d0f5115b417b976c1d05cf.zip
Added a convenience constructor for BeanItem
svn changeset:4543/svn branch:trunk
Diffstat (limited to 'src')
-rw-r--r--src/com/itmill/toolkit/data/util/BeanItem.java48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/com/itmill/toolkit/data/util/BeanItem.java b/src/com/itmill/toolkit/data/util/BeanItem.java
index 330b592634..7fa40ac37b 100644
--- a/src/com/itmill/toolkit/data/util/BeanItem.java
+++ b/src/com/itmill/toolkit/data/util/BeanItem.java
@@ -123,6 +123,54 @@ public class BeanItem extends PropertysetItem {
}
/**
+ * <p>
+ * Creates a new instance of <code>BeanItem</code> and adds all listed
+ * properties of a Java Bean to it - in specified order. The properties are
+ * identified by their respective bean names.
+ * </p>
+ *
+ * <p>
+ * Note : This version only supports introspectable bean properties and
+ * their getter and setter methods. Stand-alone <code>is</code> and
+ * <code>are</code> methods are not supported.
+ * </p>
+ *
+ * @param bean
+ * the Java Bean to copy properties from.
+ * @param propertyIds
+ * ids of the properties.
+ */
+ public BeanItem(Object bean, String[] propertyIds) {
+ this.bean = bean;
+
+ // Try to introspect, if it fails, we just have an empty Item
+ try {
+ // Create bean information
+ final BeanInfo info = Introspector.getBeanInfo(bean.getClass());
+ final PropertyDescriptor[] pd = info.getPropertyDescriptors();
+
+ // Add all the bean properties as MethodProperties to this Item
+ for (int j = 0; j < propertyIds.length; j++) {
+ final Object id = propertyIds[j];
+ for (int i = 0; i < pd.length; i++) {
+ final String name = pd[i].getName();
+ if (name.equals(id)) {
+ final Method getMethod = pd[i].getReadMethod();
+ final Method setMethod = pd[i].getWriteMethod();
+ final Class type = pd[i].getPropertyType();
+
+ final Property p = new MethodProperty(type, bean,
+ getMethod, setMethod);
+ addItemProperty(name, p);
+ }
+ }
+ }
+
+ } catch (final java.beans.IntrospectionException ignored) {
+ }
+ }
+
+ /**
* Gets the underlying JavaBean object.
*
* @return the bean object.