From: Joonas Lehtinen Date: Mon, 19 May 2008 10:03:35 +0000 (+0000) Subject: Added a convenience constructor for BeanItem X-Git-Tag: 6.7.0.beta1~4745 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e23ca8ec3702d1ef69d0f5115b417b976c1d05cf;p=vaadin-framework.git Added a convenience constructor for BeanItem svn changeset:4543/svn branch:trunk --- 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 @@ -122,6 +122,54 @@ public class BeanItem extends PropertysetItem { } + /** + *

+ * Creates a new instance of BeanItem and adds all listed + * properties of a Java Bean to it - in specified order. The properties are + * identified by their respective bean names. + *

+ * + *

+ * Note : This version only supports introspectable bean properties and + * their getter and setter methods. Stand-alone is and + * are methods are not supported. + *

+ * + * @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. *