KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joshy > html > swing > DOMInspector


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 JavaDoc;
8 import java.awt.Dimension JavaDoc;
9 import java.awt.BorderLayout JavaDoc;
10 import org.w3c.dom.*;
11 import org.joshy.u;
12 import java.awt.Component JavaDoc;
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 JavaDoc());
27         setDocument(doc);
28         
29         // tree stuff
30
TreeModel model = new DOMTreeModel(doc);
31         this.tree = new JTree();
32         tree.setModel(model);
33         //tree.setPreferredSize(new Dimension(300,300));
34
//ree.setSize(new Dimension(300,300));
35
tree.setCellRenderer(new DOMTreeCellRenderer());
36
37         this.scroll = new JScrollPane(tree);
38         //scroll.setPreferredSize(new Dimension(300,300));
39
//scroll.setHorizontalScrollBarPolicy(scroll.HORIZONTAL_SCROLLBAR_ALWAYS);
40
//scroll.setVerticalScrollBarPolicy(scroll.VERTICAL_SCROLLBAR_ALWAYS);
41
add(scroll,"Center");
42
43         //this.add(tree,"Center");
44
this.add(new JButton("close"),"South");
45         this.setPreferredSize(new Dimension JavaDoc(300,300));
46     }
47     public void paintComponent(Graphics JavaDoc 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     //Adds a listener for the TreeModelEvent posted after the tree changes.
61
public void addTreeModelListener(TreeModelListener l) {
62         this.listeners.add(l);
63     }
64
65     //Returns the child of parent at index index in the parent's child array.
66
public Object JavaDoc getChild(Object JavaDoc parent, int index) {
67         Node node = (Node)parent;
68         return node.getChildNodes().item(index);
69     }
70
71     //Returns the number of children of parent.
72
public int getChildCount(Object JavaDoc parent) {
73         Node node = (Node)parent;
74         return node.getChildNodes().getLength();
75     }
76
77     //Returns the index of child in parent.
78
public int getIndexOfChild(Object JavaDoc parent, Object JavaDoc 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     //Returns the root of the tree.
89
public Object JavaDoc getRoot() {
90         return doc;
91     }
92
93     //Returns true if node is a leaf.
94
public boolean isLeaf(Object JavaDoc nd) {
95         Node node = (Node)nd;
96         //u.p("checking leaf: " + node.getNodeName());
97
//u.p("is: " + node.hasChildNodes());
98
return !node.hasChildNodes();
99     }
100
101     //Removes a listener previously added with addTreeModelListener.
102
public void removeTreeModelListener(TreeModelListener l) {
103         this.listeners.remove(l);
104     }
105
106     //Messaged when the user has altered the value for the item identified by path to newValue.
107
public void valueForPathChanged(TreePath path, Object JavaDoc newValue) {
108         // no-op
109
}
110 }
111
112 class DOMTreeCellRenderer extends DefaultTreeCellRenderer {
113     public Component JavaDoc getTreeCellRendererComponent(JTree tree, Object JavaDoc 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