1 19 package org.netbeans.tax; 20 21 import org.netbeans.tax.event.TreeEventManager; 22 24 import org.netbeans.tax.spec.Document; 25 import org.netbeans.tax.event.TreeEventManager; 26 31 public class TreeDocument extends AbstractTreeDocument implements TreeDocumentRoot { 32 33 34 public static final String PROP_VERSION = "version"; 36 public static final String PROP_ENCODING = "encoding"; 38 public static final String PROP_STANDALONE = "standalone"; 40 41 42 private TreeEventManager eventManager; 43 44 45 private String version; 46 47 48 private String encoding; 49 50 51 private String standalone; 52 53 54 56 57 private TreeDocumentType documentType; 59 60 private TreeElement rootElement; 62 63 67 71 public TreeDocument (String version, String encoding, String standalone) throws InvalidArgumentException { 72 super (); 73 74 checkVersion (version); 75 checkEncoding (encoding); 76 checkStandalone (standalone); 77 checkHeader (version, encoding, standalone); 78 79 this.version = version; 80 this.encoding = encoding; 81 this.standalone = standalone; 82 this.eventManager = new TreeEventManager (); 83 84 this.documentType = null; 85 this.rootElement = null; 86 } 87 88 92 public TreeDocument () throws InvalidArgumentException { 93 this (null, null, null); } 95 96 97 protected TreeDocument (TreeDocument document, boolean deep) { 98 super (document, deep); 99 100 this.version = document.version; 101 this.encoding = document.encoding; 102 this.standalone = document.standalone; 103 this.eventManager = new TreeEventManager (document.eventManager); 104 } 105 106 107 111 113 public Object clone (boolean deep) { 114 return new TreeDocument (this, deep); 115 } 116 117 119 public boolean equals (Object object, boolean deep) { 120 if (!!! super.equals (object, deep)) 121 return false; 122 123 TreeDocument peer = (TreeDocument) object; 124 if (!!! Util.equals (this.getVersion (), peer.getVersion ())) 125 return false; 126 if (!!! Util.equals (this.getEncoding (), peer.getEncoding ())) 127 return false; 128 if (!!! Util.equals (this.getStandalone (), peer.getStandalone ())) 129 return false; 130 if (!!! Util.equals (this.documentType, peer.documentType)) 131 return false; 132 if (!!! Util.equals (this.rootElement, peer.rootElement)) 133 return false; 134 135 return true; 136 } 137 138 143 public void merge (TreeObject treeObject) throws CannotMergeException { 144 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("TreeDocument::merge: " + treeObject); 146 super.merge (treeObject); 147 148 TreeDocument peer = (TreeDocument) treeObject; 149 150 try { 151 setVersionImpl (peer.getVersion ()); 152 setEncodingImpl (peer.getEncoding ()); 153 setStandaloneImpl (peer.getStandalone ()); 154 } catch (Exception exc) { 155 throw new CannotMergeException (treeObject, exc); 156 } 157 158 TreeEventManager manager = getEventManager (); 160 if (manager != null) { 161 manager.setFirePolicy (TreeEventManager.FIRE_NOW); 162 } 163 164 } 165 166 167 171 173 public final String getVersion () { 174 return version; 175 } 176 177 179 private final void setVersionImpl (String newVersion) { 180 String oldVersion = this.version; 181 182 this.version = newVersion; 183 184 firePropertyChange (PROP_VERSION, oldVersion, newVersion); 185 } 186 187 191 public final void setVersion (String newVersion) throws ReadOnlyException, InvalidArgumentException { 192 if ( Util.equals (this.version, newVersion) ) 196 return; 197 checkReadOnly (); 198 checkVersion (newVersion); 199 checkHeader (newVersion, this.encoding, this.standalone); 200 201 setVersionImpl (newVersion); 205 } 206 207 209 protected final void checkVersion (String version) throws InvalidArgumentException { 210 TreeUtilities.checkDocumentVersion (version); 211 } 212 213 214 216 public final String getEncoding () { 217 return encoding; 218 } 219 220 222 private final void setEncodingImpl (String newEncoding) { 223 String oldEncoding = this.encoding; 224 225 this.encoding = newEncoding; 226 227 firePropertyChange (PROP_ENCODING, oldEncoding, newEncoding); 228 } 229 230 234 public final void setEncoding (String newEncoding) throws ReadOnlyException, InvalidArgumentException { 235 if ( Util.equals (this.encoding, newEncoding) ) 239 return; 240 checkReadOnly (); 241 checkEncoding (newEncoding); 242 checkHeader (this.version, newEncoding, this.standalone); 243 244 setEncodingImpl (newEncoding); 248 } 249 250 252 protected final void checkEncoding (String encoding) throws InvalidArgumentException { 253 TreeUtilities.checkDocumentEncoding (encoding); 254 } 255 256 257 259 public final String getStandalone () { 260 return standalone; 261 } 262 263 265 private final void setStandaloneImpl (String newStandalone) { 266 String oldStandalone = this.standalone; 267 268 this.standalone = newStandalone; 269 270 firePropertyChange (PROP_STANDALONE, oldStandalone, newStandalone); 271 } 272 273 277 public final void setStandalone (String newStandalone) throws ReadOnlyException, InvalidArgumentException { 278 if ( Util.equals (this.standalone, newStandalone) ) 282 return; 283 checkReadOnly (); 284 checkStandalone (newStandalone); 285 checkHeader (this.version, this.encoding, newStandalone); 286 287 setStandaloneImpl (newStandalone); 291 } 292 293 295 protected final void checkStandalone (String standalone) throws InvalidArgumentException { 296 TreeUtilities.checkDocumentStandalone (standalone); 297 } 298 299 301 314 315 319 public final void setHeader (String newVersion, String newEncoding, String newStandalone) throws ReadOnlyException, InvalidArgumentException { 320 boolean setVersion = !!! Util.equals (this.version, newVersion); 324 boolean setEncoding = !!! Util.equals (this.encoding, newEncoding); 325 boolean setStandalone = !!! Util.equals (this.standalone, newStandalone); 326 if ( !!! setVersion && 327 !!! setEncoding && 328 !!! setStandalone ) { 329 return; 330 } 331 checkReadOnly (); 332 if ( setVersion ) { 333 checkVersion (newVersion); 334 } 335 if ( setEncoding ) { 336 checkEncoding (newEncoding); 337 } 338 if ( setStandalone ) { 339 checkStandalone (newStandalone); 340 } 341 checkHeader (newVersion, newEncoding, newStandalone); 342 343 if ( setVersion ) { 347 setVersionImpl (newVersion); 348 } 349 if ( setEncoding ) { 350 setEncodingImpl (newEncoding); 351 } 352 if ( setStandalone ) { 353 setStandaloneImpl (newStandalone); 354 } 355 } 356 357 359 protected final void checkHeader (String version, String encoding, String standalone) throws InvalidArgumentException { 360 if ( (version == null) && ( (encoding != null) || (standalone != null) ) ) { 361 throw new InvalidArgumentException 362 (Util.THIS.getString ("EXC_invalid_document_header"), 363 new NullPointerException ()); 364 } 365 } 366 367 368 372 378 379 383 385 public final TreeEventManager getRootEventManager () { 386 return eventManager; 387 } 388 389 390 394 396 public final TreeDocumentType getDocumentType () { 397 return documentType; 398 } 399 400 405 public final void setDocumentType (TreeDocumentType newDocumentType) throws ReadOnlyException, InvalidArgumentException { 406 if ( newDocumentType == null ) { 407 removeChild (this.documentType); 408 } else if ( this.documentType == null ) { 409 if ( this.rootElement == null ) { 410 appendChild (newDocumentType); 411 } else { 412 insertChildAt (newDocumentType, 0); 413 } 414 } else { 415 replaceChild (this.documentType, newDocumentType); 416 } 417 } 418 419 420 422 public final TreeElement getDocumentElement () { 423 return rootElement; 424 } 425 426 431 public final void setDocumentElement (TreeElement newElement) throws ReadOnlyException, InvalidArgumentException { 432 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("\nTreeDocument::setDocumentElement: oldDocumentElement = " + this.rootElement); if ( Util.THIS.isLoggable() ) Util.THIS.debug (" ::setDocumentElement: newDocumentElement = " + newElement); 435 if ( newElement == null ) { 436 removeChild (this.rootElement); 437 } else if ( this.rootElement == null ) { 438 appendChild (newElement); 439 } else { 440 replaceChild (this.rootElement, newElement); 441 } 442 } 443 444 445 449 451 protected TreeObjectList.ContentManager createChildListContentManager () { 452 return new ChildListContentManager (); 453 } 454 455 456 459 protected class ChildListContentManager extends AbstractTreeDocument.ChildListContentManager { 460 461 463 public TreeNode getOwnerNode () { 464 return TreeDocument.this; 465 } 466 467 469 public void checkAssignableObject (Object obj) { 470 super.checkAssignableObject (obj); 471 checkAssignableClass (Document.Child.class, obj); 472 } 473 474 476 public void objectInserted (TreeObject obj) { 477 super.objectInserted (obj); 478 479 try { 480 if (obj instanceof TreeDocumentType) { 481 if (TreeDocument.this.documentType != null && TreeDocument.this.documentType != obj) { 482 removeChild (TreeDocument.this.documentType); 483 } 484 TreeDocument.this.documentType = (TreeDocumentType)obj; 485 } else if (obj instanceof TreeElement) { 486 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("\nTreeDocument::ChildListContentManager::objectInserted: obj = " + obj); if ( Util.THIS.isLoggable() ) Util.THIS.debug (" :: ::objectInserted: old root element = " + TreeDocument.this.rootElement); 489 if (TreeDocument.this.rootElement != null && TreeDocument.this.rootElement != obj) { 490 removeChild (TreeDocument.this.rootElement); 491 } 492 TreeDocument.this.rootElement = (TreeElement)obj; 493 494 if ( Util.THIS.isLoggable() ) Util.THIS.debug (" :: ::objectInserted: NEW root element = " + TreeDocument.this.rootElement); } 496 } catch (Exception exc) { 497 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("TreeDocument::ChildListContentManager.objectInserted", exc); } 499 } 500 501 503 public void objectRemoved (TreeObject obj) { 504 super.objectRemoved (obj); 505 506 if ( TreeDocument.this.documentType == obj ) { 507 TreeDocument.this.documentType = null; 508 } else if ( TreeDocument.this.rootElement == obj ) { 509 TreeDocument.this.rootElement = null; 510 } 511 } 512 513 } 515 } 516 | Popular Tags |