1 package org.joshy.html.swing; 2 3 import javax.swing.event.*; 4 import java.util.*; 5 import javax.swing.tree.*; 6 import javax.swing.*; 7 import java.awt.Graphics ; 8 import java.awt.Dimension ; 9 import java.awt.BorderLayout ; 10 import org.w3c.dom.*; 11 import org.joshy.u; 12 import java.awt.Component ; 13 14 public class DOMInspector extends JPanel { 15 Document doc; 16 JTree tree; 17 JScrollPane scroll; 18 public void setDocument(Document doc) { 19 this.doc = doc; 20 } 21 22 23 public DOMInspector(Document doc) { 24 super(); 25 26 this.setLayout(new java.awt.BorderLayout ()); 27 setDocument(doc); 28 29 TreeModel model = new DOMTreeModel(doc); 31 this.tree = new JTree(); 32 tree.setModel(model); 33 tree.setCellRenderer(new DOMTreeCellRenderer()); 36 37 this.scroll = new JScrollPane(tree); 38 add(scroll,"Center"); 42 43 this.add(new JButton("close"),"South"); 45 this.setPreferredSize(new Dimension (300,300)); 46 } 47 public void paintComponent(Graphics g) { 48 super.paintComponent(g); 49 g.drawLine(0,0,100,100); 50 } 51 } 52 53 class DOMTreeModel implements TreeModel { 54 Document doc; 55 public DOMTreeModel(Document doc) { 56 this.doc = doc; 57 } 58 List listeners = new ArrayList(); 59 60 public void addTreeModelListener(TreeModelListener l) { 62 this.listeners.add(l); 63 } 64 65 public Object getChild(Object parent, int index) { 67 Node node = (Node)parent; 68 return node.getChildNodes().item(index); 69 } 70 71 public int getChildCount(Object parent) { 73 Node node = (Node)parent; 74 return node.getChildNodes().getLength(); 75 } 76 77 public int getIndexOfChild(Object parent, Object child) { 79 Node node = (Node)parent; 80 for(int i=0; i<node.getChildNodes().getLength(); i++) { 81 if(child == node.getChildNodes().item(i)) { 82 return i; 83 } 84 } 85 return -1; 86 } 87 88 public Object getRoot() { 90 return doc; 91 } 92 93 public boolean isLeaf(Object nd) { 95 Node node = (Node)nd; 96 return !node.hasChildNodes(); 99 } 100 101 public void removeTreeModelListener(TreeModelListener l) { 103 this.listeners.remove(l); 104 } 105 106 public void valueForPathChanged(TreePath path, Object newValue) { 108 } 110 } 111 112 class DOMTreeCellRenderer extends DefaultTreeCellRenderer { 113 public Component getTreeCellRendererComponent(JTree tree, Object value, 114 boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { 115 Node node = (Node)value; 116 if(node.getNodeType() == node.ELEMENT_NODE) { 117 value = "<"+node.getNodeName()+">"; 118 } 119 if(node.getNodeType() == node.TEXT_NODE) { 120 value = "\""+ node.getNodeValue() + "\""; 121 } 122 if(node.getNodeType() == node.COMMENT_NODE) { 123 value = "<!-- " + node.getNodeValue() + " -->"; 124 } 125 return super.getTreeCellRendererComponent(tree,value,selected,expanded,leaf,row,hasFocus); 126 } 127 } 128 | Popular Tags |