jwo.utils.gui
Class PrintUtilities

java.lang.Object
  extended by jwo.utils.gui.PrintUtilities
All Implemented Interfaces:
Runnable, PrintJobListener

public class PrintUtilities
extends Object
implements Runnable, PrintJobListener

Handles the printing of any printable object. Includes a method for creating a print preview based on default printer settings.

Version:
2.3, 12th April, 2006.
Author:
Jo Wood,

Constructor Summary
PrintUtilities()
          Sets up the print services for possible printing.
PrintUtilities(DocFlavor flavour)
          Sets up the print services for possible printing.
 
Method Summary
 int calcNumPages(Dimension objectSize, PageFormat pageFormat)
          Calculates the number of pages that would be expected to print content of a given size assuming the given page format.
static Paper createA3()
          Returns an object representing A3 paper with 2cm margins.
static Paper createA4()
          Returns an object representing A4 paper with 2cm margins.
 float getPageScale(Dimension objectSize, PageFormat pageFormat)
          Reports the scaling that would be applied if an object of the given size was to be printed to be as large as possible on a single page.
 float getWidthScale(Dimension objectSize, PageFormat pageFormat)
          Reports the scaling that would be applied if an object of the given size was to be printed so its width matched the supplied page format printable width.
 void print(Printable printableObject, boolean showDialogue)
          Prints the given printable object.
 void printDataTransferCompleted(PrintJobEvent e)
          Reports that the transfer of data to print job has been completed, but currently ignored.
 void printJobCanceled(PrintJobEvent e)
          Reports that the print job has been cancelled for some reason.
 void printJobCompleted(PrintJobEvent e)
          Reports that the print job has been completed, but currently ignored.
 void printJobFailed(PrintJobEvent e)
          Reports that there has been some problem with the print job.
 void printJobNoMoreEvents(PrintJobEvent e)
          Reports that there will be no more events generated by this print job, but currently ignored.
 void printJobRequiresAttention(PrintJobEvent e)
          Reports that print job requires attention of some kind.
 void printPreview(Frame parent, Printable printable)
          Provides a print preview of the given printable object.
 void run()
          Queries the available print services.
 Graphics2D scalePage(Graphics g, Dimension objectSize, PageFormat pageFormat)
          Scales the given graphics to fit on a single page with the given printer page dimensions.
 Graphics2D scaleWidth(Graphics g, Dimension objectSize, PageFormat pageFormat, int pageIndex)
          Scales the given graphics to the required printer page dimensions.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PrintUtilities

public PrintUtilities()
Sets up the print services for possible printing. To print a printable object, call one of the print() methods.


PrintUtilities

public PrintUtilities(DocFlavor flavour)
Sets up the print services for possible printing. To print a printable object, call one of the print() methods.

Parameters:
flavour - Type of document to print.
Method Detail

run

public void run()
Queries the available print services. This process is called by the constructor. Printing will be queued until the enquiries have been made.

Specified by:
run in interface Runnable

print

public void print(Printable printableObject,
                  boolean showDialogue)
Prints the given printable object.

Parameters:
printableObject - Printable object.
showDialogue - Will ask for printer type if true.

printPreview

public void printPreview(Frame parent,
                         Printable printable)
Provides a print preview of the given printable object. If output is more than one page long, page navigation buttons allow user to view each in turn.

Parameters:
parent - Parent GUI requesting print preview.
printable - Printable object.

scaleWidth

public Graphics2D scaleWidth(Graphics g,
                             Dimension objectSize,
                             PageFormat pageFormat,
                             int pageIndex)
Scales the given graphics to the required printer page dimensions. Will scale over several pages if necessary. Scales width of object to width of printable area on paper.

Parameters:
g - Graphics context to print.
objectSize - Size of object to print.
pageFormat - Page format for printing (includes paper dimensions, margins etc).
pageIndex - Page index (for multiple pages).
Returns:
Scaled graphics context ready for printing.

scalePage

public Graphics2D scalePage(Graphics g,
                            Dimension objectSize,
                            PageFormat pageFormat)
Scales the given graphics to fit on a single page with the given printer page dimensions.

Parameters:
g - Graphics context to print.
objectSize - Size of object to print.
pageFormat - Page format for printing (includes paper dimensions, margins etc).
Returns:
Scaled graphics context ready for printing.

calcNumPages

public int calcNumPages(Dimension objectSize,
                        PageFormat pageFormat)
Calculates the number of pages that would be expected to print content of a given size assuming the given page format. Assumes that object is scaled to make its width match the given page format's printable width.

Parameters:
objectSize - Size of object to print.
pageFormat - Page format for printing.
Returns:
Number of pages required for printing.

getWidthScale

public float getWidthScale(Dimension objectSize,
                           PageFormat pageFormat)
Reports the scaling that would be applied if an object of the given size was to be printed so its width matched the supplied page format printable width. This can be useful for displaying scale invariant objects and text.

Parameters:
objectSize - Size of object to print.
pageFormat - Page format for printing.
Returns:
Scaling that would be applied.

getPageScale

public float getPageScale(Dimension objectSize,
                          PageFormat pageFormat)
Reports the scaling that would be applied if an object of the given size was to be printed to be as large as possible on a single page. This can be useful for displaying scale invariant objects and text.

Parameters:
objectSize - Size of object to print.
pageFormat - Page format for printing.
Returns:
Scaling that would be applied.

printDataTransferCompleted

public void printDataTransferCompleted(PrintJobEvent e)
Reports that the transfer of data to print job has been completed, but currently ignored.

Specified by:
printDataTransferCompleted in interface PrintJobListener
Parameters:
e - Event monitoring print job.

printJobCompleted

public void printJobCompleted(PrintJobEvent e)
Reports that the print job has been completed, but currently ignored.

Specified by:
printJobCompleted in interface PrintJobListener
Parameters:
e - Event monitoring print job.

printJobFailed

public void printJobFailed(PrintJobEvent e)
Reports that there has been some problem with the print job.

Specified by:
printJobFailed in interface PrintJobListener
Parameters:
e - Event monitoring print job.

printJobCanceled

public void printJobCanceled(PrintJobEvent e)
Reports that the print job has been cancelled for some reason.

Specified by:
printJobCanceled in interface PrintJobListener
Parameters:
e - Event monitoring print job.

printJobNoMoreEvents

public void printJobNoMoreEvents(PrintJobEvent e)
Reports that there will be no more events generated by this print job, but currently ignored.

Specified by:
printJobNoMoreEvents in interface PrintJobListener
Parameters:
e - Event monitoring print job.

printJobRequiresAttention

public void printJobRequiresAttention(PrintJobEvent e)
Reports that print job requires attention of some kind.

Specified by:
printJobRequiresAttention in interface PrintJobListener
Parameters:
e - Event monitoring print job.

createA4

public static Paper createA4()
Returns an object representing A4 paper with 2cm margins.

Returns:
A4 paper dimensions.

createA3

public static Paper createA3()
Returns an object representing A3 paper with 2cm margins.

Returns:
A3 paper dimensions.


Copyright Jo Wood, 1996-2009, last modified, 17th April, 2009