View Javadoc
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.ASTFactory;
23  import antlr.collections.AST;
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   */
32  public class ParseTreeModel extends AbstractTreeTableModel
33  {
34      private static final String[] COLUMN_NAMES = new String[]{
35          "Tree", "Type", "Line", "Column", "Text"
36      };
37  
38      public ParseTreeModel(DetailAST parseTree)
39      {
40          super(createArtificialTreeRoot());
41          setParseTree(parseTree);
42      }
43  
44      private static DetailAST createArtificialTreeRoot()
45      {
46          final ASTFactory factory = new ASTFactory();
47          factory.setASTNodeClass(DetailAST.class.getName());
48          // TODO: Need to resolve if need a fake root node....
49          return (DetailAST) factory.create(TokenTypes.EOF, "ROOT");
50      }
51  
52      void setParseTree(DetailAST parseTree)
53      {
54          final DetailAST root = (DetailAST) getRoot();
55          root.setFirstChild(parseTree);
56          final Object[] path = {root};
57          // no need to setup remaining info, as the call results in a
58          // table structure changed event anyway - we just pass nulls
59          fireTreeStructureChanged(this, path, null, null);
60      }
61  
62      @Override
63      public int getColumnCount()
64      {
65          return COLUMN_NAMES.length;
66      }
67  
68      @Override
69      public String getColumnName(int column)
70      {
71          return COLUMN_NAMES[column];
72      }
73  
74      @Override
75      public Class<?> getColumnClass(int column)
76      {
77          switch (column) {
78              case 0:
79                  return TreeTableModel.class;
80              case 1:
81                  return String.class;
82              case 2:
83                  return Integer.class;
84              case 3:
85                  return Integer.class;
86              case 4:
87                  return String.class;
88          }
89          return Object.class;
90      }
91  
92      @Override
93      public Object getValueAt(Object node, int column)
94      {
95          final DetailAST ast = (DetailAST) node;
96          switch (column) {
97              case 0:
98                  return null;
99              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 }