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