001: package antlr.debug.misc;
002:
003: /* ANTLR Translator Generator
004: * Project led by Terence Parr at http://www.cs.usfca.edu
005: * Software rights: http://www.antlr.org/license.html
006: */
007:
008: import antlr.collections.AST;
009:
010: import javax.swing.*;
011: import javax.swing.event.*;
012: import javax.swing.tree.*;
013:
014: public class JTreeASTModel implements TreeModel {
015:
016: AST root = null;
017:
018: public JTreeASTModel(AST t) {
019: if (t == null) {
020: throw new IllegalArgumentException("root is null");
021: }
022: root = t;
023: }
024:
025: public void addTreeModelListener(TreeModelListener l) {
026: }
027:
028: public Object getChild(Object parent, int index) {
029: if (parent == null) {
030: return null;
031: }
032: AST p = (AST) parent;
033: AST c = p.getFirstChild();
034: if (c == null) {
035: throw new ArrayIndexOutOfBoundsException(
036: "node has no children");
037: }
038: int i = 0;
039: while (c != null && i < index) {
040: c = c.getNextSibling();
041: i++;
042: }
043: return c;
044: }
045:
046: public int getChildCount(Object parent) {
047: if (parent == null) {
048: throw new IllegalArgumentException("root is null");
049: }
050: AST p = (AST) parent;
051: AST c = p.getFirstChild();
052: int i = 0;
053: while (c != null) {
054: c = c.getNextSibling();
055: i++;
056: }
057: return i;
058: }
059:
060: public int getIndexOfChild(Object parent, Object child) {
061: if (parent == null || child == null) {
062: throw new IllegalArgumentException("root or child is null");
063: }
064: AST p = (AST) parent;
065: AST c = p.getFirstChild();
066: if (c == null) {
067: throw new ArrayIndexOutOfBoundsException(
068: "node has no children");
069: }
070: int i = 0;
071: while (c != null && c != child) {
072: c = c.getNextSibling();
073: i++;
074: }
075: if (c == child) {
076: return i;
077: }
078: throw new java.util.NoSuchElementException(
079: "node is not a child");
080: }
081:
082: public Object getRoot() {
083: return root;
084: }
085:
086: public boolean isLeaf(Object node) {
087: if (node == null) {
088: throw new IllegalArgumentException("node is null");
089: }
090: AST t = (AST) node;
091: return t.getFirstChild() == null;
092: }
093:
094: public void removeTreeModelListener(TreeModelListener l) {
095: }
096:
097: public void valueForPathChanged(TreePath path, Object newValue) {
098: System.out.println("heh, who is calling this mystery method?");
099: }
100: }
|