KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > interpreter > EditorInterpreterTreeModel


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

31
32 package org.antlr.works.interpreter;
33
34 import org.antlr.runtime.CommonToken;
35 import org.antlr.runtime.NoViableAltException;
36 import org.antlr.runtime.tree.ParseTree;
37 import org.antlr.runtime.tree.Tree;
38 import org.antlr.tool.Grammar;
39 import org.antlr.works.awtree.AWTreeNode;
40
41 import javax.swing.tree.DefaultTreeModel JavaDoc;
42 import javax.swing.tree.MutableTreeNode JavaDoc;
43 import javax.swing.tree.TreeNode JavaDoc;
44 import java.awt.*;
45 import java.util.Enumeration JavaDoc;
46
47 public class EditorInterpreterTreeModel extends DefaultTreeModel JavaDoc {
48
49     protected Grammar grammar;
50
51     public EditorInterpreterTreeModel() {
52         super(null);
53     }
54
55     public void setGrammar(Grammar grammar) {
56         this.grammar = grammar;
57     }
58
59     public void setTree(Tree tree) {
60         setRoot(new InterpreterTreeNode(null, tree));
61     }
62
63     public class InterpreterTreeNode extends AWTreeNode {
64
65         protected Tree tree;
66
67         public InterpreterTreeNode(TreeNode parent, Tree tree) {
68             this.parent = (MutableTreeNode JavaDoc) parent;
69             this.tree = tree;
70         }
71
72         public TreeNode getChildAt(int childIndex) {
73             return new InterpreterTreeNode(this, tree.getChild(childIndex));
74         }
75
76         public int getChildCount() {
77             return tree.getChildCount();
78         }
79
80         public TreeNode getParent() {
81             return parent;
82         }
83
84         public int getIndex(TreeNode node) {
85             for(int i=0; i<tree.getChildCount(); i++) {
86                 if(tree.getChild(i) == node)
87                     return i;
88             }
89             return -1;
90         }
91
92         public boolean getAllowsChildren() {
93             return true;
94         }
95
96         public boolean isLeaf() {
97             return getChildCount() == 0;
98         }
99
100         public Enumeration JavaDoc children() {
101             return null;
102         }
103
104         public Object JavaDoc getPayload() {
105             if(tree instanceof ParseTree)
106                 return ((ParseTree)tree).payload;
107             else
108                 return null;
109         }
110
111         public Color getColor() {
112             return Color.black;
113         }
114
115         public String JavaDoc getInfoString() {
116             StringBuffer JavaDoc info = new StringBuffer JavaDoc();
117             Object JavaDoc payload = getPayload();
118             if(payload instanceof CommonToken) {
119                 CommonToken t = (CommonToken)payload;
120                 info.append("Type: ").append(grammar.getTokenDisplayName(t.getType())).append("\n");
121                 info.append("Text: ").append(t.getText()).append("\n");
122                 info.append("Line: ").append(t.getLine()).append("\n");
123                 info.append("Char: ").append(t.getCharPositionInLine()).append("\n");
124                 info.append("Channel: ").append(t.getChannel()).append("\n");
125             } else if(payload instanceof NoViableAltException) {
126                 NoViableAltException e = (NoViableAltException)payload;
127                 info.append("Description: ").append(e.grammarDecisionDescription).append("\n");
128                 info.append("Descision: ").append(e.decisionNumber).append("\n");
129                 info.append("State: ").append(e.stateNumber).append("\n");
130             } else {
131                 if(isLeaf())
132                     info.append(payload.toString());
133                 else
134                     info.append("Rule: ").append(payload.toString());
135             }
136             return info.toString();
137         }
138
139         public String JavaDoc toString() {
140             Object JavaDoc payload = getPayload();
141             if(payload instanceof CommonToken) {
142                 CommonToken t = (CommonToken)payload;
143                 return t.getText();
144             } else if(payload instanceof NoViableAltException)
145                 return "NoViableAltException";
146             else if(payload == null)
147                 return "<null>";
148             else
149                 return payload.toString();
150         }
151     }
152 }
153
Popular Tags