001 package biz.hammurapi.antlr; 002 003 import java.util.ArrayList; 004 import java.util.List; 005 006 import antlr.collections.AST; 007 008 import javax.swing.table.DefaultTableModel; 009 import javax.swing.table.TableModel; 010 import javax.swing.tree.TreeNode; 011 012 import biz.hammurapi.swing.LazyTreeNode; 013 import biz.hammurapi.swing.TableVisualizable; 014 015 public class AstVisualizable extends LazyTreeNode implements TableVisualizable { 016 017 private AST ast; 018 private String[] tokenNames; 019 private boolean withSiblings; 020 021 public AstVisualizable(TreeNode parent, AST ast, String[] names, boolean withSiblings) { 022 super(parent, withSiblings? "Syntax Tree" : names[ast.getType()]); 023 this.ast = ast; 024 tokenNames = names; 025 this.withSiblings = withSiblings; 026 } 027 028 protected List loadChildren() { 029 List ret = new ArrayList(); 030 for (AST child= withSiblings ? ast : ast.getFirstChild(); child!=null; child= child.getNextSibling()) { 031 ret.add(new AstVisualizable(this, child, tokenNames, false)); 032 } 033 return ret; 034 } 035 036 public TableModel toTable() { 037 DefaultTableModel ret=new DefaultTableModel(4, 2); 038 039 ret.setColumnIdentifiers(new String[] {"Property", "Value"}); 040 setRow(ret, 0, "Type", tokenNames[ast.getType()]); 041 setRow(ret, 1, "Text", ast.getText()); 042 setRow(ret, 2, "Line", String.valueOf(ast.getLine())); 043 setRow(ret, 3, "Column", String.valueOf(ast.getColumn())); 044 045 return ret; 046 } 047 048 private static void setRow(DefaultTableModel ret, int row, String name, Object value) { 049 ret.setValueAt(name, row, 0); 050 ret.setValueAt(value, row, 1); 051 } 052 053 }