View Javadoc

1   /*
2    * Copyright 2012 Vincent Demeester<vincent+shortbrain@demeester.fr>.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    * 
7    * http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11   * and limitations under the License.
12   */
13  package org.shortbrain.vaadin.container;
14  
15  import org.shortbrain.vaadin.container.property.PropertyMetadata;
16  
17  import com.vaadin.data.Container;
18  import com.vaadin.data.Container.Filterable;
19  import com.vaadin.data.Container.Hierarchical;
20  import com.vaadin.data.util.AbstractBeanContainer;
21  import com.vaadin.data.util.BeanItemContainer;
22  import com.vaadin.data.util.HierarchicalContainer;
23  import com.vaadin.data.util.IndexedContainer;
24  
25  /**
26   * Util classes for vaadin Container.
27   * 
28   * @author Vincent Demeester <vincent@demeester.fr>
29   * 
30   */
31  public final class ContainerUtils {
32  
33      /**
34       * Private constructor to force the non-instantiation of this class.
35       */
36      private ContainerUtils() {
37      }
38  
39      /**
40       * Initialize a container with the given type.
41       * 
42       * @param containerClass
43       *            type of the container
44       * @return a container.
45       * @throws IllegalAccessException
46       * @throws InstantiationException
47       * @throws IllegalArgumentException
48       *             if containerClass is null.
49       */
50      public static Container initContainer(Class<? extends Container> containerClass) throws InstantiationException,
51              IllegalAccessException {
52          if (containerClass == null) {
53              throw new IllegalArgumentException("containerClass cannot be null.");
54          }
55          Container container = null;
56          if (containerClass.isInterface()) {
57              if (containerClass == Filterable.class) {
58                  // Default Filterable is Indexed
59                  container = new IndexedContainer();
60              } else if (containerClass == Hierarchical.class) {
61                  // Default Hierarchical is HierarchicalContainer
62                  container = new HierarchicalContainer();
63              } else {
64                  throw new InstantiationException(containerClass + " not supported.");
65              }
66          } else {
67              container = containerClass.newInstance();
68          }
69          return container;
70      }
71  
72      /**
73       * Add a property to the given container using {@link PropertyMetadata}.
74       * 
75       * @see ContainerUtils#addContainerProperty(Container, Object, Class, Object)
76       * 
77       * @param container
78       *            the container.
79       * @param propertyMetadata
80       *            the porperty metadata.
81       */
82      public static void addContainerProperty(Container container, PropertyMetadata propertyMetadata) {
83          if (container instanceof AbstractBeanContainer<?, ?>) {
84              addNestedContainerProperty((AbstractBeanContainer<?, ?>) container, propertyMetadata);
85          } else {
86              addContainerProperty(container, propertyMetadata.getPropertyName(), propertyMetadata.getPropertyClass(),
87                      propertyMetadata.getDefaultValue());
88          }
89      }
90  
91      /**
92       * Add a property to the given container.
93       * 
94       * @param container
95       *            the container.
96       * @param propertyName
97       *            the name of the property.
98       * @param propertyClass
99       *            the type of the property.
100      * @param propertyDefaultValue
101      *            the default value of the propertyx
102      */
103     public static void addContainerProperty(Container container, Object propertyName, Class<?> propertyClass,
104             Object propertyDefaultValue) {
105         if (propertyName == null || propertyClass == null) {
106             throw new NullPointerException("propertyName and propertyClass cannot be null.");
107         }
108         container.addContainerProperty(propertyName, propertyClass, propertyDefaultValue);
109     }
110 
111     /**
112      * Add a property to the given container using {@link PropertyMetadata}.
113      * 
114      * @see ContainerUtils#addNestedContainerProperty(AbstractBeanContainer, String)
115      * 
116      * @param container
117      *            the container.
118      * @param propertyMetadata
119      *            the porperty metadata.
120      * 
121      * @since 0.2.0
122      */
123     public static void addNestedContainerProperty(AbstractBeanContainer<?, ?> container,
124             PropertyMetadata propertyMetadata) {
125         if (propertyMetadata.getPropertyAttribute() != null) {
126             if (container instanceof IShortcutBeanContainer) {
127                 addShortcutContainerProperty((IShortcutBeanContainer) container,
128                         propertyMetadata.getPropertyName(), propertyMetadata.getPropertyAttribute());
129             } else {
130                 addNestedContainerProperty(container, propertyMetadata.getPropertyAttribute());
131             }
132         } else {
133             addNestedContainerProperty(container, propertyMetadata.getPropertyName());
134         }
135     }
136 
137     /**
138      * Add a property to the given container (AbstractBeanContainer).
139      * 
140      * @param container
141      *            the container.
142      * @param propertyId
143      *            the id of the property.
144      * 
145      * @since 0.2.0
146      */
147     public static void addNestedContainerProperty(AbstractBeanContainer<?, ?> container, String propertyId) {
148         if (propertyId == null) {
149             throw new NullPointerException("propertyId cannot be null.");
150         }
151         container.addNestedContainerProperty(propertyId);
152     }
153 
154     /**
155      * Add a property to the given container using {@link PropertyMetadata}.
156      * 
157      * @see ContainerUtils#addShortcutContainerProperty(ShortcutBeanContainer, String, String)
158      * 
159      * @param container
160      *            the container.
161      * @param propertyMetadata
162      *            the porperty metadata.
163      * 
164      * @since 0.2.0
165      */
166     public static void addShortcutContainerProperty(IShortcutBeanContainer container,
167             PropertyMetadata propertyMetadata) {
168         if (propertyMetadata.getPropertyAttribute() != null) {
169             addShortcutContainerProperty(container, propertyMetadata.getPropertyName(),
170                     propertyMetadata.getPropertyAttribute());
171         } else {
172             addNestedContainerProperty((AbstractBeanContainer<?, ?>) container, propertyMetadata.getPropertyName());
173         }
174     }
175 
176     /**
177      * Add a property to the given container (ShortcutBeanContainer).
178      * 
179      * @param container
180      *            the container.
181      * @param propertyId
182      *            the id of the property.
183      * @param propertyPath
184      *            the path of the property.
185      * 
186      * @since 0.2.0
187      */
188     public static void addShortcutContainerProperty(IShortcutBeanContainer container, String propertyId,
189             String propertyPath) {
190         if (propertyId == null || propertyPath == null) {
191             throw new NullPointerException("propertyId and propertyPath cannot be null.");
192         }
193         container.addShortcutContainerProperty(propertyId, propertyPath);
194     }
195 }