1 package nl.tudelft.simulation.introspection.beans;
2
3 import java.beans.PropertyDescriptor;
4 import java.beans.PropertyEditor;
5 import java.beans.PropertyEditorManager;
6 import java.lang.reflect.Method;
7
8 import nl.tudelft.simulation.introspection.AbstractProperty;
9 import nl.tudelft.simulation.introspection.Property;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 public class BeanProperty extends AbstractProperty implements Property
26 {
27
28 private Object bean = null;
29
30
31 private PropertyDescriptor descriptor = null;
32
33
34
35
36
37
38 protected BeanProperty(final Object bean, final PropertyDescriptor descriptor)
39 {
40 this.bean = bean;
41 this.descriptor = descriptor;
42 }
43
44 @Override
45 public String getName()
46 {
47 return this.descriptor.getName();
48 }
49
50 @Override
51 public Class<?> getType()
52 {
53 return this.descriptor.getPropertyType();
54 }
55
56 @Override
57 protected void setRegularValue(final Object values)
58 {
59 Class<?> type = this.descriptor.getPropertyType();
60 PropertyEditor editor = PropertyEditorManager.findEditor(type);
61 Object newValue = values;
62 if (editor != null)
63 {
64 if (values instanceof String)
65 {
66 editor.setAsText((String) values);
67 }
68 else
69 {
70 editor.setValue(values);
71 }
72 newValue = editor.getValue();
73 }
74 Method writeMethod = this.descriptor.getWriteMethod();
75 try
76 {
77 writeMethod.invoke(this.bean, new Object[] {newValue});
78 }
79 catch (Throwable throwable)
80 {
81 throw new IllegalArgumentException(this + " - setRegularValue", throwable);
82 }
83 }
84
85 @Override
86 public Object getValue()
87 {
88 Object result = null;
89 Method readMethod = this.descriptor.getReadMethod();
90 try
91 {
92 if (readMethod != null)
93 { result = readMethod.invoke(this.bean, new Object[0]); }
94 }
95 catch (Exception exception)
96 {
97 throw new IllegalArgumentException(this + "getValue of " + getName(), exception);
98 }
99 return result;
100 }
101
102 @Override
103 public Object getInstance()
104 {
105 return this.bean;
106 }
107
108 @Override
109 public boolean isEditable()
110 {
111 return !(this.descriptor.getWriteMethod() == null);
112 }
113 }