KickJava   Java API By Example, From Geeks To Geeks.

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


1 package 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/RIGHTS.html
6  *
7  * $Id: //depot/code/org.antlr/main/main/antlr/debug/misc/JTreeASTModel.java#4 $
8  */

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