1
2
3
4
5
6
7
8
9
10
11
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
27
28
29
30
31 public final class ContainerUtils {
32
33
34
35
36 private ContainerUtils() {
37 }
38
39
40
41
42
43
44
45
46
47
48
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
59 container = new IndexedContainer();
60 } else if (containerClass == Hierarchical.class) {
61
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
74
75
76
77
78
79
80
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
93
94
95
96
97
98
99
100
101
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
113
114
115
116
117
118
119
120
121
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
139
140
141
142
143
144
145
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
156
157
158
159
160
161
162
163
164
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
178
179
180
181
182
183
184
185
186
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 }