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
020package com.puppycrawl.tools.checkstyle.gui;
021
022import antlr.ASTFactory;
023import antlr.collections.AST;
024import com.puppycrawl.tools.checkstyle.api.DetailAST;
025import 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 */
032public class ParseTreeModel extends AbstractTreeTableModel
033{
034    private static final String[] COLUMN_NAMES = new String[]{
035        "Tree", "Type", "Line", "Column", "Text"
036    };
037
038    public ParseTreeModel(DetailAST parseTree)
039    {
040        super(createArtificialTreeRoot());
041        setParseTree(parseTree);
042    }
043
044    private static DetailAST createArtificialTreeRoot()
045    {
046        final ASTFactory factory = new ASTFactory();
047        factory.setASTNodeClass(DetailAST.class.getName());
048        // TODO: Need to resolve if need a fake root node....
049        return (DetailAST) factory.create(TokenTypes.EOF, "ROOT");
050    }
051
052    void setParseTree(DetailAST parseTree)
053    {
054        final DetailAST root = (DetailAST) getRoot();
055        root.setFirstChild(parseTree);
056        final Object[] path = {root};
057        // no need to setup remaining info, as the call results in a
058        // table structure changed event anyway - we just pass nulls
059        fireTreeStructureChanged(this, path, null, null);
060    }
061
062    public int getColumnCount()
063    {
064        return COLUMN_NAMES.length;
065    }
066
067    public String getColumnName(int column)
068    {
069        return COLUMN_NAMES[column];
070    }
071
072    @Override
073    public Class<?> getColumnClass(int column)
074    {
075        switch (column) {
076            case 0:
077                return TreeTableModel.class;
078            case 1:
079                return String.class;
080            case 2:
081                return Integer.class;
082            case 3:
083                return Integer.class;
084            case 4:
085                return String.class;
086        }
087        return Object.class;
088    }
089
090    public Object getValueAt(Object node, int column)
091    {
092        final DetailAST ast = (DetailAST) node;
093        switch (column) {
094            case 0:
095                return null;
096            case 1:
097                return TokenTypes.getTokenName(ast.getType());
098            case 2:
099                return ast.getLineNo();
100            case 3:
101                return ast.getColumnNo();
102            case 4:
103                return ast.getText();
104        }
105        return null;
106    }
107
108    @Override
109    public void setValueAt(Object aValue, Object node, int column)
110    {
111    }
112
113    public Object getChild(Object parent, int index)
114    {
115        final 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        final DetailAST ast = (DetailAST) parent;
128        return ast.getChildCount();
129    }
130
131}