01: package biz.hammurapi.antlr;
02:
03: import javax.swing.table.DefaultTableModel;
04: import javax.swing.table.TableModel;
05: import javax.swing.tree.DefaultMutableTreeNode;
06: import javax.swing.tree.MutableTreeNode;
07:
08: import biz.hammurapi.swing.Visualizable;
09:
10: public class AstVisualizable implements Visualizable {
11:
12: private AST ast;
13: private String[] tokenNames;
14:
15: public AstVisualizable(AST ast, String[] names) {
16: super ();
17: this .ast = ast;
18: tokenNames = names;
19: }
20:
21: public MutableTreeNode toTree(final String title) {
22: DefaultMutableTreeNode ret = new DefaultMutableTreeNode(this ) {
23: public String toString() {
24: return title + " [" + tokenNames[ast.getType()] + "] "
25: + ast.getLine() + ":" + ast.getColumn() + " "
26: + ast.getText();
27: }
28: };
29:
30: for (AST child = (AST) ast.getFirstChild(); child != null; child = (AST) child
31: .getNextSibling()) {
32: ret.add(new AstVisualizable(child, tokenNames).toTree(""));
33: }
34:
35: return ret;
36: }
37:
38: public TableModel toTable() {
39: DefaultTableModel ret = new DefaultTableModel(4, 2);
40:
41: ret.setColumnIdentifiers(new String[] { "Property", "Value" });
42: setRow(ret, 0, "Type", tokenNames[ast.getType()]);
43: setRow(ret, 1, "Text", ast.getText());
44: setRow(ret, 2, "Line", String.valueOf(ast.getLine()));
45: setRow(ret, 3, "Column", String.valueOf(ast.getColumn()));
46:
47: return ret;
48: }
49:
50: private void setRow(DefaultTableModel ret, int row, String name,
51: Object value) {
52: ret.setValueAt(name, row, 0);
53: ret.setValueAt(value, row, 1);
54: }
55:
56: }
|