Debian Codegraph @

uk/ac/babraham/FastQC/Modules/AbstractQCModule.java

raw symbols SCIP
1 /**
2 * Copyright Copyright 2010-12 Pierre Lindenbaum
3 *
4 * This file is part of FastQC.
5 *
6 * FastQC is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * FastQC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with FastQC; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 package uk.ac.babraham.FastQC.Modules;
22
23 import java.awt.Graphics;
24 import java.awt.image.BufferedImage;
25 import java.io.IOException;
26 import java.util.zip.ZipEntry;
27 import java.util.zip.ZipOutputStream;
28
29 import javax.xml.stream.XMLStreamException;
30 import javax.xml.stream.XMLStreamWriter;
31 import javax.imageio.ImageIO;
32 import javax.swing.JPanel;
33 import javax.swing.table.TableModel;
34
35 import uk.ac.babraham.FastQC.FastQCConfig;
36 import uk.ac.babraham.FastQC.Report.HTMLReportArchive;
37 import uk.ac.babraham.FastQC.Utilities.ImageToBase64;
38 import uk.ac.babraham.FastQC.Utilities.ImageSaver.SVGImageSaver;
39
40 public abstract class AbstractQCModule implements QCModule {
41
42 protected void simpleXhtmlReport(HTMLReportArchive report,String svgData, BufferedImage image, String alt) throws XMLStreamException {
43 XMLStreamWriter xhtml = report.xhtmlStream();
44 xhtml.writeStartElement("p");
45 xhtml.writeEmptyElement("img");
46 xhtml.writeAttribute("class", "indented");
47 if (FastQCConfig.getInstance().svg_output) {
48 xhtml.writeAttribute("src", ImageToBase64.svgImageToBase64(svgData));
49 }
50 else {
51 xhtml.writeAttribute("src", ImageToBase64.imageToBase64(image));
52 }
53 xhtml.writeAttribute("alt", alt);
54
55 // if(svgData!=null){
56 // xhtml.writeAttribute("width",String.valueOf(img.getWidth()));
57 // xhtml.writeAttribute("height",String.valueOf(img.getHeight()));
58 // }
59
60 xhtml.writeEndElement();//p
61 }
62
63 protected void writeDefaultImage (HTMLReportArchive report, String fileName, String imageTitle, int width, int height) throws IOException, XMLStreamException {
64 ZipOutputStream zip = report.zipFile();
65
66 // Write out the svg version of the image
67 JPanel resultsPanel = getResultsPanel();
68 resultsPanel.setSize(width,height);
69 resultsPanel.validate();
70
71
72 String svgFilename = fileName.replaceAll("\\.png$", ".svg");
73 zip.putNextEntry(new ZipEntry(report.folderName()+"/Images/"+svgFilename));
74
75 String svgData = SVGImageSaver.saveImage(resultsPanel, zip);
76 zip.closeEntry();
77
78 // Write out the png version of the image
79 zip.putNextEntry(new ZipEntry(report.folderName()+"/Images/"+fileName));
80 BufferedImage b = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
81 Graphics g = b.createGraphics();
82
83 resultsPanel.setDoubleBuffered(false);
84 resultsPanel.addNotify();
85 resultsPanel.validate();
86
87 resultsPanel.print(g);
88
89 g.dispose();
90
91 ImageIO.write(b, "PNG", zip);
92 zip.closeEntry();
93
94
95 simpleXhtmlReport(report, svgData, b, imageTitle);
96
97 }
98
99 protected void writeSpecificImage (HTMLReportArchive report, JPanel resultsPanel, String fileName, String imageTitle, int width, int height) throws IOException, XMLStreamException {
100 ZipOutputStream zip = report.zipFile();
101
102 // Write out the svg version of the image
103 resultsPanel.setSize(width,height);
104 resultsPanel.validate();
105
106
107 String svgFilename = fileName.replaceAll("\\.png$", ".svg");
108 zip.putNextEntry(new ZipEntry(report.folderName()+"/Images/"+svgFilename));
109
110 String svgData = SVGImageSaver.saveImage(resultsPanel, zip);
111 zip.closeEntry();
112
113 // Write out the png version of the image
114 zip.putNextEntry(new ZipEntry(report.folderName()+"/Images/"+fileName));
115 BufferedImage b = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
116 Graphics g = b.createGraphics();
117
118 resultsPanel.setDoubleBuffered(false);
119 resultsPanel.addNotify();
120 resultsPanel.validate();
121
122 resultsPanel.print(g);
123
124 g.dispose();
125
126 ImageIO.write(b, "PNG", zip);
127 zip.closeEntry();
128
129
130 simpleXhtmlReport(report, svgData, b, imageTitle);
131 }
132
133
134 protected void writeTable(HTMLReportArchive report, TableModel table) throws IOException,XMLStreamException {
135 writeXhtmlTable(report,table);
136 writeTextTable(report,table);
137 }
138
139 protected void writeXhtmlTable(HTMLReportArchive report, TableModel table) throws IOException,XMLStreamException {
140 XMLStreamWriter w=report.xhtmlStream();
141 w.writeStartElement("table");
142 w.writeStartElement("thead");
143 w.writeStartElement("tr");
144
145 for (int c=0;c<table.getColumnCount();c++) {
146 w.writeStartElement("th");
147 w.writeCharacters(table.getColumnName(c));
148 w.writeEndElement();
149 }
150
151 w.writeEndElement();//tr
152 w.writeEndElement();//thead
153 w.writeStartElement("tbody");
154
155 for (int r=0;r<table.getRowCount();r++) {
156 w.writeStartElement("tr");
157 for (int c=0;c<table.getColumnCount();c++) {
158 w.writeStartElement("td");
159 w.writeCharacters(String.valueOf(table.getValueAt(r, c)));
160 w.writeEndElement();//td
161 }
162 w.writeEndElement();//tr
163 }
164 w.writeEndElement();//tbody
165 w.writeEndElement();
166 }
167
168
169 protected void writeTextTable(HTMLReportArchive report, TableModel table) throws IOException {
170 StringBuffer d=report.dataDocument();
171 d.append("#");
172
173 for (int c=0;c<table.getColumnCount();c++) {
174 if (c!=0) d.append("\t");
175 d.append(table.getColumnName(c));
176 }
177
178 d.append("\n");
179
180 // Do the rows
181 for (int r=0;r<table.getRowCount();r++) {
182 for (int c=0;c<table.getColumnCount();c++) {
183 if (c!=0) d.append("\t");
184 d.append(table.getValueAt(r, c));
185 }
186 d.append("\n");
187 }
188
189 }
190
191 }