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
59 EPS("EPS", MessageManager.getString("label.eps_file"), getEPSChooser()), PNG(
60 "PNG", MessageManager.getString("label.png_image"),
61 getPNGChooser()), SVG("SVG", "SVG", getSVGChooser());
63 private JalviewFileChooser chooser;
69 TYPE(String name, String label, JalviewFileChooser chooser)
73 this.chooser = chooser;
76 public String getName()
81 public JalviewFileChooser getChooser()
86 public String getLabel()
93 public ImageMaker(Component parent, TYPE type, String title, int width,
94 int height, File file, String fileTitle,
95 IProgressIndicator pIndicator, long pSessionId, boolean headless)
101 if (pIndicator != null && !headless)
103 pIndicator.setProgressBar(
104 MessageManager.formatMessage(
105 "status.waiting_for_user_to_select_output_file",
106 type.name), pSessionId);
108 JalviewFileChooser chooser;
109 chooser = type.getChooser();
110 chooser.setFileView(new jalview.io.JalviewFileView());
111 chooser.setDialogTitle(title);
112 chooser.setToolTipText(MessageManager.getString("action.save"));
113 int value = chooser.showSaveDialog(parent);
115 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
117 jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
118 .getSelectedFile().getParent());
119 file = chooser.getSelectedFile();
123 if (pIndicator != null && !headless)
125 pIndicator.setProgressBar(MessageManager.formatMessage(
126 "status.cancelled_image_export_operation", type.name),
136 out = new FileOutputStream(file);
137 if (type == TYPE.SVG)
139 setupSVG(width, height, fileTitle);
141 else if (type == TYPE.EPS)
143 setupEPS(width, height, fileTitle);
145 else if (type == TYPE.PNG)
147 setupPNG(width, height);
149 if (pIndicator != null && !headless)
151 pIndicator.setProgressBar(
152 MessageManager.formatMessage(
153 "status.export_complete", type.getName()),
156 } catch (Exception ex)
158 System.out.println("Error creating " + type.getName() + " file.");
160 pIndicator.setProgressBar(MessageManager.formatMessage(
161 "info.error_creating_file", type.getName()), pSessionId);
166 public Graphics getGraphics()
171 public void writeImage()
182 String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
183 out.write(svgData.getBytes());
188 ImageIO.write(bi, "png", out);
193 } catch (Exception ex)
195 ex.printStackTrace();
199 void setupEPS(int width, int height, String title)
201 boolean accurateText = true;
203 String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
206 // If we need to prompt, and if the GUI is visible then
207 // Prompt for EPS rendering style
208 if (renderStyle.equalsIgnoreCase("Prompt each time")
209 && !(System.getProperty("java.awt.headless") != null && System
210 .getProperty("java.awt.headless").equals("true")))
212 EPSOptions eps = new EPSOptions();
213 renderStyle = eps.getValue();
215 if (renderStyle == null || eps.cancelled)
221 if (renderStyle.equalsIgnoreCase("text"))
223 accurateText = false;
228 pg = new EpsGraphics2D(title, out, 0, 0, width, height);
230 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
231 RenderingHints.VALUE_ANTIALIAS_ON);
233 pg.setAccurateTextMode(accurateText);
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);
251 void setupSVG(int width, int height, String title)
254 g2 = new SVGGraphics2D(width, height);
257 String renderStyle = jalview.bin.Cache.getDefault("SVG_RENDERING",
260 // If we need to prompt, and if the GUI is visible then
261 // Prompt for EPS rendering style
262 if (renderStyle.equalsIgnoreCase("Prompt each time")
263 && !(System.getProperty("java.awt.headless") != null && System
264 .getProperty("java.awt.headless").equals("true")))
266 SVGOptions svgOption = new SVGOptions();
267 renderStyle = svgOption.getValue();
269 if (renderStyle == null || svgOption.cancelled)
275 if (renderStyle.equalsIgnoreCase("lineart"))
277 ig2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
278 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
284 static JalviewFileChooser getPNGChooser()
286 if (Jalview.isHeadlessMode())
290 return new jalview.io.JalviewFileChooser(
291 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
292 new String[] { "png" },
293 new String[] { "Portable network graphics" },
294 "Portable network graphics");
297 static JalviewFileChooser getEPSChooser()
299 if (Jalview.isHeadlessMode())
303 return new jalview.io.JalviewFileChooser(
304 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
305 new String[] { "eps" },
306 new String[] { "Encapsulated Postscript" },
307 "Encapsulated Postscript");
310 static JalviewFileChooser getSVGChooser()
312 if (Jalview.isHeadlessMode())
316 return new jalview.io.JalviewFileChooser(
317 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
318 new String[] { "svg" },
319 new String[] { "Scalable Vector Graphics" },
320 "Scalable Vector Graphics");