KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > internal > beans > JavaBeanObservableValue


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.internal.databinding.internal.beans;
12
13 import java.beans.PropertyChangeListener JavaDoc;
14 import java.beans.PropertyDescriptor JavaDoc;
15 import java.lang.reflect.InvocationTargetException JavaDoc;
16 import java.lang.reflect.Method JavaDoc;
17
18 import org.eclipse.jface.internal.databinding.provisional.BindingException;
19 import org.eclipse.jface.internal.databinding.provisional.observable.Diffs;
20 import org.eclipse.jface.internal.databinding.provisional.observable.value.AbstractObservableValue;
21
22 /**
23  * @since 1.0
24  *
25  */

26 public class JavaBeanObservableValue extends AbstractObservableValue {
27     private final Object JavaDoc object;
28
29     private PropertyChangeListener JavaDoc listener;
30
31     private boolean updating = false;
32
33     private final PropertyDescriptor JavaDoc propertyDescriptor;
34
35     /**
36      * @param object
37      * @param descriptor
38      */

39     public JavaBeanObservableValue(Object JavaDoc object, PropertyDescriptor JavaDoc descriptor) {
40         this.object = object;
41         this.propertyDescriptor = descriptor;
42     }
43
44     protected void firstListenerAdded() {
45         listener = new PropertyChangeListener JavaDoc() {
46             public void propertyChange(java.beans.PropertyChangeEvent JavaDoc event) {
47                 if (!updating && event.getPropertyName().equals(propertyDescriptor.getName())) {
48                     fireValueChange(Diffs.createValueDiff(event.getOldValue(), event
49                             .getNewValue()));
50                 }
51             }
52         };
53         Method JavaDoc addPropertyChangeListenerMethod = null;
54         try {
55             addPropertyChangeListenerMethod = object.getClass().getMethod(
56                     "addPropertyChangeListener", //$NON-NLS-1$
57
new Class JavaDoc[] { String JavaDoc.class, PropertyChangeListener JavaDoc.class });
58         } catch (SecurityException JavaDoc e) {
59             // ignore
60
} catch (NoSuchMethodException JavaDoc e) {
61             // ignore
62
}
63         if (addPropertyChangeListenerMethod != null) {
64             try {
65                 addPropertyChangeListenerMethod.invoke(object, new Object JavaDoc[] {
66                         propertyDescriptor.getName(), listener });
67                 return;
68             } catch (IllegalArgumentException JavaDoc e) {
69                 // ignore
70
} catch (IllegalAccessException JavaDoc e) {
71                 // ignore
72
} catch (InvocationTargetException JavaDoc e) {
73                 // ignore
74
}
75         }
76         // set listener to null because we are not listening
77
listener = null;
78     }
79
80     public void setValue(Object JavaDoc value) {
81         updating = true;
82         try {
83             Object JavaDoc oldValue = doGetValue();
84             Method JavaDoc writeMethod = propertyDescriptor.getWriteMethod();
85             if (!writeMethod.isAccessible()) {
86                 writeMethod.setAccessible(true);
87             }
88             writeMethod.invoke(object, new Object JavaDoc[] { value });
89             fireValueChange(Diffs.createValueDiff(oldValue, doGetValue()));
90         } catch (Exception JavaDoc e) {
91             // TODO log exception, or maybe throw runtime exception?
92
e.printStackTrace();
93         } finally {
94             updating = false;
95         }
96     }
97
98     public Object JavaDoc doGetValue() {
99         try {
100             Method JavaDoc readMethod = propertyDescriptor.getReadMethod();
101             if (readMethod == null) {
102                 throw new BindingException(propertyDescriptor.getName()
103                         + " property does not have a read method."); //$NON-NLS-1$
104
}
105             if (!readMethod.isAccessible()) {
106                 readMethod.setAccessible(true);
107             }
108             return readMethod.invoke(object, null);
109         } catch (Exception JavaDoc e) {
110             e.printStackTrace();
111             return null;
112         }
113     }
114
115     protected void lastListenerRemoved() {
116         if (listener != null) {
117             Method JavaDoc removePropertyChangeListenerMethod = null;
118             try {
119                 removePropertyChangeListenerMethod = object.getClass()
120                         .getMethod(
121                                 "removePropertyChangeListener", //$NON-NLS-1$
122
new Class JavaDoc[] { String JavaDoc.class,
123                                         PropertyChangeListener JavaDoc.class });
124             } catch (SecurityException JavaDoc e) {
125                 // best effort - ignore
126
} catch (NoSuchMethodException JavaDoc e) {
127                 // best effort - ignore
128
}
129             if (removePropertyChangeListenerMethod != null) {
130                 try {
131                     removePropertyChangeListenerMethod.invoke(object,
132                             new Object JavaDoc[] { propertyDescriptor.getName(),
133                                     listener });
134                 } catch (IllegalArgumentException JavaDoc e) {
135                     // best effort - ignore
136
} catch (IllegalAccessException JavaDoc e) {
137                     // best effort - ignore
138
} catch (InvocationTargetException JavaDoc e) {
139                     // best effort - ignore
140
}
141             }
142             // set listener to null because we are no longer listening
143
listener = null;
144         }
145     }
146
147     public Object JavaDoc getValueType() {
148         return propertyDescriptor.getPropertyType();
149     }
150 }
151
Popular Tags