|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
BranchTreeNode.java | 0% | 0% | 0% | 0% |
|
1 | /* | |
2 | * Copyright 2001-2005 (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 | ||
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 | 0 | public BranchTreeNode() { |
36 | } | |
37 | ||
38 | 0 | public BranchTreeNode(Branch xmlNode) { |
39 | 0 | super(xmlNode); |
40 | } | |
41 | ||
42 | 0 | public BranchTreeNode(TreeNode parent, Branch xmlNode) { |
43 | 0 | super(parent, xmlNode); |
44 | } | |
45 | ||
46 | // TreeNode methods | |
47 | // ------------------------------------------------------------------------- | |
48 | 0 | public Enumeration children() { |
49 | 0 | return new Enumeration() { |
50 | private int index = -1; | |
51 | ||
52 | 0 | public boolean hasMoreElements() { |
53 | 0 | return (index + 1) < getChildCount(); |
54 | } | |
55 | ||
56 | 0 | public Object nextElement() { |
57 | 0 | return getChildAt(++index); |
58 | } | |
59 | }; | |
60 | } | |
61 | ||
62 | 0 | public boolean getAllowsChildren() { |
63 | 0 | return true; |
64 | } | |
65 | ||
66 | 0 | public TreeNode getChildAt(int childIndex) { |
67 | 0 | return (TreeNode) getChildList().get(childIndex); |
68 | } | |
69 | ||
70 | 0 | public int getChildCount() { |
71 | 0 | return getChildList().size(); |
72 | } | |
73 | ||
74 | 0 | public int getIndex(TreeNode node) { |
75 | 0 | return getChildList().indexOf(node); |
76 | } | |
77 | ||
78 | 0 | public boolean isLeaf() { |
79 | 0 | return getXmlBranch().nodeCount() <= 0; |
80 | } | |
81 | ||
82 | 0 | public String toString() { |
83 | 0 | return xmlNode.getName(); |
84 | } | |
85 | ||
86 | // Implementation methods | |
87 | // ------------------------------------------------------------------------- | |
88 | ||
89 | /** | |
90 | * Uses Lazy Initialization pattern to create a List of children | |
91 | * | |
92 | * @return DOCUMENT ME! | |
93 | */ | |
94 | 0 | protected List getChildList() { |
95 | // for now lets just create the children once, the first time they | |
96 | // are asked for. | |
97 | // XXXX - we may wish to detect inconsistencies here.... | |
98 | 0 | if (children == null) { |
99 | 0 | children = createChildList(); |
100 | } | |
101 | ||
102 | 0 | return children; |
103 | } | |
104 | ||
105 | /** | |
106 | * Factory method to create List of children TreeNodes | |
107 | * | |
108 | * @return DOCUMENT ME! | |
109 | */ | |
110 | 0 | protected List createChildList() { |
111 | // add attributes and content as children? | |
112 | 0 | Branch branch = getXmlBranch(); |
113 | 0 | int size = branch.nodeCount(); |
114 | 0 | List childList = new ArrayList(size); |
115 | ||
116 | 0 | for (int i = 0; i < size; i++) { |
117 | 0 | Node node = branch.node(i); |
118 | ||
119 | // ignore whitespace text nodes | |
120 | 0 | if (node instanceof CharacterData) { |
121 | 0 | String text = node.getText(); |
122 | ||
123 | 0 | if (text == null) { |
124 | 0 | continue; |
125 | } | |
126 | ||
127 | 0 | text = text.trim(); |
128 | ||
129 | 0 | if (text.length() <= 0) { |
130 | 0 | continue; |
131 | } | |
132 | } | |
133 | ||
134 | 0 | childList.add(createChildTreeNode(node)); |
135 | } | |
136 | ||
137 | 0 | 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 | 0 | protected TreeNode createChildTreeNode(Node xmlNode) { |
149 | 0 | if (xmlNode instanceof Branch) { |
150 | 0 | return new BranchTreeNode(this, (Branch) xmlNode); |
151 | } else { | |
152 | 0 | return new LeafTreeNode(this, xmlNode); |
153 | } | |
154 | } | |
155 | ||
156 | 0 | protected Branch getXmlBranch() { |
157 | 0 | return (Branch) xmlNode; |
158 | } | |
159 | } | |
160 | ||
161 | /* | |
162 | * Redistribution and use of this software and associated documentation | |
163 | * ("Software"), with or without modification, are permitted provided that the | |
164 | * following conditions are met: | |
165 | * | |
166 | * 1. Redistributions of source code must retain copyright statements and | |
167 | * notices. Redistributions must also contain a copy of this document. | |
168 | * | |
169 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
170 | * this list of conditions and the following disclaimer in the documentation | |
171 | * and/or other materials provided with the distribution. | |
172 | * | |
173 | * 3. The name "DOM4J" must not be used to endorse or promote products derived | |
174 | * from this Software without prior written permission of MetaStuff, Ltd. For | |
175 | * written permission, please contact dom4j-info@metastuff.com. | |
176 | * | |
177 | * 4. Products derived from this Software may not be called "DOM4J" nor may | |
178 | * "DOM4J" appear in their names without prior written permission of MetaStuff, | |
179 | * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd. | |
180 | * | |
181 | * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org | |
182 | * | |
183 | * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND | |
184 | * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
185 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
186 | * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE | |
187 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
188 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
189 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
190 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
191 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
192 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
193 | * POSSIBILITY OF SUCH DAMAGE. | |
194 | * | |
195 | * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved. | |
196 | */ |
|