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