View Javadoc

1   /*
2    * @(#) BeanProperty.java Apr 15, 2004
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.introspection.beans;
11  
12  import java.beans.PropertyDescriptor;
13  import java.beans.PropertyEditor;
14  import java.beans.PropertyEditorManager;
15  import java.lang.reflect.Method;
16  
17  import nl.tudelft.simulation.introspection.AbstractProperty;
18  import nl.tudelft.simulation.introspection.Property;
19  import nl.tudelft.simulation.logger.Logger;
20  
21  /***
22   * The JavaBean TM implementation of the Property interface. See {see
23   * BeanIntrospector}for details.
24   * <p>
25   * (c) copyright 2003-2004 <a href="http://www.simulation.tudelft.nl">Delft
26   * University of Technology </a>, the Netherlands. <br>
27   * See for project information <a
28   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
29   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
30   * License (GPL) </a>, no warranty <br>
31   * 
32   * @author <a
33   *         href="http://web.eur.nl/fbk/dep/dep1/Introduction/Staff/People/Lang">Niels
34   *         Lang </a><a
35   *         href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
36   *         Jacobs </a>
37   * @version 1.1 Apr 15, 2004
38   * @since 1.4
39   */
40  
41  public class BeanProperty extends AbstractProperty implements Property
42  {
43  	/*** the bean whichs owns the property */
44  	private Object bean = null;
45  
46  	/*** the propertyDescriptor */
47  	private PropertyDescriptor descriptor = null;
48  
49  	/***
50  	 * constructs a new BeanProperty
51  	 * 
52  	 * @param bean the bean to introspect
53  	 * @param descriptor the descriptor of the property
54  	 */
55  	protected BeanProperty(final Object bean,
56  			final PropertyDescriptor descriptor)
57  	{
58  		this.bean = bean;
59  		this.descriptor = descriptor;
60  	}
61  
62  	/***
63  	 * @see nl.tudelft.simulation.introspection.Property#getName()
64  	 */
65  	public String getName()
66  	{
67  		return this.descriptor.getName();
68  	}
69  
70  	/***
71  	 * @see nl.tudelft.simulation.introspection.Property#getType()
72  	 */
73  	public Class getType()
74  	{
75  		return this.descriptor.getPropertyType();
76  	}
77  
78  	/***
79  	 * @see nl.tudelft.simulation.introspection.AbstractProperty
80  	 *      #setRegularValue(java.lang.Object)
81  	 */
82  	protected void setRegularValue(final Object values)
83  	{
84  		Class type = this.descriptor.getPropertyType();
85  		PropertyEditor editor = PropertyEditorManager.findEditor(type);
86  		Object newValue = values;
87  		if (editor != null)
88  		{
89  			if (values instanceof String)
90  			{
91  				editor.setAsText((String) values);
92  			} else
93  			{
94  				editor.setValue(values);
95  			}
96  			newValue = editor.getValue();
97  		}
98  		Method writeMethod = this.descriptor.getWriteMethod();
99  		try
100 		{
101 			writeMethod.invoke(this.bean, new Object[]{newValue});
102 		} catch (Throwable throwable)
103 		{
104 			Logger.warning(this, "setRegularValue", throwable);
105 		}
106 	}
107 
108 	/***
109 	 * @see nl.tudelft.simulation.introspection.Property#getValue()
110 	 */
111 	public Object getValue()
112 	{
113 		Object result = null;
114 		Method readMethod = this.descriptor.getReadMethod();
115 		try
116 		{
117 			if (readMethod != null)
118 			{
119 				result = readMethod.invoke(this.bean, new Object[0]);
120 			}
121 		} catch (Exception exception)
122 		{
123 			Logger.warning(this, "getValue of " + getName(), exception);
124 		}
125 		return result;
126 	}
127 
128 	/***
129 	 * @see nl.tudelft.simulation.introspection.Property#getInstance()
130 	 */
131 	public Object getInstance()
132 	{
133 		return this.bean;
134 	}
135 
136 	/***
137 	 * @see nl.tudelft.simulation.introspection.Property#isEditable()
138 	 */
139 	public boolean isEditable()
140 	{
141 		return !(this.descriptor.getWriteMethod() == null);
142 	}
143 }