1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
/* *************************************************************************
IT Mill Toolkit
Development of Browser User Interfaces Made Easy
Copyright (C) 2000-2006 IT Mill Ltd
*************************************************************************
This product is distributed under commercial license that can be found
from the product package on license.pdf. Use of this product might
require purchasing a commercial license from IT Mill Ltd. For guidelines
on usage, see licensing-guidelines.html
*************************************************************************
For more information, contact:
IT Mill Ltd phone: +358 2 4802 7180
Ruukinkatu 2-4 fax: +358 2 4802 7181
20540, Turku email: info@itmill.com
Finland company www: www.itmill.com
Primary source for information and releases: www.itmill.com
********************************************************************** */
package com.itmill.toolkit.data.util;
import java.beans.Introspector;
import java.beans.BeanInfo;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Iterator;
import com.itmill.toolkit.data.Property;
/** A wrapper class for adding the Item interface to any Java Bean.
*
* @author IT Mill Ltd.
* @version @VERSION@
* @since 3.0
*/
public class BeanItem extends PropertysetItem {
/** The bean wich this Item is based on. */
private Object bean;
/** <p>Creates a new instance of BeanItem and adds all properties of a
* Java Bean to it. The properties are identified by their respective
* bean names.</p>
*
* <p>Note that this version only supports introspectable bean
* properties and their getter and setter methods. Stand-alone "is" and
* "are" methods are not supported.</p>
*
* @param bean the Java Bean to copy properties from
*/
public BeanItem(Object bean) {
this.bean = bean;
// Try to introspect, if it fails, we just have an empty Item
try {
// Create bean information
BeanInfo info = Introspector.getBeanInfo(bean.getClass());
PropertyDescriptor[] pd = info.getPropertyDescriptors();
// Add all the bean properties as MethodProperties to this Item
for (int i = 0; i < pd.length; i++) {
Method getMethod = pd[i].getReadMethod();
Method setMethod = pd[i].getWriteMethod();
Class type = pd[i].getPropertyType();
String name = pd[i].getName();
Property p =
new MethodProperty(type, bean, getMethod, setMethod);
addItemProperty(name, p);
}
} catch (java.beans.IntrospectionException ignored) {
}
}
/** <p>Creates a new instance of BeanItem and adds all listed properties of a
* Java Bean to it - in specified order. The properties are identified by their
* respective bean names.</p>
*
* <p>Note that this version only supports introspectable bean
* properties and their getter and setter methods. Stand-alone "is" and
* "are" methods are not supported.</p>
*
* @param bean the Java Bean to copy properties from
*/
public BeanItem(Object bean, Collection propertyIds) {
this.bean = bean;
// Try to introspect, if it fails, we just have an empty Item
try {
// Create bean information
BeanInfo info = Introspector.getBeanInfo(bean.getClass());
PropertyDescriptor[] pd = info.getPropertyDescriptors();
// Add all the bean properties as MethodProperties to this Item
for (Iterator iter = propertyIds.iterator(); iter.hasNext();) {
Object id = iter.next();
for (int i = 0; i < pd.length; i++) {
String name = pd[i].getName();
if (name.equals(id)) {
Method getMethod = pd[i].getReadMethod();
Method setMethod = pd[i].getWriteMethod();
Class type = pd[i].getPropertyType();
Property p =
new MethodProperty(
type,
bean,
getMethod,
setMethod);
addItemProperty(name, p);
}
}
}
} catch (java.beans.IntrospectionException ignored) {
}
}
/** Get the underlying JavaBean object.
* @return the bean object.
*/
public Object getBean() {
return bean;
}
}
|