KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > menu > MenuExport


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.menu;
33
34 import org.antlr.works.components.grammar.CEditorGrammar;
35 import org.antlr.works.editor.EditorTab;
36 import org.antlr.works.stats.StatisticsAW;
37 import org.antlr.works.syntax.element.ElementRule;
38 import org.antlr.works.visualization.SDGenerator;
39 import org.antlr.works.visualization.Visual;
40 import org.antlr.works.visualization.graphics.GContext;
41 import org.antlr.works.visualization.graphics.GEngine;
42 import org.antlr.works.visualization.graphics.GEnginePS;
43 import org.antlr.works.visualization.graphics.graph.GGraphAbstract;
44 import org.antlr.xjlib.appkit.gview.GView;
45 import org.antlr.xjlib.appkit.utils.XJAlert;
46 import org.antlr.xjlib.appkit.utils.XJFileChooser;
47 import org.antlr.xjlib.foundation.XJUtils;
48
49 import javax.imageio.ImageIO JavaDoc;
50 import java.awt.image.BufferedImage JavaDoc;
51 import java.io.File JavaDoc;
52 import java.io.FileWriter JavaDoc;
53 import java.io.IOException JavaDoc;
54 import java.util.ArrayList JavaDoc;
55 import java.util.List JavaDoc;
56
57 public class MenuExport extends MenuAbstract {
58
59     public MenuExport(CEditorGrammar editor) {
60         super(editor);
61     }
62
63     public void exportEventsAsTextFile() {
64         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_EXPORT_EVENTS_AS_TEXT);
65
66         if(!XJFileChooser.shared().displaySaveDialog(editor.getWindowContainer(), "txt", "Text file", false))
67             return;
68
69         String JavaDoc file = XJFileChooser.shared().getSelectedFilePath();
70         if(file == null)
71             return;
72
73         try {
74             FileWriter JavaDoc writer = new FileWriter JavaDoc(file);
75             writer.write(editor.debugger.getEventsAsString());
76             writer.close();
77         } catch (IOException JavaDoc e) {
78             XJAlert.display(editor.getWindowContainer(), "Error", "Cannot save text file: "+file+"\nError: "+e);
79         }
80     }
81
82     public void exportAsImage() {
83         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_EXPORT_AS_BITMAP);
84
85         EditorTab tab = editor.getSelectedTab();
86         if(!tab.canExportToBitmap())
87             return;
88
89         if(tab instanceof Visual)
90             exportRuleAsImage();
91         else
92             exportGViewAsImage(tab.getExportableGView());
93     }
94
95     public void exportAllRulesAsImage() {
96         exportAllRules(true);
97     }
98
99     public void exportAllRulesAsEPS() {
100         exportAllRules(false);
101     }
102
103     public void exportAllRules(boolean asImage) {
104         List JavaDoc<String JavaDoc> extensions = null;
105         if(asImage) {
106             extensions = lookupAvailableImageFormat();
107         }
108         if(!XJFileChooser.shared().displayChooseDirectory(editor.getWindowContainer(), extensions, extensions, !asImage)) {
109             return;
110         }
111
112         String JavaDoc directory = XJFileChooser.shared().getSelectedFilePath();
113         String JavaDoc extension = XJFileChooser.shared().getSelectedFileExtension();
114
115         SDGenerator sd = new SDGenerator(editor.getEngineGrammar());
116         for(ElementRule rule : editor.getRules()) {
117             try {
118                 if(asImage) {
119                     sd.renderRuleToBitmapFile(rule.name, extension, XJUtils.concatPath(directory, rule.name+"."+extension));
120                 } else {
121                     sd.renderRuleToEPSFile(rule.name, XJUtils.concatPath(directory, rule.name+".eps"));
122                 }
123             } catch (Exception JavaDoc e) {
124                 XJAlert.display(editor.getWindowContainer(), "Error", "Images cannot be saved because:\n"+e);
125             }
126         }
127     }
128
129     public void exportRuleAsImage() {
130         if(!editor.visual.canSaveImage()) {
131             XJAlert.display(editor.getWindowContainer(), "Export Rule to Bitmap Image", "There is no rule at cursor position.");
132             return;
133         }
134
135         saveImageToDisk(editor.visual.getImage());
136     }
137
138     public void exportGViewAsImage(GView view) {
139         saveImageToDisk(view.getImage());
140     }
141
142     public void saveImageToDisk(BufferedImage JavaDoc image) {
143         List JavaDoc<String JavaDoc> extensions = lookupAvailableImageFormat();
144         if(XJFileChooser.shared().displaySaveDialog(editor.getWindowContainer(), extensions, extensions, false)) {
145             String JavaDoc file = XJFileChooser.shared().getSelectedFilePath();
146             try {
147                 ImageIO.write(image, file.substring(file.lastIndexOf(".")+1), new File JavaDoc(file));
148             } catch (IOException JavaDoc e) {
149                 XJAlert.display(editor.getWindowContainer(), "Error", "Image \""+file+"\" cannot be saved because:\n"+e);
150             }
151         }
152     }
153
154     private static List JavaDoc<String JavaDoc> lookupAvailableImageFormat() {
155         List JavaDoc<String JavaDoc> extensions = new ArrayList JavaDoc<String JavaDoc>();
156         for (int i = 0; i < ImageIO.getWriterFormatNames().length; i++) {
157             String JavaDoc ext = ImageIO.getWriterFormatNames()[i].toLowerCase();
158             if(!extensions.contains(ext))
159                 extensions.add(ext);
160         }
161         return extensions;
162     }
163
164     public void exportAsEPS() {
165         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_EXPORT_AS_EPS);
166
167         EditorTab tab = editor.getSelectedTab();
168         if(!tab.canExportToEPS())
169             return;
170
171         if(tab instanceof Visual)
172             exportRuleAsEPS();
173         else
174             exportGViewAsEPS(tab.getExportableGView());
175     }
176
177     protected void exportRuleAsEPS() {
178         if(editor.rules.getEnclosingRuleAtPosition(editor.getCaretPosition()) == null) {
179             XJAlert.display(editor.getWindowContainer(), "Export Rule to EPS", "There is no rule at cursor position.");
180             return;
181         }
182
183         GGraphAbstract graph = editor.visual.getCurrentGraph();
184
185         if(graph == null) {
186             XJAlert.display(editor.getWindowContainer(), "Export Rule to EPS", "There is no graphical visualization.");
187             return;
188         }
189
190         if(!XJFileChooser.shared().displaySaveDialog(editor.getWindowContainer(), "eps", "EPS file", false))
191             return;
192
193         String JavaDoc file = XJFileChooser.shared().getSelectedFilePath();
194         if(file == null)
195             return;
196
197         try {
198             GEnginePS engine = new GEnginePS();
199
200             GContext context = graph.getContext();
201             GEngine oldEngine = context.engine;
202             context.setEngine(engine);
203             graph.draw();
204             context.setEngine(oldEngine);
205
206             XJUtils.writeStringToFile(engine.getPSText(), file);
207         } catch (Exception JavaDoc e) {
208             editor.console.print(e);
209             XJAlert.display(editor.getWindowContainer(), "Error", "Cannot export to EPS file: "+file+"\nError: "+e);
210         }
211     }
212
213     protected void exportGViewAsEPS(GView view) {
214         if(!XJFileChooser.shared().displaySaveDialog(editor.getWindowContainer(), "eps", "EPS file", false))
215             return;
216
217         String JavaDoc file = XJFileChooser.shared().getSelectedFilePath();
218         if(file == null)
219             return;
220
221         try {
222             XJUtils.writeStringToFile(view.getEPS(), file);
223         } catch (Exception JavaDoc e) {
224             editor.console.print(e);
225             XJAlert.display(editor.getWindowContainer(), "Error", "Cannot export to EPS file: "+file+"\nError: "+e);
226         }
227     }
228
229     public void exportAsDOT() {
230         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_EXPORT_AS_DOT);
231
232         EditorTab tab = editor.getSelectedTab();
233         if(!tab.canExportToDOT())
234             return;
235
236         if(!XJFileChooser.shared().displaySaveDialog(editor.getWindowContainer(), "dot", "DOT file", false))
237             return;
238
239         String JavaDoc file = XJFileChooser.shared().getSelectedFilePath();
240         if(file == null)
241             return;
242
243         try {
244             XJUtils.writeStringToFile(tab.getDOTString(), file);
245         } catch (Exception JavaDoc e) {
246             editor.console.print(e);
247             XJAlert.display(editor.getWindowContainer(), "Error", "Cannot export to DOT file: "+file+"\nError: "+e);
248         }
249     }
250
251 }
252
Popular Tags