JAL-3746 apply copyright to source
[jalview.git] / src / jalview / gui / ImageExporter.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.gui;
22
23 import jalview.bin.Cache;
24 import jalview.bin.Jalview;
25 import jalview.io.JalviewFileChooser;
26 import jalview.io.JalviewFileView;
27 import jalview.util.ImageMaker;
28 import jalview.util.ImageMaker.TYPE;
29 import jalview.util.MessageManager;
30 import jalview.util.Platform;
31
32 import java.awt.Component;
33 import java.awt.Graphics;
34 import java.io.File;
35 import java.util.concurrent.atomic.AtomicBoolean;
36
37 /**
38  * A class that marshals steps in exporting a view in image graphics format
39  * <ul>
40  * <li>prompts the user for the output file, if not already specified</li>
41  * <li>prompts the user for Text or Lineart character rendering, if
42  * necessary</li>
43  * <li>instantiates an ImageMaker to create the appropriate Graphics output
44  * context for the image format</li>
45  * <li>invokes a callback to do the work of writing to the graphics</li>
46  * </ul>
47  * 
48  * @author gmcarstairs
49  *
50  */
51 public class ImageExporter
52 {
53   // todo move interface to jalview.api? or replace with lambda?
54   /**
55    * An interface for the callback that can be run to write the image on to the
56    * graphics object. The callback should throw any exceptions arising so they
57    * can be reported by this class.
58    */
59   public interface ImageWriterI
60   {
61     void exportImage(Graphics g) throws Exception;
62   }
63
64   private IProgressIndicator messageBoard;
65
66   private ImageWriterI imageWriter;
67
68   TYPE imageType;
69
70   private String title;
71
72   /**
73    * Constructor given a callback handler to write graphics data, an (optional)
74    * target for status messages, image type and (optional) title for output file
75    * 
76    * @param writer
77    * @param statusBar
78    * @param type
79    * @param fileTitle
80    */
81   public ImageExporter(ImageWriterI writer, IProgressIndicator statusBar,
82           TYPE type, String fileTitle)
83   {
84     this.imageWriter = writer;
85     this.messageBoard = statusBar;
86     this.imageType = type;
87     this.title = fileTitle;
88   }
89
90   /**
91    * Prompts the user for output file and Text/Lineart options as required,
92    * configures a Graphics context for output, and makes a callback to the
93    * client code to perform the image output
94    * 
95    * @param file
96    *          output file (if null, user is prompted to choose)
97    * @param parent
98    *          parent component for any dialogs shown
99    * @param width
100    * @param height
101    * @param imageSource
102    *          what the image is of e.g. Tree, Alignment
103    */
104   public void doExport(File file, Component parent, int width, int height,
105           String imageSource)
106   {
107     final long messageId = System.currentTimeMillis();
108     setStatus(
109             MessageManager.formatMessage(
110                     "status.exporting_alignment_as_x_file", imageType),
111             messageId);
112
113     /*
114      * prompt user for output file if not provided
115      */
116     if (file == null && !Jalview.isHeadlessMode())
117     {
118       JalviewFileChooser chooser = imageType.getFileChooser();
119       chooser.setFileView(new JalviewFileView());
120       MessageManager.formatMessage("label.create_image_of",
121               imageType.getName(), imageSource);
122       String title = "Create " + imageType.getName()
123               + " image from alignment";
124       chooser.setDialogTitle(title);
125       chooser.setToolTipText(MessageManager.getString("action.save"));
126       int value = chooser.showSaveDialog(parent);
127       if (value != JalviewFileChooser.APPROVE_OPTION)
128       {
129         String msg = MessageManager.formatMessage(
130                 "status.cancelled_image_export_operation", imageType.name);
131         setStatus(msg, messageId);
132         return;
133       }
134       Cache.setProperty("LAST_DIRECTORY",
135               chooser.getSelectedFile().getParent());
136       file = chooser.getSelectedFile();
137     }
138
139     /*
140      * Prompt for Text or Lineart (EPS/SVG) unless a preference is already set
141      * for this as EPS_RENDERING / SVG_RENDERING
142      * Always set to Text for JalviewJS as Lineart (glyph fonts) not available
143      */
144     String renderStyle = Cache.getDefault(
145             imageType.getName() + "_RENDERING",
146             LineartOptions.PROMPT_EACH_TIME);
147     if (Platform.isJS())
148     {
149       renderStyle = "Text";
150     }
151     AtomicBoolean textSelected = new AtomicBoolean(
152             !"Lineart".equals(renderStyle));
153     if ((imageType == TYPE.EPS || imageType == TYPE.SVG)
154             && LineartOptions.PROMPT_EACH_TIME.equals(renderStyle)
155             && !Jalview.isHeadlessMode())
156     {
157       final File chosenFile = file;
158       Runnable okAction = new Runnable()
159       {
160         @Override
161         public void run()
162         {
163           exportImage(chosenFile, !textSelected.get(), width, height,
164                   messageId);
165         }
166       };
167       LineartOptions epsOption = new LineartOptions(TYPE.EPS.getName(),
168               textSelected);
169       epsOption.setResponseAction(1, new Runnable()
170       {
171         @Override
172         public void run()
173         {
174           setStatus(MessageManager.formatMessage(
175                   "status.cancelled_image_export_operation",
176                   imageType.getName()), messageId);
177         }
178       });
179       epsOption.setResponseAction(0, okAction);
180       epsOption.showDialog();
181       /* no code here - JalviewJS cannot execute it */
182     }
183     else
184     {
185       /*
186        * character rendering not required, or preference already set 
187        * - just do the export
188        */
189       exportImage(file, !textSelected.get(), width, height, messageId);
190     }
191   }
192
193   /**
194    * Constructs a suitable graphics context and passes it to the callback
195    * handler for the image to be written. Shows status messages for export in
196    * progress, complete, or failed as appropriate.
197    * 
198    * @param chosenFile
199    * @param asLineart
200    * @param width
201    * @param height
202    * @param messageId
203    */
204   protected void exportImage(File chosenFile, boolean asLineart, int width,
205           int height, long messageId)
206   {
207     String type = imageType.getName();
208     try
209     {
210       // setStatus(
211       // MessageManager.formatMessage(
212       // "status.exporting_alignment_as_x_file", type),
213       // messageId);
214       ImageMaker im = new ImageMaker(imageType, width, height, chosenFile,
215               title, asLineart);
216       imageWriter.exportImage(im.getGraphics());
217       im.writeImage();
218       setStatus(
219               MessageManager.formatMessage("status.export_complete", type),
220               messageId);
221     } catch (Exception e)
222     {
223       System.out.println(String.format("Error creating %s file: %s", type,
224               e.toString()));
225       setStatus(MessageManager.formatMessage("info.error_creating_file",
226               type), messageId);
227     }
228   }
229
230   /**
231    * Asks the callback to show a status message with given id
232    * 
233    * @param msg
234    * @param id
235    */
236   void setStatus(String msg, long id)
237   {
238     if (messageBoard != null && !Jalview.isHeadlessMode())
239     {
240       messageBoard.setProgressBar(msg, id);
241     }
242   }
243
244 }