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.IProgressIndicator;
25 import jalview.gui.LineartOptions;
26 import jalview.io.JalviewFileChooser;
28 import java.awt.Component;
29 import java.awt.Graphics;
30 import java.awt.Graphics2D;
31 import java.awt.RenderingHints;
32 import java.awt.image.BufferedImage;
34 import java.io.FileOutputStream;
36 import javax.imageio.ImageIO;
38 import org.jfree.graphics2d.svg.SVGGraphics2D;
39 import org.jfree.graphics2d.svg.SVGHints;
40 import org.jibble.epsgraphics.EpsGraphics2D;
42 public class ImageMaker
44 public static final String SVG_DESCRIPTION = "Scalable Vector Graphics";
46 public static final String SVG_EXTENSION = "svg";
48 public static final String EPS_DESCRIPTION = "Encapsulated Postscript";
50 public static final String EPS_EXTENSION = "eps";
52 public static final String PNG_EXTENSION = "png";
54 public static final String PNG_DESCRIPTION = "Portable network graphics";
56 public static final String HTML_EXTENSION = "html";
58 public static final String HTML_DESCRIPTION = "Hypertext Markup Language";
72 private IProgressIndicator pIndicator;
74 private long pSessionId;
76 private boolean headless;
80 EPS("EPS", MessageManager.getString("label.eps_file"), getEPSChooser()),
81 PNG("PNG", MessageManager.getString("label.png_image"),
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 // TODO: JAL-3048 export SVG/EPS/PNG- not required for Jalview-JS
127 setProgressMessage(MessageManager.formatMessage(
128 "status.waiting_for_user_to_select_output_file", type.name));
129 JalviewFileChooser chooser;
130 chooser = type.getChooser();
131 chooser.setFileView(new jalview.io.JalviewFileView());
132 chooser.setDialogTitle(title);
133 chooser.setToolTipText(MessageManager.getString("action.save"));
134 int value = chooser.showSaveDialog(parent);
136 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
138 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
139 chooser.getSelectedFile().getParent());
140 file = chooser.getSelectedFile();
144 setProgressMessage(MessageManager.formatMessage(
145 "status.cancelled_image_export_operation", type.name));
148 // TODO JAL-3048 refactor to method called by constructor or callback
153 out = new FileOutputStream(file);
154 setProgressMessage(null);
155 setProgressMessage(MessageManager.formatMessage(
156 "status.exporting_alignment_as_x_file", type.getName()));
157 if (type == TYPE.SVG)
159 setupSVG(width, height, fileTitle);
161 else if (type == TYPE.EPS)
163 setupEPS(width, height, fileTitle);
165 else if (type == TYPE.PNG)
167 setupPNG(width, height);
170 } catch (Exception ex)
172 System.out.println("Error creating " + type.getName() + " file.");
174 setProgressMessage(MessageManager
175 .formatMessage("info.error_creating_file", type.getName()));
180 public Graphics getGraphics()
185 public void writeImage()
196 String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
197 out.write(svgData.getBytes());
202 ImageIO.write(bi, PNG_EXTENSION, out);
207 } catch (Exception ex)
209 ex.printStackTrace();
213 void setupEPS(int width, int height, String title)
215 boolean accurateText = true;
217 String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
220 // If we need to prompt, and if the GUI is visible then
221 // Prompt for EPS rendering style
222 if (renderStyle.equalsIgnoreCase("Prompt each time")
223 && !(System.getProperty("java.awt.headless") != null && System
224 .getProperty("java.awt.headless").equals("true")))
226 LineartOptions eps = new LineartOptions("EPS_RENDERING", "EPS");
227 renderStyle = eps.getValue();
229 if (renderStyle == null || eps.cancelled)
231 setProgressMessage(MessageManager.formatMessage(
232 "status.cancelled_image_export_operation", "EPS"));
237 if (renderStyle.equalsIgnoreCase("text"))
239 accurateText = false;
244 pg = new EpsGraphics2D(title, out, 0, 0, width, height);
246 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
247 RenderingHints.VALUE_ANTIALIAS_ON);
249 pg.setAccurateTextMode(accurateText);
252 setProgressMessage(MessageManager
253 .formatMessage("status.export_complete", type.getName()));
254 } catch (Exception ex)
259 void setupPNG(int width, int height)
261 bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
262 graphics = bi.getGraphics();
263 Graphics2D ig2 = (Graphics2D) graphics;
264 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
265 RenderingHints.VALUE_ANTIALIAS_ON);
266 setProgressMessage(MessageManager
267 .formatMessage("status.export_complete", type.getName()));
271 void setupSVG(int width, int height, String title)
274 g2 = new SVGGraphics2D(width, height);
277 String renderStyle = jalview.bin.Cache.getDefault("SVG_RENDERING",
280 // If we need to prompt, and if the GUI is visible then
281 // Prompt for EPS rendering style
282 if (renderStyle.equalsIgnoreCase("Prompt each time")
283 && !(System.getProperty("java.awt.headless") != null && System
284 .getProperty("java.awt.headless").equals("true")))
286 LineartOptions svgOption = new LineartOptions("SVG_RENDERING", "SVG");
287 renderStyle = svgOption.getValue();
289 if (renderStyle == null || svgOption.cancelled)
291 setProgressMessage(MessageManager.formatMessage(
292 "status.cancelled_image_export_operation", "SVG"));
297 if (renderStyle.equalsIgnoreCase("Lineart"))
299 ig2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
300 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
303 setProgressMessage(MessageManager
304 .formatMessage("status.export_complete", type.getName()));
308 static JalviewFileChooser getPNGChooser()
310 if (Jalview.isHeadlessMode())
314 return new JalviewFileChooser(PNG_EXTENSION, PNG_DESCRIPTION);
317 static JalviewFileChooser getEPSChooser()
319 if (Jalview.isHeadlessMode())
323 return new JalviewFileChooser(EPS_EXTENSION, EPS_DESCRIPTION);
326 private void setProgressMessage(String message)
328 if (pIndicator != null && !headless)
330 pIndicator.setProgressBar(message, pSessionId);
334 static JalviewFileChooser getSVGChooser()
336 if (Jalview.isHeadlessMode())
340 return new JalviewFileChooser(SVG_EXTENSION, SVG_DESCRIPTION);