1 19 package org.netbeans.tax.event; 20 21 import java.lang.reflect.*; 22 23 import java.util.Map ; 24 import java.util.List ; 25 import java.util.Iterator ; 26 import java.util.LinkedList ; 27 28 import java.beans.PropertyChangeListener ; 29 import java.beans.PropertyChangeSupport ; 30 31 import org.netbeans.tax.TreeObject; 32 import java.util.Set ; 33 34 39 public final class TreeEventChangeSupport { 40 41 42 private PropertyChangeSupport propertyChangeSupport; 43 44 45 private TreeObject eventSource; 46 47 48 private EventCache eventCache; 49 50 54 55 public TreeEventChangeSupport (TreeObject eventSource) { 56 this.eventSource = eventSource; 57 this.eventCache = new EventCache (); 58 } 59 60 61 65 67 public final TreeEvent createEvent (String propertyName, Object oldValue, Object newValue) { 68 return new TreeEvent (eventSource, propertyName, oldValue, newValue); 69 } 70 71 73 protected final TreeObject getEventSource () { 74 return eventSource; 75 } 76 77 78 80 private final PropertyChangeSupport getPropertyChangeSupport () { 81 if (propertyChangeSupport == null) { 82 propertyChangeSupport = new PropertyChangeSupport (eventSource); 83 } 84 return propertyChangeSupport; 85 } 86 87 90 public final void addPropertyChangeListener (PropertyChangeListener listener) { 91 getPropertyChangeSupport ().addPropertyChangeListener (listener); 92 93 if ( Util.THIS.isLoggable() ) { 94 Util.THIS.debug ("TreeEventChangeSupport::addPropertyChangeListener: listener = " + listener); Util.THIS.debug (" propertyChangeSupport = " + listListeners ()); if ( listener == null ) { 97 Util.THIS.debug (" eventSource = " + eventSource); } 99 } 100 } 101 102 104 public final void addPropertyChangeListener (String propertyName, PropertyChangeListener listener) { 105 getPropertyChangeSupport ().addPropertyChangeListener (propertyName, listener); 106 107 if ( Util.THIS.isLoggable() ) { 108 Util.THIS.debug ("TreeEventChangeSupport::addPropertyChangeListener: propertyName = " + propertyName); Util.THIS.debug (" listener = " + listener); Util.THIS.debug (" propertyChangeSupport = " + listListeners ()); if ( listener == null ) { 112 Util.THIS.debug (" eventSource = " + eventSource); Util.THIS.debug (new RuntimeException ("TreeEventChangeSupport.addPropertyChangeListener")); } 115 } 116 } 117 118 119 122 public final void removePropertyChangeListener (PropertyChangeListener listener) { 123 getPropertyChangeSupport ().removePropertyChangeListener (listener); 124 125 if ( Util.THIS.isLoggable() ) { 126 Util.THIS.debug ("TreeEventChangeSupport::removePropertyChangeListener: listener = " + listener); Util.THIS.debug (" propertyChangeSupport = " + listListeners ()); } 129 } 130 131 133 public final void removePropertyChangeListener (String propertyName, PropertyChangeListener listener) { 134 getPropertyChangeSupport ().removePropertyChangeListener (propertyName, listener); 135 136 if ( Util.THIS.isLoggable() ) { 137 Util.THIS.debug ("TreeEventChangeSupport::removePropertyChangeListener: propertyName = " + propertyName); Util.THIS.debug (" listener = " + listener); Util.THIS.debug ("- ::removePropertyChangeListener: propertyChangeSupport = " + listListeners ()); } 141 } 142 143 149 public final boolean hasPropertyChangeListeners (String propertyName) { 150 return getPropertyChangeSupport ().hasListeners (propertyName); 151 } 152 153 159 public final void firePropertyChange (TreeEvent evt) { 160 if ( Util.THIS.isLoggable() ) { 161 Util.THIS.debug ("TreeEventChangeSupport::firePropertyChange ( " + evt + " )"); Util.THIS.debug (" eventSource = " + eventSource); Util.THIS.debug (" EventManager = " + eventSource.getEventManager ()); } 165 166 if ( eventSource.getEventManager () == null ) 167 return; 168 eventSource.getEventManager ().firePropertyChange (this, evt); 169 } 170 171 173 protected final void firePropertyChangeNow (TreeEvent evt) { 174 getPropertyChangeSupport ().firePropertyChange (evt); 175 } 176 177 179 protected final void firePropertyChangeLater (TreeEvent evt) { 180 eventCache.addEvent (evt); 181 } 182 183 185 protected final void firePropertyChangeCache () { 186 eventCache.firePropertyChange (); 187 } 188 189 191 protected final void clearPropertyChangeCache () { 192 eventCache.clear (); 193 } 194 195 196 200 201 203 private String listListeners (Object instance) { 204 try { 205 Class klass = instance.getClass (); 206 Field field = klass.getDeclaredField ("listeners"); field.setAccessible (true); 208 209 return field.get (instance).toString (); 210 } catch (Exception ex) { 211 return "" + ex.getClass () + " " + ex.getMessage (); } 213 } 214 215 217 private String listChildrenListeners (PropertyChangeSupport support) { 218 try { 219 Object instance = support; 220 Class klass = instance.getClass (); 221 Field field = klass.getDeclaredField ("children"); field.setAccessible (true); 223 224 StringBuffer sb = new StringBuffer (); 225 Map map = (Map )field.get (instance); 226 if (map == null) return ""; 227 Set keys = map.keySet (); 228 Iterator it = keys.iterator (); 229 while (it.hasNext ()) { 230 Object key = it.next (); 231 sb.append ("\n[").append (key).append ("] ").append (listListeners (map.get (key))); } 233 234 return sb.toString (); 235 } catch (Exception ex) { 236 ex.printStackTrace (); 237 return "<" + ex + ">"; } 239 } 240 241 242 245 public final String listListeners () { 246 StringBuffer sb = new StringBuffer (); 247 248 sb.append ("[*general*] ").append (listListeners (getPropertyChangeSupport ())).append ("\n"); sb.append (listChildrenListeners (getPropertyChangeSupport ())); 250 251 return sb.toString (); 252 } 253 254 255 259 262 private class EventCache { 263 264 265 List eventList; 266 267 268 272 273 public EventCache () { 274 eventList = new LinkedList (); 275 } 276 277 278 282 284 public void clear () { 285 synchronized ( eventList ) { 286 eventList.clear (); 287 } 288 } 289 290 291 293 public void addEvent (TreeEvent event) { 294 synchronized ( eventList ) { 295 eventList.add (event); 296 } 297 } 298 299 301 public void firePropertyChange () { 302 List listCopy; 303 synchronized ( eventList ) { 304 listCopy = new LinkedList (eventList); 305 eventList.clear (); 306 } 307 Iterator it = listCopy.iterator (); 308 while ( it.hasNext () ) { 309 firePropertyChangeNow ((TreeEvent)it.next ()); 310 } 311 } 312 313 } 315 } 316 | Popular Tags |