KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > gui > ParseTreeModel


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.7 2006/07/07 03:44:16 oburn Exp $
32  */

33 public class ParseTreeModel extends AbstractTreeTableModel
34 {
35     private static final String JavaDoc[] COLUMN_NAMES = new String JavaDoc[]{
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         final ASTFactory factory = new ASTFactory();
48         factory.setASTNodeClass(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         final DetailAST root = (DetailAST) getRoot();
56         root.setFirstChild(parseTree);
57         final Object JavaDoc[] path = {root};
58         // no need to setup remaining info, as the call results in a
59
// table structure changed event anyway - we just pass nulls
60
fireTreeStructureChanged(this, path, null, null);
61     }
62
63     public int getColumnCount()
64     {
65         return COLUMN_NAMES.length;
66     }
67
68     public String JavaDoc getColumnName(int column)
69     {
70         return COLUMN_NAMES[column];
71     }
72
73     public Class JavaDoc getColumnClass(int column)
74     {
75         switch (column) {
76             case 0:
77                 return TreeTableModel.class;
78             case 1:
79                 return String JavaDoc.class;
80             case 2:
81                 return Integer JavaDoc.class;
82             case 3:
83                 return Integer JavaDoc.class;
84             case 4:
85                 return String JavaDoc.class;
86         }
87         return Object JavaDoc.class;
88     }
89
90     public Object JavaDoc getValueAt(Object JavaDoc node, int column)
91     {
92         final DetailAST ast = (DetailAST) node;
93         switch (column) {
94             case 0:
95                 return null;
96             case 1:
97                 return TokenTypes.getTokenName(ast.getType());
98             case 2:
99                 return new Integer JavaDoc(ast.getLineNo());
100             case 3:
101                 return new Integer JavaDoc(ast.getColumnNo());
102             case 4:
103                 return ast.getText();
104         }
105         return null;
106     }
107
108     public void setValueAt(Object JavaDoc aValue, Object JavaDoc node, int column)
109     {
110     }
111
112     public Object JavaDoc getChild(Object JavaDoc parent, int index)
113     {
114         final DetailAST ast = (DetailAST) parent;
115         int i = 0;
116         AST child = ast.getFirstChild();
117         while (i < index) {
118             child = child.getNextSibling();
119             i++;
120         }
121         return child;
122     }
123
124     public int getChildCount(Object JavaDoc parent)
125     {
126         final DetailAST ast = (DetailAST) parent;
127         return ast.getChildCount();
128     }
129
130 }
131
Popular Tags