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