KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > debugger > tree > DBTreeNode


1 package org.antlr.works.debugger.tree;
2
3 import org.antlr.runtime.Token;
4 import org.antlr.tool.Grammar;
5 import org.antlr.works.awtree.AWTreeNode;
6
7 import java.awt.*;
8 import java.util.Enumeration JavaDoc;
9 /*
10
11 [The "BSD licence"]
12 Copyright (c) 2005-2006 Jean Bovet
13 All rights reserved.
14
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions
17 are met:
18
19 1. Redistributions of source code must retain the above copyright
20 notice, this list of conditions and the following disclaimer.
21 2. Redistributions in binary form must reproduce the above copyright
22 notice, this list of conditions and the following disclaimer in the
23 documentation and/or other materials provided with the distribution.
24 3. The name of the author may not be used to endorse or promote products
25 derived from this software without specific prior written permission.
26
27 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
38 */

39
40 public class DBTreeNode extends AWTreeNode {
41
42     protected Token token;
43     protected Grammar grammar;
44
45     protected int line;
46     protected int pos;
47
48     protected Color color = Color.black;
49
50     public DBTreeNode() {
51
52     }
53     
54     public DBTreeNode(Token token, Grammar grammar) {
55         this.token = token;
56         this.grammar = grammar;
57     }
58
59     public void setPosition(int line, int pos) {
60         this.line = line;
61         this.pos = pos;
62     }
63
64     public int getLine() {
65         return line;
66     }
67
68     public int getPosition() {
69         return pos;
70     }
71
72     public void setColor(Color color) {
73         this.color = color;
74     }
75
76     public Color getColor() {
77         return color;
78     }
79
80     public Token getToken() {
81         return token;
82     }
83
84     public DBTreeNode findNodeWithToken(Token t) {
85         return findNodeWithToken(t, null);
86     }
87
88     /** Find the last node corresponding to the given token. Because multiple node
89      * can contains the same token (backtracking nodes for example), we always want
90      * to select the most recent one in the tree (the selection will come from the
91      * input panel)
92      */

93
94     public DBTreeNode findNodeWithToken(Token t, DBTreeNode lastNodeSoFar) {
95         if(t == null)
96             return lastNodeSoFar;
97
98         if(token != null) {
99             /** Little hack here. If the token is of type DBTreeToken (tree grammar), then
100              * the token index cannot be used (we don't have this information). We will use
101              * the ID unique to each tree node instead.
102              */

103             if(token instanceof DBTreeToken && t instanceof DBTreeToken) {
104                 DBTreeToken t1 = (DBTreeToken)token;
105                 DBTreeToken t2 = (DBTreeToken)t;
106                 if(t1.ID == t2.ID)
107                     lastNodeSoFar = this;
108             } else if(t.getTokenIndex() == token.getTokenIndex() &&
109                     t.getType() == token.getType())
110             {
111                 // FIX AW-61 - compare also the token type to avoid selecting the wrong one (e.g. imaginary)
112
lastNodeSoFar = this;
113             }
114         }
115
116         for(Enumeration JavaDoc childrenEnumerator = children(); childrenEnumerator.hasMoreElements(); ) {
117             DBTreeNode node = (DBTreeNode) childrenEnumerator.nextElement();
118             DBTreeNode candidate = node.findNodeWithToken(t, lastNodeSoFar);
119             if(candidate != null)
120                 lastNodeSoFar = candidate;
121         }
122
123         return lastNodeSoFar;
124     }
125
126     public String JavaDoc toString() {
127         if(token != null)
128             return token.getText(); //+" <"+grammar.getTokenDisplayName(token.getType())+">"
129
else
130             return "?";
131     }
132
133     public String JavaDoc getInfoString() {
134         return toString();
135     }
136
137 }
138
Popular Tags