2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import java.awt.Component;
24 import java.awt.Graphics;
26 import java.util.concurrent.Callable;
27 import java.util.concurrent.atomic.AtomicBoolean;
29 import jalview.bin.Cache;
30 import jalview.bin.Jalview;
31 import jalview.io.JalviewFileChooser;
32 import jalview.io.JalviewFileView;
33 import jalview.util.ImageMaker;
34 import jalview.util.ImageMaker.TYPE;
35 import jalview.util.MessageManager;
36 import jalview.util.Platform;
39 * A class that marshals steps in exporting a view in image graphics format
41 * <li>prompts the user for the output file, if not already specified</li>
42 * <li>prompts the user for Text or Lineart character rendering, if
44 * <li>instantiates an ImageMaker to create the appropriate Graphics output
45 * context for the image format</li>
46 * <li>invokes a callback to do the work of writing to the graphics</li>
52 public class ImageExporter
54 // todo move interface to jalview.api? or replace with lambda?
56 * An interface for the callback that can be run to write the image on to the
57 * graphics object. The callback should throw any exceptions arising so they
58 * can be reported by this class.
60 public interface ImageWriterI
62 void exportImage(Graphics g) throws Exception;
65 private IProgressIndicator messageBoard;
67 private ImageWriterI imageWriter;
74 * Constructor given a callback handler to write graphics data, an (optional)
75 * target for status messages, image type and (optional) title for output file
82 public ImageExporter(ImageWriterI writer, IProgressIndicator statusBar,
83 TYPE type, String fileTitle)
85 this.imageWriter = writer;
86 this.messageBoard = statusBar;
87 this.imageType = type;
88 this.title = fileTitle;
92 * Prompts the user for output file and Text/Lineart options as required,
93 * configures a Graphics context for output, and makes a callback to the
94 * client code to perform the image output
97 * output file (if null, user is prompted to choose)
99 * parent component for any dialogs shown
103 * what the image is of e.g. Tree, Alignment
105 public void doExport(File file, Component parent, int width, int height,
108 final long messageId = System.currentTimeMillis();
110 MessageManager.formatMessage(
111 "status.exporting_alignment_as_x_file", imageType),
115 * prompt user for output file if not provided
117 if (file == null && !Jalview.isHeadlessMode())
119 JalviewFileChooser chooser = imageType.getFileChooser();
120 chooser.setFileView(new JalviewFileView());
121 MessageManager.formatMessage("label.create_image_of",
122 imageType.getName(), imageSource);
123 String title = "Create " + imageType.getName()
124 + " image from alignment";
125 chooser.setDialogTitle(title);
126 chooser.setToolTipText(MessageManager.getString("action.save"));
127 int value = chooser.showSaveDialog(parent);
128 if (value != JalviewFileChooser.APPROVE_OPTION)
130 String msg = MessageManager.formatMessage(
131 "status.cancelled_image_export_operation", imageType.name);
132 setStatus(msg, messageId);
135 Cache.setProperty("LAST_DIRECTORY",
136 chooser.getSelectedFile().getParent());
137 file = chooser.getSelectedFile();
141 * Prompt for Text or Lineart (EPS/SVG) unless a preference is already set
142 * for this as EPS_RENDERING / SVG_RENDERING
143 * Always set to Text for JalviewJS as Lineart (glyph fonts) not available
145 String renderStyle = Cache.getDefault(
146 imageType.getName() + "_RENDERING",
147 LineartOptions.PROMPT_EACH_TIME);
150 renderStyle = "Text";
152 AtomicBoolean textSelected = new AtomicBoolean(
153 !"Lineart".equals(renderStyle));
154 if ((imageType == TYPE.EPS || imageType == TYPE.SVG)
155 && LineartOptions.PROMPT_EACH_TIME.equals(renderStyle)
156 && !Jalview.isHeadlessMode())
158 final File chosenFile = file;
159 Callable<Void> okAction = () -> {
160 exportImage(chosenFile, !textSelected.get(), width, height,
164 LineartOptions epsOption = new LineartOptions(TYPE.EPS.getName(),
166 epsOption.setResponseAction(1, new Callable<Void>()
171 setStatus(MessageManager.formatMessage(
172 "status.cancelled_image_export_operation",
173 imageType.getName()), messageId);
177 epsOption.setResponseAction(0, okAction);
178 epsOption.showDialog();
179 /* no code here - JalviewJS cannot execute it */
184 * character rendering not required, or preference already set
185 * - just do the export
187 exportImage(file, !textSelected.get(), width, height, messageId);
192 * Constructs a suitable graphics context and passes it to the callback
193 * handler for the image to be written. Shows status messages for export in
194 * progress, complete, or failed as appropriate.
202 protected void exportImage(File chosenFile, boolean asLineart, int width,
203 int height, long messageId)
205 String type = imageType.getName();
209 // MessageManager.formatMessage(
210 // "status.exporting_alignment_as_x_file", type),
212 ImageMaker im = new ImageMaker(imageType, width, height, chosenFile,
214 imageWriter.exportImage(im.getGraphics());
217 MessageManager.formatMessage("status.export_complete", type),
219 } catch (Exception e)
221 System.out.println(String.format("Error creating %s file: %s", type,
223 setStatus(MessageManager.formatMessage("info.error_creating_file",
229 * Asks the callback to show a status message with given id
234 void setStatus(String msg, long id)
236 if (messageBoard != null && !Jalview.isHeadlessMode())
238 messageBoard.setProgressBar(msg, id);