summaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/data/util/BeanItem.java
blob: b867491cdb2d194fc6d68a4828c2d75d44359232 (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* 
@ITMillApache2LicenseForJavaFiles@
 */

package com.itmill.toolkit.data.util;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Arrays;
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 which this Item is based on.
     */
    private final Object bean;

    /**
     * <p>
     * Creates a new instance of <code>BeanItem</code> and adds all properties
     * of a Java Bean to it. The properties are identified by their respective
     * bean names.
     * </p>
     * 
     * <p>
     * Note : This version only supports introspectable bean properties and
     * their getter and setter methods. Stand-alone <code>is</code> and
     * <code>are</code> 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
            final BeanInfo info = Introspector.getBeanInfo(bean.getClass());
            final PropertyDescriptor[] pd = info.getPropertyDescriptors();

            // Add all the bean properties as MethodProperties to this Item
            for (int i = 0; i < pd.length; i++) {
                final Method getMethod = pd[i].getReadMethod();
                final Method setMethod = pd[i].getWriteMethod();
                final Class<?> type = pd[i].getPropertyType();
                final String name = pd[i].getName();

                if ((getMethod != null)
                        && getMethod.getDeclaringClass() != Object.class) {
                    final Property p = new MethodProperty(type, bean,
                            getMethod, setMethod);
                    addItemProperty(name, p);
                }
            }
        } catch (final java.beans.IntrospectionException ignored) {
        }
    }

    /**
     * <p>
     * Creates a new instance of <code>BeanItem</code> 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 : This version only supports introspectable bean properties and
     * their getter and setter methods. Stand-alone <code>is</code> and
     * <code>are</code> methods are not supported.
     * </p>
     * 
     * @param bean
     *            the Java Bean to copy properties from.
     * @param propertyIds
     *            id of the property.
     */
    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
            final BeanInfo info = Introspector.getBeanInfo(bean.getClass());
            final PropertyDescriptor[] pd = info.getPropertyDescriptors();

            // Add all the bean properties as MethodProperties to this Item
            final Iterator iter = propertyIds.iterator();
            while (iter.hasNext()) {
                final Object id = iter.next();
                for (int i = 0; i < pd.length; i++) {
                    final String name = pd[i].getName();
                    if (name.equals(id)) {
                        final Method getMethod = pd[i].getReadMethod();
                        final Method setMethod = pd[i].getWriteMethod();
                        final Class<?> type = pd[i].getPropertyType();
                        if ((getMethod != null)) {
                            final Property p = new MethodProperty(type, bean,
                                    getMethod, setMethod);
                            addItemProperty(name, p);
                        }
                    }
                }
            }

        } catch (final java.beans.IntrospectionException ignored) {
        }

    }

    /**
     * <p>
     * Creates a new instance of <code>BeanItem</code> 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 : This version only supports introspectable bean properties and
     * their getter and setter methods. Stand-alone <code>is</code> and
     * <code>are</code> methods are not supported.
     * </p>
     * 
     * @param bean
     *            the Java Bean to copy properties from.
     * @param propertyIds
     *            ids of the properties.
     */
    public BeanItem(Object bean, String[] propertyIds) {
        this(bean, Arrays.asList(propertyIds));
    }

    /**
     * Gets the underlying JavaBean object.
     * 
     * @return the bean object.
     */
    public Object getBean() {
        return bean;
    }

}