001: /**
002: * Borrowed from Terence Parr
003: */package biz.hammurapi.antlr;
004:
005: /* ANTLR Translator Generator
006: * Project led by Terence Parr at http://www.jGuru.com
007: * Software rights: http://www.antlr.org/license.html
008: *
009: * $Id: //depot/code/org.antlr/release/antlr-2.7.5/antlr/debug/misc/ASTFrame.java#1 $
010: */
011:
012: import java.awt.BorderLayout;
013: import java.awt.Container;
014: import java.awt.Frame;
015: import java.awt.event.WindowAdapter;
016: import java.awt.event.WindowEvent;
017:
018: import javax.swing.JFrame;
019: import javax.swing.JPanel;
020: import javax.swing.JScrollPane;
021: import javax.swing.JTree;
022: import javax.swing.event.TreeModelListener;
023: import javax.swing.event.TreeSelectionListener;
024: import javax.swing.tree.TreeModel;
025: import javax.swing.tree.TreePath;
026:
027: import antlr.collections.AST;
028:
029: public class ASTFrame extends JFrame {
030:
031: public static class JTreeASTPanel extends JPanel {
032: JTree tree;
033:
034: public JTreeASTPanel(TreeModel tm,
035: TreeSelectionListener listener) {
036: // use a layout that will stretch tree to panel size
037: setLayout(new BorderLayout());
038:
039: // Create tree
040: tree = new JTree(tm);
041:
042: // Change line style
043: tree.putClientProperty("JTree.lineStyle", "Angled");
044:
045: // Add TreeSelectionListener
046: if (listener != null)
047: tree.addTreeSelectionListener(listener);
048:
049: // Put tree in a scrollable pane's viewport
050: JScrollPane sp = new JScrollPane();
051: sp.getViewport().add(tree);
052:
053: add(sp, BorderLayout.CENTER);
054: }
055: }
056:
057: public static class JTreeASTModel implements TreeModel {
058:
059: private class Node {
060: AST master;
061:
062: /**
063: * @param master
064: */
065: public Node(AST master) {
066: super ();
067: // TODO Auto-generated constructor stub
068: this .master = master;
069: }
070:
071: public String toString() {
072: return "[" + tokenNames[master.getType()] + " "
073: + master.getLine() + ":" + master.getColumn()
074: + "] " + master.getText();
075: }
076:
077: public int hashCode() {
078: return master.hashCode();
079: }
080:
081: public boolean equals(Object obj) {
082: return obj instanceof Node
083: && master.equals(((Node) obj).master);
084: }
085: }
086:
087: AST root = null;
088: private String[] tokenNames;
089:
090: public JTreeASTModel(AST t, String[] tokenNames) {
091: if (t == null) {
092: throw new IllegalArgumentException("root is null");
093: }
094: root = t;
095: this .tokenNames = tokenNames;
096: }
097:
098: public void addTreeModelListener(TreeModelListener l) {
099: }
100:
101: public Object getChild(Object parent, int index) {
102: if (parent == null) {
103: return null;
104: }
105: AST p = ((Node) parent).master;
106: AST c = p.getFirstChild();
107: if (c == null) {
108: throw new ArrayIndexOutOfBoundsException(
109: "node has no children");
110: }
111: int i = 0;
112: while (c != null && i < index) {
113: c = c.getNextSibling();
114: i++;
115: }
116:
117: return new Node(c);
118: }
119:
120: public int getChildCount(Object parent) {
121: if (parent == null) {
122: throw new IllegalArgumentException("root is null");
123: }
124: AST p = ((Node) parent).master;
125: AST c = p.getFirstChild();
126: int i = 0;
127: while (c != null) {
128: c = c.getNextSibling();
129: i++;
130: }
131: return i;
132: }
133:
134: public int getIndexOfChild(Object parent, Object child) {
135: if (parent == null || child == null) {
136: throw new IllegalArgumentException(
137: "root or child is null");
138: }
139: AST p = ((Node) parent).master;
140: AST c = p.getFirstChild();
141: if (c == null) {
142: throw new ArrayIndexOutOfBoundsException(
143: "node has no children");
144: }
145: int i = 0;
146: while (c != null && c != ((Node) child).master) {
147: c = c.getNextSibling();
148: i++;
149: }
150: if (c == ((Node) child).master) {
151: return i;
152: }
153: throw new java.util.NoSuchElementException(
154: "node is not a child");
155: }
156:
157: public Object getRoot() {
158: return new Node(root);
159: }
160:
161: public boolean isLeaf(Object node) {
162: if (node == null) {
163: throw new IllegalArgumentException("node is null");
164: }
165: AST t = ((Node) node).master;
166: return t.getFirstChild() == null;
167: }
168:
169: public void removeTreeModelListener(TreeModelListener l) {
170: }
171:
172: public void valueForPathChanged(TreePath path, Object newValue) {
173: System.out
174: .println("heh, who is calling this mystery method?");
175: }
176: }
177:
178: // The initial width and height of the frame
179: static final int FRAME_WIDTH = 400;
180: static final int FRAME_HEIGHT = 600;
181:
182: public ASTFrame(String lab, AST r, String[] tokenNames) {
183: super (lab);
184:
185: JTreeASTPanel tp = new JTreeASTPanel(new JTreeASTModel(r,
186: tokenNames), null);
187: Container content = getContentPane();
188: content.add(tp, BorderLayout.CENTER);
189: addWindowListener(new WindowAdapter() {
190: public void windowClosing(WindowEvent e) {
191: Frame f = (Frame) e.getSource();
192: f.setVisible(false);
193: f.dispose();
194: // System.exit(0);
195: }
196: });
197: setSize(FRAME_WIDTH, FRAME_HEIGHT);
198: }
199: }
|