KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.jface.internal.databinding.internal.beans;
13
14 import java.beans.PropertyChangeListener JavaDoc;
15 import java.beans.PropertyDescriptor JavaDoc;
16 import java.lang.reflect.InvocationTargetException JavaDoc;
17 import java.lang.reflect.Method JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Arrays JavaDoc;
20 import java.util.Collection JavaDoc;
21
22 import org.eclipse.jface.internal.databinding.provisional.observable.list.ObservableList;
23 import org.eclipse.jface.util.Assert;
24
25 /**
26  * @since 1.0
27  *
28  */

29 public class JavaBeanObservableList extends ObservableList {
30
31     private final Object JavaDoc object;
32
33     private PropertyChangeListener JavaDoc collectionListener = new PropertyChangeListener JavaDoc() {
34         public void propertyChange(java.beans.PropertyChangeEvent JavaDoc event) {
35             if (!updating) {
36                 if (JavaBeanObservableList.this.descriptor.getName().equals(
37                         event.getPropertyName())) {
38                     updateWrappedList(Arrays.asList(getValues()));
39                 }
40             }
41         }
42     };
43
44     private boolean updating = false;
45
46     private PropertyDescriptor JavaDoc descriptor;
47
48     private ListenerSupport collectionListenSupport = new ListenerSupport(
49             collectionListener);
50
51     /**
52      * @param object
53      * @param descriptor
54      * @param elementType
55      */

56     public JavaBeanObservableList(Object JavaDoc object, PropertyDescriptor JavaDoc descriptor,
57             Class JavaDoc elementType) {
58         super(new ArrayList JavaDoc(), elementType);
59         this.object = object;
60         this.descriptor = descriptor;
61         // initialize list without firing events
62
wrappedList.addAll(Arrays.asList(getValues()));
63     }
64
65     protected void firstListenerAdded() {
66         collectionListenSupport.hookListener(this.object);
67     }
68
69     protected void lastListenerRemoved() {
70         if (collectionListenSupport != null) {
71             collectionListenSupport.dispose();
72         }
73     }
74
75     private Object JavaDoc primGetValues() {
76         try {
77             Method JavaDoc readMethod = descriptor.getReadMethod();
78             if (!readMethod.isAccessible()) {
79                 readMethod.setAccessible(true);
80             }
81             return readMethod.invoke(object, new Object JavaDoc[0]);
82         } catch (IllegalArgumentException JavaDoc e) {
83         } catch (IllegalAccessException JavaDoc e) {
84         } catch (InvocationTargetException JavaDoc e) {
85         }
86         Assert.isTrue(false, "Could not read collection values"); //$NON-NLS-1$
87
return null;
88     }
89
90     private Object JavaDoc[] getValues() {
91         Object JavaDoc[] values = null;
92
93         Object JavaDoc result = primGetValues();
94         if (descriptor.getPropertyType().isArray())
95             values = (Object JavaDoc[]) result;
96         else {
97             // TODO add jUnit for POJO (var. SettableValue) collections
98
Collection JavaDoc list = (Collection JavaDoc) result;
99             if (list != null) {
100                 values = list.toArray();
101             } else {
102                 values = new Object JavaDoc[] {};
103             }
104         }
105         return values;
106     }
107
108 }
109
Popular Tags