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;
11 import java.awt.Component;
12 import java.awt.Graphics;
14 import java.util.concurrent.atomic.AtomicBoolean;
17 * A class that marshals steps in exporting a view in image graphics format
19 * <li>prompts the user for the output file, if not already specified</li>
20 * <li>prompts the user for Text or Lineart character rendering, if
22 * <li>instantiates an ImageMaker to create the appropriate Graphics output
23 * context for the image format</li>
24 * <li>invokes a callback to do the work of writing to the graphics</li>
30 public class ImageExporter
32 // todo move interface to jalview.api? or replace with lambda?
34 * An interface for the callback that can be run to write the image on to the
35 * graphics object. The callback should throw any exceptions arising so they
36 * can be reported by this class.
38 public interface ImageWriterI
40 void exportImage(Graphics g)
44 private IProgressIndicator messageBoard;
46 private ImageWriterI imageWriter;
53 * Constructor given a callback handler to write graphics data, an (optional)
54 * target for status messages, image type and (optional) title for output file
61 public ImageExporter(ImageWriterI writer, IProgressIndicator statusBar,
62 TYPE type, String fileTitle)
64 this.imageWriter = writer;
65 this.messageBoard = statusBar;
66 this.imageType = type;
67 this.title = fileTitle;
71 * Prompts the user for output file and Text/Lineart options as required,
72 * configures a Graphics context for output, and makes a callback to the
73 * client code to perform the image output
76 * output file (if null, user is prompted to choose)
78 * parent component for any dialogs shown
82 * what the image is of e.g. Tree, Alignment
84 public void doExport(File file, Component parent, int width, int height,
87 final long messageId = System.currentTimeMillis();
89 MessageManager.formatMessage(
90 "status.exporting_alignment_as_x_file", imageType),
94 * prompt user for output file if not provided
96 if (file == null && !Jalview.isHeadlessMode())
98 JalviewFileChooser chooser = imageType.getFileChooser();
99 chooser.setFileView(new JalviewFileView());
100 MessageManager.formatMessage("label.create_image_of",
101 imageType.getName(), imageSource);
102 String title = "Create " + imageType.getName()
103 + " image from alignment";
104 chooser.setDialogTitle(title);
105 chooser.setToolTipText(MessageManager.getString("action.save"));
106 int value = chooser.showSaveDialog(parent);
107 if (value != JalviewFileChooser.APPROVE_OPTION)
109 String msg = MessageManager.formatMessage(
110 "status.cancelled_image_export_operation", imageType.name);
111 setStatus(msg, messageId);
114 Cache.setProperty("LAST_DIRECTORY",
115 chooser.getSelectedFile().getParent());
116 file = chooser.getSelectedFile();
120 * Prompt for Text or Lineart (EPS/SVG) unless a preference is already set
121 * for this as EPS_RENDERING / SVG_RENDERING
122 * Always set to Text for JalviewJS as Lineart (glyph fonts) not available
124 String renderStyle = Cache.getDefault(
125 imageType.getName() + "_RENDERING",
126 LineartOptions.PROMPT_EACH_TIME);
129 renderStyle = "Text";
131 AtomicBoolean textSelected = new AtomicBoolean(
132 !"Lineart".equals(renderStyle));
133 if ((imageType == TYPE.EPS || imageType == TYPE.SVG)
134 && LineartOptions.PROMPT_EACH_TIME.equals(renderStyle)
135 && !Jalview.isHeadlessMode())
137 final File chosenFile = file;
138 Runnable okAction = new Runnable()
143 exportImage(chosenFile, !textSelected.get(), width, height,
147 LineartOptions epsOption = new LineartOptions(TYPE.EPS.getName(),
149 epsOption.setResponseAction(1, new Runnable()
154 setStatus(MessageManager.formatMessage(
155 "status.cancelled_image_export_operation",
156 imageType.getName()), messageId);
159 epsOption.setResponseAction(0, okAction);
160 epsOption.showDialog();
161 /* no code here - JalviewJS cannot execute it */
166 * character rendering not required, or preference already set
167 * - just do the export
169 exportImage(file, !textSelected.get(), width, height, messageId);
174 * Constructs a suitable graphics context and passes it to the callback
175 * handler for the image to be written. Shows status messages for export in
176 * progress, complete, or failed as appropriate.
184 protected void exportImage(File chosenFile, boolean asLineart, int width,
185 int height, long messageId)
187 String type = imageType.getName();
191 // MessageManager.formatMessage(
192 // "status.exporting_alignment_as_x_file", type),
194 ImageMaker im = new ImageMaker(imageType, width, height, chosenFile,
196 imageWriter.exportImage(im.getGraphics());
199 MessageManager.formatMessage("status.export_complete", type),
201 } catch (Exception e)
204 .println(String.format("Error creating %s file: %s", type,
206 setStatus(MessageManager.formatMessage("info.error_creating_file",
212 * Asks the callback to show a status message with given id
217 void setStatus(String msg, long id)
219 if (messageBoard != null && !Jalview.isHeadlessMode())
221 messageBoard.setProgressBar(msg, id);