KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > visualization > VisualDrawing


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.visualization;
33
34 import org.antlr.analysis.NFAState;
35 import org.antlr.works.ate.syntax.misc.ATEThread;
36 import org.antlr.works.prefs.AWPrefs;
37 import org.antlr.works.syntax.element.ElementRule;
38 import org.antlr.works.utils.Console;
39 import org.antlr.works.utils.ErrorListener;
40 import org.antlr.works.visualization.graphics.GFactory;
41
42 import java.util.HashMap JavaDoc;
43 import java.util.List JavaDoc;
44 import java.util.Map JavaDoc;
45
46 public class VisualDrawing extends ATEThread {
47
48     protected Visual visual;
49
50     protected GFactory factory = new GFactory();
51
52     protected String JavaDoc text;
53     protected ElementRule rule;
54
55     protected String JavaDoc threadText;
56     protected ElementRule threadRule;
57     protected ElementRule threadLastProcessedRule;
58
59     protected Map JavaDoc<ElementRule,List JavaDoc> cacheGraphs = new HashMap JavaDoc<ElementRule, List JavaDoc>();
60
61     public VisualDrawing(Visual visual) {
62         this.visual = visual;
63         start();
64     }
65
66     public void toggleNFAOptimization() {
67         factory.toggleNFAOptimization();
68         clearCacheGraphs();
69     }
70
71     public synchronized void setText(String JavaDoc text) {
72         this.text = text;
73         awakeThread(500);
74     }
75
76     public synchronized void setRule(ElementRule rule, boolean immediate) {
77         this.rule = rule;
78         awakeThread(immediate?0:500);
79     }
80
81     public synchronized void clearCacheGraphs() {
82         cacheGraphs.clear();
83     }
84
85     /**
86      * Tries to refresh the current graph in cache. If the graphs are not in cache, return false.
87      */

88     public synchronized boolean refresh() {
89         List JavaDoc graphs = cacheGraphs.get(threadLastProcessedRule);
90         if(graphs == null || graphs.isEmpty()) {
91             return false;
92         } else {
93             visual.panel.setRule(threadLastProcessedRule);
94             visual.panel.setGraphs(graphs);
95             visual.panel.update();
96             return true;
97         }
98     }
99
100     public synchronized boolean threadShouldProcess() {
101         return text != null || rule != null;
102     }
103
104     public synchronized void threadPrepareProcess() {
105         this.threadText = text;
106         this.threadRule = rule;
107
108         text = null;
109         rule = null;
110     }
111
112     private void threadProcessText() {
113         if(threadText == null)
114             return;
115
116         ErrorListener.shared().setPrintToConsole(false);
117         try {
118             visual.getEngineGrammar().createGrammars();
119         } catch (Exception JavaDoc e) {
120             // @todo ignore for now but later with prefs?
121
//visual.editor.console.print(e);
122
} finally {
123             // Flush all caches in cache because the grammar has changed
124
clearCacheGraphs();
125         }
126     }
127
128     private void threadProcessRule() throws Exception JavaDoc {
129         if(threadRule == null)
130             return;
131
132         String JavaDoc error = null;
133
134         ErrorListener.shared().setPrintToConsole(false);
135
136         if(visual.getEngineGrammar().hasGrammar()) {
137             NFAState startState = null;
138             try {
139                 startState = visual.getEngineGrammar().getRuleStartState(threadRule.name);
140                 /*Grammar g = visual.getEngineGrammar().getLexerGrammar();
141                 for(int i=104; i<152; i++) {
142                    // System.out.println(g.getTokenDisplayName(g.getTokenType("T"+i)));
143                 } */

144             } catch (Exception JavaDoc e) {
145                 // @todo ignore for now but later with prefs?
146
//visual.editor.console.print(e);
147
}
148             if(startState == null)
149                 error = "Cannot display rule \"" + threadRule + "\" because start state not found";
150         } else {
151             error = "Cannot display rule \""+threadRule+"\" because grammar cannot be generated";
152         }
153
154         if(error != null) {
155             visual.setPlaceholder(error);
156             visual.getConsole().println(error, Console.LEVEL_ERROR);
157             return;
158         }
159
160         // Try to get the optimized graph from cache first. If the grammar didn't change (i.e. user
161
// only moving cursor in the text zone), the speed-up can be important.
162
createGraphsForRule(threadRule);
163
164         threadLastProcessedRule = threadRule;
165
166         refresh();
167     }
168
169     protected synchronized void createGraphsForRule(ElementRule rule) throws Exception JavaDoc {
170         List JavaDoc graphs = cacheGraphs.get(rule);
171         if(graphs == null) {
172             factory.setOptimize(!AWPrefs.getDebugDontOptimizeNFA());
173             factory.setConsole(visual.getConsole());
174             graphs = factory.buildGraphsForRule(visual.getEngineGrammar(), rule.name, rule.errors);
175             if(graphs != null)
176                 cacheGraphs.put(rule, graphs);
177         }
178     }
179
180     public void threadReportException(Exception JavaDoc e) {
181         visual.getConsole().print(e);
182     }
183
184     public void threadRun() throws Exception JavaDoc {
185         visual.getConsole().setMode(Console.MODE_QUIET);
186
187         if(threadShouldProcess()) {
188             threadPrepareProcess();
189
190             // Process any text
191
threadProcessText();
192
193             // Process any rule
194
threadProcessRule();
195         }
196     }
197
198 }
199
Popular Tags