View Javadoc

1   package org.shortbrain.vaadin.container;
2   
3   import java.util.Collection;
4   
5   import com.vaadin.data.util.AbstractBeanContainer;
6   import com.vaadin.data.util.BeanContainer;
7   import com.vaadin.data.util.BeanItem;
8   import com.vaadin.data.util.BeanItemContainer;
9   
10  /**
11   * An extension of the {@link BeanContainer}, which adds shorcuts properties.
12   * 
13   * @author Vincent Demeester <vincent@shortbrain.org>
14   * 
15   * @param <BEANTYPE>
16   *            The type of the Bean
17   * 
18   * @since 0.2.0
19   * 
20   * @see ShortcutBeanContainer
21   * @see BeanItemContainer
22   */
23  public class ShortcutBeanItemContainer<BEANTYPE> extends ShortcutBeanContainer<BEANTYPE, BEANTYPE> implements IShortcutBeanContainer {
24  
25      private static final long serialVersionUID = 4221589659162741891L;
26  
27      /**
28       * Bean identity resolver that returns the bean itself as its item identifier.
29       * 
30       * This corresponds to the old behavior of {@link BeanItemContainer}, and requires suitable (identity-based) equals() and hashCode()
31       * methods on the beans.
32       * 
33       * @param <BT>
34       * 
35       * @since 6.5
36       */
37      private static class IdentityBeanIdResolver<BT> implements BeanIdResolver<BT, BT> {
38  
39          private static final long serialVersionUID = 1L;
40  
41          public BT getIdForBean(BT bean) {
42              return bean;
43          }
44  
45      }
46  
47      /**
48       * Constructs a {@code ShortcutBeanItemContainer}.
49       * 
50       * @param type
51       *            the type of the beans that will be added to the container.
52       * @throws IllegalArgumentException
53       *             If {@code type} is null
54       */
55      public ShortcutBeanItemContainer(Class<? super BEANTYPE> type) {
56          super(type);
57          super.setBeanIdResolver(new IdentityBeanIdResolver<BEANTYPE>());
58      }
59  
60      /**
61       * Constructs a {@code ShortcutBeanItemContainer} and adds the given beans to it.
62       * 
63       * @param type
64       *            the type of the beans that will be added to the container.
65       * @param collection
66       *            a {@link Collection} of beans (can be empty or null).
67       * @throws IllegalArgumentException
68       *             If {@code type} is null
69       */
70      public ShortcutBeanItemContainer(Class<? super BEANTYPE> type, Collection<? extends BEANTYPE> collection)
71              throws IllegalArgumentException {
72          super(type);
73          super.setBeanIdResolver(new IdentityBeanIdResolver<BEANTYPE>());
74  
75          if (collection != null) {
76              addAll(collection);
77          }
78      }
79  
80      /**
81       * Adds all the beans from a {@link Collection} in one go. More efficient than adding them one by one.
82       * 
83       * @param collection
84       *            The collection of beans to add. Must not be null.
85       */
86      @Override
87      public void addAll(Collection<? extends BEANTYPE> collection) {
88          super.addAll(collection);
89      }
90  
91      /**
92       * Adds the bean after the given bean.
93       * 
94       * The bean is used both as the item contents and as the item identifier.
95       * 
96       * @param previousItemId
97       *            the bean (of type BT) after which to add newItemId
98       * @param newItemId
99       *            the bean (of type BT) to add (not null)
100      * 
101      * @see com.vaadin.data.Container.Ordered#addItemAfter(Object, Object)
102      */
103     @Override
104     @SuppressWarnings("unchecked")
105     public BeanItem<BEANTYPE> addItemAfter(Object previousItemId, Object newItemId) throws IllegalArgumentException {
106         return super.addBeanAfter((BEANTYPE) previousItemId, (BEANTYPE) newItemId);
107     }
108 
109     /**
110      * Adds a new bean at the given index.
111      * 
112      * The bean is used both as the item contents and as the item identifier.
113      * 
114      * @param index
115      *            Index at which the bean should be added.
116      * @param newItemId
117      *            The bean to add to the container.
118      * @return Returns the new BeanItem or null if the operation fails.
119      */
120     @Override
121     @SuppressWarnings("unchecked")
122     public BeanItem<BEANTYPE> addItemAt(int index, Object newItemId) throws IllegalArgumentException {
123         return super.addBeanAt(index, (BEANTYPE) newItemId);
124     }
125 
126     /**
127      * Adds the bean to the Container.
128      * 
129      * The bean is used both as the item contents and as the item identifier.
130      * 
131      * @see com.vaadin.data.Container#addItem(Object)
132      */
133     @Override
134     @SuppressWarnings("unchecked")
135     public BeanItem<BEANTYPE> addItem(Object itemId) {
136         return super.addBean((BEANTYPE) itemId);
137     }
138 
139     /**
140      * Adds the bean to the Container.
141      * 
142      * The bean is used both as the item contents and as the item identifier.
143      * 
144      * @see com.vaadin.data.Container#addItem(Object)
145      */
146     @Override
147     public BeanItem<BEANTYPE> addBean(BEANTYPE bean) {
148         return addItem(bean);
149     }
150 
151     /**
152      * Unsupported in BeanItemContainer.
153      */
154     @Override
155     public void setBeanIdResolver(AbstractBeanContainer.BeanIdResolver<BEANTYPE, BEANTYPE> beanIdResolver)
156             throws UnsupportedOperationException {
157         throw new UnsupportedOperationException("BeanItemContainer always uses an IdentityBeanIdResolver");
158     }
159 }