From f9f0b83445a2da848d21614d94a909888484e25e Mon Sep 17 00:00:00 2001 From: gmungoc Date: Mon, 23 Jul 2018 10:30:26 +0100 Subject: [PATCH] JAL-3048 all image export (alignment, tree, PCA, Jmol) now via ImageExporter and JalviewJS compatible --- resources/lang/Messages.properties | 12 +- resources/lang/Messages_es.properties | 12 +- .../exceptions/NoFileSelectedException.java | 30 --- src/jalview/gui/AlignmentPanel.java | 66 ++--- src/jalview/gui/AppJmol.java | 61 ++--- src/jalview/gui/ChimeraViewFrame.java | 14 +- src/jalview/gui/Desktop.java | 48 ++-- src/jalview/gui/ImageExporter.java | 219 +++++++++++++++ src/jalview/gui/LineartOptions.java | 16 +- src/jalview/gui/PCAPanel.java | 74 ++---- src/jalview/gui/TreePanel.java | 123 ++------- src/jalview/io/BioJsHTMLOutput.java | 45 +--- src/jalview/io/HTMLOutput.java | 67 +++-- src/jalview/io/HtmlSvgOutput.java | 39 ++- src/jalview/jbgui/GPCAPanel.java | 13 +- src/jalview/jbgui/GStructureViewer.java | 12 +- src/jalview/jbgui/GTreePanel.java | 11 +- src/jalview/util/ImageMaker.java | 278 +++----------------- src/jalview/util/dialogrunner/Response.java | 2 +- src/org/jibble/epsgraphics/EpsDocument.java | 6 +- 20 files changed, 472 insertions(+), 676 deletions(-) delete mode 100644 src/jalview/exceptions/NoFileSelectedException.java create mode 100644 src/jalview/gui/ImageExporter.java diff --git a/resources/lang/Messages.properties b/resources/lang/Messages.properties index 88f45d9..184ae5c 100644 --- a/resources/lang/Messages.properties +++ b/resources/lang/Messages.properties @@ -894,8 +894,6 @@ label.save_feature_colours = Save Feature Colour Scheme label.select_startup_file = Select startup file label.select_default_browser = Select default web browser label.save_tree_as_newick = Save tree as newick file -label.create_eps_from_tree = Create EPS file from tree -label.create_png_from_tree = Create PNG image from tree label.save_colour_scheme = Save colour scheme label.edit_params_for = Edit parameters for {0} label.choose_filename_for_param_file = Choose a filename for this parameter file @@ -951,8 +949,6 @@ error.call_setprogressbar_before_registering_handler = call setProgressBar befor label.cancelled_params = Cancelled {0} error.implementation_error_cannot_show_view_alignment_frame = Implementation error: cannot show a view from another alignment in an AlignFrame. error.implementation_error_dont_know_about_threshold_setting = Implementation error: don't know about threshold setting for current AnnotationColourGradient. -error.eps_generation_not_implemented = EPS Generation not yet implemented -error.png_generation_not_implemented = PNG Generation not yet implemented error.try_join_vamsas_session_another = Trying to join a vamsas session when another is already connected error.invalid_vamsas_session_id = Invalid vamsas session id label.groovy_support_failed = Jalview Groovy Support Failed @@ -1097,7 +1093,6 @@ error.implementation_error_cannot_find_service_url_in_given_set_param_store = Im exception.jobsubmission_invalid_params_set = Invalid parameter set. Check Jalview implementation exception.notvaliddata_group_contains_less_than_min_seqs = Group contains less than {0} sequences. exception.outofmemory_loading_pdb_file = Out of memory loading PDB File -exception.eps_coudnt_write_output_file = Could not write to the output file: {0} exception.eps_method_not_supported = Method not currently supported by EpsGraphics2D version {0} exception.eps_unable_to_get_inverse_matrix = Unable to get inverse of matrix: {0} warn.job_cannot_be_cancelled_close_window = This job cannot be cancelled.\nJust close the window. @@ -1122,8 +1117,7 @@ status.searching_for_sequences_from = Searching for sequences from {0} status.finished_searching_for_sequences_from = Finished searching for sequences from {0} label.eps_file = EPS file label.png_image = PNG image -status.saving_file = Saving {0} -status.export_complete = {0} Export completed. +status.export_complete = {0} Export completed status.fetching_pdb = Fetching PDB {0} status.refreshing_news = Refreshing news status.importing_vamsas_session_from = Importing VAMSAS session from {0} @@ -1256,7 +1250,6 @@ exception.fts_server_unreachable = Jalview is unable to reach the {0} server. \n label.nw_mapping = Needleman & Wunsch Alignment label.sifts_mapping = SIFTs Mapping label.mapping_method = Sequence \u27f7 Structure mapping method -status.waiting_for_user_to_select_output_file = Waiting for user to select {0} file status.cancelled_image_export_operation = Cancelled {0} export operation info.error_creating_file = Error creating {0} file exception.outofmemory_loading_mmcif_file = Out of memory loading mmCIF File @@ -1367,3 +1360,6 @@ label.free_text_search = Free Text Search label.annotation_name = Annotation Name label.annotation_description = Annotation Description label.edit_annotation_name_description = Edit Annotation Name/Description +label.alignment = alignment +label.pca = PCA +label.create_image_of = Create {0} image of {1} diff --git a/resources/lang/Messages_es.properties b/resources/lang/Messages_es.properties index f92755d..8ef8aa5 100644 --- a/resources/lang/Messages_es.properties +++ b/resources/lang/Messages_es.properties @@ -819,8 +819,6 @@ label.save_feature_colours = Guardar esquema crom label.select_startup_file = Seleccionar fichero de arranque label.select_default_browser = Seleccionar navegador web por defecto label.save_tree_as_newick = Guardar árbol como fichero newick -label.create_eps_from_tree = Crear un fichero EPS a partir de un árbol -label.create_png_from_tree = Crear una imagen PNG a partir de un árbol label.save_colour_scheme = Guardar esquema cromático label.edit_params_for = Editar los parámetros de {0} label.choose_filename_for_param_file = Escoja un nombre de fichero para este fichero de parámetros @@ -876,8 +874,6 @@ error.call_setprogressbar_before_registering_handler = llamada a setProgressBar label.cancelled_params = {0} cancelado error.implementation_error_cannot_show_view_alignment_frame = Error de implementación: no es posible mostrar una vista de otro alineamiento en un AlignFrame. error.implementation_error_dont_know_about_threshold_setting = Error de implementación: no se conoce la configuración del umbral para el AnnotationColourGradient actual. -error.eps_generation_not_implemented = La generación de EPS no se ha implementado todavía -error.png_generation_not_implemented = La generación de PNG no se ha implementado todavía error.try_join_vamsas_session_another = Tratando de establecer una sesión VAMSAS cuando ya había otra conectada error.invalid_vamsas_session_id = Identificador de sesión VAMSAS no válido label.groovy_support_failed = El soporte Groovy de Jalview ha fallado @@ -1022,7 +1018,6 @@ error.implementation_error_cannot_find_service_url_in_given_set_param_store = Er exception.jobsubmission_invalid_params_set = Conjunto de parámetros no válido. Comprueba la implementación de Jalview exception.notvaliddata_group_contains_less_than_min_seqs = El grupo contiene menos de {0} secuencias. exception.outofmemory_loading_pdb_file = Sin memoria al cargar el fichero PDB -exception.eps_coudnt_write_output_file = No es posible escribir el fichero de salida: {0} exception.eps_method_not_supported = Método actualmente no suportado por la versión {0} de EpsGraphics2D exception.eps_unable_to_get_inverse_matrix = Imposible obtener la inversa de la matrix: {0} warn.job_cannot_be_cancelled_close_window = Este trabajo no se puede cancelar.\nSimplemente, cierre la ventana. @@ -1044,8 +1039,7 @@ status.searching_for_sequences_from = Buscando secuencias en {0} status.finished_searching_for_sequences_from = Finalizada la búsqueda de secuencias en {0} label.eps_file = Fichero EPS label.png_image = Imagen PNG -status.saving_file = Guardando {0} -status.export_complete = Exportación completada. +status.export_complete = Exportación completada status.fetching_pdb = Recuperando PDB {0} status.refreshing_news = Refrescando noticias status.importing_vamsas_session_from = Importando sesión VAMSAS de {0} @@ -1250,7 +1244,6 @@ label.hide_columns_not_containing=Ocultar las columnas que no contengan label.pdb_sequence_fetcher=Recuperador de secuencias PDB exception.fts_server_error=Parece que hay un error desde el servidor {0} exception.service_not_available=Servicio no disponible. El servidor se está actualizando, vuelva a intentarlo más tarde. -status.waiting_for_user_to_select_output_file=Esperando que el usuario seleccione el fichero {0} action.prev_page=<< status.cancelled_image_export_operation=Operación de exportación {0} cancelada label.couldnt_run_groovy_script=No se ha podido ejecutar el script Groovy @@ -1368,3 +1361,6 @@ label.free_text_search = B label.annotation_name = Nombre de la anotación label.annotation_description = Descripción de la anotación label.edit_annotation_name_description = Editar el nombre/descripción de la anotación +label.alignment = alineamiento +label.pca = ACP +label.create_image_of = Crear imagen {0} de {1} diff --git a/src/jalview/exceptions/NoFileSelectedException.java b/src/jalview/exceptions/NoFileSelectedException.java deleted file mode 100644 index 9a702e2..0000000 --- a/src/jalview/exceptions/NoFileSelectedException.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) - * Copyright (C) $$Year-Rel$$ The Jalview Authors - * - * This file is part of Jalview. - * - * Jalview is free software: you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation, either version 3 - * of the License, or (at your option) any later version. - * - * Jalview is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty - * of MERCHANTABILITY or FITNESS FOR A PARTICULAR - * PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Jalview. If not, see . - * The Jalview Authors are detailed in the 'AUTHORS' file. - */ -package jalview.exceptions; - -@SuppressWarnings("serial") -public class NoFileSelectedException extends JalviewException -{ - public NoFileSelectedException(String msg) - { - super(msg); - } -} diff --git a/src/jalview/gui/AlignmentPanel.java b/src/jalview/gui/AlignmentPanel.java index 6e46781..58aa645 100644 --- a/src/jalview/gui/AlignmentPanel.java +++ b/src/jalview/gui/AlignmentPanel.java @@ -24,12 +24,14 @@ import jalview.analysis.AnnotationSorter; import jalview.api.AlignViewportI; import jalview.api.AlignmentViewPanel; import jalview.bin.Cache; +import jalview.bin.Jalview; import jalview.datamodel.AlignmentI; import jalview.datamodel.HiddenColumns; import jalview.datamodel.SearchResultsI; import jalview.datamodel.SequenceFeature; import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; +import jalview.gui.ImageExporter.ImageWriterI; import jalview.io.HTMLOutput; import jalview.jbgui.GAlignmentPanel; import jalview.math.AlignmentDimension; @@ -82,11 +84,8 @@ public class AlignmentPanel extends GAlignmentPanel implements private IdPanel idPanel; - private boolean headless; - IdwidthAdjuster idwidthAdjuster; - /** DOCUMENT ME!! */ public AlignFrame alignFrame; private ScalePanel scalePanel; @@ -1288,32 +1287,14 @@ public class AlignmentPanel extends GAlignmentPanel implements */ void makeAlignmentImage(ImageMaker.TYPE type, File file) { - int borderBottomOffset = 5; - long pSessionId = System.currentTimeMillis(); - headless = (System.getProperty("java.awt.headless") != null - && System.getProperty("java.awt.headless").equals("true")); - if (alignFrame != null && !headless) - { - if (file != null) - { - alignFrame.setProgressBar(MessageManager - .formatMessage("status.saving_file", new Object[] - { type.getLabel() }), pSessionId); - } - } - try + final int borderBottomOffset = 5; + + AlignmentDimension aDimension = getAlignmentDimension(); + // todo use a lambda function in place of callback here? + ImageWriterI writer = new ImageWriterI() { - AlignmentDimension aDimension = getAlignmentDimension(); - String imageAction = "Create " + type.getName() - + " image from alignment"; - String imageTitle = alignFrame.getTitle(); - - ImageMaker im = new ImageMaker(this, type, imageAction, - aDimension.getWidth(), - aDimension.getHeight() + borderBottomOffset, file, imageTitle, - alignFrame, pSessionId, headless); - Graphics graphics = im.getGraphics(); - if (graphics != null) + @Override + public void exportImage(Graphics graphics) throws Exception { if (av.getWrapAlignment()) { @@ -1325,21 +1306,24 @@ public class AlignmentPanel extends GAlignmentPanel implements printUnwrapped(aDimension.getWidth(), aDimension.getHeight(), 0, graphics, graphics); } - im.writeImage(); } - } catch (OutOfMemoryError err) - { - // Be noisy here. - System.out.println("########################\n" + "OUT OF MEMORY " - + file + "\n" + "########################"); - new OOMWarning("Creating Image for " + file, err); - // System.out.println("Create IMAGE: " + err); - } catch (Exception ex) - { - ex.printStackTrace(); - } + }; + + String fileTitle = alignFrame.getTitle(); + ImageExporter exporter = new ImageExporter(writer, alignFrame, type, + fileTitle); + int imageWidth = aDimension.getWidth(); + int imageHeight = aDimension.getHeight() + borderBottomOffset; + String of = MessageManager.getString("label.alignment"); + exporter.doExport(file, this, imageWidth, imageHeight, of); } + /** + * Calculates and returns a suitable width and height (in pixels) for an + * exported image + * + * @return + */ public AlignmentDimension getAlignmentDimension() { int maxwidth = av.getAlignment().getWidth(); @@ -1356,7 +1340,7 @@ public class AlignmentPanel extends GAlignmentPanel implements if (av.getWrapAlignment()) { height = getWrappedHeight(); - if (headless) + if (Jalview.isHeadlessMode()) { // need to obtain default alignment width and then add in any // additional allowance for id margin diff --git a/src/jalview/gui/AppJmol.java b/src/jalview/gui/AppJmol.java index 6c934c8..16c6d8a 100644 --- a/src/jalview/gui/AppJmol.java +++ b/src/jalview/gui/AppJmol.java @@ -24,9 +24,11 @@ import jalview.bin.Cache; import jalview.datamodel.AlignmentI; import jalview.datamodel.PDBEntry; import jalview.datamodel.SequenceI; +import jalview.gui.ImageExporter.ImageWriterI; import jalview.gui.StructureViewer.ViewerType; import jalview.structures.models.AAStructureBindingModel; import jalview.util.BrowserLauncher; +import jalview.util.ImageMaker; import jalview.util.MessageManager; import jalview.util.Platform; import jalview.ws.dbsources.Pdb; @@ -549,50 +551,29 @@ public class AppJmol extends StructureViewerBase return files; } - @Override - public void eps_actionPerformed(ActionEvent e) - { - makePDBImage(jalview.util.ImageMaker.TYPE.EPS); - } - - @Override - public void png_actionPerformed(ActionEvent e) - { - makePDBImage(jalview.util.ImageMaker.TYPE.PNG); - } - - void makePDBImage(jalview.util.ImageMaker.TYPE type) + /** + * Outputs the Jmol viewer image as an image file, after prompting the user to + * choose a file and (for EPS) choice of Text or Lineart character rendering + * (unless a preference for this is set) + * + * @param type + */ + public void makePDBImage(ImageMaker.TYPE type) { int width = getWidth(); int height = getHeight(); - - jalview.util.ImageMaker im; - - if (type == jalview.util.ImageMaker.TYPE.PNG) + ImageWriterI writer = new ImageWriterI() { - im = new jalview.util.ImageMaker(this, - jalview.util.ImageMaker.TYPE.PNG, "Make PNG image from view", - width, height, null, null, null, 0, false); - } - else if (type == jalview.util.ImageMaker.TYPE.EPS) - { - im = new jalview.util.ImageMaker(this, - jalview.util.ImageMaker.TYPE.EPS, "Make EPS file from view", - width, height, null, this.getTitle(), null, 0, false); - } - else - { - - im = new jalview.util.ImageMaker(this, - jalview.util.ImageMaker.TYPE.SVG, "Make SVG file from PCA", - width, height, null, this.getTitle(), null, 0, false); - } - - if (im.getGraphics() != null) - { - jmb.viewer.renderScreenImage(im.getGraphics(), width, height); - im.writeImage(); - } + @Override + public void exportImage(Graphics g) throws Exception + { + jmb.viewer.renderScreenImage(g, width, height); + } + }; + String view = MessageManager.getString("action.view").toLowerCase(); + ImageExporter exporter = new ImageExporter(writer, + jmb.getIProgressIndicator(), type, getTitle()); + exporter.doExport(null, this, width, height, view); } @Override diff --git a/src/jalview/gui/ChimeraViewFrame.java b/src/jalview/gui/ChimeraViewFrame.java index d07a7c2..9167c00 100644 --- a/src/jalview/gui/ChimeraViewFrame.java +++ b/src/jalview/gui/ChimeraViewFrame.java @@ -32,6 +32,7 @@ import jalview.io.DataSourceType; import jalview.io.StructureFile; import jalview.structures.models.AAStructureBindingModel; import jalview.util.BrowserLauncher; +import jalview.util.ImageMaker.TYPE; import jalview.util.MessageManager; import jalview.util.Platform; import jalview.ws.dbsources.Pdb; @@ -706,17 +707,10 @@ public class ChimeraViewFrame extends StructureViewerBase } @Override - public void eps_actionPerformed(ActionEvent e) + public void makePDBImage(TYPE imageType) { - throw new Error(MessageManager - .getString("error.eps_generation_not_implemented")); - } - - @Override - public void png_actionPerformed(ActionEvent e) - { - throw new Error(MessageManager - .getString("error.png_generation_not_implemented")); + throw new UnsupportedOperationException( + "Image export for Chimera is not implemented"); } @Override diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index 305d592..81aacda 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -26,6 +26,7 @@ import jalview.api.AlignViewportI; import jalview.api.AlignmentViewPanel; import jalview.bin.Cache; import jalview.bin.Jalview; +import jalview.gui.ImageExporter.ImageWriterI; import jalview.io.DataSourceType; import jalview.io.FileFormat; import jalview.io.FileFormatException; @@ -40,7 +41,7 @@ import jalview.jbgui.GSplitFrame; import jalview.jbgui.GStructureViewer; import jalview.structure.StructureSelectionManager; import jalview.urls.IdOrgSettings; -import jalview.util.ImageMaker; +import jalview.util.ImageMaker.TYPE; import jalview.util.MessageManager; import jalview.util.Platform; import jalview.util.UrlConstants; @@ -3299,28 +3300,39 @@ public class Desktop extends jalview.jbgui.GDesktop block.release(); } + /** + * Outputs an image of the desktop to file in EPS format, after prompting the + * user for choice of Text or Lineart character rendering (unless a preference + * has been set). The file name is generated as + * + *
+   * Jalview_snapshot_nnnnn.eps where nnnnn is the current timestamp in milliseconds
+   * 
+ */ @Override protected void snapShotWindow_actionPerformed(ActionEvent e) { + // currently the menu option to do this is not shown invalidate(); - File of; - ImageMaker im = new jalview.util.ImageMaker( - this, ImageMaker.TYPE.EPS, "View of Desktop", getWidth(), - getHeight(), of = new File("Jalview_snapshot" - + System.currentTimeMillis() + ".eps"), - "View of desktop", null, 0, false); - try - { - paintAll(im.getGraphics()); - im.writeImage(); - } catch (Exception q) + + int width = getWidth(); + int height = getHeight(); + File of = new File( + "Jalview_snapshot_" + System.currentTimeMillis() + ".eps"); + ImageWriterI writer = new ImageWriterI() { - Cache.log.error("Couldn't write snapshot to " + of.getAbsolutePath(), - q); - return; - } - Cache.log.info("Successfully written snapshot to file " - + of.getAbsolutePath()); + @Override + public void exportImage(Graphics g) throws Exception + { + paintAll(g); + Cache.log.info("Successfully written snapshot to file " + + of.getAbsolutePath()); + } + }; + String title = "View of desktop"; + ImageExporter exporter = new ImageExporter(writer, null, TYPE.EPS, + title); + exporter.doExport(of, this, width, height, title); } /** diff --git a/src/jalview/gui/ImageExporter.java b/src/jalview/gui/ImageExporter.java new file mode 100644 index 0000000..1f83b7c --- /dev/null +++ b/src/jalview/gui/ImageExporter.java @@ -0,0 +1,219 @@ +package jalview.gui; + +import jalview.bin.Cache; +import jalview.bin.Jalview; +import jalview.io.JalviewFileChooser; +import jalview.io.JalviewFileView; +import jalview.util.ImageMaker; +import jalview.util.ImageMaker.TYPE; +import jalview.util.MessageManager; +import jalview.util.dialogrunner.RunResponse; + +import java.awt.Component; +import java.awt.Graphics; +import java.io.File; +import java.util.concurrent.atomic.AtomicBoolean; + +import javax.swing.JOptionPane; + +/** + * A class that marshals steps in exporting a view in image graphics format + * + * + * @author gmcarstairs + * + */ +public class ImageExporter +{ + // todo move interface to jalview.api? or replace with lambda? + /** + * An interface for the callback that can be run to write the image on to the + * graphics object. The callback should throw any exceptions arising so they + * can be reported by this class. + */ + public interface ImageWriterI + { + void exportImage(Graphics g) + throws Exception; + } + + private IProgressIndicator messageBoard; + + private ImageWriterI imageWriter; + + TYPE imageType; + + private String title; + + /** + * Constructor given a callback handler to write graphics data, an (optional) + * target for status messages, image type and (optional) title for output file + * + * @param writer + * @param statusBar + * @param type + * @param fileTitle + */ + public ImageExporter(ImageWriterI writer, IProgressIndicator statusBar, + TYPE type, String fileTitle) + { + this.imageWriter = writer; + this.messageBoard = statusBar; + this.imageType = type; + this.title = fileTitle; + } + + /** + * Prompts the user for output file and Text/Lineart options as required, + * configures a Graphics context for output, and makes a callback to the + * client code to perform the image output + * + * @param file + * output file (if null, user is prompted to choose) + * @param parent + * parent component for any dialogs shown + * @param width + * @param height + * @param imageSource + * what the image is of e.g. Tree, Alignment + */ + public void doExport(File file, Component parent, int width, int height, + String imageSource) + { + final long messageId = System.currentTimeMillis(); + + /* + * prompt user for output file if not provided + */ + if (file == null && !Jalview.isHeadlessMode()) + { + JalviewFileChooser chooser = imageType.getFileChooser(); + chooser.setFileView(new JalviewFileView()); + MessageManager.formatMessage("label.create_image_of", + imageType.getName(), imageSource); + String title = "Create " + imageType.getName() + + " image from alignment"; + chooser.setDialogTitle(title); + chooser.setToolTipText(MessageManager.getString("action.save")); + int value = chooser.showSaveDialog(parent); + if (value != JalviewFileChooser.APPROVE_OPTION) + { + String msg = MessageManager.formatMessage( + "status.cancelled_image_export_operation", imageType.name); + setStatus(msg, messageId); + return; + } + Cache.setProperty("LAST_DIRECTORY", + chooser.getSelectedFile().getParent()); + file = chooser.getSelectedFile(); + } + + /* + * Prompt for Text or Lineart (EPS/SVG) unless a preference is already set + * for this as EPS_RENDERING / SVG_RENDERING + */ + String renderStyle = Cache.getDefault( + imageType.getName() + "_RENDERING", + LineartOptions.PROMPT_EACH_TIME); + AtomicBoolean textSelected = new AtomicBoolean( + !"Lineart".equals(renderStyle)); + if ((imageType == TYPE.EPS || imageType == TYPE.SVG) + && LineartOptions.PROMPT_EACH_TIME.equals(renderStyle) + && !Jalview.isHeadlessMode()) + { + final File chosenFile = file; + RunResponse okAction = new RunResponse(JOptionPane.OK_OPTION) + { + @Override + public void run() + { + exportImage(chosenFile, !textSelected.get(), width, height, + messageId); + } + }; + LineartOptions epsOption = new LineartOptions(TYPE.EPS.getName(), + textSelected); + epsOption.setResponseAction(new RunResponse(JOptionPane.NO_OPTION) + { + @Override + public void run() + { + setStatus(MessageManager.formatMessage( + "status.cancelled_image_export_operation", + imageType.getName()), messageId); + } + }); + epsOption.setResponseAction(okAction); + epsOption.showDialog(); + /* no code here - JalviewJS cannot execute it */ + } + else + { + /* + * character rendering not required, or preference already set + * - just do the export + */ + exportImage(file, !textSelected.get(), width, height, messageId); + } + } + + /** + * Constructs a suitable graphics context and passes it to the callback + * handler for the image to be written. Shows status messages for export in + * progress, complete, or failed as appropriate. + * + * @param chosenFile + * @param asLineart + * @param width + * @param height + * @param messageId + */ + protected void exportImage(File chosenFile, boolean asLineart, int width, + int height, long messageId) + { + String type = imageType.getName(); + try + { + setStatus( + MessageManager.formatMessage( + "status.exporting_alignment_as_x_file", type), + messageId); + ImageMaker im = new ImageMaker(imageType, width, height, chosenFile, + title, asLineart); + imageWriter.exportImage(im.getGraphics()); + im.writeImage(); + setStatus( + MessageManager.formatMessage("status.export_complete", type), + messageId); + } catch (Exception e) + { + System.out + .println(String.format("Error creating %s file: %s", type, + e.toString())); + setStatus(MessageManager.formatMessage("info.error_creating_file", + type), messageId); + } + } + + /** + * Asks the callback to show a status message with given id + * + * @param msg + * @param id + */ + void setStatus(String msg, long id) + { + if (messageBoard != null) + { + messageBoard.setProgressBar(msg, id); + } + } + +} \ No newline at end of file diff --git a/src/jalview/gui/LineartOptions.java b/src/jalview/gui/LineartOptions.java index 1dd030b..a00d1e9 100644 --- a/src/jalview/gui/LineartOptions.java +++ b/src/jalview/gui/LineartOptions.java @@ -38,10 +38,12 @@ import javax.swing.JRadioButton; /** * A dialog where the user may choose Text or Lineart rendering, and optionally - * save this as a preference + * save this as a preference ("Don't ask me again") */ public class LineartOptions extends JPanel { + static final String PROMPT_EACH_TIME = "Prompt each time"; + JvOptionPane dialog; public boolean cancelled = false; @@ -61,22 +63,22 @@ public class LineartOptions extends JPanel * mutable boolean object. User action in the dialog should update this * object, and the same object should be used in any action handler * set by calling setResponseAction. + *

+ * If the user chooses an option and also "Don't ask me again", the chosen + * option is saved as a property with key type_RENDERING i.e. "EPS_RENDERING", + * "SVG_RENDERING" or "HTML_RENDERING". * - * @param preferencesKey - * the key under which the choice is saved as a user preference, if - * 'Don't ask me again' is selected * @param formatType * image type e.g. EPS, SVG * @param textOption * true to select Text, false for Lineart */ - public LineartOptions(String preferencesKey, String formatType, - AtomicBoolean textOption) + public LineartOptions(String formatType, AtomicBoolean textOption) { this.asText = textOption; dialogTitle = MessageManager.formatMessage( "label.select_character_style_title", formatType); - + String preferencesKey = formatType + "_RENDERING"; try { jbInit(preferencesKey, formatType); diff --git a/src/jalview/gui/PCAPanel.java b/src/jalview/gui/PCAPanel.java index d2f789f..aea94f4 100644 --- a/src/jalview/gui/PCAPanel.java +++ b/src/jalview/gui/PCAPanel.java @@ -27,11 +27,12 @@ import jalview.api.analysis.SimilarityParamsI; import jalview.datamodel.Alignment; import jalview.datamodel.AlignmentI; import jalview.datamodel.AlignmentView; -import jalview.datamodel.Annotation; import jalview.datamodel.HiddenColumns; import jalview.datamodel.SequenceI; +import jalview.gui.ImageExporter.ImageWriterI; import jalview.gui.JalviewColourChooser.ColourChooserListener; import jalview.jbgui.GPCAPanel; +import jalview.util.ImageMaker; import jalview.util.MessageManager; import jalview.viewmodel.AlignmentViewport; import jalview.viewmodel.PCAModel; @@ -49,8 +50,6 @@ import java.awt.print.PrinterJob; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; -import javax.swing.JColorChooser; -import javax.swing.JDialog; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; import javax.swing.event.InternalFrameAdapter; @@ -519,67 +518,26 @@ public class PCAPanel extends GPCAPanel } } - /** - * DOCUMENT ME! - * - * @param e - * DOCUMENT ME! - */ - @Override - public void eps_actionPerformed(ActionEvent e) - { - makePCAImage(jalview.util.ImageMaker.TYPE.EPS); - } - - /** - * DOCUMENT ME! - * - * @param e - * DOCUMENT ME! - */ - @Override - public void png_actionPerformed(ActionEvent e) - { - makePCAImage(jalview.util.ImageMaker.TYPE.PNG); - } - - void makePCAImage(jalview.util.ImageMaker.TYPE type) + public void makePCAImage(ImageMaker.TYPE type) { int width = rc.getWidth(); int height = rc.getHeight(); - - jalview.util.ImageMaker im; - - if (type == jalview.util.ImageMaker.TYPE.PNG) - { - im = new jalview.util.ImageMaker(this, - jalview.util.ImageMaker.TYPE.PNG, "Make PNG image from PCA", - width, height, null, null, null, 0, false); - } - else if (type == jalview.util.ImageMaker.TYPE.EPS) - { - im = new jalview.util.ImageMaker(this, - jalview.util.ImageMaker.TYPE.EPS, "Make EPS file from PCA", - width, height, null, this.getTitle(), null, 0, false); - } - else - { - im = new jalview.util.ImageMaker(this, - jalview.util.ImageMaker.TYPE.SVG, "Make SVG file from PCA", - width, height, null, this.getTitle(), null, 0, false); - - } - - if (im.getGraphics() != null) + ImageWriterI writer = new ImageWriterI() { - rc.drawBackground(im.getGraphics(), Color.black); - rc.drawScene(im.getGraphics()); - if (rc.drawAxes == true) + @Override + public void exportImage(Graphics g) throws Exception { - rc.drawAxes(im.getGraphics()); + rc.drawBackground(g, Color.black); + rc.drawScene(g); + if (rc.drawAxes) + { + rc.drawAxes(g); + } } - im.writeImage(); - } + }; + String pca = MessageManager.getString("label.pca"); + ImageExporter exporter = new ImageExporter(writer, null, type, pca); + exporter.doExport(null, this, width, height, pca); } @Override diff --git a/src/jalview/gui/TreePanel.java b/src/jalview/gui/TreePanel.java index fefa77f..5450102 100755 --- a/src/jalview/gui/TreePanel.java +++ b/src/jalview/gui/TreePanel.java @@ -28,7 +28,6 @@ import jalview.analysis.TreeModel; import jalview.analysis.scoremodels.ScoreModels; import jalview.api.analysis.ScoreModelI; import jalview.api.analysis.SimilarityParamsI; -import jalview.bin.Cache; import jalview.commands.CommandI; import jalview.commands.OrderCommand; import jalview.datamodel.Alignment; @@ -41,32 +40,28 @@ import jalview.datamodel.NodeTransformI; import jalview.datamodel.SequenceFeature; import jalview.datamodel.SequenceI; import jalview.datamodel.SequenceNode; +import jalview.gui.ImageExporter.ImageWriterI; import jalview.io.JalviewFileChooser; import jalview.io.JalviewFileView; import jalview.io.NewickFile; import jalview.jbgui.GTreePanel; -import jalview.util.ImageMaker; +import jalview.util.ImageMaker.TYPE; import jalview.util.MessageManager; -import jalview.util.dialogrunner.RunResponse; import jalview.viewmodel.AlignmentViewport; import java.awt.Font; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.awt.image.BufferedImage; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.atomic.AtomicBoolean; -import javax.imageio.ImageIO; import javax.swing.ButtonGroup; import javax.swing.JMenuItem; -import javax.swing.JOptionPane; import javax.swing.JRadioButtonMenuItem; import javax.swing.event.InternalFrameAdapter; import javax.swing.event.InternalFrameEvent; @@ -662,115 +657,27 @@ public class TreePanel extends GTreePanel } /** - * Outputs the Tree in EPS format. The user is prompted for the file to save - * to, and (unless a preference is already set) for the choice of Text or - * Lineart for character rendering. + * Outputs the Tree in image format (currently EPS or PNG). The user is + * prompted for the file to save to, and for EPS (unless a preference is + * already set) for the choice of Text or Lineart for character rendering. */ @Override - public void epsTree_actionPerformed() - { - JalviewFileChooser chooser = new JalviewFileChooser( - ImageMaker.EPS_EXTENSION, ImageMaker.EPS_EXTENSION); - chooser.setFileView(new JalviewFileView()); - chooser.setDialogTitle( - MessageManager.getString("label.create_eps_from_tree")); - chooser.setToolTipText(MessageManager.getString("action.save")); - - int value = chooser.showSaveDialog(this); - - if (value != JalviewFileChooser.APPROVE_OPTION) - { - return; - } - File outFile = chooser.getSelectedFile(); - Cache.setProperty("LAST_DIRECTORY", outFile.getParent()); - - String renderStyle = Cache.getDefault("EPS_RENDERING", - "Prompt each time"); - AtomicBoolean textOption = new AtomicBoolean( - !"Lineart".equals(renderStyle)); - - /* - * configure the export action to run on OK in the dialog - */ - RunResponse okAction = new RunResponse(JOptionPane.OK_OPTION) - { - @Override - public void run() - { - writeEpsFile(outFile, textOption.get()); - } - }; - - /* - * Prompt for character rendering style if preference is not set - */ - if (renderStyle.equalsIgnoreCase("Prompt each time") - && !(System.getProperty("java.awt.headless") != null && System - .getProperty("java.awt.headless").equals("true"))) - { - LineartOptions eps = new LineartOptions("EPS_RENDERING", "EPS", - textOption); - eps.setResponseAction(okAction); - eps.showDialog(); - /* no code here - JalviewJS won't execute it */ - } - else - { - /* - * if preference set, just run the export action - */ - writeEpsFile(outFile, textOption.get()); - } - } - - /** - * DOCUMENT ME! - * - * @param e - * DOCUMENT ME! - */ - @Override - public void pngTree_actionPerformed(ActionEvent e) + public void writeTreeImage(TYPE imageFormat) { int width = treeCanvas.getWidth(); int height = treeCanvas.getHeight(); - - try + ImageWriterI writer = new ImageWriterI() { - JalviewFileChooser chooser = new JalviewFileChooser( - ImageMaker.PNG_EXTENSION, ImageMaker.PNG_DESCRIPTION); - - chooser.setFileView(new jalview.io.JalviewFileView()); - chooser.setDialogTitle( - MessageManager.getString("label.create_png_from_tree")); - chooser.setToolTipText(MessageManager.getString("action.save")); - - int value = chooser.showSaveDialog(this); - - if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION) + @Override + public void exportImage(Graphics g) throws Exception { - return; + treeCanvas.draw(g, width, height); } - - jalview.bin.Cache.setProperty("LAST_DIRECTORY", - chooser.getSelectedFile().getParent()); - - FileOutputStream out = new FileOutputStream( - chooser.getSelectedFile()); - - BufferedImage bi = new BufferedImage(width, height, - BufferedImage.TYPE_INT_RGB); - Graphics png = bi.getGraphics(); - - treeCanvas.draw(png, width, height); - - ImageIO.write(bi, "png", out); - out.close(); - } catch (Exception ex) - { - ex.printStackTrace(); - } + }; + String tree = MessageManager.getString("label.tree"); + ImageExporter exporter = new ImageExporter(writer, null, imageFormat, + tree); + exporter.doExport(null, this, width, height, tree.toLowerCase()); } /** diff --git a/src/jalview/io/BioJsHTMLOutput.java b/src/jalview/io/BioJsHTMLOutput.java index 52ce845..11436d3 100644 --- a/src/jalview/io/BioJsHTMLOutput.java +++ b/src/jalview/io/BioJsHTMLOutput.java @@ -20,7 +20,6 @@ */ package jalview.io; -import jalview.exceptions.NoFileSelectedException; import jalview.gui.AlignmentPanel; import jalview.gui.OOMWarning; import jalview.json.binding.biojs.BioJSReleasePojo; @@ -41,7 +40,6 @@ import java.util.TreeMap; public class BioJsHTMLOutput extends HTMLOutput { - private static File currentBJSTemplateFile; private static TreeMap bioJsMSAVersions; @@ -61,33 +59,6 @@ public class BioJsHTMLOutput extends HTMLOutput super(ap); } - @Override - public void exportHTML(String outputFile) - { - exportStarted(); - try - { - if (outputFile == null) - { - outputFile = getOutputFile(); - } - generatedFile = new File(outputFile); - } catch (NoFileSelectedException e) - { - setProgressMessage(MessageManager.formatMessage( - "status.cancelled_image_export_operation", "BioJS MSA")); - return; - } catch (Exception e) - { - setProgressMessage(MessageManager - .formatMessage("info.error_creating_file", "BioJS MSA")); - e.printStackTrace(); - return; - } - new Thread(this).start(); - - } - public static void refreshVersionInfo(String dirName) throws URISyntaxException { @@ -271,12 +242,6 @@ public class BioJsHTMLOutput extends HTMLOutput } @Override - public File getExportedFile() - { - return generatedFile; - } - - @Override public void run() { try @@ -293,7 +258,7 @@ public class BioJsHTMLOutput extends HTMLOutput out.flush(); out.close(); setProgressMessage(MessageManager - .formatMessage("status.export_complete", "BioJS")); + .formatMessage("status.export_complete", getDescription())); exportCompleted(); } catch (OutOfMemoryError err) @@ -304,10 +269,16 @@ public class BioJsHTMLOutput extends HTMLOutput } catch (Exception e) { setProgressMessage(MessageManager - .formatMessage("info.error_creating_file", "HTML")); + .formatMessage("info.error_creating_file", getDescription())); e.printStackTrace(); } } + @Override + protected String getDescription() + { + return "BioJS MSA"; + } + } diff --git a/src/jalview/io/HTMLOutput.java b/src/jalview/io/HTMLOutput.java index 86da67a..f7179cf 100644 --- a/src/jalview/io/HTMLOutput.java +++ b/src/jalview/io/HTMLOutput.java @@ -21,9 +21,9 @@ package jalview.io; import jalview.api.AlignExportSettingsI; +import jalview.bin.Cache; import jalview.datamodel.AlignExportSettingsAdapter; import jalview.datamodel.AlignmentExportData; -import jalview.exceptions.NoFileSelectedException; import jalview.gui.AlignmentPanel; import jalview.gui.IProgressIndicator; import jalview.util.MessageManager; @@ -187,15 +187,15 @@ public abstract class HTMLOutput implements Runnable } - public String getOutputFile() throws NoFileSelectedException + /** + * Prompts the user to choose an output file and returns the file path, or + * null on Cancel + * + * @return + */ + public String getOutputFile() { String selectedFile = null; - if (pIndicator != null && !isHeadless()) - { - pIndicator.setProgressBar(MessageManager.formatMessage( - "status.waiting_for_user_to_select_output_file", "HTML"), - pSessionId); - } // TODO: JAL-3048 generate html rendered view (requires SvgGraphics and/or // Jalview HTML rendering system- probably not required for Jalview-JS) @@ -210,14 +210,11 @@ public abstract class HTMLOutput implements Runnable int fileChooserOpt = jvFileChooser.showSaveDialog(null); if (fileChooserOpt == JalviewFileChooser.APPROVE_OPTION) { - jalview.bin.Cache.setProperty("LAST_DIRECTORY", + Cache.setProperty("LAST_DIRECTORY", jvFileChooser.getSelectedFile().getParent()); selectedFile = jvFileChooser.getSelectedFile().getPath(); } - else - { - throw new NoFileSelectedException("No file was selected."); - } + return selectedFile; } @@ -295,13 +292,47 @@ public abstract class HTMLOutput implements Runnable * * @return */ - public abstract File getExportedFile(); + public File getExportedFile() + { + return generatedFile; + } + + public void exportHTML(String outputFile) + { + exportStarted(); + try + { + if (outputFile == null) + { + /* + * prompt for output file + */ + outputFile = getOutputFile(); + if (outputFile == null) + { + setProgressMessage(MessageManager.formatMessage( + "status.cancelled_image_export_operation", + getDescription())); + return; + } + } + generatedFile = new File(outputFile); + } catch (Exception e) + { + setProgressMessage(MessageManager + .formatMessage("info.error_creating_file", getDescription())); + e.printStackTrace(); + return; + } + new Thread(this).start(); + + } /** - * This is the main method to handle the HTML generation. + * Answers a short description of the image format suitable for display in + * messages * - * @param outputFile - * the file path of the generated HTML + * @return */ - public abstract void exportHTML(String outputFile); + protected abstract String getDescription(); } \ No newline at end of file diff --git a/src/jalview/io/HtmlSvgOutput.java b/src/jalview/io/HtmlSvgOutput.java index 30c40a9..a943a81 100644 --- a/src/jalview/io/HtmlSvgOutput.java +++ b/src/jalview/io/HtmlSvgOutput.java @@ -21,7 +21,6 @@ package jalview.io; import jalview.bin.Cache; -import jalview.exceptions.NoFileSelectedException; import jalview.gui.AlignmentPanel; import jalview.gui.LineartOptions; import jalview.gui.OOMWarning; @@ -43,7 +42,6 @@ import org.jfree.graphics2d.svg.SVGHints; public class HtmlSvgOutput extends HTMLOutput { - public HtmlSvgOutput(AlignmentPanel ap) { super(ap); @@ -58,13 +56,14 @@ public class HtmlSvgOutput extends HTMLOutput if (outputFile == null) { outputFile = getOutputFile(); + if (outputFile == null) + { + setProgressMessage(MessageManager.formatMessage( + "status.cancelled_image_export_operation", "HTML")); + return; + } } generatedFile = new File(outputFile); - } catch (NoFileSelectedException e) - { - setProgressMessage(MessageManager.formatMessage( - "status.cancelled_image_export_operation", "HTML")); - return; } catch (Exception e) { setProgressMessage(MessageManager @@ -230,19 +229,13 @@ public class HtmlSvgOutput extends HTMLOutput } @Override - public File getExportedFile() - { - return generatedFile; - } - - @Override public void run() { try { setProgressMessage(null); setProgressMessage(MessageManager.formatMessage( - "status.exporting_alignment_as_x_file", "HTML")); + "status.exporting_alignment_as_x_file", getDescription())); String renderStyle = Cache.getDefault("HTML_RENDERING", "Prompt each time"); @@ -266,15 +259,15 @@ public class HtmlSvgOutput extends HTMLOutput */ if (renderStyle.equalsIgnoreCase("Prompt each time") && !isHeadless()) { - LineartOptions svgOption = new LineartOptions("HTML_RENDERING", - "HTML", textOption); + LineartOptions svgOption = new LineartOptions("HTML", textOption); svgOption.setResponseAction(new RunResponse(JOptionPane.NO_OPTION) { @Override public void run() { setProgressMessage(MessageManager.formatMessage( - "status.cancelled_image_export_operation", "HTML")); + "status.cancelled_image_export_operation", + getDescription())); } }); svgOption.setResponseAction(okAction); @@ -297,7 +290,7 @@ public class HtmlSvgOutput extends HTMLOutput { e.printStackTrace(); setProgressMessage(MessageManager - .formatMessage("info.error_creating_file", "HTML")); + .formatMessage("info.error_creating_file", getDescription())); } } @@ -347,13 +340,19 @@ public class HtmlSvgOutput extends HTMLOutput out.flush(); out.close(); setProgressMessage(MessageManager - .formatMessage("status.export_complete", "HTML")); + .formatMessage("status.export_complete", getDescription())); exportCompleted(); } catch (Exception e) { e.printStackTrace(); setProgressMessage(MessageManager - .formatMessage("info.error_creating_file", "HTML")); + .formatMessage("info.error_creating_file", getDescription())); } } + + @Override + protected String getDescription() + { + return "HTML"; + } } diff --git a/src/jalview/jbgui/GPCAPanel.java b/src/jalview/jbgui/GPCAPanel.java index a183794..4a9770f 100755 --- a/src/jalview/jbgui/GPCAPanel.java +++ b/src/jalview/jbgui/GPCAPanel.java @@ -20,6 +20,7 @@ */ package jalview.jbgui; +import jalview.util.ImageMaker.TYPE; import jalview.util.MessageManager; import java.awt.BorderLayout; @@ -152,7 +153,7 @@ public class GPCAPanel extends JInternalFrame @Override public void actionPerformed(ActionEvent e) { - eps_actionPerformed(e); + makePCAImage(TYPE.EPS); } }); JMenuItem png = new JMenuItem("PNG"); @@ -161,7 +162,7 @@ public class GPCAPanel extends JInternalFrame @Override public void actionPerformed(ActionEvent e) { - png_actionPerformed(e); + makePCAImage(TYPE.PNG); } }); JMenuItem outputValues = new JMenuItem(); @@ -384,14 +385,8 @@ public class GPCAPanel extends JInternalFrame { } - public void eps_actionPerformed(ActionEvent e) + public void makePCAImage(TYPE imageType) { - - } - - public void png_actionPerformed(ActionEvent e) - { - } public void outputValues_actionPerformed(ActionEvent e) diff --git a/src/jalview/jbgui/GStructureViewer.java b/src/jalview/jbgui/GStructureViewer.java index 83d8590..97cdf125 100644 --- a/src/jalview/jbgui/GStructureViewer.java +++ b/src/jalview/jbgui/GStructureViewer.java @@ -22,6 +22,7 @@ package jalview.jbgui; import jalview.api.structures.JalviewStructureDisplayI; import jalview.gui.ColourMenuHelper.ColourChangeListener; +import jalview.util.ImageMaker.TYPE; import jalview.util.MessageManager; import java.awt.BorderLayout; @@ -115,7 +116,7 @@ public abstract class GStructureViewer extends JInternalFrame @Override public void actionPerformed(ActionEvent actionEvent) { - png_actionPerformed(actionEvent); + makePDBImage(TYPE.PNG); } }); @@ -126,7 +127,7 @@ public abstract class GStructureViewer extends JInternalFrame @Override public void actionPerformed(ActionEvent actionEvent) { - eps_actionPerformed(actionEvent); + makePDBImage(TYPE.EPS); } }); @@ -228,12 +229,7 @@ public abstract class GStructureViewer extends JInternalFrame } - public void png_actionPerformed(ActionEvent actionEvent) - { - - } - - public void eps_actionPerformed(ActionEvent actionEvent) + public void makePDBImage(TYPE imageType) { } diff --git a/src/jalview/jbgui/GTreePanel.java b/src/jalview/jbgui/GTreePanel.java index 916a98f..150640f 100755 --- a/src/jalview/jbgui/GTreePanel.java +++ b/src/jalview/jbgui/GTreePanel.java @@ -20,6 +20,7 @@ */ package jalview.jbgui; +import jalview.util.ImageMaker.TYPE; import jalview.util.MessageManager; import java.awt.BorderLayout; @@ -175,7 +176,7 @@ public class GTreePanel extends JInternalFrame { public void actionPerformed(ActionEvent e) { - epsTree_actionPerformed(); + writeTreeImage(TYPE.EPS); } }); pngTree.setText("PNG"); @@ -183,7 +184,7 @@ public class GTreePanel extends JInternalFrame { public void actionPerformed(ActionEvent e) { - pngTree_actionPerformed(e); + writeTreeImage(TYPE.PNG); } }); saveAsMenu.setText(MessageManager.getString("action.save_as")); @@ -255,11 +256,7 @@ public class GTreePanel extends JInternalFrame { } - public void pngTree_actionPerformed(ActionEvent e) - { - } - - public void epsTree_actionPerformed() + public void writeTreeImage(TYPE imageType) { } diff --git a/src/jalview/util/ImageMaker.java b/src/jalview/util/ImageMaker.java index 64771c4..162e79f 100755 --- a/src/jalview/util/ImageMaker.java +++ b/src/jalview/util/ImageMaker.java @@ -20,24 +20,17 @@ */ package jalview.util; -import jalview.bin.Cache; -import jalview.bin.Jalview; -import jalview.gui.IProgressIndicator; -import jalview.gui.LineartOptions; import jalview.io.JalviewFileChooser; -import jalview.util.dialogrunner.RunResponse; -import java.awt.Component; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; -import java.util.concurrent.atomic.AtomicBoolean; +import java.io.IOException; import javax.imageio.ImageIO; -import javax.swing.JOptionPane; import org.jfree.graphics2d.svg.SVGGraphics2D; import org.jfree.graphics2d.svg.SVGHints; @@ -57,10 +50,6 @@ public class ImageMaker public static final String PNG_DESCRIPTION = "Portable network graphics"; - public static final String HTML_EXTENSION = "html"; - - public static final String HTML_DESCRIPTION = "Hypertext Markup Language"; - EpsGraphics2D pg; Graphics graphics; @@ -71,12 +60,6 @@ public class ImageMaker TYPE type; - private IProgressIndicator pIndicator; - - private long pSessionId; - - private boolean headless; - public enum TYPE { EPS("EPS", MessageManager.getString("label.eps_file"), EPS_EXTENSION, @@ -119,79 +102,34 @@ public class ImageMaker } /** - * Constructor builds the image and writes it to file. If the supplied file - * name is null, the user is prompted for the output file. + * Constructor configures the graphics context ready for writing to * - * @param parent - * @param type - * @param title + * @param imageType * @param width * @param height * @param file * @param fileTitle - * @param pIndicator - * @param pSessionId - * @param headless + * @param useLineart + * @throws IOException */ - public ImageMaker(Component parent, TYPE type, String title, int width, - int height, File file, String fileTitle, - IProgressIndicator pIndicator, long pSessionId, boolean headless) + public ImageMaker(TYPE imageType, int width, int height, + File file, String fileTitle, boolean useLineart) + throws IOException { - this.pIndicator = pIndicator; - this.type = type; - this.pSessionId = pSessionId; - this.headless = headless; - if (file == null && !Jalview.isHeadlessMode()) + this.type = imageType; + + out = new FileOutputStream(file); + if (imageType == TYPE.SVG) { - setProgressMessage(MessageManager.formatMessage( - "status.waiting_for_user_to_select_output_file", type.name)); - JalviewFileChooser chooser = type.getFileChooser(); - chooser.setFileView(new jalview.io.JalviewFileView()); - chooser.setDialogTitle(title); - chooser.setToolTipText(MessageManager.getString("action.save")); - int value = chooser.showSaveDialog(parent); - - if (value == JalviewFileChooser.APPROVE_OPTION) - { - Cache.setProperty("LAST_DIRECTORY", - chooser.getSelectedFile().getParent()); - file = chooser.getSelectedFile(); - } - else - { - setProgressMessage(MessageManager.formatMessage( - "status.cancelled_image_export_operation", type.name)); - } + setupSVG(width, height, useLineart); } - - if (file != null) + else if (imageType == TYPE.EPS) { - try - { - out = new FileOutputStream(file); - setProgressMessage(null); - setProgressMessage(MessageManager.formatMessage( - "status.exporting_alignment_as_x_file", type.getName())); - if (type == TYPE.SVG) - { - setupSVG(width, height, fileTitle); - } - else if (type == TYPE.EPS) - { - setupEPS(width, height, fileTitle); - } - else if (type == TYPE.PNG) - { - setupPNG(width, height); - } - - } catch (Exception ex) - { - System.out.println("Error creating " + type.getName() + " file."); - - setProgressMessage(MessageManager - .formatMessage("info.error_creating_file", type.getName())); - } + setupEPS(width, height, fileTitle, useLineart); + } + else if (imageType == TYPE.PNG) + { + setupPNG(width, height); } } @@ -234,146 +172,18 @@ public class ImageMaker } /** - * Generates an EPS image and writes it to the (previously set) output buffer. - * The user is first prompted for choice of Text or Lineart rendering, unless - * a preference for this has been set. - * - * @param width - * @param height - * @param title - */ - void setupEPS(int width, int height, String title) - { - String renderStyle = Cache.getDefault("EPS_RENDERING", - "Prompt each time"); - AtomicBoolean textOption = new AtomicBoolean( - !"Lineart".equals(renderStyle)); - - /* - * configure the action to run on OK in the dialog - */ - RunResponse okAction = new RunResponse(JOptionPane.OK_OPTION) - { - @Override - public void run() - { - writeEPS(width, height, title, textOption.get()); - } - }; - - /* - * Prompt for character rendering style if preference is not set - */ - if (renderStyle.equalsIgnoreCase("Prompt each time") - && !(System.getProperty("java.awt.headless") != null && System - .getProperty("java.awt.headless").equals("true"))) - { - LineartOptions epsOption = new LineartOptions("EPS_RENDERING", - TYPE.EPS.getName(), textOption); - epsOption.setResponseAction(new RunResponse(JOptionPane.NO_OPTION) - { - @Override - public void run() - { - setProgressMessage(MessageManager.formatMessage( - "status.cancelled_image_export_operation", "EPS")); - } - }); - epsOption.setResponseAction(okAction); - epsOption.showDialog(); - /* no code here - JalviewJS cannot execute it */ - } - else - { - /* - * else (if preference set) just do the export action - */ - writeEPS(width, height, title, textOption.get()); - } - } - - /** * Sets up a graphics object for the PNG image to be written on * * @param width * @param height */ - void setupPNG(int width, int height) + protected void setupPNG(int width, int height) { bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); graphics = bi.getGraphics(); Graphics2D ig2 = (Graphics2D) graphics; ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - setProgressMessage(MessageManager - .formatMessage("status.export_complete", type.getName())); - } - - /** - * Sets up a graphics object for the SVG image to be written on. The user is - * first prompted for choice of Text or Lineart rendering, unless a preference - * for this has been set. - * - * @param width - * @param height - * @param title - */ - void setupSVG(int width, int height, String title) - { - String renderStyle = Cache.getDefault("SVG_RENDERING", - "Prompt each time"); - AtomicBoolean textOption = new AtomicBoolean( - !"Lineart".equals(renderStyle)); - - /* - * configure the action to run on OK in the dialog - */ - RunResponse okAction = new RunResponse(JOptionPane.OK_OPTION) - { - @Override - public void run() - { - setupSVG(width, height, textOption.get()); - } - }; - - /* - * Prompt for character rendering style if preference is not set - */ - if (renderStyle.equalsIgnoreCase("Prompt each time") - && !(System.getProperty("java.awt.headless") != null && System - .getProperty("java.awt.headless").equals("true"))) - { - LineartOptions svgOption = new LineartOptions("SVG_RENDERING", - TYPE.SVG.getName(), textOption); - svgOption.setResponseAction(new RunResponse(JOptionPane.NO_OPTION) - { - @Override - public void run() - { - setProgressMessage(MessageManager.formatMessage( - "status.cancelled_image_export_operation", "SVG")); - } - }); - svgOption.setResponseAction(okAction); - svgOption.showDialog(); - /* no code here - JalviewJS cannot execute it */ - } - else - { - /* - * else (if preference set) just do the export action - */ - setupSVG(width, height, textOption.get()); - } - } - - void setProgressMessage(String message) - { - if (pIndicator != null && !headless) - { - pIndicator.setProgressBar(message, pSessionId); - } } /** @@ -382,19 +192,17 @@ public class ImageMaker * * @param width * @param height - * @param textOption - * true for Text, false for Lineart + * @param useLineart + * true for Lineart character rendering, false for Text */ - protected void setupSVG(int width, int height, boolean textOption) + protected void setupSVG(int width, int height, boolean useLineart) { SVGGraphics2D g2 = new SVGGraphics2D(width, height); - if (!textOption) // Lineart selected + if (useLineart) { g2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE, SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR); } - setProgressMessage(MessageManager - .formatMessage("status.export_complete", type.getName())); graphics = g2; } @@ -405,34 +213,18 @@ public class ImageMaker * @param width * @param height * @param title - * @param textOption - * true for Text, false for Lineart + * @param useLineart + * true for Lineart character rendering, false for Text + * @throws IOException */ - protected void writeEPS(int width, int height, String title, - boolean textOption) + protected void setupEPS(int width, int height, String title, + boolean useLineart) throws IOException { - try - { - pg = new EpsGraphics2D(title, out, 0, 0, width, height); - Graphics2D ig2 = pg; - ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, - RenderingHints.VALUE_ANTIALIAS_ON); - pg.setAccurateTextMode(!textOption); // true = Lineart - graphics = pg; - setProgressMessage(MessageManager - .formatMessage("status.export_complete", type.getName())); - } catch (Exception ex) - { - System.err.println("Error writing PNG: " + ex.toString()); - } - } - - static JalviewFileChooser getSVGChooser() - { - if (Jalview.isHeadlessMode()) - { - return null; - } - return new JalviewFileChooser(SVG_EXTENSION, SVG_DESCRIPTION); + pg = new EpsGraphics2D(title, out, 0, 0, width, height); + Graphics2D ig2 = pg; + ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + pg.setAccurateTextMode(useLineart); + graphics = pg; } } diff --git a/src/jalview/util/dialogrunner/Response.java b/src/jalview/util/dialogrunner/Response.java index 3c771b8..f168e20 100644 --- a/src/jalview/util/dialogrunner/Response.java +++ b/src/jalview/util/dialogrunner/Response.java @@ -108,7 +108,7 @@ public class Response case 1: return "DialogRunner str: '" + stringresp + "'"; case 2: - return "DialogRunner obj: " + objresp.toString(); + return "DialogRunner obj: " + String.valueOf(objresp); } return "Unconfigured response."; } diff --git a/src/org/jibble/epsgraphics/EpsDocument.java b/src/org/jibble/epsgraphics/EpsDocument.java index 7d25364..4a58098 100644 --- a/src/org/jibble/epsgraphics/EpsDocument.java +++ b/src/org/jibble/epsgraphics/EpsDocument.java @@ -20,8 +20,6 @@ */ package org.jibble.epsgraphics; -import jalview.util.MessageManager; - import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStream; @@ -164,9 +162,7 @@ public class EpsDocument _bufferedWriter.write(line + "\n"); } catch (IOException e) { - throw new EpsException(MessageManager.formatMessage( - "exception.eps_coudnt_write_output_file", - new String[] { e.getMessage() })); + throw new EpsException("Could not write to the output file: " + e); } } -- 1.7.10.2