LISTSERV mailing list manager LISTSERV 16.5

Help for HPS-SVN Archives


HPS-SVN Archives

HPS-SVN Archives


HPS-SVN@LISTSERV.SLAC.STANFORD.EDU


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

HPS-SVN Home

HPS-SVN Home

HPS-SVN  April 2015

HPS-SVN April 2015

Subject:

r2648 - in /java/trunk: monitoring-app/src/main/java/org/hps/monitoring/application/ monitoring-util/ monitoring-util/src/main/java/org/hps/monitoring/plotting/

From:

[log in to unmask]

Reply-To:

Notification of commits to the hps svn repository <[log in to unmask]>

Date:

Wed, 1 Apr 2015 20:25:56 -0000

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (281 lines)

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()));
     }
 }

Top of Message | Previous Page | Permalink

Advanced Options


Options

Log In

Log In

Get Password

Get Password


Search Archives

Search Archives


Subscribe or Unsubscribe

Subscribe or Unsubscribe


Archives

November 2017
August 2017
July 2017
January 2017
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
January 2016
December 2015
November 2015
October 2015
September 2015
August 2015
July 2015
June 2015
May 2015
April 2015
March 2015
February 2015
January 2015
December 2014
November 2014
October 2014
September 2014
August 2014
July 2014
June 2014
May 2014
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013

ATOM RSS1 RSS2



LISTSERV.SLAC.STANFORD.EDU

Secured by F-Secure Anti-Virus CataList Email List Search Powered by the LISTSERV Email List Manager

Privacy Notice, Security Notice and Terms of Use