1 19 20 package org.netbeans.editor; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeEvent ; 24 import java.lang.ref.WeakReference ; 25 import java.util.ArrayList ; 26 27 34 35 public class WeakPropertyChangeSupport { 36 37 private transient ArrayList listeners = new ArrayList (); 38 39 private transient ArrayList interestNames = new ArrayList (); 40 41 42 46 public synchronized void addPropertyChangeListener(PropertyChangeListener l) { 47 addLImpl(null, l); 48 } 49 50 54 public synchronized void addPropertyChangeListener(String propertyName, 55 PropertyChangeListener l) { 56 addLImpl(propertyName, l); 57 } 58 59 60 public synchronized void removePropertyChangeListener(PropertyChangeListener l) { 61 int cnt = listeners.size(); 62 for (int i = 0; i < cnt; i++) { 63 Object o = ((WeakReference )listeners.get(i)).get(); 64 if (o == null || o == l) { listeners.remove(i); 66 interestNames.remove(i); 67 i--; 68 cnt--; 69 } 70 } 71 } 72 73 public void firePropertyChange(Object source, String propertyName, 74 Object oldValue, Object newValue) { 75 if (oldValue != null && newValue != null && oldValue.equals(newValue)) { 76 return; 77 } 78 PropertyChangeListener la[]; 79 String isa[]; 80 int cnt; 81 synchronized (this) { 82 cnt = listeners.size(); 83 la = new PropertyChangeListener [cnt]; 84 for (int i = 0; i < cnt; i++) { 85 PropertyChangeListener l = (PropertyChangeListener ) 86 ((WeakReference )listeners.get(i)).get(); 87 if (l == null) { listeners.remove(i); 89 interestNames.remove(i); 90 i--; 91 cnt--; 92 } else { 93 la[i] = l; 94 } 95 } 96 isa = (String [])interestNames.toArray(new String [cnt]); 97 } 98 99 PropertyChangeEvent evt = new PropertyChangeEvent (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 sn, PropertyChangeListener l) { 110 listeners.add(new WeakReference (l)); 111 interestNames.add(sn); 112 } 113 114 } 115 | Popular Tags |