moving to core/
[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          */
39         public void print(OpenRocketDocument doc, Iterator<PrintableContext> toBePrinted, OutputStream outputFile,
40                                                 PrintSettings settings) {
41                 
42                 Document idoc = new Document(getSize(settings));
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                         } 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 FinSetPrintStrategy finWriter = new FinSetPrintStrategy(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                 case TRANSITION_TEMPLATE:
81                     final TransitionStrategy tranWriter = new TransitionStrategy(idoc, writer, stages);
82                     tranWriter.writeToDocument(doc.getRocket(), false);
83                     idoc.newPage();
84                     break;
85
86                 case NOSE_CONE_TEMPLATE:
87                     final TransitionStrategy coneWriter = new TransitionStrategy(idoc, writer, stages);
88                     coneWriter.writeToDocument(doc.getRocket(), true);
89                     idoc.newPage();
90                     break;
91
92                 case FIN_MARKING_GUIDE:
93                     final FinMarkingGuideStrategy fmg = new FinMarkingGuideStrategy(idoc, writer);
94                     fmg.writeToDocument(doc.getRocket());
95                     idoc.newPage();
96                     break;
97                 }
98                         }
99                         //Stupid iText throws a really nasty exception if there is no data when close is called.
100                         if (writer.getCurrentDocumentSize() <= 140) {
101                                 writer.setPageEmpty(false);
102                         }
103                         writer.close();
104                         idoc.close();
105                 } catch (DocumentException e) {
106                 } catch (ExceptionConverter ec) {
107                 } finally {
108                         if (outputFile != null) {
109                                 try {
110                                         outputFile.close();
111                                 } catch (IOException e) {
112                                 }
113                         }
114                 }
115         }
116         
117         /**
118          * Get the correct paper size from the print settings.
119          * 
120          * @param settings      the print settings
121          * @return                      the paper size
122          */
123         private Rectangle getSize(PrintSettings settings) {
124                 PaperSize size = settings.getPaperSize();
125                 PaperOrientation orientation = settings.getPaperOrientation();
126                 return orientation.orient(size.getSize());
127         }
128         
129 }