1
2
3
4 package net.sourceforge.pmd.util.viewer.model;
5
6
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.Enumeration;
10 import java.util.List;
11
12 import javax.swing.tree.TreeNode;
13
14 import net.sourceforge.pmd.lang.ast.Node;
15
16
17
18
19
20
21
22
23
24 public class SimpleNodeTreeNodeAdapter implements TreeNode {
25
26 private Node node;
27 private List<TreeNode> children;
28 private SimpleNodeTreeNodeAdapter parent;
29
30
31
32
33
34
35 public SimpleNodeTreeNodeAdapter(SimpleNodeTreeNodeAdapter parent, Node node) {
36 this.parent = parent;
37 this.node = node;
38 }
39
40
41
42
43
44
45 public Node getSimpleNode() {
46 return node;
47 }
48
49
50
51
52
53 public TreeNode getChildAt(int childIndex) {
54 checkChildren();
55 return children.get(childIndex);
56 }
57
58
59
60
61
62 public int getChildCount() {
63 checkChildren();
64 return children.size();
65 }
66
67
68
69
70
71 public TreeNode getParent() {
72 return parent;
73 }
74
75
76
77
78 public int getIndex(TreeNode node) {
79 checkChildren();
80 return children.indexOf(node);
81 }
82
83
84
85
86
87 public boolean getAllowsChildren() {
88 return true;
89 }
90
91
92
93
94
95
96 public boolean isLeaf() {
97 checkChildren();
98 return children.isEmpty();
99 }
100
101
102
103
104
105
106 public Enumeration<TreeNode> children() {
107 return Collections.enumeration(children);
108 }
109
110
111
112
113
114 private void checkChildren() {
115 if (children == null) {
116 children = new ArrayList<TreeNode>(node.jjtGetNumChildren());
117 for (int i = 0; i < node.jjtGetNumChildren(); i++) {
118 children.add(new SimpleNodeTreeNodeAdapter(this, node.jjtGetChild(i)));
119 }
120 }
121 }
122
123
124
125
126 public String toString() {
127 return node.toString();
128 }
129 }
130