KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.InvocationTargetException JavaDoc;
15 import java.lang.reflect.Method JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Set JavaDoc;
19
20
21 /**
22  * This is a helper that will hook up and listen for <code>PropertyChangeEvent</code> events
23  * for a set of target JavaBeans
24  *
25  * @since 1.0
26  */

27 public class ListenerSupport {
28
29     /**
30      * @since 1.0
31      */

32      static class IdentityWrapper {
33         final Object JavaDoc o;
34         IdentityWrapper(Object JavaDoc o) {
35             this.o = o;
36         }
37         public boolean equals(Object JavaDoc obj) {
38             if(obj.getClass()!=IdentityWrapper.class) {
39                 return false;
40             }
41             return o==((IdentityWrapper)obj).o;
42         }
43         public int hashCode() {
44             return System.identityHashCode(o);
45         }
46     }
47     
48     private Set JavaDoc elementsListenedTo = new HashSet JavaDoc();
49     
50     private PropertyChangeListener JavaDoc listener;
51     
52     /**
53      * @param listener is the callback that will be called
54      * when a <code>PropertyChangeEvent</code> is fired on any
55      * of the target objects.
56      */

57     public ListenerSupport (PropertyChangeListener JavaDoc listener) {
58         this.listener=listener;
59     }
60     
61     
62     /**
63      * Start listen to target (if it supports the JavaBean property change listener pattern)
64      *
65      * @param target
66      */

67     public void hookListener(Object JavaDoc target) {
68         Method JavaDoc addPropertyChangeListenerMethod = null;
69         try {
70             addPropertyChangeListenerMethod = target.getClass().getMethod(
71                     "addPropertyChangeListener", //$NON-NLS-1$
72
new Class JavaDoc[] { PropertyChangeListener JavaDoc.class });
73         } catch (SecurityException JavaDoc e) {
74             // ignore
75
} catch (NoSuchMethodException JavaDoc e) {
76             // ignore
77
}
78         if (addPropertyChangeListenerMethod != null) {
79             try {
80                 addPropertyChangeListenerMethod.invoke(target,
81                         new Object JavaDoc[] { listener });
82                 elementsListenedTo.add(new IdentityWrapper(target));
83                 return;
84             } catch (IllegalArgumentException JavaDoc e) {
85                 // ignore
86
} catch (IllegalAccessException JavaDoc e) {
87                 // ignore
88
} catch (InvocationTargetException JavaDoc e) {
89                 // ignore
90
}
91         }
92     }
93         
94     /**
95      * Add listeners for new targets (those this instance of<code>ListenerSupport</code> does not
96      * already listen to),
97      * Stop to listen to those object that this instance listen to and is one of the object in targets
98      *
99      * @param targets
100      */

101     public void setHookTargets(Object JavaDoc[] targets) {
102         Set JavaDoc elementsToUnhook = new HashSet JavaDoc(elementsListenedTo);
103         if (targets!=null) {
104             for (int i = 0; i < targets.length; i++) {
105                 Object JavaDoc newValue = targets[i];
106                 IdentityWrapper identityWrapper = new IdentityWrapper(newValue);
107                 if(!elementsToUnhook.remove(identityWrapper))
108                     hookListener(newValue);
109             }
110         }
111             
112         for (Iterator JavaDoc it = elementsToUnhook.iterator(); it.hasNext();) {
113             Object JavaDoc o = it.next();
114             if (o.getClass()!=IdentityWrapper.class)
115                 o = new IdentityWrapper(o);
116             elementsListenedTo.remove(o);
117             unhookListener(o);
118         }
119     }
120     
121     /**
122      * Stop listen to target
123      *
124      * @param target
125      */

126     public void unhookListener(Object JavaDoc target) {
127         if (target.getClass()==IdentityWrapper.class)
128             target = ((IdentityWrapper)target).o;
129         
130         Method JavaDoc removePropertyChangeListenerMethod = null;
131         try {
132             removePropertyChangeListenerMethod = target.getClass().getMethod(
133                     "removePropertyChangeListener", //$NON-NLS-1$
134
new Class JavaDoc[] { PropertyChangeListener JavaDoc.class });
135         } catch (SecurityException JavaDoc e) {
136             // ignore
137
} catch (NoSuchMethodException JavaDoc e) {
138             // ignore
139
}
140         if (removePropertyChangeListenerMethod != null) {
141             try {
142                 removePropertyChangeListenerMethod.invoke(target,
143                         new Object JavaDoc[] { listener });
144                 elementsListenedTo.remove(new IdentityWrapper(target));
145                 return;
146             } catch (IllegalArgumentException JavaDoc e) {
147                 // ignore
148
} catch (IllegalAccessException JavaDoc e) {
149                 // ignore
150
} catch (InvocationTargetException JavaDoc e) {
151                 // ignore
152
}
153         }
154     }
155     
156     
157     /**
158      *
159      */

160     public void dispose() {
161         if (elementsListenedTo!=null) {
162             Object JavaDoc[] targets = elementsListenedTo.toArray();
163             for (int i = 0; i < targets.length; i++) {
164                 unhookListener(targets[i]);
165             }
166             elementsListenedTo=null;
167             listener=null;
168         }
169     }
170     
171     /**
172      * @return elements that were registred to
173      */

174     public Object JavaDoc[] getHookedTargets() {
175         Object JavaDoc[] targets = null;
176         if (elementsListenedTo!=null && elementsListenedTo.size()>0) {
177             Object JavaDoc[] identityList = elementsListenedTo.toArray();
178             targets = new Object JavaDoc[identityList.length];
179             for (int i = 0; i < identityList.length; i++)
180                 targets[i]=((IdentityWrapper)identityList[i]).o;
181         }
182         return targets;
183     }
184 }
185
Popular Tags