3 import jalview.bin.Cache;
4 import jalview.bin.Jalview;
5 import jalview.io.JalviewFileChooser;
6 import jalview.io.JalviewFileView;
7 import jalview.util.ImageMaker;
8 import jalview.util.ImageMaker.TYPE;
9 import jalview.util.MessageManager;
10 import jalview.util.Platform;
12 import java.awt.Component;
13 import java.awt.Graphics;
15 import java.util.concurrent.atomic.AtomicBoolean;
18 * A class that marshals steps in exporting a view in image graphics format
20 * <li>prompts the user for the output file, if not already specified</li>
21 * <li>prompts the user for Text or Lineart character rendering, if
23 * <li>instantiates an ImageMaker to create the appropriate Graphics output
24 * context for the image format</li>
25 * <li>invokes a callback to do the work of writing to the graphics</li>
31 public class ImageExporter
33 // todo move interface to jalview.api? or replace with lambda?
35 * An interface for the callback that can be run to write the image on to the
36 * graphics object. The callback should throw any exceptions arising so they
37 * can be reported by this class.
39 public interface ImageWriterI
41 void exportImage(Graphics g)
45 private IProgressIndicator messageBoard;
47 private ImageWriterI imageWriter;
54 * Constructor given a callback handler to write graphics data, an (optional)
55 * target for status messages, image type and (optional) title for output file
62 public ImageExporter(ImageWriterI writer, IProgressIndicator statusBar,
63 TYPE type, String fileTitle)
65 this.imageWriter = writer;
66 this.messageBoard = statusBar;
67 this.imageType = type;
68 this.title = fileTitle;
72 * Prompts the user for output file and Text/Lineart options as required,
73 * configures a Graphics context for output, and makes a callback to the
74 * client code to perform the image output
77 * output file (if null, user is prompted to choose)
79 * parent component for any dialogs shown
83 * what the image is of e.g. Tree, Alignment
85 public void doExport(File file, Component parent, int width, int height,
88 final long messageId = System.currentTimeMillis();
90 MessageManager.formatMessage(
91 "status.exporting_alignment_as_x_file", imageType),
95 * prompt user for output file if not provided
97 if (file == null && !Jalview.isHeadlessMode())
99 JalviewFileChooser chooser = imageType.getFileChooser();
100 chooser.setFileView(new JalviewFileView());
101 MessageManager.formatMessage("label.create_image_of",
102 imageType.getName(), imageSource);
103 String title = "Create " + imageType.getName()
104 + " image from alignment";
105 chooser.setDialogTitle(title);
106 chooser.setToolTipText(MessageManager.getString("action.save"));
107 int value = chooser.showSaveDialog(parent);
108 if (value != JalviewFileChooser.APPROVE_OPTION)
110 String msg = MessageManager.formatMessage(
111 "status.cancelled_image_export_operation", imageType.name);
112 setStatus(msg, messageId);
115 Cache.setProperty("LAST_DIRECTORY",
116 chooser.getSelectedFile().getParent());
117 file = chooser.getSelectedFile();
121 * Prompt for Text or Lineart (EPS/SVG) unless a preference is already set
122 * for this as EPS_RENDERING / SVG_RENDERING
123 * Always set to Text for JalviewJS as Lineart (glyph fonts) not available
125 String renderStyle = Cache.getDefault(
126 imageType.getName() + "_RENDERING",
127 LineartOptions.PROMPT_EACH_TIME);
130 renderStyle = "Text";
132 AtomicBoolean textSelected = new AtomicBoolean(
133 !"Lineart".equals(renderStyle));
134 if ((imageType == TYPE.EPS || imageType == TYPE.SVG)
135 && LineartOptions.PROMPT_EACH_TIME.equals(renderStyle)
136 && !Jalview.isHeadlessMode())
138 final File chosenFile = file;
139 Runnable okAction = new Runnable()
144 exportImage(chosenFile, !textSelected.get(), width, height,
148 LineartOptions epsOption = new LineartOptions(TYPE.EPS.getName(),
150 epsOption.setResponseAction(1, new Runnable()
155 setStatus(MessageManager.formatMessage(
156 "status.cancelled_image_export_operation",
157 imageType.getName()), messageId);
160 epsOption.setResponseAction(0, okAction);
161 epsOption.showDialog();
162 /* no code here - JalviewJS cannot execute it */
167 * character rendering not required, or preference already set
168 * - just do the export
170 exportImage(file, !textSelected.get(), width, height, messageId);
175 * Constructs a suitable graphics context and passes it to the callback
176 * handler for the image to be written. Shows status messages for export in
177 * progress, complete, or failed as appropriate.
185 protected void exportImage(File chosenFile, boolean asLineart, int width,
186 int height, long messageId)
188 String type = imageType.getName();
192 // MessageManager.formatMessage(
193 // "status.exporting_alignment_as_x_file", type),
195 ImageMaker im = new ImageMaker(imageType, width, height, chosenFile,
197 imageWriter.exportImage(im.getGraphics());
200 MessageManager.formatMessage("status.export_complete", type),
202 } catch (Exception e)
205 .println(String.format("Error creating %s file: %s", type,
207 setStatus(MessageManager.formatMessage("info.error_creating_file",
213 * Asks the callback to show a status message with given id
218 void setStatus(String msg, long id)
220 if (messageBoard != null && !Jalview.isHeadlessMode())
222 messageBoard.setProgressBar(msg, id);