KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > databinding > internal > beans > JavaBeanObservableSet


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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  * Brad Reynolds - bug 171616
11  *******************************************************************************/

12
13 package org.eclipse.core.internal.databinding.internal.beans;
14
15 import java.beans.PropertyChangeListener JavaDoc;
16 import java.beans.PropertyDescriptor JavaDoc;
17 import java.lang.reflect.InvocationTargetException JavaDoc;
18 import java.lang.reflect.Method JavaDoc;
19 import java.util.Arrays JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.eclipse.core.databinding.beans.IBeanObservable;
25 import org.eclipse.core.databinding.observable.Diffs;
26 import org.eclipse.core.databinding.observable.Realm;
27 import org.eclipse.core.databinding.observable.set.ObservableSet;
28 import org.eclipse.core.runtime.Assert;
29
30 /**
31  * @since 1.0
32  *
33  */

34 public class JavaBeanObservableSet extends ObservableSet implements IBeanObservable {
35
36     private final Object JavaDoc object;
37
38     private PropertyChangeListener JavaDoc collectionListener = new PropertyChangeListener JavaDoc() {
39         public void propertyChange(java.beans.PropertyChangeEvent JavaDoc event) {
40             if (!updating) {
41                     Set JavaDoc newElements = new HashSet JavaDoc(Arrays.asList(getValues()));
42                     Set JavaDoc addedElements = new HashSet JavaDoc(newElements);
43                     Set JavaDoc removedElements = new HashSet JavaDoc(wrappedSet);
44                     // remove all new elements from old elements to compute the
45
// removed elements
46
removedElements.removeAll(newElements);
47                     addedElements.removeAll(wrappedSet);
48                     wrappedSet = newElements;
49                     fireSetChange(Diffs.createSetDiff(addedElements,
50                             removedElements));
51             }
52         }
53     };
54
55     private boolean updating = false;
56
57     private PropertyDescriptor JavaDoc descriptor;
58
59     private ListenerSupport collectionListenSupport;
60
61     /**
62      * @param realm
63      * @param object
64      * @param descriptor
65      * @param elementType
66      */

67     public JavaBeanObservableSet(Realm realm, Object JavaDoc object, PropertyDescriptor JavaDoc descriptor,
68             Class JavaDoc elementType) {
69         super(realm, new HashSet JavaDoc(), elementType);
70         this.object = object;
71         this.descriptor = descriptor;
72         this.collectionListenSupport = new ListenerSupport(collectionListener, descriptor.getName());
73         
74         wrappedSet.addAll(Arrays.asList(getValues()));
75     }
76
77     protected void firstListenerAdded() {
78         collectionListenSupport.hookListener(this.object);
79     }
80
81     protected void lastListenerRemoved() {
82         if (collectionListenSupport != null) {
83             collectionListenSupport.dispose();
84         }
85     }
86
87     private Object JavaDoc primGetValues() {
88         try {
89             Method JavaDoc readMethod = descriptor.getReadMethod();
90             if (!readMethod.isAccessible()) {
91                 readMethod.setAccessible(true);
92             }
93             return readMethod.invoke(object, new Object JavaDoc[0]);
94         } catch (IllegalArgumentException JavaDoc e) {
95         } catch (IllegalAccessException JavaDoc e) {
96         } catch (InvocationTargetException JavaDoc e) {
97         }
98         Assert.isTrue(false, "Could not read collection values"); //$NON-NLS-1$
99
return null;
100     }
101
102     private Object JavaDoc[] getValues() {
103         Object JavaDoc[] values = null;
104
105         Object JavaDoc result = primGetValues();
106         if (descriptor.getPropertyType().isArray())
107             values = (Object JavaDoc[]) result;
108         else {
109             // TODO add jUnit for POJO (var. SettableValue) collections
110
Collection JavaDoc list = (Collection JavaDoc) result;
111             if (list != null) {
112                 values = list.toArray();
113             } else {
114                 values = new Object JavaDoc[] {};
115             }
116         }
117         return values;
118     }
119
120     /* (non-Javadoc)
121      * @see org.eclipse.core.databinding.beans.IBeanObservable#getObserved()
122      */

123     public Object JavaDoc getObserved() {
124         return object;
125     }
126
127     /* (non-Javadoc)
128      * @see org.eclipse.core.databinding.beans.IBeanObservable#getPropertyDescriptor()
129      */

130     public PropertyDescriptor JavaDoc getPropertyDescriptor() {
131         return descriptor;
132     }
133
134 }
135
Popular Tags