1 19 package org.netbeans.tax; 20 21 import java.io.PrintStream ; 22 import java.beans.PropertyChangeListener ; 23 24 import org.netbeans.tax.event.TreeEventManager; 25 import org.netbeans.tax.event.TreeEventModel; 26 import org.netbeans.tax.event.TreeEvent; 27 import org.netbeans.tax.event.TreeEventChangeSupport; 28 29 45 public abstract class TreeObject implements TreeEventModel { 46 47 48 public static final String PROP_READ_ONLY = "readOnly"; 50 51 private boolean readOnly; 52 53 54 transient private TreeEventChangeSupport eventChangeSupport; 55 56 57 61 62 63 protected TreeObject () { 64 this.readOnly = false; 65 this.eventChangeSupport = null; 66 } 67 68 72 protected TreeObject (TreeObject object) { 73 this.readOnly = object.readOnly; 74 this.eventChangeSupport = null; 75 } 76 77 78 82 85 public abstract Object clone (); 86 87 88 92 94 protected final boolean isInstance (Object object) { 95 return ( this.getClass ().isInstance (object) ); 96 } 97 98 99 103 105 abstract public boolean isInContext (); 106 107 109 abstract public void removeFromContext () throws ReadOnlyException; 110 111 112 116 118 public boolean equals (Object object) { 119 return super.equals (object); 120 121 } 124 125 127 public boolean equals (Object object, boolean deep) { 128 if (!!! isInstance (object)) 129 return false; 130 131 TreeObject peer = (TreeObject) object; 132 133 return (this.readOnly == peer.readOnly); 134 } 135 136 137 141 172 public void merge (TreeObject treeObject) throws CannotMergeException { 173 if (treeObject == this) 174 return; 175 176 checkMergeObject (treeObject); 177 178 TreeObject peer = treeObject; 179 180 setReadOnly (peer.isReadOnly ()); 181 } 182 183 185 protected final void checkMergeObject (TreeObject treeObject) throws CannotMergeException { 186 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("TreeObject::checkMergeObject: this = " + this); if ( Util.THIS.isLoggable() ) Util.THIS.debug (" ::checkMergeObject: treeObject = " + treeObject); if ( Util.THIS.isLoggable() ) Util.THIS.debug (" checkMergeObject: isSameClass ? " + isInstance (treeObject)); 190 if ( (treeObject == null) || (!!! isInstance (treeObject)) ) { 191 throw new CannotMergeException (treeObject); 192 } 193 } 194 195 199 201 public final boolean isReadOnly () { 202 return readOnly; 203 } 204 205 207 protected void setReadOnly (boolean newReadOnly) { 208 if (readOnly == newReadOnly) 209 return; 210 211 boolean oldReadOnly = this.readOnly; 212 this.readOnly = newReadOnly; 213 firePropertyChange (getEventChangeSupport ().createEvent (PROP_READ_ONLY, oldReadOnly ? Boolean.TRUE : Boolean.FALSE, newReadOnly ? Boolean.TRUE : Boolean.FALSE)); 214 } 215 216 218 protected final void checkReadOnly () throws ReadOnlyException { 219 if (readOnly == true) { 220 throw new ReadOnlyException (this); 221 } 222 } 223 224 225 226 230 233 protected final TreeEventChangeSupport getEventChangeSupport () { 234 if (eventChangeSupport == null) { 235 eventChangeSupport = new TreeEventChangeSupport (this); 236 } 237 return eventChangeSupport; 238 } 239 240 247 public abstract TreeEventManager getEventManager (); 248 249 250 252 256 258 262 264 public final void addReadonlyChangeListener (PropertyChangeListener listener) { 265 getEventChangeSupport ().addPropertyChangeListener (PROP_READ_ONLY, listener); 266 } 267 268 270 public final void removeReadonlyChangeListener (PropertyChangeListener listener) { 271 getEventChangeSupport ().removePropertyChangeListener (PROP_READ_ONLY, listener); 272 } 273 274 275 279 public final void addPropertyChangeListener (PropertyChangeListener listener) { 280 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Tree " + this + "attached listener" + listener); 282 getEventChangeSupport ().addPropertyChangeListener (listener); 283 } 284 285 286 290 public final void removePropertyChangeListener (PropertyChangeListener listener) { 291 getEventChangeSupport ().removePropertyChangeListener (listener); 292 } 293 294 300 protected final void firePropertyChange (TreeEvent evt) { 301 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("TreeObject firing " + evt); 303 getEventChangeSupport ().firePropertyChange (evt); 304 bubblePropertyChange (evt); 305 } 306 307 308 312 public final void addPropertyChangeListener (String propertyName, PropertyChangeListener listener) { 313 getEventChangeSupport ().addPropertyChangeListener (propertyName, listener); 314 } 315 316 320 public final void removePropertyChangeListener (String propertyName, PropertyChangeListener listener) { 321 getEventChangeSupport ().removePropertyChangeListener (propertyName, listener); 322 } 323 324 325 331 public final boolean hasPropertyChangeListeners (String propertyName) { 332 return getEventChangeSupport ().hasPropertyChangeListeners (propertyName); 333 } 334 335 336 344 protected final void firePropertyChange (String propertyName, Object oldValue, Object newValue) { 345 firePropertyChange (getEventChangeSupport ().createEvent (propertyName, oldValue, newValue)); 346 } 347 348 349 352 protected final void bubblePropertyChange (TreeEvent origEvt) { 353 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("\nTreeObject [ " + this + " ]::bubblePropertyChange: origEvt = " + origEvt.getPropertyName ()); 355 TreeObject source = (TreeObject)origEvt.getSource (); 356 if ( source instanceof TreeAttribute ) { 357 TreeAttribute attr = (TreeAttribute)source; 358 TreeElement ownElem = attr.getOwnerElement (); 359 if ( ownElem != null ) { 360 ownElem.firePropertyChange (TreeElement.PROP_ATTRIBUTES, attr, null); 361 } 362 } else if ( source instanceof TreeChild ) { 363 while ( source != null ) { 364 TreeChild child = (TreeChild)source; 365 TreeParentNode parent = child.getParentNode (); 366 367 if ( Util.THIS.isLoggable() ) Util.THIS.debug (" ::bubblePropertyChange::parentNode = " + parent); 369 if ( parent != null ) { 370 parent.getEventChangeSupport ().firePropertyChange (origEvt.createBubbling (parent)); 371 } 372 source = parent; 373 } 374 } 375 } 376 377 378 382 385 public final String listListeners () { 386 return getEventChangeSupport ().listListeners (); 387 } 388 389 } 390 | Popular Tags |