1 /*
2 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3 *
4 * This software is open source.
5 * See the bottom of this file for the licence.
6 *
7 * $Id: BranchTreeNode.java,v 1.8 2004/06/25 08:03:40 maartenc Exp $
8 */
9
10 package org.dom4j.swing;
11
12 import java.util.ArrayList;
13 import java.util.Enumeration;
14 import java.util.List;
15
16 import javax.swing.tree.TreeNode;
17
18 import org.dom4j.Branch;
19 import org.dom4j.CharacterData;
20 import org.dom4j.Node;
21
22 /*** <p><code>BranchTreeNode</code> implements the Swing TreeNode interface
23 * to bind dom4j XML Branch nodes (i.e. Document and Element nodes) to a Swing TreeModel.</p>
24 *
25 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a> (james.strachan@metastuff.com)
26 * @author Jakob Jenkov
27 * @version $Revision: 1.8 $
28 */
29 public class BranchTreeNode extends LeafTreeNode {
30
31 /*** Stores the child tree nodes */
32 protected List children;
33
34
35 public BranchTreeNode() {
36 }
37
38 public BranchTreeNode(Branch xmlNode) {
39 super(xmlNode);
40 }
41
42 public BranchTreeNode(TreeNode parent, Branch xmlNode) {
43 super( parent, xmlNode );
44 }
45
46
47 // TreeNode methods
48 //-------------------------------------------------------------------------
49 public Enumeration children() {
50 return new Enumeration() {
51 int index = -1;
52
53 public boolean hasMoreElements() {
54 return index + 1 < getChildCount();
55 }
56
57 public Object nextElement() {
58 return getChildAt( ++index );
59 }
60 };
61 }
62
63 public boolean getAllowsChildren() {
64 return true;
65 }
66
67 public TreeNode getChildAt(int childIndex) {
68 return (TreeNode) getChildList().get(childIndex);
69 }
70
71 public int getChildCount() {
72 return getChildList().size();
73 }
74
75 public int getIndex(TreeNode node) {
76 return getChildList().indexOf(node);
77 }
78
79 public boolean isLeaf() {
80 return getXmlBranch().nodeCount() <= 0;
81 }
82
83 public String toString() {
84 return xmlNode.getName();
85 }
86
87
88 // Implementation methods
89 //-------------------------------------------------------------------------
90
91 /*** Uses Lazy Initialization pattern to create a List of children */
92 protected List getChildList() {
93 // for now lets just create the children once, the first time they
94 // are asked for.
95 // XXXX - we may wish to detect inconsistencies here....
96 if ( children == null ) {
97 children = createChildList();
98 }
99 return children;
100 }
101
102
103 /*** Factory method to create List of children TreeNodes */
104 protected List createChildList() {
105 // add attributes and content as children?
106 Branch branch = getXmlBranch();
107 int size = branch.nodeCount();
108 List children = new ArrayList( size );
109 for ( int i = 0; i < size; i++ ) {
110 Node node = branch.node(i);
111
112 // ignore whitespace text nodes
113 if ( node instanceof CharacterData ) {
114 String text = node.getText();
115 if ( text == null ) {
116 continue;
117 }
118 text = text.trim();
119 if ( text.length() <= 0 ) {
120 continue;
121 }
122 }
123 children.add( createChildTreeNode( node ) );
124 }
125 return children;
126 }
127
128 /*** Factory method to create child tree nodes for a given XML node type
129 */
130 protected TreeNode createChildTreeNode( Node xmlNode ) {
131 if ( xmlNode instanceof Branch ) {
132 return new BranchTreeNode( this, (Branch) xmlNode );
133 }
134 else {
135 return new LeafTreeNode( this, xmlNode );
136 }
137
138 }
139 protected Branch getXmlBranch() {
140 return (Branch) xmlNode;
141 }
142 }
143
144
145
146
147 /*
148 * Redistribution and use of this software and associated documentation
149 * ("Software"), with or without modification, are permitted provided
150 * that the following conditions are met:
151 *
152 * 1. Redistributions of source code must retain copyright
153 * statements and notices. Redistributions must also contain a
154 * copy of this document.
155 *
156 * 2. Redistributions in binary form must reproduce the
157 * above copyright notice, this list of conditions and the
158 * following disclaimer in the documentation and/or other
159 * materials provided with the distribution.
160 *
161 * 3. The name "DOM4J" must not be used to endorse or promote
162 * products derived from this Software without prior written
163 * permission of MetaStuff, Ltd. For written permission,
164 * please contact dom4j-info@metastuff.com.
165 *
166 * 4. Products derived from this Software may not be called "DOM4J"
167 * nor may "DOM4J" appear in their names without prior written
168 * permission of MetaStuff, Ltd. DOM4J is a registered
169 * trademark of MetaStuff, Ltd.
170 *
171 * 5. Due credit should be given to the DOM4J Project -
172 * http://www.dom4j.org
173 *
174 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
175 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
176 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
177 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
178 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
179 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
180 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
181 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
182 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
183 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
184 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
185 * OF THE POSSIBILITY OF SUCH DAMAGE.
186 *
187 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
188 *
189 * $Id: BranchTreeNode.java,v 1.8 2004/06/25 08:03:40 maartenc Exp $
190 */