ensure lastSeq is refreshed if new feature added
[jalview.git] / src / jalview / util / ImageMaker.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 package jalview.util;\r
20 \r
21 import java.awt.RenderingHints;\r
22 import javax.imageio.ImageIO;\r
23 import org.jibble.epsgraphics.EpsGraphics2D;\r
24 import jalview.gui.EPSOptions;\r
25 import java.awt.Graphics2D;\r
26 import java.io.*;\r
27 import java.awt.image.BufferedImage;\r
28 import java.awt.Graphics;\r
29 import jalview.io.*;\r
30 import java.awt.*;\r
31 \r
32 \r
33 public class ImageMaker\r
34 {\r
35   public static final int EPS = 0;\r
36   public static final int PNG = 1;\r
37   int type = -1;\r
38 \r
39   EpsGraphics2D pg;\r
40   Graphics graphics;\r
41   FileOutputStream out;\r
42   BufferedImage bi;\r
43 \r
44   public ImageMaker(Component parent, int type, String title,\r
45                            int width, int height, File file, String EPStitle)\r
46   {\r
47     this.type = type;\r
48 \r
49     if (file == null)\r
50     {\r
51       JalviewFileChooser chooser;\r
52       chooser = type == EPS ? getEPSChooser() : getPNGChooser();\r
53 \r
54       chooser.setFileView(new jalview.io.JalviewFileView());\r
55       chooser.setDialogTitle(title);\r
56       chooser.setToolTipText("Save");\r
57 \r
58       int value = chooser.showSaveDialog(parent);\r
59 \r
60       if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)\r
61       {\r
62         jalview.bin.Cache.setProperty("LAST_DIRECTORY",\r
63                                       chooser.getSelectedFile().getParent());\r
64 \r
65         file = chooser.getSelectedFile();\r
66       }\r
67     }\r
68 \r
69     if(file!=null)\r
70     {\r
71       try\r
72       {\r
73         out = new FileOutputStream(file);\r
74 \r
75         if (type == EPS)\r
76           setupEPS(width, height, EPStitle);\r
77         else\r
78           setupPNG(width, height);\r
79       }\r
80       catch (Exception ex)\r
81       {\r
82         System.out.println("Error creating " + (type == EPS ? "EPS" : "PNG") +\r
83                            " file.");\r
84       }\r
85     }\r
86   }\r
87 \r
88   public Graphics getGraphics()\r
89   {\r
90     return graphics;\r
91   }\r
92 \r
93 \r
94   void setupPNG(int width, int height)\r
95   {\r
96         bi = new BufferedImage(width, height,\r
97                 BufferedImage.TYPE_INT_RGB);\r
98         graphics  = bi.getGraphics();\r
99         Graphics2D ig2 = (Graphics2D) graphics;\r
100         ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,\r
101                                  RenderingHints.VALUE_ANTIALIAS_ON);\r
102   }\r
103 \r
104   public void writeImage()\r
105   {\r
106     try{\r
107       switch(type)\r
108       {\r
109         case EPS:\r
110           pg.flush();\r
111           pg.close();\r
112           break;\r
113         case PNG:\r
114           ImageIO.write(bi, "png", out);\r
115           out.close();\r
116           break;\r
117       }\r
118     }\r
119     catch (Exception ex)\r
120     {\r
121         ex.printStackTrace();\r
122     }\r
123   }\r
124 \r
125   void setupEPS(int width, int height, String title)\r
126   {\r
127     boolean accurateText = true;\r
128 \r
129     String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",\r
130         "Prompt each time");\r
131 \r
132     // If we need to prompt, and if the GUI is visible then\r
133     // Prompt for EPS rendering style\r
134     if (renderStyle.equalsIgnoreCase("Prompt each time")\r
135         && !\r
136         (System.getProperty("java.awt.headless") != null\r
137          && System.getProperty("java.awt.headless").equals("true")))\r
138     {\r
139       EPSOptions eps = new EPSOptions();\r
140       renderStyle = eps.getValue();\r
141 \r
142       if (renderStyle == null || eps.cancelled)\r
143         return;\r
144     }\r
145 \r
146     if (renderStyle.equalsIgnoreCase("text"))\r
147     {\r
148       accurateText = false;\r
149     }\r
150 \r
151     try   {\r
152       pg = new EpsGraphics2D(title, out, 0, 0, width,\r
153                                            height);\r
154       Graphics2D ig2 = (Graphics2D) pg;\r
155       ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,\r
156                            RenderingHints.VALUE_ANTIALIAS_ON);\r
157 \r
158       pg.setAccurateTextMode(accurateText);\r
159 \r
160       graphics = pg;\r
161     }\r
162     catch (Exception ex) {  }\r
163   }\r
164 \r
165 \r
166   JalviewFileChooser getPNGChooser()\r
167   {\r
168     return new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(\r
169         "LAST_DIRECTORY"), new String[]\r
170                                              {"png"},\r
171                                              new String[]\r
172                                              {"Portable network graphics"},\r
173                                              "Portable network graphics");\r
174   }\r
175 \r
176   JalviewFileChooser getEPSChooser()\r
177   {\r
178     return new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(\r
179         "LAST_DIRECTORY"), new String[]\r
180                                              {"eps"},\r
181                                              new String[]\r
182                                              {"Encapsulated Postscript"},\r
183                                              "Encapsulated Postscript");\r
184   }\r
185 }\r