1 57 package ui; 58 59 import java.io.Serializable ; 60 import java.util.Hashtable ; 61 62 import javax.swing.JTree ; 63 import javax.swing.tree.DefaultMutableTreeNode ; 64 import javax.swing.tree.DefaultTreeModel ; 65 import javax.swing.tree.MutableTreeNode ; 66 67 import org.w3c.dom.Document ; 68 import org.w3c.dom.NamedNodeMap ; 69 import org.w3c.dom.Node ; 70 import org.w3c.dom.NodeList ; 71 72 78 public class DOMTree 79 extends JTree 80 { 81 82 86 87 public DOMTree() { 88 this(null); 89 } 90 91 92 public DOMTree(Document document) { 93 super(new Model ()); 94 95 setRootVisible(false); 97 98 setDocument(document); 100 101 } 103 107 108 public void setDocument(Document document) { 109 ((Model )getModel()).setDocument(document); 110 expandRow(0); 111 } 112 113 114 public Document getDocument() { 115 return ((Model )getModel()).getDocument(); 116 } 117 118 119 public Node getNode(Object treeNode) { 120 return ((Model )getModel()).getNode(treeNode); 121 } 122 123 127 133 static class Model 134 extends DefaultTreeModel 135 implements Serializable 136 { 137 138 142 143 private Document document; 144 145 private Hashtable nodeMap = new Hashtable (); 146 147 148 152 153 public Model() { 154 this(null); 155 } 156 157 158 public Model(Document document) { 159 super(new DefaultMutableTreeNode ()); 160 setDocument(document); 161 } 162 163 167 168 public synchronized void setDocument(Document document) { 169 170 this.document = document; 172 173 ((DefaultMutableTreeNode )getRoot()).removeAllChildren(); 175 nodeMap.clear(); 176 buildTree(); 177 fireTreeStructureChanged(this, new Object [] { getRoot() }, new int[0], new Object [0]); 178 179 } 181 182 public Document getDocument() { 183 return document; 184 } 185 186 187 public Node getNode(Object treeNode) { 188 return (Node )nodeMap.get(treeNode); 189 } 190 191 195 196 private void buildTree() { 197 198 if (document == null) { return; } 200 201 NodeList nodes = document.getChildNodes(); 203 int len = (nodes != null) ? nodes.getLength() : 0; 204 MutableTreeNode root = (MutableTreeNode )getRoot(); 205 for (int i = 0; i < len; i++) { 206 Node node = nodes.item(i); 207 switch (node.getNodeType()) { 208 case Node.DOCUMENT_NODE: { 209 root = insertDocumentNode(node, root); 210 break; 211 } 212 213 case Node.ELEMENT_NODE: { 214 insertElementNode(node, root); 215 break; 216 } 217 218 default: 220 } 222 } 224 } 226 227 private MutableTreeNode insertNode(String what, MutableTreeNode where) { 228 229 MutableTreeNode node = new DefaultMutableTreeNode (what); 230 insertNodeInto(node, where, where.getChildCount()); 231 return node; 232 233 } 235 236 private MutableTreeNode insertDocumentNode(Node what, MutableTreeNode where) { 237 MutableTreeNode treeNode = insertNode("<"+what.getNodeName()+'>', where); 238 nodeMap.put(treeNode, what); 239 return treeNode; 240 } 241 242 243 private MutableTreeNode insertElementNode(Node what, MutableTreeNode where) { 244 245 StringBuffer name = new StringBuffer (); 247 name.append('<'); 248 name.append(what.getNodeName()); 249 NamedNodeMap attrs = what.getAttributes(); 250 int attrCount = (attrs != null) ? attrs.getLength() : 0; 251 for (int i = 0; i < attrCount; i++) { 252 Node attr = attrs.item(i); 253 name.append(' '); 254 name.append(attr.getNodeName()); 255 name.append("=\""); 256 name.append(attr.getNodeValue()); 257 name.append('"'); 258 } 259 name.append('>'); 260 261 263 MutableTreeNode element = insertNode(name.toString(), where); 264 nodeMap.put(element, what); 265 266 NodeList children = what.getChildNodes(); 268 int len = (children != null) ? children.getLength() : 0; 269 for (int i = 0; i < len; i++) { 270 Node node = children.item(i); 271 switch (node.getNodeType()) { 272 case Node.CDATA_SECTION_NODE: { 273 insertCDataSectionNode( node, element ); break; 275 } 276 case Node.TEXT_NODE: { 277 insertTextNode(node, element); 278 break; 279 } 280 case Node.ELEMENT_NODE: { 281 insertElementNode(node, element); 282 break; 283 } 284 } 285 } 286 287 return element; 288 289 } 291 292 private MutableTreeNode insertTextNode(Node what, MutableTreeNode where) { 293 String value = what.getNodeValue().trim(); 294 if (value.length() > 0) { 295 MutableTreeNode treeNode = insertNode(value, where); 296 nodeMap.put(treeNode, what); 297 return treeNode; 298 } 299 return null; 300 } 301 302 303 304 private MutableTreeNode insertCDataSectionNode(Node what, MutableTreeNode where) { 305 StringBuffer CSectionBfr = new StringBuffer (); 306 CSectionBfr.append( what.getNodeValue() ); 308 if (CSectionBfr.length() > 0) { 310 MutableTreeNode treeNode = insertNode(CSectionBfr.toString(), where); 311 nodeMap.put(treeNode, what); 312 return treeNode; 313 } 314 return null; 315 } 316 317 318 } 320 321 322 } | Popular Tags |