org.tentackle.print
Class PrintHelper

java.lang.Object
  extended by org.tentackle.print.PrintHelper

public class PrintHelper
extends Object

Utility methods for printing.

Author:
harald

Constructor Summary
PrintHelper()
           
 
Method Summary
static boolean adjustGraphics2D(Graphics2D g, PageFormat format)
          Adjusts the graphics context according to the given format.
The job can be printed in two ways: by job.print() and by job.print(aset).
static MediaSizeName getMediaSizeName()
          Gets the media size name.
static PageFormat getPageFormat(PrintRequestAttributeSet aset)
          Builds a pageformat from an attribute set.
This is the "missing" link between printDialog(aset) and setting the pageFormat without calling pageDialog().
static Runnable getRunAfterPrint()
          Gets the Runnable to be invoked after job.print(aset).
static Runnable getRunBeforePrint()
          Gets the Runnable to be invoked before job.print(aset).
static boolean print(Printable printable, String jobName)
          Asks the user for a printer and settings and prints a printable.
static PageFormat print(PrinterJob job, String jobName, PrintRequestAttributeSet aset)
          Asks the user for a printer and settings and return the pageformat.
Does not print! Use printJob(java.awt.print.PrinterJob, javax.print.attribute.PrintRequestAttributeSet, boolean) to print.
static WorkerThread printJob(PrinterJob job, PrintRequestAttributeSet aset)
          Prints a job in separate thread in a modal dialog.
static WorkerThread printJob(PrinterJob job, PrintRequestAttributeSet aset, boolean modal)
          Prints a job in separate worker thread while displaying a printing worker dialog.
Requests are automatically queued in background if jobs are not finished yet.
static void setMediaSizeName(MediaSizeName name)
          Sets the media size.
static void setRunAfterPrint(Runnable run)
          Sets a Runnable to be invoked after job.print(aset).
static void setRunBeforePrint(Runnable run)
          Sets a Runnable to be invoked before job.print(aset).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PrintHelper

public PrintHelper()
Method Detail

print

public static boolean print(Printable printable,
                            String jobName)
Asks the user for a printer and settings and prints a printable.

Parameters:
printable - the printable
jobName - the name of the job
Returns:
true if printed, false if error or user aborted

print

public static PageFormat print(PrinterJob job,
                               String jobName,
                               PrintRequestAttributeSet aset)
Asks the user for a printer and settings and return the pageformat.
Does not print! Use printJob(java.awt.print.PrinterJob, javax.print.attribute.PrintRequestAttributeSet, boolean) to print.

Parameters:
job - is the new printerjob
jobName - is the jobname, null = none
aset - is an initialized (and possibly empty) attribute set, must not be null, because its returned!
Returns:
the pageformat and possibly updated aset, null if printing aborted

adjustGraphics2D

public static boolean adjustGraphics2D(Graphics2D g,
                                       PageFormat format)
Adjusts the graphics context according to the given format.
The job can be printed in two ways: by job.print() and by job.print(aset). The latter analyzes the aset, builds a pageformat and transforms the graphics-context accordingly. However, the first variant does not, i.e. if the pageformat returned by print(job, jobname) above is *not* PORTRAIT, the graphics context still has to be transformed and the page-dimensions changed.

Parameters:
g - the graphics context
format - the pageformat
Returns:
true if transformation adjusted, else false

getPageFormat

public static PageFormat getPageFormat(PrintRequestAttributeSet aset)
Builds a pageformat from an attribute set.
This is the "missing" link between printDialog(aset) and setting the pageFormat without calling pageDialog().

Parameters:
aset - the attribute set
Returns:
the pageformat

printJob

public static WorkerThread printJob(PrinterJob job,
                                    PrintRequestAttributeSet aset,
                                    boolean modal)
Prints a job in separate worker thread while displaying a printing worker dialog.
Requests are automatically queued in background if jobs are not finished yet. The jobs are printed in the order of invocations of this method. There are 2 queues: one for modal and one for non-modal status dialogs. At any time there is no more than one modal and no more than one non-modal worker dialog. It is recommended (though not strictly necessary) to invoke this method from another thread than swing's event dispatch thread. If not invoked from the dispatch thread the first job request will wait until the worker dialog is shown before actually start printing (better user feedback).

Parameters:
job - the printer job
aset - the printing attributes
modal - true if modal dialog
Returns:
the WorkerThread assigned to the job

printJob

public static WorkerThread printJob(PrinterJob job,
                                    PrintRequestAttributeSet aset)
Prints a job in separate thread in a modal dialog.

Parameters:
job - the printer job
aset - the printing attributes
Returns:
the WorkerThread assigned to the job
See Also:
printJob(java.awt.print.PrinterJob, javax.print.attribute.PrintRequestAttributeSet, boolean)

setMediaSizeName

public static void setMediaSizeName(MediaSizeName name)
Sets the media size.

Parameters:
name - the MediaSizeName

getMediaSizeName

public static MediaSizeName getMediaSizeName()
Gets the media size name.

Returns:
the media size name

setRunBeforePrint

public static void setRunBeforePrint(Runnable run)
Sets a Runnable to be invoked before job.print(aset). Default is null.

Parameters:
run - the runnable

getRunBeforePrint

public static Runnable getRunBeforePrint()
Gets the Runnable to be invoked before job.print(aset).

Returns:
the runnable, null if none

setRunAfterPrint

public static void setRunAfterPrint(Runnable run)
Sets a Runnable to be invoked after job.print(aset). Default is null.

Parameters:
run - the runnable

getRunAfterPrint

public static Runnable getRunAfterPrint()
Gets the Runnable to be invoked after job.print(aset).

Returns:
the runnable, null if none


Copyright © 2001-2008 Harald Krake, Bergstr. 48, 78098 Triberg, Germany, harald@krake.de