1 package antlr.debug.misc; 2 3 9 10 import antlr.*; 11 import antlr.collections.AST; 12 13 import java.awt.*; 14 import java.awt.event.*; 15 import javax.swing.*; 16 import javax.swing.event.*; 17 import javax.swing.tree.*; 18 19 public class ASTFrame extends JFrame { 20 static final int WIDTH = 200; 22 static final int HEIGHT = 300; 23 24 class MyTreeSelectionListener 25 implements TreeSelectionListener { 26 public void valueChanged(TreeSelectionEvent event) { 27 TreePath path = event.getPath(); 28 System.out.println("Selected: " + 29 path.getLastPathComponent()); 30 Object elements[] = path.getPath(); 31 for (int i = 0; i < elements.length; i++) { 32 System.out.print("->" + elements[i]); 33 } 34 System.out.println(); 35 } 36 } 37 38 public ASTFrame(String lab, AST r) { 39 super(lab); 40 41 TreeSelectionListener listener = new MyTreeSelectionListener(); 43 JTreeASTPanel tp = new JTreeASTPanel(new JTreeASTModel(r), null); 44 Container content = getContentPane(); 45 content.add(tp, BorderLayout.CENTER); 46 addWindowListener(new WindowAdapter() { 47 public void windowClosing(WindowEvent e) { 48 Frame f = (Frame)e.getSource(); 49 f.setVisible(false); 50 f.dispose(); 51 } 53 }); 54 setSize(WIDTH, HEIGHT); 55 } 56 57 public static void main(String args[]) { 58 ASTFactory factory = new ASTFactory(); 60 CommonAST r = (CommonAST)factory.create(0, "ROOT"); 61 r.addChild((CommonAST)factory.create(0, "C1")); 62 r.addChild((CommonAST)factory.create(0, "C2")); 63 r.addChild((CommonAST)factory.create(0, "C3")); 64 65 ASTFrame frame = new ASTFrame("AST JTree Example", r); 66 frame.setVisible(true); 67 } 68 } 69 | Popular Tags |