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
|
/*
@ITMillApache2LicenseForJavaFiles@
*/
package com.vaadin.data.util;
import java.lang.reflect.Constructor;
import com.vaadin.data.Property;
/**
* A simple data object containing one typed value. This class is a
* straightforward implementation of the the {@link com.vaadin.data.Property}
* interface.
*
* @author IT Mill Ltd.
* @version
* @VERSION@
* @since 3.0
*/
@SuppressWarnings("serial")
public class ObjectProperty<T> extends AbstractProperty {
/**
* The value contained by the Property.
*/
private T value;
/**
* Data type of the Property's value.
*/
private final Class<T> type;
/**
* Creates a new instance of ObjectProperty with the given value. The type
* of the property is automatically initialized to be the type of the given
* value.
*
* @param value
* the Initial value of the Property.
*/
@SuppressWarnings("unchecked")
// the cast is safe, because an object of type T has class Class<T>
public ObjectProperty(T value) {
this(value, (Class<T>) value.getClass());
}
/**
* Creates a new instance of ObjectProperty with the given value and type.
*
* Any value of type Object is accepted because, if the type class contains
* a string constructor, the toString of the value is used to create the new
* value. See {@link #setValue(Object)}.
*
* @param value
* the Initial value of the Property.
* @param type
* the type of the value. The value must be assignable to given
* type.
*/
public ObjectProperty(Object value, Class<T> type) {
// Set the values
this.type = type;
setValue(value);
}
/**
* Creates a new instance of ObjectProperty with the given value, type and
* read-only mode status.
*
* Any value of type Object is accepted, see
* {@link #ObjectProperty(Object, Class)}.
*
* @param value
* the Initial value of the property.
* @param type
* the type of the value. <code>value</code> must be assignable
* to this type.
* @param readOnly
* Sets the read-only mode.
*/
public ObjectProperty(Object value, Class<T> type, boolean readOnly) {
this(value, type);
setReadOnly(readOnly);
}
/**
* Returns the type of the ObjectProperty. The methods <code>getValue</code>
* and <code>setValue</code> must be compatible with this type: one must be
* able to safely cast the value returned from <code>getValue</code> to the
* given type and pass any variable assignable to this type as an argument
* to <code>setValue</code>.
*
* @return type of the Property
*/
public final Class<T> getType() {
return type;
}
/**
* Gets the value stored in the Property.
*
* @return the value stored in the Property
*/
public T getValue() {
return value;
}
/**
* Sets the value of the property. This method supports setting from
* <code>String</code> if either <code>String</code> is directly assignable
* to property type, or the type class contains a string constructor.
*
* @param newValue
* the New value of the property.
* @throws <code>Property.ReadOnlyException</code> if the object is in
* read-only mode
* @throws <code>Property.ConversionException</code> if the newValue can't
* be converted into the Property's native type directly or through
* <code>String</code>
*/
public void setValue(Object newValue) throws Property.ReadOnlyException,
Property.ConversionException {
// Checks the mode
if (isReadOnly()) {
throw new Property.ReadOnlyException();
}
// Tries to assign the compatible value directly
if (newValue == null || type.isAssignableFrom(newValue.getClass())) {
@SuppressWarnings("unchecked")
// the cast is safe after an isAssignableFrom check
T value = (T) newValue;
this.value = value;
} else {
try {
// Gets the string constructor
final Constructor<T> constr = getType().getConstructor(
new Class[] { String.class });
// Creates new object from the string
value = constr
.newInstance(new Object[] { newValue.toString() });
} catch (final java.lang.Exception e) {
throw new Property.ConversionException(e);
}
}
fireValueChange();
}
}
|