]> git.gag.com Git - debian/openrocket/blob - src/net/sf/openrocket/gui/print/PrintController.java
DGP - back out exception handler
[debian/openrocket] / src / net / sf / openrocket / gui / print / PrintController.java
1 /*
2  * PrintController.java
3  *
4  */
5 package net.sf.openrocket.gui.print;
6
7 import com.itextpdf.text.Document;
8 import com.itextpdf.text.DocumentException;
9 import com.itextpdf.text.ExceptionConverter;
10 import com.itextpdf.text.Rectangle;
11 import com.itextpdf.text.pdf.PdfBoolean;
12 import com.itextpdf.text.pdf.PdfName;
13 import com.itextpdf.text.pdf.PdfWriter;
14 import net.sf.openrocket.document.OpenRocketDocument;
15 import net.sf.openrocket.gui.print.visitor.FinSetVisitorStrategy;
16 import net.sf.openrocket.gui.print.visitor.PartsDetailVisitorStrategy;
17
18 import javax.print.attribute.standard.MediaSizeName;
19 import java.io.IOException;
20 import java.io.OutputStream;
21 import java.util.Iterator;
22 import java.util.Set;
23
24 /**
25  * This is the main active object for printing.  It performs all actions necessary to create and populate the print
26  * file.
27  */
28 public class PrintController {
29
30     /**
31      * Print the selected components to a PDF document.
32      *
33      * @param doc         the OR document
34      * @param toBePrinted the user chosen items to print
35      * @param outputFile  the file being written to
36      * @param msn         the paper size
37      */
38     public void print(OpenRocketDocument doc, Iterator<PrintableContext> toBePrinted, OutputStream outputFile,
39                       MediaSizeName msn) {
40
41         Document idoc = new Document(convertWithDefault(msn));
42         PdfWriter writer = null;
43         try {
44             writer = PdfWriter.getInstance(idoc, outputFile);
45             writer.setStrictImageSequence(true);
46
47             writer.addViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);
48             writer.addViewerPreference(PdfName.PICKTRAYBYPDFSIZE, PdfBoolean.PDFTRUE);
49             try {
50                 idoc.open();
51                 Thread.sleep(1000);
52             }
53             catch (InterruptedException e) {
54             }
55             while (toBePrinted.hasNext()) {
56                 PrintableContext printableContext = toBePrinted.next();
57
58                 Set<Integer> stages = printableContext.getStageNumber();
59
60                 switch (printableContext.getPrintable()) {
61                     case DESIGN_REPORT:
62                         DesignReport dp = new DesignReport(doc, idoc);
63                         dp.writeToDocument(writer);
64                         idoc.newPage();
65                         break;
66                     case FIN_TEMPLATE:
67                         final FinSetVisitorStrategy finWriter = new FinSetVisitorStrategy(idoc,
68                                                                                           writer,
69                                                                                           stages);
70                         finWriter.writeToDocument(doc.getRocket());
71                         break;
72                     case PARTS_DETAIL:
73                         final PartsDetailVisitorStrategy detailVisitor = new PartsDetailVisitorStrategy(idoc,
74                                                                                                         writer,
75                                                                                                         stages);
76                         detailVisitor.writeToDocument(doc.getRocket());
77                         detailVisitor.close();
78                         idoc.newPage();
79                         break;
80                 }
81             }
82             //Stupid iText throws a really nasty exception if there is no data when close is called.
83             if (writer.getCurrentDocumentSize() <= 140) {
84                 writer.setPageEmpty(false);
85             }
86             writer.close();
87             idoc.close();
88         }
89         catch (DocumentException e) {
90         }
91         catch (ExceptionConverter ec) {
92         }
93         finally {
94             if (outputFile != null) {
95                 try {
96                     outputFile.close();
97                 }
98                 catch (IOException e) {
99                 }
100             }
101         }
102     }
103
104     /**
105      * Convert a media size name to a rectangle that defines the bounds of the corresponding paper size.
106      *
107      * @param msn the MediaSizeName to convert
108      * @return the corresponding Rectangle
109      */
110     private Rectangle convertWithDefault(final MediaSizeName msn) {
111         Rectangle result = PaperSize.convert(msn);
112         if (result == null) {
113             result = PaperSize.convert(PrintUtilities.getDefaultMedia().getMediaSizeName());
114         }
115         return result;
116     }
117 }