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
45 public static final String SVG_DESCRIPTION = "Scalable Vector Graphics";
47 public static final String SVG_EXTENSION = "svg";
49 public static final String EPS_DESCRIPTION = "Encapsulated Postscript";
51 public static final String EPS_EXTENSION = "eps";
53 public static final String PNG_EXTENSION = "png";
55 public static final String PNG_DESCRIPTION = "Portable network graphics";
57 public static final String HTML_EXTENSION = "html";
59 public static final String HTML_DESCRIPTION = "Hypertext Markup Language";
73 private IProgressIndicator pIndicator;
75 private long pSessionId;
77 private boolean headless;
81 EPS("EPS", MessageManager.getString("label.eps_file"), getEPSChooser()),
82 PNG("PNG", MessageManager.getString("label.png_image"), getPNGChooser()),
83 SVG("SVG", "SVG", getSVGChooser());
85 private JalviewFileChooser chooser;
91 TYPE(String name, String label, JalviewFileChooser chooser)
95 this.chooser = chooser;
98 public String getName()
103 public JalviewFileChooser getChooser()
108 public String getLabel()
115 public ImageMaker(Component parent, TYPE type, String title, int width,
116 int height, File file, String fileTitle,
117 IProgressIndicator pIndicator, long pSessionId, boolean headless)
119 this.pIndicator = pIndicator;
121 this.pSessionId = pSessionId;
122 this.headless = headless;
125 setProgressMessage(MessageManager.formatMessage(
126 "status.waiting_for_user_to_select_output_file", type.name));
127 JalviewFileChooser chooser;
128 chooser = type.getChooser();
129 chooser.setFileView(new jalview.io.JalviewFileView());
130 chooser.setDialogTitle(title);
131 chooser.setToolTipText(MessageManager.getString("action.save"));
132 int value = chooser.showSaveDialog(parent);
134 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
136 jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
137 .getSelectedFile().getParent());
138 file = chooser.getSelectedFile();
142 setProgressMessage(MessageManager.formatMessage(
143 "status.cancelled_image_export_operation", type.name));
151 out = new FileOutputStream(file);
152 setProgressMessage(null);
153 setProgressMessage(MessageManager.formatMessage(
154 "status.exporting_alignment_as_x_file", type.getName()));
155 if (type == TYPE.SVG)
157 setupSVG(width, height, fileTitle);
159 else if (type == TYPE.EPS)
161 setupEPS(width, height, fileTitle);
163 else if (type == TYPE.PNG)
165 setupPNG(width, height);
168 } catch (Exception ex)
170 System.out.println("Error creating " + type.getName() + " file.");
172 setProgressMessage(MessageManager.formatMessage(
173 "info.error_creating_file", type.getName()));
178 public Graphics getGraphics()
183 public void writeImage()
194 String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
195 out.write(svgData.getBytes());
200 ImageIO.write(bi, PNG_EXTENSION, out);
205 } catch (Exception ex)
207 ex.printStackTrace();
211 void setupEPS(int width, int height, String title)
213 boolean accurateText = true;
215 String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
218 // If we need to prompt, and if the GUI is visible then
219 // Prompt for EPS rendering style
220 if (renderStyle.equalsIgnoreCase("Prompt each time")
221 && !(System.getProperty("java.awt.headless") != null && System
222 .getProperty("java.awt.headless").equals("true")))
224 EPSOptions eps = new EPSOptions();
225 renderStyle = eps.getValue();
227 if (renderStyle == null || eps.cancelled)
229 setProgressMessage(MessageManager.formatMessage(
230 "status.cancelled_image_export_operation", "EPS"));
235 if (renderStyle.equalsIgnoreCase("text"))
237 accurateText = false;
242 pg = new EpsGraphics2D(title, out, 0, 0, width, height);
244 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
245 RenderingHints.VALUE_ANTIALIAS_ON);
247 pg.setAccurateTextMode(accurateText);
250 setProgressMessage(MessageManager.formatMessage(
251 "status.export_complete", type.getName()));
252 } catch (Exception ex)
257 void setupPNG(int width, int height)
259 bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
260 graphics = bi.getGraphics();
261 Graphics2D ig2 = (Graphics2D) graphics;
262 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
263 RenderingHints.VALUE_ANTIALIAS_ON);
264 setProgressMessage(MessageManager.formatMessage(
265 "status.export_complete", type.getName()));
269 void setupSVG(int width, int height, String title)
272 g2 = new SVGGraphics2D(width, height);
275 String renderStyle = jalview.bin.Cache.getDefault("SVG_RENDERING",
278 // If we need to prompt, and if the GUI is visible then
279 // Prompt for EPS rendering style
280 if (renderStyle.equalsIgnoreCase("Prompt each time")
281 && !(System.getProperty("java.awt.headless") != null && System
282 .getProperty("java.awt.headless").equals("true")))
284 SVGOptions svgOption = new SVGOptions();
285 renderStyle = svgOption.getValue();
287 if (renderStyle == null || svgOption.cancelled)
289 setProgressMessage(MessageManager.formatMessage(
290 "status.cancelled_image_export_operation", "SVG"));
295 if (renderStyle.equalsIgnoreCase("Lineart"))
297 ig2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
298 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
301 setProgressMessage(MessageManager.formatMessage(
302 "status.export_complete", type.getName()));
306 static JalviewFileChooser getPNGChooser()
308 if (Jalview.isHeadlessMode())
312 return new JalviewFileChooser(PNG_EXTENSION, PNG_DESCRIPTION);
315 static JalviewFileChooser getEPSChooser()
317 if (Jalview.isHeadlessMode())
321 return new JalviewFileChooser(EPS_EXTENSION, EPS_DESCRIPTION);
324 private void setProgressMessage(String message)
326 if (pIndicator != null && !headless)
328 pIndicator.setProgressBar(message, pSessionId);
332 static JalviewFileChooser getSVGChooser()
334 if (Jalview.isHeadlessMode())
338 return new JalviewFileChooser(SVG_EXTENSION, SVG_DESCRIPTION);