1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.gui;
21
22 import antlr.ASTFactory;
23 import antlr.collections.AST;
24 import com.puppycrawl.tools.checkstyle.api.DetailAST;
25 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26
27
28
29
30
31
32 public class ParseTreeModel extends AbstractTreeTableModel
33 {
34 private static final String[] COLUMN_NAMES = new String[]{
35 "Tree", "Type", "Line", "Column", "Text"
36 };
37
38 public ParseTreeModel(DetailAST parseTree)
39 {
40 super(createArtificialTreeRoot());
41 setParseTree(parseTree);
42 }
43
44 private static DetailAST createArtificialTreeRoot()
45 {
46 final ASTFactory factory = new ASTFactory();
47 factory.setASTNodeClass(DetailAST.class.getName());
48
49 return (DetailAST) factory.create(TokenTypes.EOF, "ROOT");
50 }
51
52 void setParseTree(DetailAST parseTree)
53 {
54 final DetailAST root = (DetailAST) getRoot();
55 root.setFirstChild(parseTree);
56 final Object[] path = {root};
57
58
59 fireTreeStructureChanged(this, path, null, null);
60 }
61
62 @Override
63 public int getColumnCount()
64 {
65 return COLUMN_NAMES.length;
66 }
67
68 @Override
69 public String getColumnName(int column)
70 {
71 return COLUMN_NAMES[column];
72 }
73
74 @Override
75 public Class<?> getColumnClass(int column)
76 {
77 switch (column) {
78 case 0:
79 return TreeTableModel.class;
80 case 1:
81 return String.class;
82 case 2:
83 return Integer.class;
84 case 3:
85 return Integer.class;
86 case 4:
87 return String.class;
88 }
89 return Object.class;
90 }
91
92 @Override
93 public Object getValueAt(Object node, int column)
94 {
95 final DetailAST ast = (DetailAST) node;
96 switch (column) {
97 case 0:
98 return null;
99 case 1:
100 return TokenTypes.getTokenName(ast.getType());
101 case 2:
102 return ast.getLineNo();
103 case 3:
104 return ast.getColumnNo();
105 case 4:
106 return ast.getText();
107 }
108 return null;
109 }
110
111 @Override
112 public void setValueAt(Object aValue, Object node, int column)
113 {
114 }
115
116 @Override
117 public Object getChild(Object parent, int index)
118 {
119 final DetailAST ast = (DetailAST) parent;
120 int i = 0;
121 AST child = ast.getFirstChild();
122 while (i < index) {
123 child = child.getNextSibling();
124 i++;
125 }
126 return child;
127 }
128
129 @Override
130 public int getChildCount(Object parent)
131 {
132 final DetailAST ast = (DetailAST) parent;
133 return ast.getChildCount();
134 }
135
136 }