KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > tax > TreeDocument


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 package org.netbeans.tax;
20
21 import org.netbeans.tax.event.TreeEventManager;
22 // import org.netbeans.tax.grammar.Grammar; // will be ...
23

24 import org.netbeans.tax.spec.Document;
25 import org.netbeans.tax.event.TreeEventManager;
26 /**
27  *
28  * @author Libor Kramolis
29  * @version 0.1
30  */

31 public class TreeDocument extends AbstractTreeDocument implements TreeDocumentRoot {
32
33     /** */
34     public static final String JavaDoc PROP_VERSION = "version"; // NOI18N
35
/** */
36     public static final String JavaDoc PROP_ENCODING = "encoding"; // NOI18N
37
/** */
38     public static final String JavaDoc PROP_STANDALONE = "standalone"; // NOI18N
39

40     
41     /** Own event manager. */
42     private TreeEventManager eventManager;
43     
44     /** -- can be null. */
45     private String JavaDoc version;
46     
47     /** -- can be null. */
48     private String JavaDoc encoding;
49     
50     /** -- can be null. */
51     private String JavaDoc standalone;
52     
53     /** -- can be null. */
54     // private Grammar grammar; // will be ...
55

56     /** -- can be null. */
57     private TreeDocumentType documentType; //cache of some child node
58

59     /** -- can be null. */
60     private TreeElement rootElement; //cache of some child node
61

62     
63     //
64
// init
65
//
66

67     /**
68      * Creates new TreeDocument.
69      * @throws InvalidArgumentException
70      */

71     public TreeDocument (String JavaDoc version, String JavaDoc encoding, String JavaDoc 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     /**
89      * Creates new TreeDocument.
90      * @throws InvalidArgumentException
91      */

92     public TreeDocument () throws InvalidArgumentException {
93         this (null, null, null); // Q: is it valid? A: yes, header is not mandatory
94
}
95     
96     /** Creates new TreeDocument -- copy constructor. */
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     //
108
// from TreeObject
109
//
110

111     /**
112      */

113     public Object JavaDoc clone (boolean deep) {
114         return new TreeDocument (this, deep);
115     }
116     
117     /**
118      */

119     public boolean equals (Object JavaDoc 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     /*
139      * Merges following paroperties: version, encoding and standlaone.
140      * Reassignes caches (root, doctype) since all events are already fired
141      * during merging superclass TreeObjectList.
142      */

143     public void merge (TreeObject treeObject) throws CannotMergeException {
144         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("TreeDocument::merge: " + treeObject);//, new RuntimeException ()); // NOI18N
145

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 JavaDoc exc) {
155             throw new CannotMergeException (treeObject, exc);
156         }
157         
158         // just to be sure that we never miss
159
TreeEventManager manager = getEventManager ();
160         if (manager != null) {
161             manager.setFirePolicy (TreeEventManager.FIRE_NOW);
162         }
163         
164     }
165     
166     
167     //
168
// itself
169
//
170

171     /**
172      */

173     public final String JavaDoc getVersion () {
174         return version;
175     }
176     
177     /**
178      */

179     private final void setVersionImpl (String JavaDoc newVersion) {
180         String JavaDoc oldVersion = this.version;
181         
182         this.version = newVersion;
183         
184         firePropertyChange (PROP_VERSION, oldVersion, newVersion);
185     }
186     
187     /**
188      * @throws ReadOnlyException
189      * @throws InvalidArgumentException
190      */

191     public final void setVersion (String JavaDoc newVersion) throws ReadOnlyException, InvalidArgumentException {
192         //
193
// check new value
194
//
195
if ( Util.equals (this.version, newVersion) )
196             return;
197         checkReadOnly ();
198         checkVersion (newVersion);
199         checkHeader (newVersion, this.encoding, this.standalone);
200         
201         //
202
// set new value
203
//
204
setVersionImpl (newVersion);
205     }
206     
207     /**
208      */

209     protected final void checkVersion (String JavaDoc version) throws InvalidArgumentException {
210         TreeUtilities.checkDocumentVersion (version);
211     }
212     
213     
214     /**
215      */

216     public final String JavaDoc getEncoding () {
217         return encoding;
218     }
219     
220     /**
221      */

222     private final void setEncodingImpl (String JavaDoc newEncoding) {
223         String JavaDoc oldEncoding = this.encoding;
224         
225         this.encoding = newEncoding;
226         
227         firePropertyChange (PROP_ENCODING, oldEncoding, newEncoding);
228     }
229     
230     /**
231      * @throws ReadOnlyException
232      * @throws InvalidArgumentException
233      */

234     public final void setEncoding (String JavaDoc newEncoding) throws ReadOnlyException, InvalidArgumentException {
235         //
236
// check new value
237
//
238
if ( Util.equals (this.encoding, newEncoding) )
239             return;
240         checkReadOnly ();
241         checkEncoding (newEncoding);
242         checkHeader (this.version, newEncoding, this.standalone);
243         
244         //
245
// set new value
246
//
247
setEncodingImpl (newEncoding);
248     }
249     
250     /**
251      */

252     protected final void checkEncoding (String JavaDoc encoding) throws InvalidArgumentException {
253         TreeUtilities.checkDocumentEncoding (encoding);
254     }
255     
256     
257     /**
258      */

259     public final String JavaDoc getStandalone () {
260         return standalone;
261     }
262     
263     /**
264      */

265     private final void setStandaloneImpl (String JavaDoc newStandalone) {
266         String JavaDoc oldStandalone = this.standalone;
267         
268         this.standalone = newStandalone;
269         
270         firePropertyChange (PROP_STANDALONE, oldStandalone, newStandalone);
271     }
272     
273     /**
274      * @throws ReadOnlyException
275      * @throws InvalidArgumentException
276      */

277     public final void setStandalone (String JavaDoc newStandalone) throws ReadOnlyException, InvalidArgumentException {
278         //
279
// check new value
280
//
281
if ( Util.equals (this.standalone, newStandalone) )
282             return;
283         checkReadOnly ();
284         checkStandalone (newStandalone);
285         checkHeader (this.version, this.encoding, newStandalone);
286         
287         //
288
// set new value
289
//
290
setStandaloneImpl (newStandalone);
291     }
292     
293     /**
294      */

295     protected final void checkStandalone (String JavaDoc standalone) throws InvalidArgumentException {
296         TreeUtilities.checkDocumentStandalone (standalone);
297     }
298     
299     /**
300      */

301 /* private final void setHeaderImpl (String newVersion, String newEncoding, String newStandalone) {
302         String oldVersion = this.version;
303         String oldEncoding = this.encoding;
304         String oldStandalone = this.standalone;
305         
306         this.version = newVersion;
307         this.encoding = newEncoding;
308         this.standalone = newStandalone;
309         
310         firePropertyChange (PROP_VERSION, oldVersion, newVersion);
311         firePropertyChange (PROP_ENCODING, oldEncoding, newEncoding);
312         firePropertyChange (PROP_STANDALONE, oldStandalone, newStandalone);
313     }*/

314     
315     /**
316      * @throws ReadOnlyException
317      * @throws InvalidArgumentException
318      */

319     public final void setHeader (String JavaDoc newVersion, String JavaDoc newEncoding, String JavaDoc newStandalone) throws ReadOnlyException, InvalidArgumentException {
320         //
321
// check new value
322
//
323
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         //
344
// set new value
345
//
346
if ( setVersion ) {
347             setVersionImpl (newVersion);
348         }
349         if ( setEncoding ) {
350             setEncodingImpl (newEncoding);
351         }
352         if ( setStandalone ) {
353             setStandaloneImpl (newStandalone);
354         }
355     }
356     
357     /**
358      */

359     protected final void checkHeader (String JavaDoc version, String JavaDoc encoding, String JavaDoc 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 JavaDoc ());
364         }
365     }
366     
367     
368     //
369
// grammar // will be ...
370
//
371

372     // /**
373
// */
374
// public final Grammar getGrammar () {
375
// return grammar;
376
// }
377

378     
379     //
380
// event model
381
//
382

383     /**
384      */

385     public final TreeEventManager getRootEventManager () {
386         return eventManager;
387     }
388     
389     
390     //
391
// itself
392
//
393

394     /**
395      */

396     public final TreeDocumentType getDocumentType () {
397         return documentType;
398     }
399     
400     /**
401      * Update cache and propagate new DOCTYPE into children.
402      * @throws ReadOnlyException
403      * @throws InvalidArgumentException
404      */

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     /**
421      */

422     public final TreeElement getDocumentElement () {
423         return rootElement;
424     }
425     
426     /**
427      * Update cache and propagate new root element into children.
428      * @throws ReadOnlyException
429      * @throws InvalidArgumentException
430      */

431     public final void setDocumentElement (TreeElement newElement) throws ReadOnlyException, InvalidArgumentException {
432         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\nTreeDocument::setDocumentElement: oldDocumentElement = " + this.rootElement); // NOI18N
433
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (" ::setDocumentElement: newDocumentElement = " + newElement); // NOI18N
434

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     //
446
// TreeObjectList.ContentManager
447
//
448

449     /**
450      */

451     protected TreeObjectList.ContentManager createChildListContentManager () {
452         return new ChildListContentManager ();
453     }
454     
455     
456     /**
457      *
458      */

459     protected class ChildListContentManager extends AbstractTreeDocument.ChildListContentManager {
460         
461         /**
462          */

463         public TreeNode getOwnerNode () {
464             return TreeDocument.this;
465         }
466         
467         /**
468          */

469         public void checkAssignableObject (Object JavaDoc obj) {
470             super.checkAssignableObject (obj);
471             checkAssignableClass (Document.Child.class, obj);
472         }
473         
474         /**
475          */

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() ) /* then */ Util.THIS.debug ("\nTreeDocument::ChildListContentManager::objectInserted: obj = " + obj); // NOI18N
487
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (" :: ::objectInserted: old root element = " + TreeDocument.this.rootElement); // NOI18N
488

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() ) /* then */ Util.THIS.debug (" :: ::objectInserted: NEW root element = " + TreeDocument.this.rootElement);//, new RuntimeException ()); // NOI18N
495
}
496             } catch (Exception JavaDoc exc) {
497                 if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("TreeDocument::ChildListContentManager.objectInserted", exc); // NOI18N
498
}
499         }
500         
501         /**
502          */

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     } // end: class ChildListContentManager
514

515 }
516
Popular Tags