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.5KB

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