DGP - design report figure honors rotation angle of main figure; added rocksim consta...
[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.FinMarkingGuideStrategy;
16 import net.sf.openrocket.gui.print.visitor.FinSetPrintStrategy;
17 import net.sf.openrocket.gui.print.visitor.PartsDetailVisitorStrategy;
18 import net.sf.openrocket.gui.print.visitor.TransitionStrategy;
19
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 settings    the print settings
38      * @param rotation    the angle the rocket figure is rotated
39          */
40         public void print(OpenRocketDocument doc, Iterator<PrintableContext> toBePrinted, OutputStream outputFile,
41                                                 PrintSettings settings, double rotation) {
42                 
43                 Document idoc = new Document(getSize(settings));
44                 PdfWriter writer = null;
45                 try {
46                         writer = PdfWriter.getInstance(idoc, outputFile);
47                         writer.setStrictImageSequence(true);
48                         
49                         writer.addViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);
50                         writer.addViewerPreference(PdfName.PICKTRAYBYPDFSIZE, PdfBoolean.PDFTRUE);
51                         try {
52                                 idoc.open();
53                                 Thread.sleep(1000);
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, rotation);
64                                         dp.writeToDocument(writer);
65                                         idoc.newPage();
66                                         break;
67                                 case FIN_TEMPLATE:
68                                         final FinSetPrintStrategy finWriter = new FinSetPrintStrategy(idoc, writer, stages);
69                     finWriter.writeToDocument(doc.getRocket());
70                     break;
71                     case PARTS_DETAIL:
72                                         final PartsDetailVisitorStrategy detailVisitor = new PartsDetailVisitorStrategy(idoc, writer, stages);
73                         detailVisitor.writeToDocument(doc.getRocket());
74                         detailVisitor.close();
75                         idoc.newPage();
76                                         break;
77                 case TRANSITION_TEMPLATE:
78                     final TransitionStrategy tranWriter = new TransitionStrategy(idoc, writer, stages);
79                     tranWriter.writeToDocument(doc.getRocket(), false);
80                     idoc.newPage();
81                     break;
82
83                 case NOSE_CONE_TEMPLATE:
84                     final TransitionStrategy coneWriter = new TransitionStrategy(idoc, writer, stages);
85                     coneWriter.writeToDocument(doc.getRocket(), true);
86                     idoc.newPage();
87                     break;
88
89                 case FIN_MARKING_GUIDE:
90                     final FinMarkingGuideStrategy fmg = new FinMarkingGuideStrategy(idoc, writer);
91                     fmg.writeToDocument(doc.getRocket());
92                     idoc.newPage();
93                     break;
94                 }
95                         }
96                         //Stupid iText throws a really nasty exception if there is no data when close is called.
97                         if (writer.getCurrentDocumentSize() <= 140) {
98                                 writer.setPageEmpty(false);
99                         }
100                         writer.close();
101                         idoc.close();
102                 } catch (DocumentException e) {
103                 } catch (ExceptionConverter ec) {
104                 } finally {
105                         if (outputFile != null) {
106                                 try {
107                                         outputFile.close();
108                                 } catch (IOException e) {
109                                 }
110                         }
111                 }
112         }
113         
114         /**
115          * Get the correct paper size from the print settings.
116          * 
117          * @param settings      the print settings
118          * @return                      the paper size
119          */
120         private Rectangle getSize(PrintSettings settings) {
121                 PaperSize size = settings.getPaperSize();
122                 PaperOrientation orientation = settings.getPaperOrientation();
123                 return orientation.orient(size.getSize());
124         }
125         
126 }