KickJava   Java API By Example, From Geeks To Geeks.

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


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.Arrays JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import org.eclipse.jface.internal.databinding.provisional.observable.Diffs;
24 import org.eclipse.jface.internal.databinding.provisional.observable.set.ObservableSet;
25 import org.eclipse.jface.util.Assert;
26
27 /**
28  * @since 1.0
29  *
30  */

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

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