KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 import org.netbeans.tax.spec.DocumentFragment;
24 import org.netbeans.tax.spec.DTD;
25
26 /**
27  * It may contain <b>multiple "root elements"</b> because it must be placed somewhere
28  * anyway.
29  * <p>It maps to external entities.
30  *
31  * @author Libor Kramolis
32  * @version 0.1
33  */

34 public class TreeDocumentFragment extends AbstractTreeDocument implements TreeDocumentRoot {
35     /** */
36     public static final String JavaDoc PROP_VERSION = "version"; // NOI18N
37
/** */
38     public static final String JavaDoc PROP_ENCODING = "encoding"; // NOI18N
39

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

54     /**
55      * Creates new TreeDocumentFragment.
56      * @throws InvalidArgumentException
57      */

58     public TreeDocumentFragment (String JavaDoc version, String JavaDoc encoding) throws InvalidArgumentException {
59         super ();
60         
61         checkVersion (version);
62         checkEncoding (encoding);
63         checkHeader (version, encoding);
64         
65         this.version = version;
66         this.encoding = encoding;
67         this.eventManager = new TreeEventManager ();
68     }
69     
70     
71     /**
72      * Creates new TreeDocumentFragment.
73      * @throws InvalidArgumentException
74      */

75     public TreeDocumentFragment () throws InvalidArgumentException {
76         this (null, null); // Q: is it valid? A: yes, header is not mandatory
77
}
78
79
80     /** Creates new TreeDocumentFragment -- copy constructor. */
81     protected TreeDocumentFragment (TreeDocumentFragment documentFragment, boolean deep) {
82         super (documentFragment, deep);
83         
84         this.version = documentFragment.version;
85         this.encoding = documentFragment.encoding;
86         this.eventManager = new TreeEventManager (documentFragment.eventManager);
87     }
88     
89     
90     //
91
// from TreeObject
92
//
93

94     /**
95      */

96     public Object JavaDoc clone (boolean deep) {
97         return new TreeDocumentFragment (this, deep);
98     }
99     
100     /**
101      */

102     public boolean equals (Object JavaDoc object, boolean deep) {
103         if (!!! super.equals (object, deep))
104             return false;
105         
106         TreeDocumentFragment peer = (TreeDocumentFragment) object;
107         if (!!! Util.equals (this.getVersion (), peer.getVersion ()))
108             return false;
109         if (!!! Util.equals (this.getEncoding (), peer.getEncoding ()))
110             return false;
111         
112         return true;
113     }
114     
115     /*
116      * Merges version and encoding properties
117      */

118     public void merge (TreeObject treeObject) throws CannotMergeException {
119         super.merge (treeObject);
120         
121         TreeDocumentFragment peer = (TreeDocumentFragment) treeObject;
122         
123         try {
124             setVersionImpl (peer.getVersion ());
125             setEncodingImpl (peer.getEncoding ());
126         } catch (Exception JavaDoc exc) {
127             throw new CannotMergeException (treeObject, exc);
128         }
129     }
130     
131     
132     //
133
// from TreeDocumentRoot
134
//
135

136     /**
137      */

138     public String JavaDoc getVersion () {
139         return version;
140     }
141     
142     /**
143      */

144     private final void setVersionImpl (String JavaDoc newVersion) {
145         String JavaDoc oldVersion = this.version;
146         
147         this.version = newVersion;
148         
149         firePropertyChange (PROP_VERSION, oldVersion, newVersion);
150     }
151     
152     /**
153      * @throws ReadOnlyException
154      * @throws InvalidArgumentException
155      */

156     public final void setVersion (String JavaDoc newVersion) throws ReadOnlyException, InvalidArgumentException {
157         //
158
// check new value
159
//
160
if ( Util.equals (this.version, newVersion) )
161             return;
162         checkReadOnly ();
163         checkVersion (newVersion);
164         checkHeader (newVersion, this.encoding);
165         
166         //
167
// set new value
168
//
169
setVersionImpl (newVersion);
170     }
171     
172     /**
173      */

174     protected final void checkVersion (String JavaDoc version) throws InvalidArgumentException {
175         TreeUtilities.checkDocumentFragmentVersion (version);
176     }
177     
178     
179     /**
180      */

181     public String JavaDoc getEncoding () {
182         return encoding;
183     }
184     
185     /**
186      */

187     private void setEncodingImpl (String JavaDoc newEncoding) {
188         String JavaDoc oldEncoding = this.encoding;
189         
190         this.encoding = newEncoding;
191         
192         firePropertyChange (PROP_ENCODING, oldEncoding, newEncoding);
193     }
194     
195     /**
196      * @throws ReadOnlyException
197      * @throws InvalidArgumentException
198      */

199     public final void setEncoding (String JavaDoc newEncoding) throws ReadOnlyException, InvalidArgumentException {
200         //
201
// check new value
202
//
203
if ( Util.equals (this.encoding, newEncoding) )
204             return;
205         checkReadOnly ();
206         checkEncoding (newEncoding);
207         checkHeader (this.version, newEncoding);
208         
209         //
210
// set new value
211
//
212
setEncodingImpl (newEncoding);
213     }
214     
215     /**
216      */

217     protected final void checkEncoding (String JavaDoc encoding) throws InvalidArgumentException {
218         TreeUtilities.checkDocumentFragmentEncoding (encoding);
219     }
220         
221     /**
222      * @throws ReadOnlyException
223      * @throws InvalidArgumentException
224      */

225     public final void setHeader (String JavaDoc newVersion, String JavaDoc newEncoding) throws ReadOnlyException, InvalidArgumentException {
226         //
227
// check new value
228
//
229
boolean setVersion = !!! Util.equals (this.version, newVersion);
230         boolean setEncoding = !!! Util.equals (this.encoding, newEncoding);
231         if ( !!! setVersion &&
232              !!! setEncoding ) {
233             return;
234         }
235         checkReadOnly ();
236         if ( setVersion ) {
237             checkVersion (newVersion);
238         }
239         if ( setEncoding ) {
240             checkEncoding (newEncoding);
241         }
242         checkHeader (newVersion, newEncoding);
243         
244         //
245
// set new value
246
//
247
if ( setVersion ) {
248             setVersionImpl (newVersion);
249         }
250         if ( setEncoding ) {
251             setEncodingImpl (newEncoding);
252         }
253     }
254     
255     /**
256      */

257     protected final void checkHeader (String JavaDoc version, String JavaDoc encoding) throws InvalidArgumentException {
258         if ((version != null) && (encoding == null)) {
259             throw new InvalidArgumentException
260             (Util.THIS.getString ("EXC_invalid_document_fragment_header"),
261             new NullPointerException JavaDoc ());
262         }
263     }
264     
265     
266     //
267
// event model
268
//
269

270     /**
271      */

272     public TreeEventManager getRootEventManager () {
273         return eventManager;
274     }
275     
276     
277     //
278
// TreeObjectList.ContentManager
279
//
280

281     /**
282      */

283     protected TreeObjectList.ContentManager createChildListContentManager () {
284         return new ChildListContentManager ();
285     }
286     
287     
288     /**
289      *
290      */

291     protected class ChildListContentManager extends AbstractTreeDocument.ChildListContentManager {
292         
293         /**
294          */

295         public TreeNode getOwnerNode () {
296             return TreeDocumentFragment.this;
297         }
298         
299         /**
300          */

301         public void checkAssignableObject (Object JavaDoc obj) {
302             super.checkAssignableObject (obj);
303             checkAssignableClass (DocumentFragment.Child.class, obj);
304         }
305         
306     } // end: class ChildListContentManager
307

308
309 }
310
Popular Tags