KickJava   Java API By Example, From Geeks To Geeks.

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


1 package persistence.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/license.html
6  *
7  */

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