KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > data > JavaBeanDataModel


1 /*
2  * $Id: JavaBeanDataModel.java,v 1.1.1.1 2004/06/16 01:43:39 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.data;
9
10 import java.beans.BeanInfo JavaDoc;
11 import java.beans.Introspector JavaDoc;
12 import java.beans.IntrospectionException JavaDoc;
13 import java.beans.PropertyDescriptor JavaDoc;
14
15 import java.lang.reflect.Method JavaDoc;
16
17 /**
18  * A class that creates a collection of MetaData based BeanInfo
19  * PropertyDescriptors. To use this class:
20  * <ol>
21  * <li>Construct the model using the Bean class you wish to model
22  * <li>use <code>setJavaBean</code> to set the current object of this class.
23  * <li>Updates made to the form will update the property values of the bean.
24  * </ol>
25  * <p>
26  * TODO: Using JavaBeans as a data source should be generalized and not
27  * constrained to FormModels.
28  *
29  * @author Mark Davidson
30  */

31 public class JavaBeanDataModel extends DefaultDataModel {
32
33     private BeanInfo JavaDoc info;
34     private Class JavaDoc beanClass;
35     private Object JavaDoc bean; // current bean instance
36

37     public JavaBeanDataModel(Class JavaDoc beanClass) throws IntrospectionException JavaDoc {
38         this(beanClass, null);
39     }
40
41     /**
42      * Constructs a JavaBeanDataModel by introspecting on the class and using the data from
43      * the object as the current bean
44      *
45      * @param beanClass the class to use to introspect properties
46      * @param bean the object where the current values will be retrieved and stored.
47      */

48     public JavaBeanDataModel(Class JavaDoc beanClass, Object JavaDoc bean) throws IntrospectionException JavaDoc {
49         this.beanClass = beanClass;
50     this.bean = bean;
51         info = Introspector.getBeanInfo(beanClass);
52
53     //
54
PropertyDescriptor JavaDoc[] props = info.getPropertyDescriptors();
55         for (int i = 0; i < props.length; i++) {
56             addField(new MetaData(props[i].getName(),
57                                     props[i].getPropertyType(),
58                                     props[i].getDisplayName()));
59         }
60     }
61
62     /**
63      * Set the JavaBean instance that this model will use.
64      */

65     public void setJavaBean(Object JavaDoc bean) {
66         if (bean != null && bean.getClass() != beanClass) {
67             throw new RuntimeException JavaDoc("ERROR: argument is not a " +
68                                        beanClass.toString());
69         }
70
71         Object JavaDoc oldBean = this.bean;
72         this.bean = bean;
73         if (bean != oldBean) {
74             // Should update all the values
75
String JavaDoc[] fieldNames = getFieldNames();
76             for (int i = 0; i < fieldNames.length; i++) {
77                 fireValueChanged(fieldNames[i]);
78             }
79         }
80     }
81
82     /**
83      * Get the JavaBean instance that this model uses.
84      */

85     public Object JavaDoc getJavaBean() {
86         return bean;
87     }
88
89     public Object JavaDoc getValue(String JavaDoc fieldName) {
90         if (getJavaBean() == null) {
91             return null;
92         }
93         PropertyDescriptor JavaDoc prop = getPropertyDescriptor(fieldName);
94         Method JavaDoc readMethod = prop.getReadMethod();
95         if (readMethod != null) {
96             try {
97                 return readMethod.invoke(getJavaBean(), new Object JavaDoc[0]);
98             }
99             catch (Exception JavaDoc ex) {
100                 // XXX excecption for illegal access, etc..
101
ex.printStackTrace();
102             }
103         }
104         return null;
105     }
106
107     // XXX should be protected.
108
protected void setValueImpl(String JavaDoc fieldName, Object JavaDoc value) {
109         if (getJavaBean() == null) {
110             return;
111         }
112         PropertyDescriptor JavaDoc prop = getPropertyDescriptor(fieldName);
113         Method JavaDoc writeMethod = prop.getWriteMethod();
114         if (writeMethod != null) {
115             try {
116                 writeMethod.invoke(getJavaBean(), new Object JavaDoc[] {value});
117             }
118             catch (Exception JavaDoc ex) {
119                 // XXX excecption for illegal access, etc..
120
ex.printStackTrace();
121             }
122         }
123     }
124
125     private PropertyDescriptor JavaDoc getPropertyDescriptor(String JavaDoc name) {
126         PropertyDescriptor JavaDoc pd = null;
127         PropertyDescriptor JavaDoc[] desc = info.getPropertyDescriptors();
128         for (int i = 0; i < desc.length; i++) {
129             if (name.equals(desc[i].getName())) {
130                 pd = desc[i];
131                 break;
132             }
133         }
134         return pd;
135     }
136 }
137
Popular Tags