Author: [log in to unmask]
Date: Wed Apr 1 13:25:48 2015
New Revision: 2648
Log:
Add header text and summary info to plots PDF. HPSJAVA-481
Modified:
java/trunk/monitoring-app/src/main/java/org/hps/monitoring/application/MonitoringApplication.java
java/trunk/monitoring-util/pom.xml
java/trunk/monitoring-util/src/main/java/org/hps/monitoring/plotting/ExportPdf.java
java/trunk/monitoring-util/src/main/java/org/hps/monitoring/plotting/PlotterRegistry.java
Modified: java/trunk/monitoring-app/src/main/java/org/hps/monitoring/application/MonitoringApplication.java
=============================================================================
--- java/trunk/monitoring-app/src/main/java/org/hps/monitoring/application/MonitoringApplication.java (original)
+++ java/trunk/monitoring-app/src/main/java/org/hps/monitoring/application/MonitoringApplication.java Wed Apr 1 13:25:48 2015
@@ -19,6 +19,7 @@
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
+import java.util.Date;
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.Level;
@@ -505,7 +506,7 @@
try {
if (extension.equals("pdf")) {
// Write to a single PDF file.
- ExportPdf.write(MonitoringPlotFactory.getPlotterRegistry().getPlotters(), fileName);
+ ExportPdf.write(MonitoringPlotFactory.getPlotterRegistry().getPlotters(), fileName, getRunData());
} else {
// Save plot object data to AIDA or ROOT file.
AIDA.defaultInstance().saveAs(fileName);
@@ -519,6 +520,23 @@
DialogUtil.showErrorDialog(frame, "File Exists", "Selected file already exists.");
}
}
+ }
+
+ /**
+ * Get a list of run data for writing to a PDF.
+ * @return The list of run data from the model.
+ */
+ List<String> getRunData() {
+ List<String> data = new ArrayList<String>();
+ data.add("Created: " + new Date());
+ data.add("Run Number: " + runModel.getRunNumber());
+ data.add("Started: " + runModel.getStartDate());
+ data.add("Ended: " + runModel.getEndDate());
+ data.add("Length: " + runModel.getRunLength() + " seconds");
+ data.add("Total Events: " + runModel.getTotalEvents());
+ data.add("Elapsed Time: " + runModel.getElapsedTime());
+ data.add("Events Processed: " + runModel.getEventsReceived());
+ return data;
}
/**
Modified: java/trunk/monitoring-util/pom.xml
=============================================================================
--- java/trunk/monitoring-util/pom.xml (original)
+++ java/trunk/monitoring-util/pom.xml Wed Apr 1 13:25:48 2015
@@ -28,10 +28,5 @@
<artifactId>itextpdf</artifactId>
<version>5.5.5</version>
</dependency>
- <dependency>
- <groupId>org.apache.pdfbox</groupId>
- <artifactId>pdfbox</artifactId>
- <version>1.8.8</version>
- </dependency>
</dependencies>
</project>
Modified: java/trunk/monitoring-util/src/main/java/org/hps/monitoring/plotting/ExportPdf.java
=============================================================================
--- java/trunk/monitoring-util/src/main/java/org/hps/monitoring/plotting/ExportPdf.java (original)
+++ java/trunk/monitoring-util/src/main/java/org/hps/monitoring/plotting/ExportPdf.java Wed Apr 1 13:25:48 2015
@@ -6,22 +6,18 @@
import java.awt.Component;
import java.awt.Image;
import java.awt.image.BufferedImage;
-import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collection;
import java.util.List;
-
-import javax.swing.JPanel;
-
-import org.apache.pdfbox.exceptions.COSVisitorException;
-import org.apache.pdfbox.util.PDFMergerUtility;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
+import com.itextpdf.text.Element;
+import com.itextpdf.text.Font;
+import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.PageSize;
+import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
@@ -32,41 +28,41 @@
*/
public class ExportPdf {
- private ExportPdf() {
+ private ExportPdf() {
}
/**
- * Export a JPanel to a single PDF.
- * @param panel The JPanel.
- * @param name The output file name.
- * @throws IOException If there is a problem opening the PDF document.
+ * Write the graphics from a list of plotters to a PDF, with one plotter per page,
+ * including a summary page with the run data.
+ * @param plotters The list of plotters.
+ * @param fileName The output file name.
+ * @param runData A list of run data to include on the first page.
+ * @throws IOException If there is a problem opening or writing to the PDF document.
*/
- static void write(JPanel panel, String name) throws IOException {
+ public static void write(List<IPlotter> plotters, String fileName, List<String> runData) throws IOException {
- Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);
+ // Open the document and the writer.
+ Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);
PdfWriter writer;
try {
- writer = PdfWriter.getInstance(document, new FileOutputStream(name));
+ writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
} catch (DocumentException e) {
throw new IOException(e);
}
- document.open();
+ document.open();
- Image awtImage = getImageFromPanel(panel);
-
- com.itextpdf.text.Image iTextImage = null;
+ // Create 1st page with run summary data.
try {
- iTextImage = com.itextpdf.text.Image.getInstance(writer, awtImage, 1f);
- } catch (BadElementException e) {
- throw new IOException(e);
- }
-
- iTextImage.setAbsolutePosition(50, 50);
- iTextImage.scalePercent(60);
- try {
- document.add(iTextImage);
+ writeRunData(document, runData);
} catch (DocumentException e) {
throw new IOException(e);
+ }
+
+ // Write out the plots to the PDF, one page per plotter.
+ for (int i = 0; i < plotters.size(); i++) {
+ document.newPage();
+ IPlotter plotter = plotters.get(i);
+ writePage(document, writer, plotter);
}
document.close();
@@ -77,41 +73,58 @@
* @param component The Swing component.
* @return The image from the component.
*/
- static java.awt.Image getImageFromPanel(Component component) {
+ public static BufferedImage getImage(Component component) {
BufferedImage image = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_RGB);
component.paint(image.getGraphics());
return image;
}
-
+
/**
- * Write a single PDF containing the graphics from the collection of plotters,
- * with one plotter per page.
- *
- * @param plotters The collection of plotters.
- * @param fileName The name of the output file.
- * @throws IOException If there is a problem merging all the plotter PDFs together.
+ * Write plotter graphics into a single PDF page.
+ * @param document The output PDF document.
+ * @param writer The PDF writer.
+ * @param plotter The plotter with the graphics.
+ * @throws IOException If there is a problem writing to the PDF document.
*/
- public static void write(Collection<IPlotter> plotters, String fileName) throws IOException {
-
- PDFMergerUtility merge = new PDFMergerUtility();
- merge.setDestinationFileName(fileName);
-
- List<File> tempFiles = new ArrayList<File>();
- for (IPlotter plotter : plotters) {
- File tempFile = File.createTempFile("plot", ".pdf");
- tempFiles.add(tempFile);
- write(((Plotter)plotter).panel(), tempFile.getPath());
- merge.addSource(tempFile);
+ static void writePage(Document document, PdfWriter writer, IPlotter plotter) throws IOException {
+
+ // Add header label.
+ Paragraph p = new Paragraph(plotter.title(), new Font(FontFamily.HELVETICA, 24));
+ p.setAlignment(Element.ALIGN_CENTER);
+ try {
+ document.add(p);
+ } catch (DocumentException e) {
+ throw new IOException(e);
}
+ // Create image from panel.
+ Image awtImage = getImage(((Plotter)plotter).panel());
+
+ // Write image into the document.
+ com.itextpdf.text.Image iTextImage = null;
try {
- merge.mergeDocuments();
- } catch (COSVisitorException e) {
- throw new IOException("Error merging documents", e);
+ iTextImage = com.itextpdf.text.Image.getInstance(writer, awtImage, 1f);
+ } catch (BadElementException e) {
+ throw new IOException(e);
+ }
+ iTextImage.setAbsolutePosition(50, 50);
+ iTextImage.scalePercent(60);
+ try {
+ document.add(iTextImage);
+ } catch (DocumentException e) {
+ throw new IOException(e);
}
-
- for (File tempFile : tempFiles) {
- tempFile.delete();
+ }
+
+ /**
+ * Add a page with the run summary data.
+ * @param runData The list of run summary information.
+ */
+ static void writeRunData(Document document, List<String> runData) throws DocumentException {
+ for (String line : runData) {
+ Paragraph p = new Paragraph(line, new Font(FontFamily.HELVETICA, 20));
+ p.setAlignment(Element.ALIGN_LEFT);
+ document.add(p);
}
}
}
Modified: java/trunk/monitoring-util/src/main/java/org/hps/monitoring/plotting/PlotterRegistry.java
=============================================================================
--- java/trunk/monitoring-util/src/main/java/org/hps/monitoring/plotting/PlotterRegistry.java (original)
+++ java/trunk/monitoring-util/src/main/java/org/hps/monitoring/plotting/PlotterRegistry.java Wed Apr 1 13:25:48 2015
@@ -3,9 +3,10 @@
import hep.aida.IPlotter;
import hep.aida.IPlotterRegion;
-import java.util.Collection;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
+import java.util.List;
import java.util.Map.Entry;
/**
@@ -71,7 +72,7 @@
* Get the current collection of plotters as an unmodifiable collection.
* @return The current collection of plotters.
*/
- public Collection<IPlotter> getPlotters() {
- return Collections.unmodifiableCollection(plotterMap.keySet());
+ public List<IPlotter> getPlotters() {
+ return Collections.unmodifiableList(new ArrayList<IPlotter>(plotterMap.keySet()));
}
}
|