Source for org.jfree.report.modules.gui.swing.printing.DrawablePrintable

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  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:  */
  31: 
  32: package org.jfree.report.modules.gui.swing.printing;
  33: 
  34: import java.awt.Graphics;
  35: import java.awt.Graphics2D;
  36: import java.awt.geom.Rectangle2D;
  37: import java.awt.print.PageFormat;
  38: import java.awt.print.Printable;
  39: import java.awt.print.PrinterException;
  40: 
  41: import org.jfree.ui.Drawable;
  42: 
  43: /**
  44:  * Creation-Date: 15.11.2006, 22:14:09
  45:  *
  46:  * @author Thomas Morgner
  47:  */
  48: public class DrawablePrintable implements Printable
  49: {
  50:   private Drawable drawable;
  51: 
  52:   public DrawablePrintable(final Drawable drawable)
  53:   {
  54:     this.drawable = drawable;
  55:   }
  56: 
  57:   /**
  58:    * Prints the page at the specified index into the specified {@link
  59:    * java.awt.Graphics} context in the specified format.  A
  60:    * <code>PrinterJob</code> calls the <code>Printable</code> interface to
  61:    * request that a page be rendered into the context specified by
  62:    * <code>graphics</code>.  The format of the page to be drawn is specified by
  63:    * <code>pageFormat</code>.  The zero based index of the requested page is
  64:    * specified by <code>pageIndex</code>. If the requested page does not exist
  65:    * then this method returns NO_SUCH_PAGE; otherwise PAGE_EXISTS is returned.
  66:    * The <code>Graphics</code> class or subclass implements the {@link
  67:    * java.awt.print.PrinterGraphics} interface to provide additional
  68:    * information.  If the <code>Printable</code> object aborts the print job
  69:    * then it throws a {@link java.awt.print.PrinterException}.
  70:    *
  71:    * @param graphics   the context into which the page is drawn
  72:    * @param pageFormat the size and orientation of the page being drawn
  73:    * @param pageIndex  the zero based index of the page to be drawn
  74:    * @return PAGE_EXISTS if the page is rendered successfully or NO_SUCH_PAGE if
  75:    *         <code>pageIndex</code> specifies a non-existent page.
  76:    * @throws java.awt.print.PrinterException
  77:    *          thrown when the print job is terminated.
  78:    */
  79:   public int print(final Graphics graphics, final PageFormat pageFormat, final int pageIndex)
  80:       throws PrinterException
  81:   {
  82:     if (drawable == null)
  83:     {
  84:       return NO_SUCH_PAGE;
  85:     }
  86: 
  87:     final Graphics2D g2 = (Graphics2D) graphics;
  88:     final Rectangle2D bounds = new Rectangle2D.Double
  89:         (0,0, pageFormat.getImageableWidth(), pageFormat.getImageableHeight());
  90:     drawable.draw(g2, bounds);
  91:     return PAGE_EXISTS;
  92:   }
  93: }