KickJava   Java API By Example, From Geeks To Geeks.

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


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.collections.AST;
10
11 import javax.swing.*;
12 import javax.swing.event.*;
13 import javax.swing.tree.*;
14
15 public class JTreeASTModel implements TreeModel {
16
17     AST root = null;
18
19     public JTreeASTModel(AST t) {
20         if (t == null) {
21             throw new IllegalArgumentException JavaDoc("root is null");
22         }
23         root = t;
24     }
25
26     public void addTreeModelListener(TreeModelListener l) {
27     }
28
29     public Object JavaDoc getChild(Object JavaDoc parent, int index) {
30         if (parent == null) {
31             return null;
32         }
33         AST p = (AST)parent;
34         AST c = p.getFirstChild();
35         if (c == null) {
36             throw new ArrayIndexOutOfBoundsException JavaDoc("node has no children");
37         }
38         int i = 0;
39         while (c != null && i < index) {
40             c = c.getNextSibling();
41             i++;
42         }
43         return c;
44     }
45
46     public int getChildCount(Object JavaDoc parent) {
47         if (parent == null) {
48             throw new IllegalArgumentException JavaDoc("root is null");
49         }
50         AST p = (AST)parent;
51         AST c = p.getFirstChild();
52         int i = 0;
53         while (c != null) {
54             c = c.getNextSibling();
55             i++;
56         }
57         return i;
58     }
59
60     public int getIndexOfChild(Object JavaDoc parent, Object JavaDoc child) {
61         if (parent == null || child == null) {
62             throw new IllegalArgumentException JavaDoc("root or child is null");
63         }
64         AST p = (AST)parent;
65         AST c = p.getFirstChild();
66         if (c == null) {
67             throw new ArrayIndexOutOfBoundsException JavaDoc("node has no children");
68         }
69         int i = 0;
70         while (c != null && c != child) {
71             c = c.getNextSibling();
72             i++;
73         }
74         if (c == child) {
75             return i;
76         }
77         throw new java.util.NoSuchElementException JavaDoc("node is not a child");
78     }
79
80     public Object JavaDoc getRoot() {
81         return root;
82     }
83
84     public boolean isLeaf(Object JavaDoc node) {
85         if (node == null) {
86             throw new IllegalArgumentException JavaDoc("node is null");
87         }
88         AST t = (AST)node;
89         return t.getFirstChild() == null;
90     }
91
92     public void removeTreeModelListener(TreeModelListener l) {
93     }
94
95     public void valueForPathChanged(TreePath path, Object JavaDoc newValue) {
96         System.out.println("heh, who is calling this mystery method?");
97     }
98 }
99
Popular Tags