source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/util/DOMTree.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 3.7 KB
Line 
1package org.greenstone.gatherer.util;
2
3import java.util.*;
4import javax.swing.*;
5import javax.swing.tree.*;
6import org.w3c.dom.*;
7
8public class DOMTree
9 extends JTree {
10
11 public DOMTree(Document document) {
12 super();
13 setDocument(document);
14 }
15
16 public void setDocument(Document document) {
17 setModel(new DefaultTreeModel(new DOMTreeNode(document.getDocumentElement(), null)));
18 }
19
20 private class DOMTreeNode
21 implements TreeNode {
22
23 private Node node = null;
24 private NodeList children = null;
25 private String text = null;
26 private TreeNode parent = null;
27 private TreeNode self = null;
28
29 DOMTreeNode(Node node, TreeNode parent) {
30 this.node = node;
31 this.parent = parent;
32 this.self = this;
33 }
34
35 /** Returns the children of the receiver as an Enumeration. */
36 public Enumeration children() {
37 if(children == null) {
38 children = node.getChildNodes();
39 }
40 return new NodeListEnumeration();
41 }
42
43 /** Returns true if the receiver allows children. */
44 public boolean getAllowsChildren() {
45 return true;
46 }
47
48 /** Returns the child TreeNode at index childIndex. */
49 public TreeNode getChildAt(int childIndex) {
50 if(children == null) {
51 children = node.getChildNodes();
52 }
53 return new DOMTreeNode(children.item(childIndex), this);
54 }
55
56 /** Returns the number of children TreeNodes the receiver contains. */
57 public int getChildCount() {
58 if(children == null) {
59 children = node.getChildNodes();
60 }
61 return children.getLength();
62 }
63
64 /** Returns the index of node in the receivers children. */
65 public int getIndex(TreeNode find_node) {
66 if(children == null) {
67 children = node.getChildNodes();
68 }
69 Node target_node = ((DOMTreeNode)find_node).getNode();
70 int children_count = children.getLength();
71 for(int i = 0; i < children_count; i++) {
72 Node test_node = children.item(i);
73 if(target_node == test_node) {
74 test_node = null;
75 target_node = null;
76 return i;
77 }
78 test_node = null;
79 }
80 target_node = null;
81 return -1;
82 }
83
84 public Node getNode() {
85 return node;
86 }
87
88 /** Returns the parent TreeNode of the receiver. */
89 public TreeNode getParent() {
90 return parent;
91 }
92
93 /** Returns true if the receiver is a leaf. */
94 public boolean isLeaf() {
95 return !node.hasChildNodes();
96 }
97
98 public String toString() {
99 if(text == null) {
100 StringBuffer temp = new StringBuffer(node.getNodeName());
101 String value = node.getNodeValue();
102 if(value != null && value.length() != 0) {
103 temp.append("=\"");
104 temp.append(value);
105 temp.append("\"");
106 }
107 value = null;
108 if(node instanceof Element) {
109 Element element = (Element) node;
110 temp.append(": ");
111 NamedNodeMap attributes = element.getAttributes();
112 int attribute_count = attributes.getLength();
113 for(int i = 0; i < attribute_count; i++) {
114 Node attribute = attributes.item(i);
115 temp.append(attribute.getNodeName());
116 temp.append("=\"");
117 temp.append(attribute.getNodeValue());
118 if(i < attribute_count - 1) {
119 temp.append("\" ");
120 }
121 else {
122 temp.append("\"");
123 }
124 }
125 attributes = null;
126 element = null;
127 }
128 text = temp.toString();
129 temp = null;
130 }
131 return text;
132 }
133
134 private class NodeListEnumeration
135 implements Enumeration {
136 private int index = 0;
137 /** Tests if this enumeration contains more elements. */
138 public boolean hasMoreElements() {
139 return index < children.getLength();
140 }
141 /** Returns the next element of this enumeration if this enumeration object has at least one more element to provide. */
142 public Object nextElement() {
143 int old_index = index;
144 index = index + 1;
145 return new DOMTreeNode(children.item(old_index), self);
146 }
147 }
148 }
149}
Note: See TracBrowser for help on using the repository browser.