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    @Override
063    public int getColumnCount()
064    {
065        return COLUMN_NAMES.length;
066    }
067
068    @Override
069    public String getColumnName(int column)
070    {
071        return COLUMN_NAMES[column];
072    }
073
074    @Override
075    public Class<?> getColumnClass(int column)
076    {
077        switch (column) {
078            case 0:
079                return TreeTableModel.class;
080            case 1:
081                return String.class;
082            case 2:
083                return Integer.class;
084            case 3:
085                return Integer.class;
086            case 4:
087                return String.class;
088        }
089        return Object.class;
090    }
091
092    @Override
093    public Object getValueAt(Object node, int column)
094    {
095        final DetailAST ast = (DetailAST) node;
096        switch (column) {
097            case 0:
098                return null;
099            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}