KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > debug > misc > ASTFrame


1 package antlr.debug.misc;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/RIGHTS.html
6  *
7  * $Id: //depot/code/org.antlr/main/main/antlr/debug/misc/ASTFrame.java#4 $
8  */

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     // The initial width and height of the frame
21
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 JavaDoc 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 JavaDoc lab, AST r) {
39         super(lab);
40
41         // Create the TreeSelectionListener
42
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                 // System.exit(0);
52
}
53         });
54         setSize(WIDTH, HEIGHT);
55     }
56
57     public static void main(String JavaDoc args[]) {
58         // Create the tree nodes
59
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