|
1
|
|
/*
|
|
2
|
|
* Angkor Web Framework
|
|
3
|
|
*
|
|
4
|
|
* Distributable under LGPL license.
|
|
5
|
|
* See terms of license at gnu.org.
|
|
6
|
|
*/
|
|
7
|
|
|
|
8
|
|
package com.tirsen.angkor.beans;
|
|
9
|
|
|
|
10
|
|
import com.tirsen.angkor.widget.AbstractValueModel;
|
|
11
|
|
import org.apache.commons.logging.Log;
|
|
12
|
|
import org.apache.commons.logging.LogFactory;
|
|
13
|
|
|
|
14
|
|
import java.beans.IntrospectionException;
|
|
15
|
|
import java.beans.Introspector;
|
|
16
|
|
import java.beans.PropertyDescriptor;
|
|
17
|
|
import java.beans.PropertyEditor;
|
|
18
|
|
import java.beans.PropertyEditorManager;
|
|
19
|
|
import java.lang.reflect.InvocationTargetException;
|
|
20
|
|
|
|
21
|
|
/**
|
|
22
|
|
* A value model implementation which fetches and updates it's value according to the patterns of
|
|
23
|
|
* the JavaBean specification, useful together with BeanModel.
|
|
24
|
|
*
|
|
25
|
|
* <!-- $Id: PropertyValueModel.java,v 1.4 2002/10/09 21:37:37 tirsen Exp $ -->
|
|
26
|
|
*
|
|
27
|
|
* @author $Author: tirsen $
|
|
28
|
|
* @version $Revision: 1.4 $
|
|
29
|
|
*/
|
|
30
|
|
class PropertyValueModel extends AbstractValueModel
|
|
31
|
|
{
|
|
32
|
|
private static final Log logger = LogFactory.getLog(PropertyValueModel.class);
|
|
33
|
|
|
|
34
|
|
private Object bean;
|
|
35
|
|
private BeanModel beanModel;
|
|
36
|
|
|
|
37
|
|
private transient PropertyDescriptor property;
|
|
38
|
|
|
|
39
|
|
/**
|
|
40
|
|
* If the object can be null and created at a later stage use the {@link BeanModel} to create
|
|
41
|
|
* a value model for a property.
|
|
42
|
|
* @throws NullPointerException if object is <code>null</code>.
|
|
43
|
|
*/
|
|
44
|
0
|
public PropertyValueModel(Object object, String property)
|
|
45
|
|
{
|
|
46
|
0
|
this(object, findPropertyDescriptor(object.getClass(), property));
|
|
47
|
|
}
|
|
48
|
|
|
|
49
|
16
|
public PropertyValueModel(Object object, PropertyDescriptor property)
|
|
50
|
|
{
|
|
51
|
16
|
this.bean = object;
|
|
52
|
16
|
this.property = property;
|
|
53
|
|
}
|
|
54
|
|
|
|
55
|
6
|
public PropertyValueModel(BeanModel proxy, String property)
|
|
56
|
|
{
|
|
57
|
6
|
this.beanModel = proxy;
|
|
58
|
6
|
this.property = findPropertyDescriptor(proxy.getObjectClass(), property);
|
|
59
|
|
}
|
|
60
|
|
|
|
61
|
6
|
private static PropertyDescriptor findPropertyDescriptor(Class klass, String property)
|
|
62
|
|
{
|
|
63
|
6
|
PropertyDescriptor[] properties;
|
|
64
|
6
|
try
|
|
65
|
|
{
|
|
66
|
6
|
properties = Introspector.getBeanInfo(klass).getPropertyDescriptors();
|
|
67
|
|
}
|
|
68
|
|
catch (IntrospectionException e)
|
|
69
|
|
{
|
|
70
|
0
|
throw new RuntimeException(e.getMessage());
|
|
71
|
|
}
|
|
72
|
|
|
|
73
|
6
|
PropertyDescriptor propertyDesc = null;
|
|
74
|
33
|
for (int i = 0; i < properties.length; i++)
|
|
75
|
|
{
|
|
76
|
33
|
if (properties[i].getName().equals(property))
|
|
77
|
|
{
|
|
78
|
6
|
propertyDesc = properties[i];
|
|
79
|
6
|
break;
|
|
80
|
|
}
|
|
81
|
|
}
|
|
82
|
0
|
if (propertyDesc == null) throw new RuntimeException("No property " + property + " in bean of type " + klass.getName());
|
|
83
|
|
|
|
84
|
6
|
return propertyDesc;
|
|
85
|
|
}
|
|
86
|
|
|
|
87
|
28
|
Object getBean()
|
|
88
|
|
{
|
|
89
|
28
|
Object bean = this.bean;
|
|
90
|
11
|
if (bean == null) bean = beanModel.getBean();
|
|
91
|
28
|
if (bean == null)
|
|
92
|
|
{
|
|
93
|
0
|
throw new IllegalStateException("Tried to access value of property with no valid value-bean.");
|
|
94
|
|
}
|
|
95
|
28
|
return bean;
|
|
96
|
|
}
|
|
97
|
|
|
|
98
|
21
|
public Object getValue()
|
|
99
|
|
{
|
|
100
|
21
|
try
|
|
101
|
|
{
|
|
102
|
21
|
return property.getReadMethod().invoke(getBean(), null);
|
|
103
|
|
}
|
|
104
|
|
catch (IllegalAccessException e)
|
|
105
|
|
{
|
|
106
|
0
|
throw new IllegalAccessError(e.getMessage());
|
|
107
|
|
}
|
|
108
|
|
catch (InvocationTargetException e)
|
|
109
|
|
{
|
|
110
|
|
// handle this better?
|
|
111
|
0
|
throw new RuntimeException(e.getMessage());
|
|
112
|
|
}
|
|
113
|
|
}
|
|
114
|
|
|
|
115
|
7
|
public boolean isReadOnly()
|
|
116
|
|
{
|
|
117
|
7
|
return property.getWriteMethod() == null || getPropertyEditor() == null;
|
|
118
|
|
}
|
|
119
|
|
|
|
120
|
11
|
public Class getValueClass()
|
|
121
|
|
{
|
|
122
|
11
|
return property.getPropertyType();
|
|
123
|
|
}
|
|
124
|
|
|
|
125
|
7
|
public void setValue(Object value)
|
|
126
|
|
{
|
|
127
|
0
|
if (isReadOnly()) throw new IllegalStateException("Trying to update a read-only property.");
|
|
128
|
7
|
try
|
|
129
|
|
{
|
|
130
|
7
|
property.getWriteMethod().invoke(getBean(), new Object[]{value});
|
|
131
|
7
|
changed();
|
|
132
|
|
}
|
|
133
|
|
catch (IllegalAccessException e)
|
|
134
|
|
{
|
|
135
|
0
|
throw new IllegalAccessError("Could not access property \"" + property.getName() + "\"." +
|
|
136
|
|
"Note: If you are using inner classes as models they need to be public.");
|
|
137
|
|
}
|
|
138
|
|
catch (InvocationTargetException e)
|
|
139
|
|
{
|
|
140
|
|
// handle this better?
|
|
141
|
0
|
throw new RuntimeException(e.getMessage());
|
|
142
|
|
}
|
|
143
|
|
}
|
|
144
|
|
|
|
145
|
8
|
public PropertyEditor getPropertyEditor()
|
|
146
|
|
{
|
|
147
|
8
|
PropertyEditor editor = null;
|
|
148
|
|
|
|
149
|
8
|
Class editorClass = property.getPropertyEditorClass();
|
|
150
|
8
|
if (editorClass != null)
|
|
151
|
|
{
|
|
152
|
0
|
try
|
|
153
|
|
{
|
|
154
|
0
|
editor = (PropertyEditor) editorClass.newInstance();
|
|
155
|
|
}
|
|
156
|
|
catch (InstantiationException ignore)
|
|
157
|
|
{
|
|
158
|
|
}
|
|
159
|
|
catch (IllegalAccessException ignore)
|
|
160
|
|
{
|
|
161
|
|
}
|
|
162
|
|
}
|
|
163
|
|
|
|
164
|
8
|
if (editor == null)
|
|
165
|
|
{
|
|
166
|
8
|
editor = PropertyEditorManager.findEditor(getValueClass());
|
|
167
|
|
}
|
|
168
|
|
|
|
169
|
8
|
return editor;
|
|
170
|
|
}
|
|
171
|
|
}
|
|
172
|
|
|