KickJava   Java API By Example, From Geeks To Geeks.

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


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.works.ate.syntax.misc.ATEToken;
35 import org.antlr.works.components.grammar.CEditorGrammar;
36 import org.antlr.works.editor.EditorTab;
37 import org.antlr.works.grammar.EngineGrammar;
38 import org.antlr.works.prefs.AWPrefs;
39 import org.antlr.works.syntax.GrammarSyntaxEngine;
40 import org.antlr.works.syntax.element.ElementRule;
41 import org.antlr.works.utils.Console;
42 import org.antlr.works.visualization.graphics.GContext;
43 import org.antlr.works.visualization.graphics.GContextProvider;
44 import org.antlr.works.visualization.graphics.GEngineGraphics;
45 import org.antlr.works.visualization.graphics.graph.GGraphAbstract;
46 import org.antlr.works.visualization.graphics.panel.GPanel;
47 import org.antlr.works.visualization.skin.Skin;
48 import org.antlr.works.visualization.skin.syntaxdiagram.SDSkin;
49
50 import java.awt.*;
51 import java.awt.image.BufferedImage JavaDoc;
52
53 public class Visual extends EditorTab implements GContextProvider {
54
55     protected CEditorGrammar editor;
56
57     protected VisualDrawing drawing;
58
59     protected GContext context;
60     protected Skin skin;
61
62     protected GPanel panel;
63
64     protected boolean enable = true;
65
66     public Visual(CEditorGrammar editor) {
67         this.editor = editor;
68
69         skin = new SDSkin();
70
71         context = new GContext();
72         context.setEngine(new GEngineGraphics());
73         context.setSkin(skin);
74         context.setProvider(this);
75
76         panel = new GPanel(editor, context);
77
78         drawing = new VisualDrawing(this);
79     }
80
81     public Console getConsole() {
82         return editor.getConsole();
83     }
84
85     public void close() {
86         panel.close();
87         drawing.stop();
88         final long t = System.currentTimeMillis();
89         while(drawing.isRunning() && (System.currentTimeMillis() - t) < 5000) {
90             try {
91                 Thread.sleep(10);
92             } catch (InterruptedException JavaDoc e) {
93                 // We don't care if sleep has been interrupted
94
}
95         }
96     }
97
98     public void setEnable(boolean flag) {
99         this.enable = flag;
100     }
101
102     public boolean isEnable() {
103         return enable;
104     }
105
106     public void toggleNFAOptimization() {
107         drawing.toggleNFAOptimization();
108     }
109
110     public Color contextGetColorForLabel(String JavaDoc label) {
111         if(label.charAt(0) == '\'' || label.charAt(0) == '"')
112             return AWPrefs.getSyntaxColor(AWPrefs.PREF_SYNTAX_STRING);
113         else {
114             if(ATEToken.isLexerName(label))
115                 return GrammarSyntaxEngine.COLOR_LEXER;
116             else
117                 return GrammarSyntaxEngine.COLOR_PARSER;
118         }
119     }
120
121     public void setText(String JavaDoc text, String JavaDoc filename) {
122         if(isEnable())
123             drawing.setText(text);
124     }
125
126     public void setRule(ElementRule rule, boolean immediate) {
127         if(isEnable())
128             drawing.setRule(rule, immediate);
129     }
130
131     public void setPlaceholder(String JavaDoc placeholder) {
132         panel.setPlaceholder(placeholder);
133         panel.setGraphs(null);
134     }
135
136     public void cancelDrawingProcess() {
137         drawing.skip();
138     }
139
140     public void clearCacheGraphs() {
141         drawing.clearCacheGraphs();
142     }
143
144     public void createGraphsForRule(ElementRule rule) throws Exception JavaDoc {
145         drawing.createGraphsForRule(rule);
146     }
147
148     public boolean update() {
149         panel.createPanel();
150         return drawing.refresh();
151     }
152     
153     public EngineGrammar getEngineGrammar() {
154         return editor.getEngineGrammar();
155     }
156
157     public Container getContainer() {
158         return panel.getContainer();
159     }
160
161     public GGraphAbstract getCurrentGraph() {
162         return panel.getCurrentGraph();
163     }
164
165     public boolean canSaveImage() {
166         return panel.getImageOfView() != null;
167     }
168
169     public BufferedImage JavaDoc getImage() {
170         return panel.getImageOfView();
171     }
172
173     public boolean canExportToBitmap() {
174         return true;
175     }
176
177     public boolean canExportToEPS() {
178         return true;
179     }
180
181     public String JavaDoc getTabName() {
182         return "Syntax Diagram";
183     }
184
185     public Component getTabComponent() {
186         return getContainer();
187     }
188
189 }
190
Popular Tags