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