package com.xoetrope.print;
import java.awt.Container;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import net.xoetrope.xui.XProject;
import net.xoetrope.xui.XProjectManager;
import net.xoetrope.xui.style.XStyle;
/**
* A decoration of the XPage to allow printing. The class can also support printing in panels or other containers.
*
* <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
* the GNU Public License (GPL), please see license.txt for more details. If
* you make commercial use of this software you must purchase a commercial
* license from Xoetrope.</p>
* <p> $Revision: 1.4 $</p>
*/
public class PrintablePage implements Printable
{
/**
* The current page or container being printed
*/
protected Container currentPage;
/**
* The PrintOut class to which this printable page belongs
*/
protected Printout printout;
/**
* The owner project and the context in which this object operates.
*/
protected XProject currentProject = XProjectManager.getCurrentProject();
/**
* Create a new printable page
* @param page The page or container to be printed
* @param po The printout that will 'own' the printable page
*/
public PrintablePage( Container page, Printout po )
{
currentPage = page;
printout = po;
}
/**
* Print the page
* @param g The graphics context
* @param pf the page format
* @param pageIndex the index of the page within the printout page set
* @throws java.awt.print.PrinterException Some printing exception occured
* @return Printable.PAGE_EXISTS on success
*/
public int print( Graphics g, PageFormat pf, int pageIndex ) throws PrinterException
{
Font f = currentProject.getStyleManager().getFont( "base" );
g.setFont( f );
g.setColor( currentProject.getStyleManager().getStyle( "base" ).getStyleAsColor( XStyle.COLOR_FORE ));
FontMetrics fm = g.getFontMetrics();
double imageableX = pf.getImageableX();
double imageableY = pf.getImageableY();
double imageableW = pf.getImageableWidth();
double imageableH = pf.getImageableHeight();
Graphics2D g2 = ( Graphics2D )g;
int headerHeight = 0, footerHeight = 0;
for ( int i = 0; i < 3; i++ )
headerHeight = Math.max( headerHeight, printout.printDecoration( g2, pf, true, i, imageableX + i * imageableW / 3.0, imageableY, fm ) );
for ( int i = 0; i < 3; i++ )
footerHeight = Math.max( footerHeight, printout.printDecoration( g2, pf, false, i, imageableX + i * imageableW / 3.0, imageableY + imageableH - ( headerHeight + footerHeight ), fm ) );
g2.translate( imageableX, imageableY );
g2.setClip( 0, 0, currentPage.getWidth(), currentPage.getHeight() );
// Setup the scaling
double scale;
imageableH -= ( headerHeight + footerHeight );
if ( pf.getOrientation() == PageFormat.LANDSCAPE )
scale = imageableH / currentPage.getHeight();
else
scale = imageableW / currentPage.getWidth();
g2.translate( imageableX, imageableY + headerHeight );
g2.scale( scale, scale );
g2.setClip( 0, 0, currentPage.getWidth(), currentPage.getHeight() );
currentPage.print( g );
currentPage.printComponents( g );
return Printable.PAGE_EXISTS;
}
/**
* Get the page or container wrapped by this class.
* @return the wrapped container
*/
public Container getPage()
{
return currentPage;
}
}
|