You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BeanItem.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.data.util;
  5. import java.beans.BeanInfo;
  6. import java.beans.Introspector;
  7. import java.beans.PropertyDescriptor;
  8. import java.lang.reflect.Method;
  9. import java.util.Collection;
  10. import java.util.Iterator;
  11. import com.itmill.toolkit.data.Property;
  12. /**
  13. * A wrapper class for adding the Item interface to any Java Bean.
  14. *
  15. * @author IT Mill Ltd.
  16. * @version
  17. * @VERSION@
  18. * @since 3.0
  19. */
  20. public class BeanItem extends PropertysetItem {
  21. /**
  22. * The bean which this Item is based on.
  23. */
  24. private final Object bean;
  25. /**
  26. * <p>
  27. * Creates a new instance of <code>BeanItem</code> and adds all properties
  28. * of a Java Bean to it. The properties are identified by their respective
  29. * bean names.
  30. * </p>
  31. *
  32. * <p>
  33. * Note : This version only supports introspectable bean properties and
  34. * their getter and setter methods. Stand-alone <code>is</code> and
  35. * <code>are</code> methods are not supported.
  36. * </p>
  37. *
  38. * @param bean
  39. * the Java Bean to copy properties from.
  40. *
  41. */
  42. public BeanItem(Object bean) {
  43. this.bean = bean;
  44. // Try to introspect, if it fails, we just have an empty Item
  45. try {
  46. // Create bean information
  47. final BeanInfo info = Introspector.getBeanInfo(bean.getClass());
  48. final PropertyDescriptor[] pd = info.getPropertyDescriptors();
  49. // Add all the bean properties as MethodProperties to this Item
  50. for (int i = 0; i < pd.length; i++) {
  51. final Method getMethod = pd[i].getReadMethod();
  52. final Method setMethod = pd[i].getWriteMethod();
  53. final Class type = pd[i].getPropertyType();
  54. final String name = pd[i].getName();
  55. if ((getMethod != null) && (setMethod != null)) {
  56. final Property p = new MethodProperty(type, bean,
  57. getMethod, setMethod);
  58. addItemProperty(name, p);
  59. }
  60. }
  61. } catch (final java.beans.IntrospectionException ignored) {
  62. }
  63. }
  64. /**
  65. * <p>
  66. * Creates a new instance of <code>BeanItem</code> and adds all listed
  67. * properties of a Java Bean to it - in specified order. The properties are
  68. * identified by their respective bean names.
  69. * </p>
  70. *
  71. * <p>
  72. * Note : This version only supports introspectable bean properties and
  73. * their getter and setter methods. Stand-alone <code>is</code> and
  74. * <code>are</code> methods are not supported.
  75. * </p>
  76. *
  77. * @param bean
  78. * the Java Bean to copy properties from.
  79. * @param propertyIds
  80. * id of the property.
  81. */
  82. public BeanItem(Object bean, Collection propertyIds) {
  83. this.bean = bean;
  84. // Try to introspect, if it fails, we just have an empty Item
  85. try {
  86. // Create bean information
  87. final BeanInfo info = Introspector.getBeanInfo(bean.getClass());
  88. final PropertyDescriptor[] pd = info.getPropertyDescriptors();
  89. // Add all the bean properties as MethodProperties to this Item
  90. for (final Iterator iter = propertyIds.iterator(); iter.hasNext();) {
  91. final Object id = iter.next();
  92. for (int i = 0; i < pd.length; i++) {
  93. final String name = pd[i].getName();
  94. if (name.equals(id)) {
  95. final Method getMethod = pd[i].getReadMethod();
  96. final Method setMethod = pd[i].getWriteMethod();
  97. final Class type = pd[i].getPropertyType();
  98. final Property p = new MethodProperty(type, bean,
  99. getMethod, setMethod);
  100. addItemProperty(name, p);
  101. }
  102. }
  103. }
  104. } catch (final java.beans.IntrospectionException ignored) {
  105. }
  106. }
  107. /**
  108. * Gets the underlying JavaBean object.
  109. *
  110. * @return the bean object.
  111. */
  112. public Object getBean() {
  113. return bean;
  114. }
  115. }