KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > tax > event > TreeEventChangeSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.tax.event;
20
21 import java.lang.reflect.*;
22
23 import java.util.Map JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.LinkedList JavaDoc;
27
28 import java.beans.PropertyChangeListener JavaDoc;
29 import java.beans.PropertyChangeSupport JavaDoc;
30
31 import org.netbeans.tax.TreeObject;
32 import java.util.Set JavaDoc;
33
34 /**
35  *
36  * @author Libor Kramolis
37  * @version 0.1
38  */

39 public final class TreeEventChangeSupport {
40
41     /** Utility field used by bound properties. */
42     private PropertyChangeSupport JavaDoc propertyChangeSupport;
43
44     /** Event source. */
45     private TreeObject eventSource;
46     
47     /** Its event cache. */
48     private EventCache eventCache;
49     
50     //
51
// init
52
//
53

54     /** Creates new TreeEventChangeSupport. */
55     public TreeEventChangeSupport (TreeObject eventSource) {
56         this.eventSource = eventSource;
57         this.eventCache = new EventCache ();
58     }
59     
60     
61     //
62
// itself
63
//
64

65     /**
66      */

67     public final TreeEvent createEvent (String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue) {
68         return new TreeEvent (eventSource, propertyName, oldValue, newValue);
69     }
70     
71     /**
72      */

73     protected final TreeObject getEventSource () {
74         return eventSource;
75     }
76     
77     
78     /**
79      */

80     private final PropertyChangeSupport JavaDoc getPropertyChangeSupport () {
81         if (propertyChangeSupport == null) {
82             propertyChangeSupport = new PropertyChangeSupport JavaDoc (eventSource);
83         }
84         return propertyChangeSupport;
85     }
86     
87     /** Add a PropertyChangeListener to the listener list.
88      * @param listener The listener to add.
89      */

90     public final void addPropertyChangeListener (PropertyChangeListener JavaDoc listener) {
91         getPropertyChangeSupport ().addPropertyChangeListener (listener);
92         
93         if ( Util.THIS.isLoggable() ) /* then */ {
94             Util.THIS.debug ("TreeEventChangeSupport::addPropertyChangeListener: listener = " + listener); // NOI18N
95
Util.THIS.debug (" propertyChangeSupport = " + listListeners ()); // NOI18N
96
if ( listener == null ) {
97                 Util.THIS.debug (" eventSource = " + eventSource); // NOI18N
98
}
99         }
100     }
101     
102     /**
103      */

104     public final void addPropertyChangeListener (String JavaDoc propertyName, PropertyChangeListener JavaDoc listener) {
105         getPropertyChangeSupport ().addPropertyChangeListener (propertyName, listener);
106         
107         if ( Util.THIS.isLoggable() ) /* then */ {
108             Util.THIS.debug ("TreeEventChangeSupport::addPropertyChangeListener: propertyName = " + propertyName); // NOI18N
109
Util.THIS.debug (" listener = " + listener); // NOI18N
110
Util.THIS.debug (" propertyChangeSupport = " + listListeners ()); // NOI18N
111
if ( listener == null ) {
112                 Util.THIS.debug (" eventSource = " + eventSource); // NOI18N
113
Util.THIS.debug (new RuntimeException JavaDoc ("TreeEventChangeSupport.addPropertyChangeListener")); // NOI18N
114
}
115         }
116     }
117     
118     
119     /** Removes a PropertyChangeListener from the listener list.
120      * @param listener The listener to remove.
121      */

122     public final void removePropertyChangeListener (PropertyChangeListener JavaDoc listener) {
123         getPropertyChangeSupport ().removePropertyChangeListener (listener);
124         
125         if ( Util.THIS.isLoggable() ) /* then */ {
126             Util.THIS.debug ("TreeEventChangeSupport::removePropertyChangeListener: listener = " + listener); // NOI18N
127
Util.THIS.debug (" propertyChangeSupport = " + listListeners ()); // NOI18N
128
}
129     }
130     
131     /**
132      */

133     public final void removePropertyChangeListener (String JavaDoc propertyName, PropertyChangeListener JavaDoc listener) {
134         getPropertyChangeSupport ().removePropertyChangeListener (propertyName, listener);
135         
136         if ( Util.THIS.isLoggable() ) /* then */ {
137             Util.THIS.debug ("TreeEventChangeSupport::removePropertyChangeListener: propertyName = " + propertyName); // NOI18N
138
Util.THIS.debug (" listener = " + listener); // NOI18N
139
Util.THIS.debug ("- ::removePropertyChangeListener: propertyChangeSupport = " + listListeners ()); // NOI18N
140
}
141     }
142     
143     /**
144      * Check if there are any listeners for a specific property.
145      *
146      * @param propertyName the property name.
147      * @return true if there are ore or more listeners for the given property
148      */

149     public final boolean hasPropertyChangeListeners (String JavaDoc propertyName) {
150         return getPropertyChangeSupport ().hasListeners (propertyName);
151     }
152     
153     /**
154      * Fire an existing PropertyChangeEvent to any registered listeners.
155      * No event is fired if the given event's old and new values are
156      * equal and non-null.
157      * @param evt The PropertyChangeEvent object.
158      */

159     public final void firePropertyChange (TreeEvent evt) {
160         if ( Util.THIS.isLoggable() ) /* then */ {
161             Util.THIS.debug ("TreeEventChangeSupport::firePropertyChange ( " + evt + " )"); // NOI18N
162
Util.THIS.debug (" eventSource = " + eventSource); // NOI18N
163
Util.THIS.debug (" EventManager = " + eventSource.getEventManager ()); // NOI18N
164
}
165         
166         if ( eventSource.getEventManager () == null )
167             return;
168         eventSource.getEventManager ().firePropertyChange (this, evt);
169     }
170     
171     /**
172      */

173     protected final void firePropertyChangeNow (TreeEvent evt) {
174         getPropertyChangeSupport ().firePropertyChange (evt);
175     }
176     
177     /**
178      */

179     protected final void firePropertyChangeLater (TreeEvent evt) {
180         eventCache.addEvent (evt);
181     }
182     
183     /**
184      */

185     protected final void firePropertyChangeCache () {
186         eventCache.firePropertyChange ();
187     }
188     
189     /**
190      */

191     protected final void clearPropertyChangeCache () {
192         eventCache.clear ();
193     }
194     
195     
196     //
197
// debug
198
//
199

200     
201     /**
202      */

203     private String JavaDoc listListeners (Object JavaDoc instance) {
204         try {
205             Class JavaDoc klass = instance.getClass ();
206             Field field = klass.getDeclaredField ("listeners"); // NOI18N
207
field.setAccessible (true);
208             
209             return field.get (instance).toString ();
210         } catch (Exception JavaDoc ex) {
211             return "" + ex.getClass () + " " + ex.getMessage (); // NOI18N
212
}
213     }
214     
215     /**
216      */

217     private String JavaDoc listChildrenListeners (PropertyChangeSupport JavaDoc support) {
218         try {
219             Object JavaDoc instance = support;
220             Class JavaDoc klass = instance.getClass ();
221             Field field = klass.getDeclaredField ("children"); // NOI18N
222
field.setAccessible (true);
223             
224             StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
225             Map JavaDoc map = (Map JavaDoc)field.get (instance);
226             if (map == null) return "";
227             Set JavaDoc keys = map.keySet ();
228             Iterator JavaDoc it = keys.iterator ();
229             while (it.hasNext ()) {
230                 Object JavaDoc key = it.next ();
231                 sb.append ("\n[").append (key).append ("] ").append (listListeners (map.get (key))); // NOI18N
232
}
233             
234             return sb.toString ();
235         } catch (Exception JavaDoc ex) {
236             ex.printStackTrace ();
237             return "<" + ex + ">"; // NOI18N
238
}
239     }
240     
241     
242     /**
243      * For debug purposes list all registered listeners
244      */

245     public final String JavaDoc listListeners () {
246         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
247         
248         sb.append ("[*general*] ").append (listListeners (getPropertyChangeSupport ())).append ("\n"); // NOI18N
249
sb.append (listChildrenListeners (getPropertyChangeSupport ()));
250         
251         return sb.toString ();
252     }
253     
254     
255     //
256
// Event Cache
257
//
258

259     /**
260      * EventCache for later event firing.
261      */

262     private class EventCache {
263         
264         /** */
265         List JavaDoc eventList;
266         
267         
268         //
269
// init
270
//
271

272         /** Creates new EventCache. */
273         public EventCache () {
274             eventList = new LinkedList JavaDoc ();
275         }
276         
277         
278         //
279
// itself
280
//
281

282         /**
283          */

284         public void clear () {
285             synchronized ( eventList ) {
286                 eventList.clear ();
287             }
288         }
289         
290         
291         /**
292          */

293         public void addEvent (TreeEvent event) {
294             synchronized ( eventList ) {
295                 eventList.add (event);
296             }
297         }
298         
299         /**
300          */

301         public void firePropertyChange () {
302             List JavaDoc listCopy;
303             synchronized ( eventList ) {
304                 listCopy = new LinkedList JavaDoc (eventList);
305                 eventList.clear ();
306             }
307             Iterator JavaDoc it = listCopy.iterator ();
308             while ( it.hasNext () ) {
309                 firePropertyChangeNow ((TreeEvent)it.next ());
310             }
311         }
312         
313     } // end: class EventCache
314

315 }
316
Popular Tags