create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / print / PrintUtilities.java
1 /*
2  * PrintUtilities.java
3  */
4 package net.sf.openrocket.gui.print;
5
6
7 import java.awt.Component;
8 import java.awt.Graphics;
9 import java.awt.Graphics2D;
10 import java.awt.print.PageFormat;
11 import java.awt.print.Printable;
12
13 import javax.swing.RepaintManager;
14
15 import net.sf.openrocket.logging.LogHelper;
16 import net.sf.openrocket.startup.Application;
17
18 import com.itextpdf.text.Chunk;
19 import com.itextpdf.text.Document;
20 import com.itextpdf.text.DocumentException;
21 import com.itextpdf.text.Font;
22 import com.itextpdf.text.Paragraph;
23
24 /**
25  * Utilities methods and fonts used for printing.
26  */
27 public class PrintUtilities implements Printable {
28         
29         /**
30          * The logger.
31          */
32         private static final LogHelper log = Application.getLogger();
33         
34         public static final int NORMAL_FONT_SIZE = Font.DEFAULTSIZE - 3;
35         public static final int SMALL_FONT_SIZE = NORMAL_FONT_SIZE - 3;
36         
37         public static final Font BOLD = new Font(ITextHelper.getBaseFont(), NORMAL_FONT_SIZE, Font.BOLD);
38         public static final Font BIG_BOLD = new Font(ITextHelper.getBaseFont(), NORMAL_FONT_SIZE + 3, Font.BOLD);
39         public static final Font BOLD_UNDERLINED = new Font(ITextHelper.getBaseFont(), NORMAL_FONT_SIZE,
40                                                                                                                 Font.BOLD | Font.UNDERLINE);
41         public static final Font NORMAL = new Font(ITextHelper.getBaseFont(), NORMAL_FONT_SIZE);
42         public static final Font SMALL = new Font(ITextHelper.getBaseFont(), SMALL_FONT_SIZE);
43         
44
45         private Component componentToBePrinted;
46         
47         public PrintUtilities(Component componentToBePrinted) {
48                 this.componentToBePrinted = componentToBePrinted;
49         }
50         
51         @Override
52         public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
53                 if (pageIndex > 0) {
54                         return (NO_SUCH_PAGE);
55                 } else {
56                         Graphics2D g2d = (Graphics2D) g;
57                         translateToJavaOrigin(g2d, pageFormat);
58                         disableDoubleBuffering(componentToBePrinted);
59                         componentToBePrinted.paint(g2d);
60                         enableDoubleBuffering(componentToBePrinted);
61                         return (PAGE_EXISTS);
62                 }
63         }
64         
65         public static void disableDoubleBuffering(Component c) {
66                 RepaintManager currentManager = RepaintManager.currentManager(c);
67                 currentManager.setDoubleBufferingEnabled(false);
68         }
69         
70         public static void enableDoubleBuffering(Component c) {
71                 RepaintManager currentManager = RepaintManager.currentManager(c);
72                 currentManager.setDoubleBufferingEnabled(true);
73         }
74         
75         
76         /**
77          * Translate the page format coordinates onto the graphics object using Java's origin (top left).
78          *
79          * @param g2d        the graphics object
80          * @param pageFormat the print page format
81          */
82         public static void translateToJavaOrigin(Graphics2D g2d, PageFormat pageFormat) {
83                 g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
84         }
85         
86         /**
87          * Add text as a new paragraph in a given font to the document.
88          *
89          * @param document  the document
90          * @param font      the font
91          * @param title     the title
92          */
93         public static void addText(Document document, com.itextpdf.text.Font font, String title) {
94                 Chunk sectionHeader = new Chunk(title);
95                 sectionHeader.setFont(font);
96                 try {
97                         Paragraph p = new Paragraph();
98                         p.add(sectionHeader);
99                         document.add(p);
100                 } catch (DocumentException e) {
101                         log.error("Could not add paragraph.", e);
102                 }
103         }
104 }