001: ////////////////////////////////////////////////////////////////////////////////
002: // checkstyle: Checks Java source code for adherence to a set of rules.
003: // Copyright (C) 2001-2002 Oliver Burn
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU Lesser General Public
007: // License as published by the Free Software Foundation; either
008: // version 2.1 of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: // Lesser General Public License for more details.
014: //
015: // You should have received a copy of the GNU Lesser General Public
016: // License along with this library; if not, write to the Free Software
017: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: ////////////////////////////////////////////////////////////////////////////////
019:
020: package com.puppycrawl.tools.checkstyle.gui;
021:
022: import antlr.collections.AST;
023: import antlr.ASTFactory;
024: import com.puppycrawl.tools.checkstyle.api.DetailAST;
025: import com.puppycrawl.tools.checkstyle.api.TokenTypes;
026:
027: /**
028: * The model that backs the parse tree in the GUI.
029: *
030: * @author Lars Kühne
031: */
032: public class ParseTreeModel extends AbstractTreeTableModel {
033: private static final String[] COLUMN_NAMES = new String[] { "Tree",
034: "Type", "Line", "Column", "Text" };
035:
036: public ParseTreeModel(DetailAST parseTree) {
037: super (createArtificialTreeRoot());
038: setParseTree(parseTree);
039: }
040:
041: private static DetailAST createArtificialTreeRoot() {
042: final ASTFactory factory = new ASTFactory();
043: factory.setASTNodeClass(DetailAST.class.getName());
044: // TODO: Need to resolve if need a fake root node....
045: return (DetailAST) factory.create(TokenTypes.EOF, "ROOT");
046: }
047:
048: void setParseTree(DetailAST parseTree) {
049: final DetailAST root = (DetailAST) getRoot();
050: root.setFirstChild(parseTree);
051: final Object[] path = { root };
052: // no need to setup remaining info, as the call results in a
053: // table structure changed event anyway - we just pass nulls
054: fireTreeStructureChanged(this , path, null, null);
055: }
056:
057: public int getColumnCount() {
058: return COLUMN_NAMES.length;
059: }
060:
061: public String getColumnName(int column) {
062: return COLUMN_NAMES[column];
063: }
064:
065: public Class getColumnClass(int column) {
066: switch (column) {
067: case 0:
068: return TreeTableModel.class;
069: case 1:
070: return String.class;
071: case 2:
072: return Integer.class;
073: case 3:
074: return Integer.class;
075: case 4:
076: return String.class;
077: }
078: return Object.class;
079: }
080:
081: public Object getValueAt(Object node, int column) {
082: final DetailAST ast = (DetailAST) node;
083: switch (column) {
084: case 0:
085: return null;
086: case 1:
087: return TokenTypes.getTokenName(ast.getType());
088: case 2:
089: return new Integer(ast.getLineNo());
090: case 3:
091: return new Integer(ast.getColumnNo());
092: case 4:
093: return ast.getText();
094: }
095: return null;
096: }
097:
098: public void setValueAt(Object aValue, Object node, int column) {
099: }
100:
101: public Object getChild(Object parent, int index) {
102: final DetailAST ast = (DetailAST) parent;
103: int i = 0;
104: AST child = ast.getFirstChild();
105: while (i < index) {
106: child = child.getNextSibling();
107: i++;
108: }
109: return child;
110: }
111:
112: public int getChildCount(Object parent) {
113: final DetailAST ast = (DetailAST) parent;
114: return ast.getChildCount();
115: }
116:
117: }
|