/*
* 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.io;
import jalview.api.AlignExportSettingI;
import jalview.api.FeatureRenderer;
import jalview.datamodel.AlignmentExportData;
import jalview.gui.AlignViewport;
import jalview.gui.AlignmentPanel;
import jalview.gui.HTMLOptions;
import jalview.gui.IProgressIndicator;
import jalview.gui.OOMWarning;
import jalview.math.AlignmentDimension;
import jalview.util.MessageManager;
import java.awt.Graphics;
import java.awt.print.PrinterException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jfree.graphics2d.svg.SVGGraphics2D;
import org.jfree.graphics2d.svg.SVGHints;
public class HtmlSvgOutput
{
AlignViewport av;
FeatureRenderer fr;
AlignmentPanel ap;
private IProgressIndicator pIndicator;
private long pSessionId;
private boolean headless;
public HtmlSvgOutput(AlignmentPanel ap)
{
this.av = ap.av;
this.ap = ap;
fr = ap.cloneFeatureRenderer();
}
public void generateHtmlSvgOutput(File file)
{
pIndicator = ap.alignFrame;
pSessionId = System.currentTimeMillis();
try
{
headless = (System.getProperty("java.awt.headless") != null && System
.getProperty("java.awt.headless").equals("true"));
if (file == null)
{
setProgressMessage(MessageManager.formatMessage(
"status.waiting_for_user_to_select_output_file", "HTML"));
JalviewFileChooser chooser = getHTMLChooser();
chooser.setFileView(new jalview.io.JalviewFileView());
chooser.setDialogTitle(ap.alignFrame.getTitle());
chooser.setToolTipText(MessageManager.getString("action.save"));
int value = chooser.showSaveDialog(ap.alignFrame);
if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
{
jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
.getSelectedFile().getParent());
file = chooser.getSelectedFile();
ap.alignFrame.repaint();
}
else
{
setProgressMessage(MessageManager.formatMessage(
"status.cancelled_image_export_operation", "HTML"));
return;
}
}
} catch (Exception e)
{
pIndicator.setProgressBar(MessageManager.formatMessage(
"info.error_creating_file", "HTML"), pSessionId);
e.printStackTrace();
return;
}
final File fileX = file;
new Thread()
{
@Override
public void run()
{
try
{
setProgressMessage(null);
setProgressMessage(MessageManager.formatMessage(
"status.exporting_alignment_as_x_file", "HTML"));
AlignmentDimension aDimension = ap.getAlignmentDimension();
SVGGraphics2D idPanelGraphics = new SVGGraphics2D(
aDimension.getWidth(),
aDimension.getHeight());
SVGGraphics2D alignPanelGraphics = new SVGGraphics2D(
aDimension.getWidth(),
aDimension.getHeight());
String renderStyle = jalview.bin.Cache.getDefault(
"HTML_RENDERING", "Prompt each time");
// If we need to prompt, and if the GUI is visible then
// Prompt for rendering style
if (renderStyle.equalsIgnoreCase("Prompt each time")
&& !(System.getProperty("java.awt.headless") != null && System
.getProperty("java.awt.headless").equals("true")))
{
HTMLOptions svgOption = new HTMLOptions();
renderStyle = svgOption.getValue();
if (renderStyle == null || svgOption.cancelled)
{
setProgressMessage(MessageManager.formatMessage(
"status.cancelled_image_export_operation", "HTML"));
return;
}
}
if (renderStyle.equalsIgnoreCase("Lineart"))
{
idPanelGraphics.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
alignPanelGraphics.setRenderingHint(
SVGHints.KEY_DRAW_STRING_TYPE,
SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
}
if (av.getWrapAlignment())
{
printWrapped(aDimension.getWidth(), aDimension.getHeight(), 0,
alignPanelGraphics);
}
else
{
printUnwrapped(aDimension.getWidth(), aDimension.getHeight(), 0,
idPanelGraphics, alignPanelGraphics);
}
String idPanelSvgData = idPanelGraphics.getSVGDocument();
String alignPanelSvgData = alignPanelGraphics.getSVGDocument();
String jsonData = null;
boolean isEmbbedBioJSON = Boolean.valueOf(jalview.bin.Cache
.getDefault("EXPORT_EMBBED_BIOJSON", "true"));
if (isEmbbedBioJSON)
{
AlignExportSettingI exportSettings = new AlignExportSettingI()
{
@Override
public boolean isExportHiddenSequences()
{
return true;
}
@Override
public boolean isExportHiddenColumns()
{
return true;
}
@Override
public boolean isExportAnnotations()
{
return true;
}
@Override
public boolean isExportFeatures()
{
return true;
}
@Override
public boolean isExportGroups()
{
return true;
}
@Override
public boolean isCancelled()
{
return false;
}
};
AlignmentExportData exportData = jalview.gui.AlignFrame
.getAlignmentForExport(JSONFile.FILE_DESC, av,
exportSettings);
jsonData = new FormatAdapter(ap, exportData.getSettings())
.formatSequences(JSONFile.FILE_DESC,
exportData.getAlignment(),
exportData.getOmitHidden(),
exportData.getStartEndPostions(),
av.getColumnSelection());
}
String htmlData = getHtml(idPanelSvgData, alignPanelSvgData, jsonData,
av.getWrapAlignment());
FileOutputStream out = new FileOutputStream(fileX);
out.write(htmlData.getBytes());
out.flush();
out.close();
if (!(System.getProperty("java.awt.headless") != null && System
.getProperty("java.awt.headless").equals("true")))
{
jalview.util.BrowserLauncher.openURL("file:///" + fileX);
}
} catch (OutOfMemoryError err)
{
System.out.println("########################\n"
+ "OUT OF MEMORY " + fileX + "\n"
+ "########################");
new OOMWarning("Creating Image for " + fileX, err);
} catch (Exception e)
{
e.printStackTrace();
pIndicator.setProgressBar(MessageManager.formatMessage(
"info.error_creating_file", "HTML"), pSessionId);
}
setProgressMessage(MessageManager.formatMessage(
"status.export_complete", "HTML"));
}
}.start();
}
private void setProgressMessage(String message)
{
if (pIndicator != null && !headless)
{
pIndicator.setProgressBar(message, pSessionId);
}
else
{
System.out.println(message);
}
}
static JalviewFileChooser getHTMLChooser()
{
return new jalview.io.JalviewFileChooser(
jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
new String[] { "html" },
new String[] { "Hypertext Markup Language" },
"Hypertext Markup Language");
}
public int printUnwrapped(int pwidth, int pheight, int pi, Graphics... pg)
throws PrinterException
{
return ap.printUnwrapped(pwidth, pheight, pi, pg);
}
public int printWrapped(int pwidth, int pheight, int pi, Graphics... pg)
throws PrinterException
{
return ap.printWrappedAlignment(pwidth, pheight, pi, pg[0]);
}
private String getHtml(String titleSvg, String alignmentSvg,
String jsonData, boolean wrapped)
{
StringBuilder htmlSvg = new StringBuilder();
htmlSvg.append("\n");
if (jsonData != null)
{
htmlSvg.append(" ");
htmlSvg.append("");
htmlSvg.append("
"
+ jsonData + "
");
htmlSvg.append("
");
}
htmlSvg.append("\n");
if (!wrapped)
{
htmlSvg.append("");
htmlSvg.append("
\n");
htmlSvg.append("
\n");
htmlSvg.append(titleSvg);
htmlSvg.append("
");
htmlSvg.append("
\n\n\n\n");
htmlSvg.append("
");
htmlSvg.append(
"
")
.append(alignmentSvg).append("
").append("
");
htmlSvg.append("
");
htmlSvg.append("\n"
+ "\n"
+ "\n");
}
else
{
htmlSvg.append("\n")
.append(alignmentSvg).append("
");
htmlSvg.append("\n"
+ "\n");
}
// javascript for launching file in Jalview
htmlSvg.append("\n");
htmlSvg.append("");
return htmlSvg.toString();
}
}