View Javadoc

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