001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: ComponentPrinter.java,v $
023   Revision 1.4  2004/05/05 21:22:45  markl
024   Comment header updates.
025
026   Revision 1.3  2003/01/19 09:42:38  markl
027   Javadoc & comment header updates.
028
029   Revision 1.2  2002/08/11 09:57:21  markl
030   Version number change.
031
032   Revision 1.1  2002/08/11 09:48:38  markl
033   New class.
034   ----------------------------------------------------------------------------
035*/
036
037package kiwi.util;
038
039import java.awt.*;
040import java.awt.print.*;
041import javax.swing.*;
042
043/** A class for printing arbitrary components. To print a given component,
044 * simply use the class as follows:
045 *
046 * <pre>
047 * ComponentPrinter cp = new ComponentPrinter(someComponent);
048 * cp.print();
049 * </pre>
050 *
051 * The <code>print()</code> method may be invoked multiple times.
052 *
053 * @author <a href="http://www.apl.jhu.edu/~hall/java/">Marty Hall</a>
054 * @author John N. Kostaras
055 * @author Mark Lindner
056 * @since Kiwi 1.3.4
057 */
058
059public class ComponentPrinter implements Printable
060  {
061  private Component component;
062
063  /** Construct a new <code>ComponentPrinter</code> for printing the specified
064   * component.
065   *
066   * @param component The component to be printed.
067   */
068  
069  public ComponentPrinter(Component component)
070    {
071    this.component = component;
072    }
073
074  /** Specify a different component to be printed.
075   *
076   * @param component The new component.
077   */
078
079  public void setComponent(Component component)
080    {
081    this.component = component;
082    }
083
084  /** Print the component.
085   *
086   * @throws java.awt.print.PrinterException If an error occurred during
087   * printing.
088   */
089  
090  public void print() throws PrinterException
091    {
092    PrinterJob printJob = PrinterJob.getPrinterJob();
093    PageFormat pageformat = printJob.pageDialog(printJob.defaultPage());
094    printJob.setPrintable(this);
095    if(printJob.printDialog())
096      printJob.print();
097    }
098  
099  /** Implementation of the <code>Printable</code> interface; this method
100   *  should not be called directly.
101   */
102  
103  public int print(Graphics g, PageFormat pageFormat, int pageIndex)
104    {
105    if(pageIndex != 0)
106      return(NO_SUCH_PAGE);
107    
108    RepaintManager repmgr = RepaintManager.currentManager(component);
109    
110    Graphics2D g2d = (Graphics2D)g;
111    g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
112    boolean flag = repmgr.isDoubleBufferingEnabled();
113    repmgr.setDoubleBufferingEnabled(false);
114    component.paint(g2d);
115    repmgr.setDoubleBufferingEnabled(flag);
116    
117    return(PAGE_EXISTS);
118    }
119    
120  }
121
122/* end of source file */