create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / print / PrintableComponent.java
1 /*
2  * PrintableComponent.java
3  */
4 package net.sf.openrocket.gui.print;
5
6 import javax.swing.JPanel;
7 import java.awt.Graphics;
8 import java.awt.Graphics2D;
9 import java.awt.print.PageFormat;
10 import java.awt.print.Printable;
11 import java.awt.print.PrinterException;
12
13 /**
14  * Common interface for components we want to print. Used by PageFitPrintStrategy
15  *
16  * @author Jason Blood <dyster2000@gmail.com>
17  */
18 public class PrintableComponent extends JPanel implements Printable, Comparable<PrintableComponent> {
19
20     /**
21      * The printing offsets.
22      */
23     private int offsetX = 0;
24     private int offsetY = 0;
25
26     /**
27      * Constructor.
28      */
29     public PrintableComponent() {
30         super(false);
31     }
32
33     /**
34      * From the java.awt.print.Printable interface.
35      * <p/>
36      * Prints the page at the specified index into the specified {@link java.awt.Graphics} context in the specified
37      * format. A <code>PrinterJob</code> calls the <code>Printable</code> interface to request that a page be rendered
38      * into the context specified by <code>graphics</code>.  The format of the page to be drawn is specified by
39      * <code>pageFormat</code>.  The zero based index of the requested page is specified by <code>pageIndex</code>. If
40      * the requested page does not exist then this method returns NO_SUCH_PAGE; otherwise PAGE_EXISTS is returned. The
41      * <code>Graphics</code> class or subclass implements the {@link java.awt.print.PrinterGraphics} interface to
42      * provide additional information.  If the <code>Printable</code> object aborts the print job then it throws a
43      * {@link java.awt.print.PrinterException}.
44      * <p/>
45      * Note: This is not currently used in OpenRocket.  It's only here for reference.
46      *
47      * @param graphics   the context into which the page is drawn
48      * @param pageFormat the size and orientation of the page being drawn
49      * @param pageIndex  the zero based index of the page to be drawn
50      *
51      * @return PAGE_EXISTS if the page is rendered successfully or NO_SUCH_PAGE if <code>pageIndex</code> specifies a
52      *         non-existent page.
53      *
54      * @throws java.awt.print.PrinterException
55      *          thrown when the print job is terminated.
56      */
57     @Override
58     public int print (final Graphics graphics, final PageFormat pageFormat, final int pageIndex)
59             throws PrinterException {
60
61         Graphics2D g2d = (Graphics2D) graphics;
62         PrintUtilities.translateToJavaOrigin(g2d, pageFormat);
63         PrintUtilities.disableDoubleBuffering(this);
64         paint(g2d);
65         PrintUtilities.enableDoubleBuffering(this);
66         return Printable.PAGE_EXISTS;
67     }
68
69         /**
70          * Set the offset this component will be printed to the page.
71          * @param x     X offset to print at.
72          * @param y     Y offset to print at.
73          */
74         public void setPrintOffset(int x, int y) {
75         offsetX = x;
76         offsetY = y;
77         }
78
79         /**
80          * Get the X offset this component will be printed to the page.
81          * @return X offset to print at.
82          */
83         public int getOffsetX() {
84                 return offsetX;
85         }
86
87         /**
88          * Get the Y offset this component will be printed to the page.
89          * @return Y offset to print at.
90          */
91         public int getOffsetY() {
92                 return offsetY;
93         }
94
95
96     /**
97      * Compares this object with the specified object for order.  Returns a negative integer, zero, or a positive integer
98      * as this object is less than, equal to, or greater than the specified object.
99      *
100      * Bin packing theory says that trying to fit the biggest items first may have a better outcome. So this is sorted
101      * in size descending order, with width taking precedence over height.
102      *
103      * @param other the object to be compared.
104      *
105      * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the
106      *         specified object.
107      *
108      * @throws NullPointerException if the specified object is null
109      * @throws ClassCastException   if the specified object's type prevents it from being compared to this object.
110      */
111     @Override
112     public int compareTo(final PrintableComponent other) {
113         int widthDiff = other.getWidth() - getWidth();
114         if (widthDiff > 0) {
115             return 1;
116         }
117         else if (widthDiff < 0) {
118             return -1;
119         }
120         return other.getHeight() - getHeight();
121     }
122 }