1 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 33 public class ParseTreeModel extends AbstractTreeTableModel 34 { 35 private static final String [] COLUMN_NAMES = new String []{ 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 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 [] path = {root}; 58 fireTreeStructureChanged(this, path, null, null); 61 } 62 63 public int getColumnCount() 64 { 65 return COLUMN_NAMES.length; 66 } 67 68 public String getColumnName(int column) 69 { 70 return COLUMN_NAMES[column]; 71 } 72 73 public Class getColumnClass(int column) 74 { 75 switch (column) { 76 case 0: 77 return TreeTableModel.class; 78 case 1: 79 return String .class; 80 case 2: 81 return Integer .class; 82 case 3: 83 return Integer .class; 84 case 4: 85 return String .class; 86 } 87 return Object .class; 88 } 89 90 public Object getValueAt(Object 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 (ast.getLineNo()); 100 case 3: 101 return new Integer (ast.getColumnNo()); 102 case 4: 103 return ast.getText(); 104 } 105 return null; 106 } 107 108 public void setValueAt(Object aValue, Object node, int column) 109 { 110 } 111 112 public Object getChild(Object 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 parent) 125 { 126 final DetailAST ast = (DetailAST) parent; 127 return ast.getChildCount(); 128 } 129 130 } 131 | Popular Tags |