1
2
3
4
5
6
7
8 package org.dom4j.swing;
9
10 import java.util.ArrayList;
11 import java.util.Enumeration;
12 import java.util.List;
13
14 import javax.swing.tree.TreeNode;
15
16 import org.dom4j.Branch;
17 import org.dom4j.CharacterData;
18 import org.dom4j.Node;
19
20 /***
21 * <p>
22 * <code>BranchTreeNode</code> implements the Swing TreeNode interface to bind
23 * dom4j XML Branch nodes (i.e. Document and Element nodes) to a Swing
24 * TreeModel.
25 * </p>
26 *
27 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
28 * @author Jakob Jenkov
29 * @version $Revision: 1.10 $
30 */
31 public class BranchTreeNode extends LeafTreeNode {
32 /*** Stores the child tree nodes */
33 protected List children;
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 public Enumeration children() {
49 return new Enumeration() {
50 private int index = -1;
51
52 public boolean hasMoreElements() {
53 return (index + 1) < getChildCount();
54 }
55
56 public Object nextElement() {
57 return getChildAt(++index);
58 }
59 };
60 }
61
62 public boolean getAllowsChildren() {
63 return true;
64 }
65
66 public TreeNode getChildAt(int childIndex) {
67 return (TreeNode) getChildList().get(childIndex);
68 }
69
70 public int getChildCount() {
71 return getChildList().size();
72 }
73
74 public int getIndex(TreeNode node) {
75 return getChildList().indexOf(node);
76 }
77
78 public boolean isLeaf() {
79 return getXmlBranch().nodeCount() <= 0;
80 }
81
82 public String toString() {
83 return xmlNode.getName();
84 }
85
86
87
88
89 /***
90 * Uses Lazy Initialization pattern to create a List of children
91 *
92 * @return DOCUMENT ME!
93 */
94 protected List getChildList() {
95
96
97
98 if (children == null) {
99 children = createChildList();
100 }
101
102 return children;
103 }
104
105 /***
106 * Factory method to create List of children TreeNodes
107 *
108 * @return DOCUMENT ME!
109 */
110 protected List createChildList() {
111
112 Branch branch = getXmlBranch();
113 int size = branch.nodeCount();
114 List childList = new ArrayList(size);
115
116 for (int i = 0; i < size; i++) {
117 Node node = branch.node(i);
118
119
120 if (node instanceof CharacterData) {
121 String text = node.getText();
122
123 if (text == null) {
124 continue;
125 }
126
127 text = text.trim();
128
129 if (text.length() <= 0) {
130 continue;
131 }
132 }
133
134 childList.add(createChildTreeNode(node));
135 }
136
137 return childList;
138 }
139
140 /***
141 * Factory method to create child tree nodes for a given XML node type
142 *
143 * @param xmlNode
144 * DOCUMENT ME!
145 *
146 * @return DOCUMENT ME!
147 */
148 protected TreeNode createChildTreeNode(Node xmlNode) {
149 if (xmlNode instanceof Branch) {
150 return new BranchTreeNode(this, (Branch) xmlNode);
151 } else {
152 return new LeafTreeNode(this, xmlNode);
153 }
154 }
155
156 protected Branch getXmlBranch() {
157 return (Branch) xmlNode;
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
191
192
193
194
195
196