01: /**
02: * ========================================
03: * JFreeReport : a free Java report library
04: * ========================================
05: *
06: * Project Info: http://reporting.pentaho.org/
07: *
08: * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
09: *
10: * This library is free software; you can redistribute it and/or modify it under the terms
11: * of the GNU Lesser General Public License as published by the Free Software Foundation;
12: * either version 2.1 of the License, or (at your option) any later version.
13: *
14: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16: * See the GNU Lesser General Public License for more details.
17: *
18: * You should have received a copy of the GNU Lesser General Public License along with this
19: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20: * Boston, MA 02111-1307, USA.
21: *
22: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
23: * in the United States and other countries.]
24: *
25: * ------------
26: * $Id: DrawablePrintable.java 3525 2007-10-16 11:43:48Z tmorgner $
27: * ------------
28: * (C) Copyright 2000-2005, by Object Refinery Limited.
29: * (C) Copyright 2005-2007, by Pentaho Corporation.
30: */package org.jfree.report.modules.gui.swing.printing;
31:
32: import java.awt.Graphics;
33: import java.awt.Graphics2D;
34: import java.awt.geom.Rectangle2D;
35: import java.awt.print.PageFormat;
36: import java.awt.print.Printable;
37: import java.awt.print.PrinterException;
38:
39: import org.jfree.ui.Drawable;
40:
41: /**
42: * Creation-Date: 15.11.2006, 22:14:09
43: *
44: * @author Thomas Morgner
45: */
46: public class DrawablePrintable implements Printable {
47: private Drawable drawable;
48:
49: public DrawablePrintable(final Drawable drawable) {
50: this .drawable = drawable;
51: }
52:
53: /**
54: * Prints the page at the specified index into the specified {@link
55: * java.awt.Graphics} context in the specified format. A
56: * <code>PrinterJob</code> calls the <code>Printable</code> interface to
57: * request that a page be rendered into the context specified by
58: * <code>graphics</code>. The format of the page to be drawn is specified by
59: * <code>pageFormat</code>. The zero based index of the requested page is
60: * specified by <code>pageIndex</code>. If the requested page does not exist
61: * then this method returns NO_SUCH_PAGE; otherwise PAGE_EXISTS is returned.
62: * The <code>Graphics</code> class or subclass implements the {@link
63: * java.awt.print.PrinterGraphics} interface to provide additional
64: * information. If the <code>Printable</code> object aborts the print job
65: * then it throws a {@link java.awt.print.PrinterException}.
66: *
67: * @param graphics the context into which the page is drawn
68: * @param pageFormat the size and orientation of the page being drawn
69: * @param pageIndex the zero based index of the page to be drawn
70: * @return PAGE_EXISTS if the page is rendered successfully or NO_SUCH_PAGE if
71: * <code>pageIndex</code> specifies a non-existent page.
72: * @throws java.awt.print.PrinterException
73: * thrown when the print job is terminated.
74: */
75: public int print(final Graphics graphics,
76: final PageFormat pageFormat, final int pageIndex)
77: throws PrinterException {
78: if (drawable == null) {
79: return NO_SUCH_PAGE;
80: }
81:
82: final Graphics2D g2 = (Graphics2D) graphics;
83: final Rectangle2D bounds = new Rectangle2D.Double(0, 0,
84: pageFormat.getImageableWidth(), pageFormat
85: .getImageableHeight());
86: drawable.draw(g2, bounds);
87: return PAGE_EXISTS;
88: }
89: }
|