4088de702de62fc79969f7a86195956abf75a2e9
[debian/openrocket] / core / 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.CenteringRingStrategy;
16 import net.sf.openrocket.gui.print.visitor.FinMarkingGuideStrategy;
17 import net.sf.openrocket.gui.print.visitor.FinSetPrintStrategy;
18 import net.sf.openrocket.gui.print.visitor.PageFitPrintStrategy;
19 import net.sf.openrocket.gui.print.visitor.PartsDetailVisitorStrategy;
20 import net.sf.openrocket.gui.print.visitor.TransitionStrategy;
21
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.util.Iterator;
25 import java.util.Set;
26
27 /**
28  * This is the main active object for printing.  It performs all actions necessary to create and populate the print
29  * file.
30  */
31 public class PrintController {
32
33     /**
34      * Print the selected components to a PDF document.
35      *
36      * @param doc         the OR document
37      * @param toBePrinted the user chosen items to print
38      * @param outputFile  the file being written to
39      * @param settings    the print settings
40      * @param rotation    the angle the rocket figure is rotated
41      */
42     public void print(OpenRocketDocument doc, Iterator<PrintableContext> toBePrinted, OutputStream outputFile,
43                       PrintSettings settings, double rotation) {
44
45         Document idoc = new Document(getSize(settings));
46         PdfWriter writer = null;
47         try {
48             writer = PdfWriter.getInstance(idoc, outputFile);
49             writer.setStrictImageSequence(true);
50
51             writer.addViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);
52             writer.addViewerPreference(PdfName.PICKTRAYBYPDFSIZE, PdfBoolean.PDFTRUE);
53             try {
54                 idoc.open();
55                 Thread.sleep(1000);
56             }
57             catch (InterruptedException e) {
58             }
59
60             // Used to combine multiple components onto fewer sheets of paper
61             PageFitPrintStrategy pageFitPrint = new PageFitPrintStrategy(idoc, writer);
62
63             while (toBePrinted.hasNext()) {
64                 PrintableContext printableContext = toBePrinted.next();
65
66                 Set<Integer> stages = printableContext.getStageNumber();
67
68                 switch (printableContext.getPrintable()) {
69                     case DESIGN_REPORT:
70                         DesignReport dp = new DesignReport(doc, idoc, rotation);
71                         dp.writeToDocument(writer);
72                         idoc.newPage();
73                         break;
74                     case FIN_TEMPLATE:
75                         final FinSetPrintStrategy finWriter = new FinSetPrintStrategy(idoc, writer, stages, pageFitPrint);
76                         finWriter.writeToDocument(doc.getRocket());
77                         break;
78                     case PARTS_DETAIL:
79                         final PartsDetailVisitorStrategy detailVisitor = new PartsDetailVisitorStrategy(idoc, writer, stages);
80                         detailVisitor.writeToDocument(doc.getRocket());
81                         detailVisitor.close();
82                         idoc.newPage();
83                         break;
84                     case TRANSITION_TEMPLATE:
85                         final TransitionStrategy tranWriter = new TransitionStrategy(idoc, writer, stages, pageFitPrint);
86                         tranWriter.writeToDocument(doc.getRocket(), false);
87                         idoc.newPage();
88                         break;
89
90                     case NOSE_CONE_TEMPLATE:
91                         final TransitionStrategy coneWriter = new TransitionStrategy(idoc, writer, stages, pageFitPrint);
92                         coneWriter.writeToDocument(doc.getRocket(), true);
93                         idoc.newPage();
94                         break;
95
96                     case CENTERING_RING_TEMPLATE:
97                         final CenteringRingStrategy crWriter = new CenteringRingStrategy(idoc, writer, stages,
98                                 pageFitPrint);
99                         crWriter.writeToDocument(doc.getRocket());
100                         idoc.newPage();
101                         break;
102
103                     case FIN_MARKING_GUIDE:
104                         final FinMarkingGuideStrategy fmg = new FinMarkingGuideStrategy(idoc, writer);
105                         fmg.writeToDocument(doc.getRocket());
106                         idoc.newPage();
107                         break;
108                 }
109             }
110             // Write out parts that we are going to combine onto single sheets of paper
111             pageFitPrint.writeToDocument(doc.getRocket());
112             idoc.newPage();
113
114             //Stupid iText throws a really nasty exception if there is no data when close is called.
115             if (writer.getCurrentDocumentSize() <= 140) {
116                 writer.setPageEmpty(false);
117             }
118             writer.close();
119             idoc.close();
120         }
121         catch (DocumentException e) {
122         }
123         catch (ExceptionConverter ec) {
124         }
125         finally {
126             if (outputFile != null) {
127                 try {
128                     outputFile.close();
129                 }
130                 catch (IOException e) {
131                 }
132             }
133         }
134     }
135
136     /**
137      * Get the correct paper size from the print settings.
138      *
139      * @param settings the print settings
140      *
141      * @return the paper size
142      */
143     private Rectangle getSize(PrintSettings settings) {
144         PaperSize size = settings.getPaperSize();
145         PaperOrientation orientation = settings.getPaperOrientation();
146         return orientation.orient(size.getSize());
147     }
148
149 }