net.sf.banglapad
Class DocumentRenderer

java.lang.Object
  extended by net.sf.banglapad.DocumentRenderer
All Implemented Interfaces:
java.awt.print.Printable

public class DocumentRenderer
extends java.lang.Object
implements java.awt.print.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() or setJEditorPane(). The Document may then be printed by setting the instance of DocumentRenderer in any PrinterJob.


Field Summary
 
Fields inherited from interface java.awt.print.Printable
NO_SUCH_PAGE, PAGE_EXISTS
 
Constructor Summary
DocumentRenderer()
          The constructor initializes the pFormat and PJob variables.
 
Method Summary
 javax.swing.text.Document getDocument()
          Method to get the current Document
 boolean getScaleWidthToFit()
          Method to get the current choice the width scaling option.
 void pageDialog()
          pageDialog() displays a page setup dialog.
 int print(java.awt.Graphics graphics, java.awt.print.PageFormat pageFormat, int pageIndex)
          The print method implements the Printable interface.
 void print(javax.swing.text.html.HTMLDocument htmlDocument)
          print(HTMLDocument) is called to set an HTMLDocument for printing.
 void print(javax.swing.JEditorPane jedPane)
          print(JEditorPane) prints a Document contained within a JEDitorPane.
 void print(javax.swing.text.PlainDocument plainDocument)
          print(PlainDocument) is called to set a PlainDocument for printing.
 void setDocument(javax.swing.text.html.HTMLDocument htmlDocument)
          Method to set an HTMLDocument as the Document to print.
 void setDocument(javax.swing.JEditorPane jedPane)
          Method to set the Document to print as the one contained in a JEditorPane.
 void setDocument(javax.swing.text.PlainDocument plainDocument)
          Method to set a PlainDocument as the Document to print.
 void setScaleWidthToFit(boolean scaleWidth)
          Method to set the current choice of the width scaling option.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DocumentRenderer

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

Method Detail

getDocument

public javax.swing.text.Document getDocument()
Method to get the current Document


getScaleWidthToFit

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


pageDialog

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


print

public int print(java.awt.Graphics graphics,
                 java.awt.print.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:

  1. The Graphics object is cast to a Graphics2D object to allow for scaling.
  2. 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.
  3. 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.
  4. If the scaleWidthToFit option is chosen, a scaling ratio is determined, and the graphics2D object is scaled.
  5. The Graphics2D object is clipped to the size of the printable page.
  6. currentPage is checked to see if this is a new page to render. If so, pageStartY and pageEndY are reset.
  7. 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.
  8. 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.

  9. 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 java.awt.print.Printable

print

public void print(javax.swing.text.html.HTMLDocument htmlDocument)
print(HTMLDocument) is called to set an HTMLDocument for printing.


print

public void print(javax.swing.JEditorPane jedPane)
print(JEditorPane) prints a Document contained within a JEDitorPane.


print

public void print(javax.swing.text.PlainDocument plainDocument)
print(PlainDocument) is called to set a PlainDocument for printing.


setDocument

public void setDocument(javax.swing.text.html.HTMLDocument htmlDocument)
Method to set an HTMLDocument as the Document to print.


setDocument

public void setDocument(javax.swing.JEditorPane jedPane)
Method to set the Document to print as the one contained in a JEditorPane. This method is useful when Java does not provide direct access to a particular Document type, such as a Rich Text Format document. With this method such a document can be sent to the DocumentRenderer class enclosed in a JEditorPane.


setDocument

public void setDocument(javax.swing.text.PlainDocument plainDocument)
Method to set a PlainDocument as the Document to print.


setScaleWidthToFit

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