KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ui > DOMTree


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57 package ui;
58
59 import java.io.Serializable JavaDoc;
60 import java.util.Hashtable JavaDoc;
61
62 import javax.swing.JTree JavaDoc;
63 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
64 import javax.swing.tree.DefaultTreeModel JavaDoc;
65 import javax.swing.tree.MutableTreeNode JavaDoc;
66
67 import org.w3c.dom.Document JavaDoc;
68 import org.w3c.dom.NamedNodeMap JavaDoc;
69 import org.w3c.dom.Node JavaDoc;
70 import org.w3c.dom.NodeList JavaDoc;
71
72 /**
73  * Displays a DOM document in a tree control.
74  *
75  * @author Andy Clark, IBM
76  * @version
77  */

78 public class DOMTree
79     extends JTree JavaDoc
80     {
81
82     //
83
// Constructors
84
//
85

86     /** Default constructor. */
87     public DOMTree() {
88         this(null);
89         }
90
91     /** Constructs a tree with the specified document. */
92     public DOMTree(Document JavaDoc document) {
93         super(new Model JavaDoc());
94
95         // set tree properties
96
setRootVisible(false);
97
98         // set properties
99
setDocument(document);
100
101         } // <init>()
102

103     //
104
// Public methods
105
//
106

107     /** Sets the document. */
108     public void setDocument(Document JavaDoc document) {
109         ((Model JavaDoc)getModel()).setDocument(document);
110         expandRow(0);
111         }
112
113     /** Returns the document. */
114     public Document JavaDoc getDocument() {
115         return ((Model JavaDoc)getModel()).getDocument();
116         }
117
118     /** get the org.w3c.Node for a MutableTreeNode. */
119     public Node JavaDoc getNode(Object JavaDoc treeNode) {
120         return ((Model JavaDoc)getModel()).getNode(treeNode);
121     }
122
123     //
124
// Classes
125
//
126

127     /**
128      * DOM tree model.
129      *
130      * @author Andy Clark, IBM
131      * @version
132      */

133     static class Model
134         extends DefaultTreeModel JavaDoc
135         implements Serializable JavaDoc
136         {
137
138         //
139
// Data
140
//
141

142         /** Document. */
143         private Document JavaDoc document;
144         /** Node Map. */
145         private Hashtable JavaDoc nodeMap = new Hashtable JavaDoc();
146         
147
148         //
149
// Constructors
150
//
151

152         /** Default constructor. */
153         public Model() {
154             this(null);
155             }
156
157         /** Constructs a model from the specified document. */
158         public Model(Document JavaDoc document) {
159             super(new DefaultMutableTreeNode JavaDoc());
160             setDocument(document);
161             }
162
163         //
164
// Public methods
165
//
166

167         /** Sets the document. */
168         public synchronized void setDocument(Document JavaDoc document) {
169
170             // save document
171
this.document = document;
172
173             // clear tree and re-populate
174
((DefaultMutableTreeNode JavaDoc)getRoot()).removeAllChildren();
175             nodeMap.clear();
176             buildTree();
177             fireTreeStructureChanged(this, new Object JavaDoc[] { getRoot() }, new int[0], new Object JavaDoc[0]);
178
179             } // setDocument(Document)
180

181         /** Returns the document. */
182         public Document JavaDoc getDocument() {
183             return document;
184             }
185
186         /** get the org.w3c.Node for a MutableTreeNode. */
187         public Node JavaDoc getNode(Object JavaDoc treeNode) {
188             return (Node JavaDoc)nodeMap.get(treeNode);
189         }
190
191         //
192
// Private methods
193
//
194

195         /** Builds the tree. */
196         private void buildTree() {
197             
198             // is there anything to do?
199
if (document == null) { return; }
200
201             // iterate over children of this node
202
NodeList JavaDoc nodes = document.getChildNodes();
203             int len = (nodes != null) ? nodes.getLength() : 0;
204             MutableTreeNode JavaDoc root = (MutableTreeNode JavaDoc)getRoot();
205             for (int i = 0; i < len; i++) {
206                 Node JavaDoc node = nodes.item(i);
207                 switch (node.getNodeType()) {
208                     case Node.DOCUMENT_NODE: {
209                         root = insertDocumentNode(node, root);
210                         break;
211                         }
212
213                     case Node.ELEMENT_NODE: {
214                         insertElementNode(node, root);
215                         break;
216                         }
217
218                     default: // ignore
219

220                     } // switch
221

222                 } // for
223

224             } // buildTree()
225

226         /** Inserts a node and returns a reference to the new node. */
227         private MutableTreeNode JavaDoc insertNode(String JavaDoc what, MutableTreeNode JavaDoc where) {
228
229             MutableTreeNode JavaDoc node = new DefaultMutableTreeNode JavaDoc(what);
230             insertNodeInto(node, where, where.getChildCount());
231             return node;
232
233             } // insertNode(Node,MutableTreeNode):MutableTreeNode
234

235         /** Inserts the document node. */
236         private MutableTreeNode JavaDoc insertDocumentNode(Node JavaDoc what, MutableTreeNode JavaDoc where) {
237             MutableTreeNode JavaDoc treeNode = insertNode("<"+what.getNodeName()+'>', where);
238             nodeMap.put(treeNode, what);
239             return treeNode;
240             }
241
242         /** Inserts an element node. */
243         private MutableTreeNode JavaDoc insertElementNode(Node JavaDoc what, MutableTreeNode JavaDoc where) {
244
245             // build up name
246
StringBuffer JavaDoc name = new StringBuffer JavaDoc();
247             name.append('<');
248             name.append(what.getNodeName());
249             NamedNodeMap JavaDoc attrs = what.getAttributes();
250             int attrCount = (attrs != null) ? attrs.getLength() : 0;
251             for (int i = 0; i < attrCount; i++) {
252                 Node JavaDoc attr = attrs.item(i);
253                 name.append(' ');
254                 name.append(attr.getNodeName());
255                 name.append("=\"");
256                 name.append(attr.getNodeValue());
257                 name.append('"');
258                 }
259             name.append('>');
260
261             // insert element node
262

263             MutableTreeNode JavaDoc element = insertNode(name.toString(), where);
264             nodeMap.put(element, what);
265             
266             // gather up attributes and children nodes
267
NodeList JavaDoc children = what.getChildNodes();
268             int len = (children != null) ? children.getLength() : 0;
269             for (int i = 0; i < len; i++) {
270                 Node JavaDoc node = children.item(i);
271                 switch (node.getNodeType()) {
272                     case Node.CDATA_SECTION_NODE: {
273                        insertCDataSectionNode( node, element ); //Add a Section Node
274
break;
275                       }
276                     case Node.TEXT_NODE: {
277                         insertTextNode(node, element);
278                         break;
279                         }
280                     case Node.ELEMENT_NODE: {
281                         insertElementNode(node, element);
282                         break;
283                         }
284                     }
285                 }
286
287             return element;
288
289             } // insertElementNode(Node,MutableTreeNode):MutableTreeNode
290

291         /** Inserts a text node. */
292         private MutableTreeNode JavaDoc insertTextNode(Node JavaDoc what, MutableTreeNode JavaDoc where) {
293             String JavaDoc value = what.getNodeValue().trim();
294             if (value.length() > 0) {
295                 MutableTreeNode JavaDoc treeNode = insertNode(value, where);
296                 nodeMap.put(treeNode, what);
297                 return treeNode;
298                 }
299             return null;
300             }
301
302         
303       /** Inserts a CData Section Node. */
304       private MutableTreeNode JavaDoc insertCDataSectionNode(Node JavaDoc what, MutableTreeNode JavaDoc where) {
305          StringBuffer JavaDoc CSectionBfr = new StringBuffer JavaDoc();
306          //--- optional --- CSectionBfr.append( "<![CDATA[" );
307
CSectionBfr.append( what.getNodeValue() );
308          //--- optional --- CSectionBfr.append( "]]>" );
309
if (CSectionBfr.length() > 0) {
310             MutableTreeNode JavaDoc treeNode = insertNode(CSectionBfr.toString(), where);
311             nodeMap.put(treeNode, what);
312             return treeNode;
313             }
314          return null;
315          }
316
317
318       } // class Model
319

320
321
322     } // class DOMTree
323
Popular Tags