2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.bin.Jalview;
24 import jalview.gui.EPSOptions;
25 import jalview.gui.IProgressIndicator;
26 import jalview.gui.SVGOptions;
27 import jalview.io.JalviewFileChooser;
29 import java.awt.Component;
30 import java.awt.Graphics;
31 import java.awt.Graphics2D;
32 import java.awt.RenderingHints;
33 import java.awt.image.BufferedImage;
35 import java.io.FileOutputStream;
37 import javax.imageio.ImageIO;
39 import org.jfree.graphics2d.svg.SVGGraphics2D;
40 import org.jfree.graphics2d.svg.SVGHints;
41 import org.jibble.epsgraphics.EpsGraphics2D;
43 public class ImageMaker
57 private IProgressIndicator pIndicator;
59 private long pSessionId;
61 private boolean headless;
65 EPS("EPS", MessageManager.getString("label.eps_file"), getEPSChooser()),
66 PNG("PNG", MessageManager.getString("label.png_image"), getPNGChooser()),
67 SVG("SVG", "SVG", getSVGChooser());
69 private JalviewFileChooser chooser;
75 TYPE(String name, String label, JalviewFileChooser chooser)
79 this.chooser = chooser;
82 public String getName()
87 public JalviewFileChooser getChooser()
92 public String getLabel()
99 public ImageMaker(Component parent, TYPE type, String title, int width,
100 int height, File file, String fileTitle,
101 IProgressIndicator pIndicator, long pSessionId, boolean headless)
103 this.pIndicator = pIndicator;
105 this.pSessionId = pSessionId;
106 this.headless = headless;
109 setProgressMessage(MessageManager.formatMessage(
110 "status.waiting_for_user_to_select_output_file", type.name));
111 JalviewFileChooser chooser;
112 chooser = type.getChooser();
113 chooser.setFileView(new jalview.io.JalviewFileView());
114 chooser.setDialogTitle(title);
115 chooser.setToolTipText(MessageManager.getString("action.save"));
116 int value = chooser.showSaveDialog(parent);
118 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
120 jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
121 .getSelectedFile().getParent());
122 file = chooser.getSelectedFile();
126 setProgressMessage(MessageManager.formatMessage(
127 "status.cancelled_image_export_operation", type.name));
135 out = new FileOutputStream(file);
136 setProgressMessage(null);
137 setProgressMessage(MessageManager.formatMessage(
138 "status.exporting_alignment_as_x_file", type.getName()));
139 if (type == TYPE.SVG)
141 setupSVG(width, height, fileTitle);
143 else if (type == TYPE.EPS)
145 setupEPS(width, height, fileTitle);
147 else if (type == TYPE.PNG)
149 setupPNG(width, height);
152 } catch (Exception ex)
154 System.out.println("Error creating " + type.getName() + " file.");
156 setProgressMessage(MessageManager.formatMessage(
157 "info.error_creating_file", type.getName()));
162 public Graphics getGraphics()
167 public void writeImage()
178 String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
179 out.write(svgData.getBytes());
184 ImageIO.write(bi, "png", out);
189 } catch (Exception ex)
191 ex.printStackTrace();
195 void setupEPS(int width, int height, String title)
197 boolean accurateText = true;
199 String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
202 // If we need to prompt, and if the GUI is visible then
203 // Prompt for EPS rendering style
204 if (renderStyle.equalsIgnoreCase("Prompt each time")
205 && !(System.getProperty("java.awt.headless") != null && System
206 .getProperty("java.awt.headless").equals("true")))
208 EPSOptions eps = new EPSOptions();
209 renderStyle = eps.getValue();
211 if (renderStyle == null || eps.cancelled)
213 setProgressMessage(MessageManager.formatMessage(
214 "status.cancelled_image_export_operation", "EPS"));
219 if (renderStyle.equalsIgnoreCase("text"))
221 accurateText = false;
226 pg = new EpsGraphics2D(title, out, 0, 0, width, height);
228 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
229 RenderingHints.VALUE_ANTIALIAS_ON);
231 pg.setAccurateTextMode(accurateText);
234 setProgressMessage(MessageManager.formatMessage(
235 "status.export_complete", type.getName()));
236 } catch (Exception ex)
241 void setupPNG(int width, int height)
243 bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
244 graphics = bi.getGraphics();
245 Graphics2D ig2 = (Graphics2D) graphics;
246 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
247 RenderingHints.VALUE_ANTIALIAS_ON);
248 setProgressMessage(MessageManager.formatMessage(
249 "status.export_complete", type.getName()));
253 void setupSVG(int width, int height, String title)
256 g2 = new SVGGraphics2D(width, height);
259 String renderStyle = jalview.bin.Cache.getDefault("SVG_RENDERING",
262 // If we need to prompt, and if the GUI is visible then
263 // Prompt for EPS rendering style
264 if (renderStyle.equalsIgnoreCase("Prompt each time")
265 && !(System.getProperty("java.awt.headless") != null && System
266 .getProperty("java.awt.headless").equals("true")))
268 SVGOptions svgOption = new SVGOptions();
269 renderStyle = svgOption.getValue();
271 if (renderStyle == null || svgOption.cancelled)
273 setProgressMessage(MessageManager.formatMessage(
274 "status.cancelled_image_export_operation", "SVG"));
279 if (renderStyle.equalsIgnoreCase("Lineart"))
281 ig2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
282 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
285 setProgressMessage(MessageManager.formatMessage(
286 "status.export_complete", type.getName()));
290 static JalviewFileChooser getPNGChooser()
292 if (Jalview.isHeadlessMode())
296 return new jalview.io.JalviewFileChooser(
297 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
298 new String[] { "png" },
299 new String[] { "Portable network graphics" },
300 "Portable network graphics");
303 static JalviewFileChooser getEPSChooser()
305 if (Jalview.isHeadlessMode())
309 return new jalview.io.JalviewFileChooser(
310 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
311 new String[] { "eps" },
312 new String[] { "Encapsulated Postscript" },
313 "Encapsulated Postscript");
316 private void setProgressMessage(String message)
318 if (pIndicator != null && !headless)
320 pIndicator.setProgressBar(message, pSessionId);
324 static JalviewFileChooser getSVGChooser()
326 if (Jalview.isHeadlessMode())
330 return new jalview.io.JalviewFileChooser(
331 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
332 new String[] { "svg" },
333 new String[] { "Scalable Vector Graphics" },
334 "Scalable Vector Graphics");