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