1
2
3
4
5
6
7
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
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
89
90
91 /*** Uses Lazy Initialization pattern to create a List of children */
92 protected List getChildList() {
93
94
95
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
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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190