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;
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"), EPS_EXTENSION,
83 PNG("PNG", MessageManager.getString("label.png_image"), PNG_EXTENSION,
85 SVG("SVG", "SVG", SVG_EXTENSION, SVG_DESCRIPTION);
87 public final String name;
89 public final String label;
91 public final String extension;
93 public final String description;
95 TYPE(String name, String label, String ext, String desc)
100 this.description = desc;
103 public String getName()
108 public JalviewFileChooser getChooser()
110 return new JalviewFileChooser(extension, description);
113 public String getLabel()
121 * Constructor builds the image and writes it to file. If the supplied file
122 * name is null, the user is prompted for the output file.
135 public ImageMaker(Component parent, TYPE type, String title, int width,
136 int height, File file, String fileTitle,
137 IProgressIndicator pIndicator, long pSessionId, boolean headless)
139 this.pIndicator = pIndicator;
141 this.pSessionId = pSessionId;
142 this.headless = headless;
143 if (file == null && !Jalview.isHeadlessMode())
145 setProgressMessage(MessageManager.formatMessage(
146 "status.waiting_for_user_to_select_output_file", type.name));
147 JalviewFileChooser chooser;
148 chooser = type.getChooser();
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));
166 // TODO JAL-3048 refactor to method called by constructor or callback
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()
203 public void writeImage()
214 String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
215 out.write(svgData.getBytes());
220 ImageIO.write(bi, PNG_EXTENSION, out);
225 } catch (Exception ex)
227 ex.printStackTrace();
231 void setupEPS(int width, int height, String title)
233 boolean accurateText = true;
235 String renderStyle = Cache.getDefault("EPS_RENDERING",
238 // If we need to prompt, and if the GUI is visible then
239 // Prompt for EPS rendering style
240 if (renderStyle.equalsIgnoreCase("Prompt each time")
241 && !(System.getProperty("java.awt.headless") != null && System
242 .getProperty("java.awt.headless").equals("true")))
244 LineartOptions eps = new LineartOptions("EPS_RENDERING", "EPS");
245 renderStyle = eps.getValue();
247 if (renderStyle == null || eps.cancelled)
249 setProgressMessage(MessageManager.formatMessage(
250 "status.cancelled_image_export_operation", "EPS"));
255 if (renderStyle.equalsIgnoreCase("text"))
257 accurateText = false;
262 pg = new EpsGraphics2D(title, out, 0, 0, width, height);
264 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
265 RenderingHints.VALUE_ANTIALIAS_ON);
267 pg.setAccurateTextMode(accurateText);
270 setProgressMessage(MessageManager
271 .formatMessage("status.export_complete", type.getName()));
272 } catch (Exception ex)
274 System.err.println("Error writing PNG: " + ex.toString());
278 void setupPNG(int width, int height)
280 bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
281 graphics = bi.getGraphics();
282 Graphics2D ig2 = (Graphics2D) graphics;
283 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
284 RenderingHints.VALUE_ANTIALIAS_ON);
285 setProgressMessage(MessageManager
286 .formatMessage("status.export_complete", type.getName()));
290 void setupSVG(int width, int height, String title)
293 g2 = new SVGGraphics2D(width, height);
296 String renderStyle = jalview.bin.Cache.getDefault("SVG_RENDERING",
299 // If we need to prompt, and if the GUI is visible then
300 // Prompt for EPS rendering style
301 if (renderStyle.equalsIgnoreCase("Prompt each time")
302 && !(System.getProperty("java.awt.headless") != null && System
303 .getProperty("java.awt.headless").equals("true")))
305 LineartOptions svgOption = new LineartOptions("SVG_RENDERING", "SVG");
306 renderStyle = svgOption.getValue();
308 if (renderStyle == null || svgOption.cancelled)
310 setProgressMessage(MessageManager.formatMessage(
311 "status.cancelled_image_export_operation", "SVG"));
316 if (renderStyle.equalsIgnoreCase("Lineart"))
318 ig2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
319 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
322 setProgressMessage(MessageManager
323 .formatMessage("status.export_complete", type.getName()));
327 static JalviewFileChooser getPNGChooser()
329 if (Jalview.isHeadlessMode())
333 return new JalviewFileChooser(PNG_EXTENSION, PNG_DESCRIPTION);
336 static JalviewFileChooser getEPSChooser()
338 if (Jalview.isHeadlessMode())
342 return new JalviewFileChooser(EPS_EXTENSION, EPS_DESCRIPTION);
345 private void setProgressMessage(String message)
347 if (pIndicator != null && !headless)
349 pIndicator.setProgressBar(message, pSessionId);
353 static JalviewFileChooser getSVGChooser()
355 if (Jalview.isHeadlessMode())
359 return new JalviewFileChooser(SVG_EXTENSION, SVG_DESCRIPTION);