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.Cache;
24 import jalview.bin.Jalview;
25 import jalview.gui.IProgressIndicator;
26 import jalview.gui.LineartOptions;
27 import jalview.io.JalviewFileChooser;
28 import jalview.util.dialogrunner.RunResponse;
30 import java.awt.Component;
31 import java.awt.Graphics;
32 import java.awt.Graphics2D;
33 import java.awt.RenderingHints;
34 import java.awt.image.BufferedImage;
36 import java.io.FileOutputStream;
37 import java.util.concurrent.atomic.AtomicBoolean;
39 import javax.imageio.ImageIO;
40 import javax.swing.JOptionPane;
42 import org.jfree.graphics2d.svg.SVGGraphics2D;
43 import org.jfree.graphics2d.svg.SVGHints;
44 import org.jibble.epsgraphics.EpsGraphics2D;
46 public class ImageMaker
48 public static final String SVG_DESCRIPTION = "Scalable Vector Graphics";
50 public static final String SVG_EXTENSION = "svg";
52 public static final String EPS_DESCRIPTION = "Encapsulated Postscript";
54 public static final String EPS_EXTENSION = "eps";
56 public static final String PNG_EXTENSION = "png";
58 public static final String PNG_DESCRIPTION = "Portable network graphics";
60 public static final String HTML_EXTENSION = "html";
62 public static final String HTML_DESCRIPTION = "Hypertext Markup Language";
74 private IProgressIndicator pIndicator;
76 private long pSessionId;
78 private boolean headless;
82 EPS("EPS", MessageManager.getString("label.eps_file"), EPS_EXTENSION,
84 PNG("PNG", MessageManager.getString("label.png_image"), PNG_EXTENSION,
86 SVG("SVG", "SVG", SVG_EXTENSION, SVG_DESCRIPTION);
88 public final String name;
90 public final String label;
92 public final String extension;
94 public final String description;
96 TYPE(String name, String label, String ext, String desc)
100 this.extension = ext;
101 this.description = desc;
104 public String getName()
109 public JalviewFileChooser getFileChooser()
111 return new JalviewFileChooser(extension, description);
114 public String getLabel()
122 * Constructor builds the image and writes it to file. If the supplied file
123 * name is null, the user is prompted for the output file.
136 public ImageMaker(Component parent, TYPE type, String title, int width,
137 int height, File file, String fileTitle,
138 IProgressIndicator pIndicator, long pSessionId, boolean headless)
140 this.pIndicator = pIndicator;
142 this.pSessionId = pSessionId;
143 this.headless = headless;
144 if (file == null && !Jalview.isHeadlessMode())
146 setProgressMessage(MessageManager.formatMessage(
147 "status.waiting_for_user_to_select_output_file", type.name));
148 JalviewFileChooser chooser = type.getFileChooser();
149 chooser.setFileView(new jalview.io.JalviewFileView());
150 chooser.setDialogTitle(title);
151 chooser.setToolTipText(MessageManager.getString("action.save"));
152 int value = chooser.showSaveDialog(parent);
154 if (value == JalviewFileChooser.APPROVE_OPTION)
156 Cache.setProperty("LAST_DIRECTORY",
157 chooser.getSelectedFile().getParent());
158 file = chooser.getSelectedFile();
162 setProgressMessage(MessageManager.formatMessage(
163 "status.cancelled_image_export_operation", type.name));
171 out = new FileOutputStream(file);
172 setProgressMessage(null);
173 setProgressMessage(MessageManager.formatMessage(
174 "status.exporting_alignment_as_x_file", type.getName()));
175 if (type == TYPE.SVG)
177 setupSVG(width, height, fileTitle);
179 else if (type == TYPE.EPS)
181 setupEPS(width, height, fileTitle);
183 else if (type == TYPE.PNG)
185 setupPNG(width, height);
188 } catch (Exception ex)
190 System.out.println("Error creating " + type.getName() + " file.");
192 setProgressMessage(MessageManager
193 .formatMessage("info.error_creating_file", type.getName()));
198 public Graphics getGraphics()
204 * For SVG or PNG, writes the generated graphics data to the file output
205 * stream. For EPS, flushes the output graphics (which is written to file as
208 public void writeImage()
219 String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
220 out.write(svgData.getBytes());
225 ImageIO.write(bi, PNG_EXTENSION, out);
230 } catch (Exception ex)
232 ex.printStackTrace();
237 * Generates an EPS image and writes it to the (previously set) output buffer.
238 * The user is first prompted for choice of Text or Lineart rendering, unless
239 * a preference for this has been set.
245 void setupEPS(int width, int height, String title)
247 String renderStyle = Cache.getDefault("EPS_RENDERING",
249 AtomicBoolean textOption = new AtomicBoolean(
250 !"Lineart".equals(renderStyle));
253 * configure the action to run on OK in the dialog
255 RunResponse okAction = new RunResponse(JOptionPane.OK_OPTION)
260 writeEPS(width, height, title, textOption.get());
265 * Prompt for character rendering style if preference is not set
267 if (renderStyle.equalsIgnoreCase("Prompt each time")
268 && !(System.getProperty("java.awt.headless") != null && System
269 .getProperty("java.awt.headless").equals("true")))
271 LineartOptions epsOption = new LineartOptions("EPS_RENDERING",
272 TYPE.EPS.getName(), textOption);
273 epsOption.setResponseAction(new RunResponse(JOptionPane.NO_OPTION)
278 setProgressMessage(MessageManager.formatMessage(
279 "status.cancelled_image_export_operation", "EPS"));
282 epsOption.setResponseAction(okAction);
283 epsOption.showDialog();
284 /* no code here - JalviewJS cannot execute it */
289 * else (if preference set) just do the export action
291 writeEPS(width, height, title, textOption.get());
296 * Sets up a graphics object for the PNG image to be written on
301 void setupPNG(int width, int height)
303 bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
304 graphics = bi.getGraphics();
305 Graphics2D ig2 = (Graphics2D) graphics;
306 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
307 RenderingHints.VALUE_ANTIALIAS_ON);
308 setProgressMessage(MessageManager
309 .formatMessage("status.export_complete", type.getName()));
313 * Sets up a graphics object for the SVG image to be written on. The user is
314 * first prompted for choice of Text or Lineart rendering, unless a preference
315 * for this has been set.
321 void setupSVG(int width, int height, String title)
323 String renderStyle = Cache.getDefault("SVG_RENDERING",
325 AtomicBoolean textOption = new AtomicBoolean(
326 !"Lineart".equals(renderStyle));
329 * configure the action to run on OK in the dialog
331 RunResponse okAction = new RunResponse(JOptionPane.OK_OPTION)
336 setupSVG(width, height, textOption.get());
341 * Prompt for character rendering style if preference is not set
343 if (renderStyle.equalsIgnoreCase("Prompt each time")
344 && !(System.getProperty("java.awt.headless") != null && System
345 .getProperty("java.awt.headless").equals("true")))
347 LineartOptions svgOption = new LineartOptions("SVG_RENDERING",
348 TYPE.SVG.getName(), textOption);
349 svgOption.setResponseAction(new RunResponse(JOptionPane.NO_OPTION)
354 setProgressMessage(MessageManager.formatMessage(
355 "status.cancelled_image_export_operation", "SVG"));
358 svgOption.setResponseAction(okAction);
359 svgOption.showDialog();
360 /* no code here - JalviewJS cannot execute it */
365 * else (if preference set) just do the export action
367 setupSVG(width, height, textOption.get());
371 void setProgressMessage(String message)
373 if (pIndicator != null && !headless)
375 pIndicator.setProgressBar(message, pSessionId);
380 * A helper method to configure the SVG output graphics, with choice of Text
381 * or Lineart character rendering
386 * true for Text, false for Lineart
388 protected void setupSVG(int width, int height, boolean textOption)
390 SVGGraphics2D g2 = new SVGGraphics2D(width, height);
391 if (!textOption) // Lineart selected
393 g2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
394 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
396 setProgressMessage(MessageManager
397 .formatMessage("status.export_complete", type.getName()));
402 * A helper method that sets up the EPS graphics output with user choice of
403 * Text or Lineart character rendering
409 * true for Text, false for Lineart
411 protected void writeEPS(int width, int height, String title,
416 pg = new EpsGraphics2D(title, out, 0, 0, width, height);
418 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
419 RenderingHints.VALUE_ANTIALIAS_ON);
420 pg.setAccurateTextMode(!textOption); // true = Lineart
422 setProgressMessage(MessageManager
423 .formatMessage("status.export_complete", type.getName()));
424 } catch (Exception ex)
426 System.err.println("Error writing PNG: " + ex.toString());
430 static JalviewFileChooser getSVGChooser()
432 if (Jalview.isHeadlessMode())
436 return new JalviewFileChooser(SVG_EXTENSION, SVG_DESCRIPTION);