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"),
84 SVG("SVG", "SVG", getSVGChooser());
86 private JalviewFileChooser chooser;
92 TYPE(String name, String label, JalviewFileChooser chooser)
96 this.chooser = chooser;
99 public String getName()
104 public JalviewFileChooser getChooser()
109 public String getLabel()
116 public ImageMaker(Component parent, TYPE type, String title, int width,
117 int height, File file, String fileTitle,
118 IProgressIndicator pIndicator, long pSessionId, boolean headless)
120 this.pIndicator = pIndicator;
122 this.pSessionId = pSessionId;
123 this.headless = headless;
126 // TODO: JAL-3048 export SVG/EPS/PNG- not required for Jalview-JS
128 setProgressMessage(MessageManager.formatMessage(
129 "status.waiting_for_user_to_select_output_file", type.name));
130 JalviewFileChooser chooser;
131 chooser = type.getChooser();
132 chooser.setFileView(new jalview.io.JalviewFileView());
133 chooser.setDialogTitle(title);
134 chooser.setToolTipText(MessageManager.getString("action.save"));
135 int value = chooser.showSaveDialog(parent);
137 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
139 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
140 chooser.getSelectedFile().getParent());
141 file = chooser.getSelectedFile();
145 setProgressMessage(MessageManager.formatMessage(
146 "status.cancelled_image_export_operation", type.name));
149 // TODO JAL-3048 refactor to method called by constructor or callback
154 out = new FileOutputStream(file);
155 setProgressMessage(null);
156 setProgressMessage(MessageManager.formatMessage(
157 "status.exporting_alignment_as_x_file", type.getName()));
158 if (type == TYPE.SVG)
160 setupSVG(width, height, fileTitle);
162 else if (type == TYPE.EPS)
164 setupEPS(width, height, fileTitle);
166 else if (type == TYPE.PNG)
168 setupPNG(width, height);
171 } catch (Exception ex)
173 System.out.println("Error creating " + type.getName() + " file.");
175 setProgressMessage(MessageManager
176 .formatMessage("info.error_creating_file", type.getName()));
181 public Graphics getGraphics()
186 public void writeImage()
197 String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
198 out.write(svgData.getBytes());
203 ImageIO.write(bi, PNG_EXTENSION, out);
208 } catch (Exception ex)
210 ex.printStackTrace();
214 void setupEPS(int width, int height, String title)
216 boolean accurateText = true;
218 String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
221 // If we need to prompt, and if the GUI is visible then
222 // Prompt for EPS rendering style
223 if (renderStyle.equalsIgnoreCase("Prompt each time")
224 && !(System.getProperty("java.awt.headless") != null && System
225 .getProperty("java.awt.headless").equals("true")))
227 EPSOptions eps = new EPSOptions();
228 renderStyle = eps.getValue();
230 if (renderStyle == null || eps.cancelled)
232 setProgressMessage(MessageManager.formatMessage(
233 "status.cancelled_image_export_operation", "EPS"));
238 if (renderStyle.equalsIgnoreCase("text"))
240 accurateText = false;
245 pg = new EpsGraphics2D(title, out, 0, 0, width, height);
247 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
248 RenderingHints.VALUE_ANTIALIAS_ON);
250 pg.setAccurateTextMode(accurateText);
253 setProgressMessage(MessageManager
254 .formatMessage("status.export_complete", type.getName()));
255 } catch (Exception ex)
260 void setupPNG(int width, int height)
262 bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
263 graphics = bi.getGraphics();
264 Graphics2D ig2 = (Graphics2D) graphics;
265 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
266 RenderingHints.VALUE_ANTIALIAS_ON);
267 setProgressMessage(MessageManager
268 .formatMessage("status.export_complete", type.getName()));
272 void setupSVG(int width, int height, String title)
275 g2 = new SVGGraphics2D(width, height);
278 String renderStyle = jalview.bin.Cache.getDefault("SVG_RENDERING",
281 // If we need to prompt, and if the GUI is visible then
282 // Prompt for EPS rendering style
283 if (renderStyle.equalsIgnoreCase("Prompt each time")
284 && !(System.getProperty("java.awt.headless") != null && System
285 .getProperty("java.awt.headless").equals("true")))
287 SVGOptions svgOption = new SVGOptions();
288 renderStyle = svgOption.getValue();
290 if (renderStyle == null || svgOption.cancelled)
292 setProgressMessage(MessageManager.formatMessage(
293 "status.cancelled_image_export_operation", "SVG"));
298 if (renderStyle.equalsIgnoreCase("Lineart"))
300 ig2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
301 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
304 setProgressMessage(MessageManager
305 .formatMessage("status.export_complete", type.getName()));
309 static JalviewFileChooser getPNGChooser()
311 if (Jalview.isHeadlessMode())
315 return new JalviewFileChooser(PNG_EXTENSION, PNG_DESCRIPTION);
318 static JalviewFileChooser getEPSChooser()
320 if (Jalview.isHeadlessMode())
324 return new JalviewFileChooser(EPS_EXTENSION, EPS_DESCRIPTION);
327 private void setProgressMessage(String message)
329 if (pIndicator != null && !headless)
331 pIndicator.setProgressBar(message, pSessionId);
335 static JalviewFileChooser getSVGChooser()
337 if (Jalview.isHeadlessMode())
341 return new JalviewFileChooser(SVG_EXTENSION, SVG_DESCRIPTION);