JAL-1976 added an intermediate message between a user selecting an export file and...
[jalview.git] / src / jalview / util / ImageMaker.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.util;
22
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;
28
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;
34 import java.io.File;
35 import java.io.FileOutputStream;
36
37 import javax.imageio.ImageIO;
38
39 import org.jfree.graphics2d.svg.SVGGraphics2D;
40 import org.jfree.graphics2d.svg.SVGHints;
41 import org.jibble.epsgraphics.EpsGraphics2D;
42
43 public class ImageMaker
44 {
45   EpsGraphics2D pg;
46
47   SVGGraphics2D g2;
48
49   Graphics graphics;
50
51   FileOutputStream out;
52
53   BufferedImage bi;
54
55   TYPE type;
56
57   public enum TYPE
58   {
59     EPS("EPS", MessageManager.getString("label.eps_file"), getEPSChooser()), PNG(
60             "PNG", MessageManager.getString("label.png_image"),
61             getPNGChooser()), SVG("SVG", "SVG", getSVGChooser());
62
63     private JalviewFileChooser chooser;
64
65     private String name;
66
67     private String label;
68
69     TYPE(String name, String label, JalviewFileChooser chooser)
70     {
71       this.name = name;
72       this.label = label;
73       this.chooser = chooser;
74     }
75
76     public String getName()
77     {
78       return name;
79     }
80
81     public JalviewFileChooser getChooser()
82     {
83       return chooser;
84     }
85
86     public String getLabel()
87     {
88       return label;
89     }
90
91   }
92
93   public ImageMaker(Component parent, TYPE type, String title, int width,
94           int height, File file, String fileTitle,
95           IProgressIndicator pIndicator, long pSessionId, boolean headless)
96   {
97     this.type = type;
98
99     if (file == null)
100     {
101       if (pIndicator != null && !headless)
102       {
103         pIndicator.setProgressBar(
104                 MessageManager.formatMessage(
105                         "status.waiting_for_user_to_select_output_file",
106                         type.name), pSessionId);
107       }
108       JalviewFileChooser chooser;
109       chooser = type.getChooser();
110       chooser.setFileView(new jalview.io.JalviewFileView());
111       chooser.setDialogTitle(title);
112       chooser.setToolTipText(MessageManager.getString("action.save"));
113       int value = chooser.showSaveDialog(parent);
114
115       if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
116       {
117         jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
118                 .getSelectedFile().getParent());
119         file = chooser.getSelectedFile();
120       }
121       else
122       {
123         if (pIndicator != null && !headless)
124         {
125           pIndicator.setProgressBar(MessageManager.formatMessage(
126                   "status.cancelled_image_export_operation", type.name),
127                   pSessionId);
128         }
129       }
130     }
131
132     if (file != null)
133     {
134       try
135       {
136         out = new FileOutputStream(file);
137         pIndicator.setProgressBar(null, pSessionId);
138         pIndicator.setProgressBar(MessageManager.formatMessage(
139                 "status.exporting_alignment_as_x_file", type.getName()),
140                 pSessionId);
141         if (type == TYPE.SVG)
142         {
143           setupSVG(width, height, fileTitle);
144         }
145         else if (type == TYPE.EPS)
146         {
147           setupEPS(width, height, fileTitle);
148         }
149         else if (type == TYPE.PNG)
150         {
151           setupPNG(width, height);
152         }
153         if (pIndicator != null && !headless)
154         {
155           pIndicator.setProgressBar(
156 MessageManager.formatMessage(
157                   "status.export_complete", type.getName()),
158                   pSessionId);
159         }
160       } catch (Exception ex)
161       {
162         System.out.println("Error creating " + type.getName() + " file.");
163
164         pIndicator.setProgressBar(MessageManager.formatMessage(
165                 "info.error_creating_file", type.getName()), pSessionId);
166       }
167     }
168   }
169
170   public Graphics getGraphics()
171   {
172     return graphics;
173   }
174
175   public void writeImage()
176   {
177     try
178     {
179       switch (type)
180       {
181       case EPS:
182         pg.flush();
183         pg.close();
184         break;
185       case SVG:
186         String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
187         out.write(svgData.getBytes());
188         out.flush();
189         out.close();
190         break;
191       case PNG:
192         ImageIO.write(bi, "png", out);
193         out.flush();
194         out.close();
195         break;
196       }
197     } catch (Exception ex)
198     {
199       ex.printStackTrace();
200     }
201   }
202
203   void setupEPS(int width, int height, String title)
204   {
205     boolean accurateText = true;
206
207     String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
208             "Prompt each time");
209
210     // If we need to prompt, and if the GUI is visible then
211     // Prompt for EPS rendering style
212     if (renderStyle.equalsIgnoreCase("Prompt each time")
213             && !(System.getProperty("java.awt.headless") != null && System
214                     .getProperty("java.awt.headless").equals("true")))
215     {
216       EPSOptions eps = new EPSOptions();
217       renderStyle = eps.getValue();
218
219       if (renderStyle == null || eps.cancelled)
220       {
221         return;
222       }
223     }
224
225     if (renderStyle.equalsIgnoreCase("text"))
226     {
227       accurateText = false;
228     }
229
230     try
231     {
232       pg = new EpsGraphics2D(title, out, 0, 0, width, height);
233       Graphics2D ig2 = pg;
234       ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
235               RenderingHints.VALUE_ANTIALIAS_ON);
236
237       pg.setAccurateTextMode(accurateText);
238
239       graphics = pg;
240     } catch (Exception ex)
241     {
242     }
243   }
244
245   void setupPNG(int width, int height)
246   {
247     bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
248     graphics = bi.getGraphics();
249     Graphics2D ig2 = (Graphics2D) graphics;
250     ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
251             RenderingHints.VALUE_ANTIALIAS_ON);
252
253   }
254
255   void setupSVG(int width, int height, String title)
256   {
257
258     g2 = new SVGGraphics2D(width, height);
259     Graphics2D ig2 = g2;
260
261     String renderStyle = jalview.bin.Cache.getDefault("SVG_RENDERING",
262             "Prompt each time");
263
264     // If we need to prompt, and if the GUI is visible then
265     // Prompt for EPS rendering style
266     if (renderStyle.equalsIgnoreCase("Prompt each time")
267             && !(System.getProperty("java.awt.headless") != null && System
268                     .getProperty("java.awt.headless").equals("true")))
269     {
270       SVGOptions svgOption = new SVGOptions();
271       renderStyle = svgOption.getValue();
272
273       if (renderStyle == null || svgOption.cancelled)
274       {
275         return;
276       }
277     }
278
279     if (renderStyle.equalsIgnoreCase("Lineart"))
280     {
281       ig2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
282               SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
283     }
284
285     graphics = g2;
286   }
287
288   static JalviewFileChooser getPNGChooser()
289   {
290     if (Jalview.isHeadlessMode())
291     {
292       return null;
293     }
294     return new jalview.io.JalviewFileChooser(
295             jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
296             new String[] { "png" },
297             new String[] { "Portable network graphics" },
298             "Portable network graphics");
299   }
300
301   static JalviewFileChooser getEPSChooser()
302   {
303     if (Jalview.isHeadlessMode())
304     {
305       return null;
306     }
307     return new jalview.io.JalviewFileChooser(
308             jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
309             new String[] { "eps" },
310             new String[] { "Encapsulated Postscript" },
311             "Encapsulated Postscript");
312   }
313
314   static JalviewFileChooser getSVGChooser()
315   {
316     if (Jalview.isHeadlessMode())
317     {
318       return null;
319     }
320     return new jalview.io.JalviewFileChooser(
321             jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
322             new String[] { "svg" },
323             new String[] { "Scalable Vector Graphics" },
324             "Scalable Vector Graphics");
325   }
326 }