1 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 31 public class BranchTreeNode extends LeafTreeNode { 32 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 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 89 94 protected List getChildList() { 95 if (children == null) { 99 children = createChildList(); 100 } 101 102 return children; 103 } 104 105 110 protected List createChildList() { 111 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 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 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 197 | Popular Tags |