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