TV-Browser 2.7.5 API

printplugin.printer.singleprogramprinter
Class DocumentRenderer

java.lang.Object
  extended by printplugin.printer.singleprogramprinter.DocumentRenderer
All Implemented Interfaces:
Printable

public class DocumentRenderer
extends Object
implements Printable

DocumentRenderer prints objects of type Document. Text attributes, including fonts, color, and small icons, will be rendered to a printed page. DocumentRenderer computes line breaks, paginates, and performs other formatting. An HTMLDocument is printed by sending it as an argument to the print(HTMLDocument) method. A PlainDocument is printed the same way. Other types of documents must be sent in a JEditorPane as an argument to the print(JEditorPane) method. Printing Documents in this way will automatically display a print dialog. As objects which implement the Printable Interface, instances of the DocumentRenderer class can also be used as the argument in the setPrintable method of the PrinterJob class. Instead of using the print() methods detailed above, a programmer may gain access to the formatting capabilities of this class without using its print dialog by creating an instance of DocumentRenderer and setting the document to be printed with the setDocument(). The Document may then be printed by setting the instance of DocumentRenderer in any PrinterJob.


Field Summary
protected  int mCurrentPage
          Used to keep track of when the page to print changes.
protected  JEditorPane mJEditorPane
          Container to hold the Document.
protected  double mPageEndY
          Location of the current page end
protected  PageFormat mPageFormat
          The DocumentRenderer class uses pFormat and pJob in its methods.
protected  double mPageStartY
          Location of the current page start
protected  PrinterJob mPrintJob
          The PrintJob
protected  boolean mScaleWidthToFit
          boolean to allow control over whether pages too wide to fit on a page will be scaled.
 
Fields inherited from interface java.awt.print.Printable
NO_SUCH_PAGE, PAGE_EXISTS
 
Constructor Summary
DocumentRenderer(PageFormat pageFormat)
          The constructor initializes the pFormat and PJob variables.
 
Method Summary
 int getPageCount()
          Calculates the Number of Pages
 boolean getScaleWidthToFit()
          Method to get the current choice the width scaling option.
 void pageDialog()
          pageDialog() displays a page setup dialog.
 int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
          The print method implements the Printable interface.
protected  boolean printView(Graphics2D graphics2D, Shape allocation, View view)
          printView is a recursive method which iterates through the tree structure of the view sent to it.
protected  void setContentType(String type)
          Method to set the content type the JEditorPane.
protected  void setDocument(String type, Document document)
          Method to set the content type and document of the JEditorPane.
 void setEditorPane(JEditorPane jedPane)
          Method to set the JEditorPane that will be printed
 void setScaleWidthToFit(boolean scaleWidth)
          Method to set the current choice of the width scaling option.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mCurrentPage

protected int mCurrentPage
Used to keep track of when the page to print changes.


mJEditorPane

protected JEditorPane mJEditorPane
Container to hold the Document. This object will be used to lay out the Document for printing.


mPageEndY

protected double mPageEndY
Location of the current page end


mPageStartY

protected double mPageStartY
Location of the current page start


mScaleWidthToFit

protected boolean mScaleWidthToFit
boolean to allow control over whether pages too wide to fit on a page will be scaled.


mPageFormat

protected PageFormat mPageFormat
The DocumentRenderer class uses pFormat and pJob in its methods. Note that pFormat is not the variable name used by the print method of the DocumentRenderer. Although it would always be expected to reference the pFormat object, the print method gets its PageFormat as an argument.


mPrintJob

protected PrinterJob mPrintJob
The PrintJob

Constructor Detail

DocumentRenderer

public DocumentRenderer(PageFormat pageFormat)
The constructor initializes the pFormat and PJob variables.

Parameters:
pageFormat - Foramt of the Page
Method Detail

pageDialog

public void pageDialog()
pageDialog() displays a page setup dialog.


print

public int print(Graphics graphics,
                 PageFormat pageFormat,
                 int pageIndex)
The print method implements the Printable interface. Although Printables may be called to render a page more than once, each page is painted in order. We may, therefore, keep track of changes in the page being rendered by setting the currentPage variable to equal the pageIndex, and then comparing these variables on subsequent calls to this method. When the two variables match, it means that the page is being rendered for the second or third time. When the currentPage differs from the pageIndex, a new page is being requested. The highlights of the process used print a page are as follows: I. The Graphics object is cast to a Graphics2D object to allow for scaling. II. The JEditorPane is laid out using the width of a printable page. This will handle line breaks. If the JEditorPane cannot be sized at the width of the graphics clip, scaling will be allowed. III. The root view of the JEditorPane is obtained. By examining this root view and all of its children, printView will be able to determine the location of each printable element of the document. IV. If the scaleWidthToFit option is chosen, a scaling ratio is determined, and the graphics2D object is scaled. V. The Graphics2D object is clipped to the size of the printable page. VI. currentPage is checked to see if this is a new page to render. If so, pageStartY and pageEndY are reset. VII. To match the coordinates of the printable clip of graphics2D and the allocation rectangle which will be used to lay out the views, graphics2D is translated to begin at the printable X and Y coordinates of the graphics clip. VIII. An allocation Rectangle is created to represent the layout of the Views. The Printable Interface always prints the area indexed by reference to the Graphics object. For instance, with a standard 8.5 x 11 inch page with 1 inch margins the rectangle X = 72, Y = 72, Width = 468, and Height = 648, the area 72, 72, 468, 648 will be painted regardless of which page is actually being printed. To align the allocation Rectangle with the graphics2D object two things are done. The first step is to translate the X and Y coordinates of the graphics2D object to begin at the X and Y coordinates of the printable clip, see step VII. Next, when printing other than the first page, the allocation rectangle must start laying out in coordinates represented by negative numbers. After page one, the beginning of the allocation is started at minus the page end of the prior page. This moves the part which has already been rendered to before the printable clip of the graphics2D object. X. The printView method is called to paint the page. Its return value will indicate if a page has been rendered. Although public, print should not ordinarily be called by programs other than PrinterJob.

Specified by:
print in interface Printable

printView

protected boolean printView(Graphics2D graphics2D,
                            Shape allocation,
                            View view)
printView is a recursive method which iterates through the tree structure of the view sent to it. If the view sent to printView is a branch view, that is one with children, the method calls itself on each of these children. If the view is a leaf view, that is a view without children which represents an actual piece of text to be painted, printView attempts to render the view to the Graphics2D object. I. When any view starts after the beginning of the current printable page, this means that there are pages to print and the method sets pageExists to true. II. When a leaf view is taller than the printable area of a page, it cannot, of course, be broken down to fit a single page. Such a View will be printed whenever it intersects with the Graphics2D clip. III. If a leaf view intersects the printable area of the graphics clip and fits vertically within the printable area, it will be rendered. IV. If a leaf view does not exceed the printable area of a page but does not fit vertically within the Graphics2D clip of the current page, the method records that this page should end at the start of the view. This information is stored in pageEndY.


setDocument

protected void setDocument(String type,
                           Document document)
Method to set the content type and document of the JEditorPane.


setContentType

protected void setContentType(String type)
Method to set the content type the JEditorPane.


setEditorPane

public void setEditorPane(JEditorPane jedPane)
Method to set the JEditorPane that will be printed


setScaleWidthToFit

public void setScaleWidthToFit(boolean scaleWidth)
Method to set the current choice of the width scaling option.


getScaleWidthToFit

public boolean getScaleWidthToFit()
Method to get the current choice the width scaling option.


getPageCount

public int getPageCount()
Calculates the Number of Pages

Returns:
Number of Pages

TV-Browser 2.7.5 API

TV-Browser 2.7.5, Copyright (C) 04-2003 Martin Oberhauser (martin@tvbrowser.org)