001    /**
002     * ========================================
003     * JFreeReport : a free Java report library
004     * ========================================
005     *
006     * Project Info:  http://reporting.pentaho.org/
007     *
008     * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
009     *
010     * This library is free software; you can redistribute it and/or modify it under the terms
011     * of the GNU Lesser General Public License as published by the Free Software Foundation;
012     * either version 2.1 of the License, or (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016     * See the GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public License along with this
019     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020     * Boston, MA 02111-1307, USA.
021     *
022     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023     * in the United States and other countries.]
024     *
025     * ------------
026     * $Id: DrawablePrintable.java 3525 2007-10-16 11:43:48Z tmorgner $
027     * ------------
028     * (C) Copyright 2000-2005, by Object Refinery Limited.
029     * (C) Copyright 2005-2007, by Pentaho Corporation.
030     */
031    
032    package org.jfree.report.modules.gui.swing.printing;
033    
034    import java.awt.Graphics;
035    import java.awt.Graphics2D;
036    import java.awt.geom.Rectangle2D;
037    import java.awt.print.PageFormat;
038    import java.awt.print.Printable;
039    import java.awt.print.PrinterException;
040    
041    import org.jfree.ui.Drawable;
042    
043    /**
044     * Creation-Date: 15.11.2006, 22:14:09
045     *
046     * @author Thomas Morgner
047     */
048    public class DrawablePrintable implements Printable
049    {
050      private Drawable drawable;
051    
052      public DrawablePrintable(final Drawable drawable)
053      {
054        this.drawable = drawable;
055      }
056    
057      /**
058       * Prints the page at the specified index into the specified {@link
059       * java.awt.Graphics} context in the specified format.  A
060       * <code>PrinterJob</code> calls the <code>Printable</code> interface to
061       * request that a page be rendered into the context specified by
062       * <code>graphics</code>.  The format of the page to be drawn is specified by
063       * <code>pageFormat</code>.  The zero based index of the requested page is
064       * specified by <code>pageIndex</code>. If the requested page does not exist
065       * then this method returns NO_SUCH_PAGE; otherwise PAGE_EXISTS is returned.
066       * The <code>Graphics</code> class or subclass implements the {@link
067       * java.awt.print.PrinterGraphics} interface to provide additional
068       * information.  If the <code>Printable</code> object aborts the print job
069       * then it throws a {@link java.awt.print.PrinterException}.
070       *
071       * @param graphics   the context into which the page is drawn
072       * @param pageFormat the size and orientation of the page being drawn
073       * @param pageIndex  the zero based index of the page to be drawn
074       * @return PAGE_EXISTS if the page is rendered successfully or NO_SUCH_PAGE if
075       *         <code>pageIndex</code> specifies a non-existent page.
076       * @throws java.awt.print.PrinterException
077       *          thrown when the print job is terminated.
078       */
079      public int print(final Graphics graphics, final PageFormat pageFormat, final int pageIndex)
080          throws PrinterException
081      {
082        if (drawable == null)
083        {
084          return NO_SUCH_PAGE;
085        }
086    
087        final Graphics2D g2 = (Graphics2D) graphics;
088        final Rectangle2D bounds = new Rectangle2D.Double
089            (0,0, pageFormat.getImageableWidth(), pageFormat.getImageableHeight());
090        drawable.draw(g2, bounds);
091        return PAGE_EXISTS;
092      }
093    }