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.

BeanContainer.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util;
  5. import java.util.Collection;
  6. /**
  7. * An in-memory container for JavaBeans.
  8. *
  9. * <p>
  10. * The properties of the container are determined automatically by introspecting
  11. * the used JavaBean class. Only beans of the same type can be added to the
  12. * container.
  13. * </p>
  14. *
  15. * <p>
  16. * In BeanContainer (unlike {@link BeanItemContainer}), the item IDs do not have
  17. * to be the beans themselves. The container can be used either with explicit
  18. * item IDs or the item IDs can be generated when adding beans.
  19. * </p>
  20. *
  21. * <p>
  22. * To use explicit item IDs, use the methods {@link #addItem(Object, Object)},
  23. * {@link #addItemAfter(Object, Object, Object)} and
  24. * {@link #addItemAt(int, Object, Object)}.
  25. * </p>
  26. *
  27. * <p>
  28. * If a bean id resolver is set using
  29. * {@link #setBeanIdResolver(com.vaadin.data.util.AbstractBeanContainer.BeanIdResolver)}
  30. * or {@link #setBeanIdProperty(Object)}, the methods {@link #addBean(Object)},
  31. * {@link #addBeanAfter(Object, Object)}, {@link #addBeanAt(int, Object)} and
  32. * {@link #addAll(java.util.Collection)} can be used to add items to the
  33. * container. If one of these methods is called, the resolver is used to
  34. * generate an identifier for the item (must not return null).
  35. * </p>
  36. *
  37. * <p>
  38. * Note that explicit item identifiers can also be used when a resolver has been
  39. * set by calling the addItem*() methods - the resolver is only used when adding
  40. * beans using the addBean*() or {@link #addAll(Collection)} methods.
  41. * </p>
  42. *
  43. * <p>
  44. * It is not possible to add additional properties to the container and nested
  45. * bean properties are not supported.
  46. * </p>
  47. *
  48. * @param <IDTYPE>
  49. * The type of the item identifier
  50. * @param <BEANTYPE>
  51. * The type of the Bean
  52. *
  53. * @see AbstractBeanContainer
  54. * @see BeanItemContainer
  55. *
  56. * @since 6.5
  57. */
  58. public class BeanContainer<IDTYPE, BEANTYPE> extends
  59. AbstractBeanContainer<IDTYPE, BEANTYPE> {
  60. public BeanContainer(Class<? super BEANTYPE> type) {
  61. super(type);
  62. }
  63. /**
  64. * Adds the bean to the Container.
  65. *
  66. * @see com.vaadin.data.Container#addItem(Object)
  67. */
  68. @Override
  69. public BeanItem<BEANTYPE> addItem(IDTYPE itemId, BEANTYPE bean) {
  70. if (itemId != null && bean != null) {
  71. return super.addItem(itemId, bean);
  72. } else {
  73. return null;
  74. }
  75. }
  76. /**
  77. * Adds the bean after the given item id.
  78. *
  79. * @see com.vaadin.data.Container.Ordered#addItemAfter(Object, Object)
  80. */
  81. @Override
  82. public BeanItem<BEANTYPE> addItemAfter(IDTYPE previousItemId,
  83. IDTYPE newItemId, BEANTYPE bean) {
  84. if (newItemId != null && bean != null) {
  85. return super.addItemAfter(previousItemId, newItemId, bean);
  86. } else {
  87. return null;
  88. }
  89. }
  90. /**
  91. * Adds a new bean at the given index.
  92. *
  93. * The bean is used both as the item contents and as the item identifier.
  94. *
  95. * @param index
  96. * Index at which the bean should be added.
  97. * @param newItemId
  98. * The item id for the bean to add to the container.
  99. * @param bean
  100. * The bean to add to the container.
  101. *
  102. * @return Returns the new BeanItem or null if the operation fails.
  103. */
  104. @Override
  105. public BeanItem<BEANTYPE> addItemAt(int index, IDTYPE newItemId,
  106. BEANTYPE bean) {
  107. if (newItemId != null && bean != null) {
  108. return super.addItemAt(index, newItemId, bean);
  109. } else {
  110. return null;
  111. }
  112. }
  113. // automatic item id resolution
  114. /**
  115. * Sets the bean id resolver to use a property of the beans as the
  116. * identifier.
  117. *
  118. * @param propertyId
  119. * the identifier of the property to use to find item identifiers
  120. */
  121. public void setBeanIdProperty(Object propertyId) {
  122. setBeanIdResolver(createBeanPropertyResolver(propertyId));
  123. }
  124. @Override
  125. // overridden to make public
  126. public void setBeanIdResolver(
  127. BeanIdResolver<IDTYPE, BEANTYPE> beanIdResolver) {
  128. super.setBeanIdResolver(beanIdResolver);
  129. }
  130. @Override
  131. // overridden to make public
  132. public BeanItem<BEANTYPE> addBean(BEANTYPE bean)
  133. throws IllegalStateException, IllegalArgumentException {
  134. return super.addBean(bean);
  135. }
  136. @Override
  137. // overridden to make public
  138. public BeanItem<BEANTYPE> addBeanAfter(IDTYPE previousItemId, BEANTYPE bean)
  139. throws IllegalStateException, IllegalArgumentException {
  140. return super.addBeanAfter(previousItemId, bean);
  141. }
  142. @Override
  143. // overridden to make public
  144. public BeanItem<BEANTYPE> addBeanAt(int index, BEANTYPE bean)
  145. throws IllegalStateException, IllegalArgumentException {
  146. return super.addBeanAt(index, bean);
  147. }
  148. @Override
  149. // overridden to make public
  150. public void addAll(Collection<? extends BEANTYPE> collection)
  151. throws IllegalStateException {
  152. super.addAll(collection);
  153. }
  154. }