KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > WeakPropertyChangeSupport


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
20 package org.netbeans.editor;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.lang.ref.WeakReference JavaDoc;
25 import java.util.ArrayList JavaDoc;
26
27 /**
28 * Similair functionality as PropertyChangeSupport but holds only
29 * weak references to listener classes
30 *
31 * @author Miloslav Metelka
32 * @version 1.00
33 */

34
35 public class WeakPropertyChangeSupport {
36
37     private transient ArrayList JavaDoc listeners = new ArrayList JavaDoc();
38
39     private transient ArrayList JavaDoc interestNames = new ArrayList JavaDoc();
40
41
42     /** Add weak listener to listen to change of any property. The caller must
43     * hold the listener object in some instance variable to prevent it
44     * from being garbage collected.
45     */

46     public synchronized void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
47         addLImpl(null, l);
48     }
49
50     /** Add weak listener to listen to change of the specified property.
51     * The caller must hold the listener object in some instance variable
52     * to prevent it from being garbage collected.
53     */

54     public synchronized void addPropertyChangeListener(String JavaDoc propertyName,
55             PropertyChangeListener JavaDoc l) {
56         addLImpl(propertyName, l);
57     }
58
59     /** Remove listener for changes in properties */
60     public synchronized void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
61         int cnt = listeners.size();
62         for (int i = 0; i < cnt; i++) {
63             Object JavaDoc o = ((WeakReference JavaDoc)listeners.get(i)).get();
64             if (o == null || o == l) { // remove null references and the required one
65
listeners.remove(i);
66                 interestNames.remove(i);
67                 i--;
68                 cnt--;
69             }
70         }
71     }
72
73     public void firePropertyChange(Object JavaDoc source, String JavaDoc propertyName,
74                                    Object JavaDoc oldValue, Object JavaDoc newValue) {
75         if (oldValue != null && newValue != null && oldValue.equals(newValue)) {
76             return;
77         }
78         PropertyChangeListener JavaDoc la[];
79         String JavaDoc isa[];
80         int cnt;
81         synchronized (this) {
82             cnt = listeners.size();
83             la = new PropertyChangeListener JavaDoc[cnt];
84             for (int i = 0; i < cnt; i++) {
85                 PropertyChangeListener JavaDoc l = (PropertyChangeListener JavaDoc)
86                                            ((WeakReference JavaDoc)listeners.get(i)).get();
87                 if (l == null) { // remove null references
88
listeners.remove(i);
89                     interestNames.remove(i);
90                     i--;
91                     cnt--;
92                 } else {
93                     la[i] = l;
94                 }
95             }
96             isa = (String JavaDoc [])interestNames.toArray(new String JavaDoc[cnt]);
97         }
98
99         // now create and fire the event
100
PropertyChangeEvent JavaDoc evt = new PropertyChangeEvent JavaDoc(source, propertyName,
101                                   oldValue, newValue);
102         for (int i = 0; i < cnt; i++) {
103             if (isa[i] == null || propertyName == null || isa[i].equals(propertyName)) {
104                 la[i].propertyChange(evt);
105             }
106         }
107     }
108
109     private void addLImpl(String JavaDoc sn, PropertyChangeListener JavaDoc l) {
110         listeners.add(new WeakReference JavaDoc(l));
111         interestNames.add(sn);
112     }
113
114 }
115
Popular Tags