Source code: com/puppycrawl/tools/checkstyle/gui/ParseTreeModel.java
1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2002 Oliver Burn
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.gui;
21
22 import antlr.collections.AST;
23 import antlr.ASTFactory;
24 import com.puppycrawl.tools.checkstyle.api.DetailAST;
25 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26
27 /**
28 * The model that backs the parse tree in the GUI.
29 *
30 * @author Lars Kühne
31 * @version $Id: ParseTreeModel.java,v 1.3 2003/01/11 13:09:04 lkuehne Exp $
32 */
33 public class ParseTreeModel extends AbstractTreeTableModel
34 {
35 private static final String[] COLUMN_NAMES = new String[]{
36 "Tree", "Type", "Line", "Column", "Text"
37 };
38
39 public ParseTreeModel(DetailAST parseTree)
40 {
41 super(createArtificialTreeRoot());
42 setParseTree(parseTree);
43 }
44
45 private static DetailAST createArtificialTreeRoot()
46 {
47 ASTFactory factory = new ASTFactory();
48 factory.setASTNodeType(DetailAST.class.getName());
49 // TODO: Need to resolve if need a fake root node....
50 return (DetailAST) factory.create(TokenTypes.EOF, "ROOT");
51 }
52
53 void setParseTree(DetailAST parseTree)
54 {
55 DetailAST root = (DetailAST) getRoot();
56 root.setFirstChild(parseTree);
57 Object[] path = {root};
58
59 // no need to setup remaining info, as the call results in a
60 // table structure changed event anyway - we just pass nulls
61 fireTreeStructureChanged(this, path, null, null);
62 }
63
64 public int getColumnCount()
65 {
66 return COLUMN_NAMES.length;
67 }
68
69 public String getColumnName(int column)
70 {
71 return COLUMN_NAMES[column];
72 }
73
74 public Class getColumnClass(int column)
75 {
76 switch (column) {
77 case 0:
78 return TreeTableModel.class;
79 case 1:
80 return String.class;
81 case 2:
82 return Integer.class;
83 case 3:
84 return Integer.class;
85 case 4:
86 return String.class;
87 }
88 return Object.class;
89 }
90
91 public Object getValueAt(Object node, int column)
92 {
93 DetailAST ast = (DetailAST) node;
94 switch (column) {
95 case 0:
96 return null;
97 case 1:
98 return TokenTypes.getTokenName(ast.getType());
99 case 2:
100 return new Integer(ast.getLineNo());
101 case 3:
102 return new Integer(ast.getColumnNo());
103 case 4:
104 return ast.getText();
105 }
106 return null;
107 }
108
109 public void setValueAt(Object aValue, Object node, int column)
110 {
111 }
112
113 public Object getChild(Object parent, int index)
114 {
115 DetailAST ast = (DetailAST) parent;
116 int i = 0;
117 AST child = ast.getFirstChild();
118 while (i < index) {
119 child = child.getNextSibling();
120 i++;
121 }
122 return child;
123 }
124
125 public int getChildCount(Object parent)
126 {
127 DetailAST ast = (DetailAST) parent;
128 return ast.getChildCount();
129 }
130
131 }