edu.northwestern.at.utils.swing.printing
Class PrintView

java.lang.Object
  extended by javax.swing.text.View
      extended by javax.swing.text.CompositeView
          extended by javax.swing.text.BoxView
              extended by edu.northwestern.at.utils.swing.printing.PrintView
All Implemented Interfaces:
javax.swing.SwingConstants

public class PrintView
extends javax.swing.text.BoxView

Arranges Swing text component page images within a rectangular area for printing.

Swing text components such as JTextPane maintain document text as a series of paragraphs. Corresponding to each paragraph is a View which may be rendered onto a graphics context. The paragraph view typically contains lines of styled text stored as child row views, or components such as labels and images.

To print the text from a text component, we create a vertical stack of the paragraph views in the document, asking each to reformat itself to the provided printer page dimensions. We try to fit as many paragraph views as possible on a single printer page. When a paragraph view won't fit within the remaining space on the page, we print as many of its child row views as will fit on the current page, and then we print the rest on the next page. We maintain state for each page to remember which view and how much of it was printed on a page, so that the next time we are called to print another page, we know where to start.

Some objects (such as images) may be too large horizontally or vertically to fit on a single printer page. We handle this by scaling the object to fit within the confines of a single printer page. Some alternative ways to handle oversized objects like images, which are not implemented here, include just not printing the oversized object at all; clipping the object in its original size to fit within the page boundaries; combining clipping/arbitrary splitting across pages; etc.


Nested Class Summary
protected  class PrintView.PageState
          Class holding information about last view on a printed page.
 
Field Summary
protected  int currentPageIndex
          Index of current page.
protected  int firstOnPage
          Index of first view to be rendered on current page.
protected  int firstOnPageOffset
          Offset into first view to be rendered on current page.
protected  PrintHeaderFooter headerAndFooter
          Header and footer.
protected  PrintView.PageState pageState
          Current page state.
protected  java.util.HashMap pageStateMap
          Hashmap holds page state for each printed page.
 
Fields inherited from class javax.swing.text.View
BadBreakWeight, ExcellentBreakWeight, ForcedBreakWeight, GoodBreakWeight, X_AXIS, Y_AXIS
 
Fields inherited from interface javax.swing.SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
 
Constructor Summary
PrintView(javax.swing.text.Element elem, javax.swing.text.View root, int width, int height)
          Create PrintView.
PrintView(javax.swing.text.Element elem, javax.swing.text.View root, int width, int height, PrintHeaderFooter headerAndFooter)
          Create PrintView.
 
Method Summary
 int getScaledVerticalSpan(javax.swing.text.View view, int printerPageWidth, int printerPageHeight)
          Get scaled vertical span used by view.
 void printChildView(java.awt.Graphics2D graphics2D, java.awt.Rectangle rect, javax.swing.text.View view, double scaleFactor)
          Paint child view row using a specified scale factor.
 boolean printPage(java.awt.Graphics graphics, int pageHeight, int pageIndex)
          Print one page.
 
Methods inherited from class javax.swing.text.BoxView
baselineLayout, baselineRequirements, calculateMajorAxisRequirements, calculateMinorAxisRequirements, childAllocation, flipEastAndWestAtEnds, forwardUpdate, getAlignment, getAxis, getChildAllocation, getHeight, getMaximumSpan, getMinimumSpan, getOffset, getPreferredSpan, getResizeWeight, getSpan, getViewAtPoint, getWidth, isAfter, isAllocationValid, isBefore, isLayoutValid, layout, layoutChanged, layoutMajorAxis, layoutMinorAxis, modelToView, paint, paintChild, preferenceChanged, replace, setAxis, setSize, viewToModel
 
Methods inherited from class javax.swing.text.CompositeView
getBottomInset, getInsideAllocation, getLeftInset, getNextEastWestVisualPositionFrom, getNextNorthSouthVisualPositionFrom, getNextVisualPositionFrom, getRightInset, getTopInset, getView, getViewAtPosition, getViewCount, getViewIndex, getViewIndexAtPosition, loadChildren, modelToView, setInsets, setParagraphInsets, setParent
 
Methods inherited from class javax.swing.text.View
append, breakView, changedUpdate, createFragment, forwardUpdateToView, getAttributes, getBreakWeight, getContainer, getDocument, getElement, getEndOffset, getGraphics, getParent, getStartOffset, getToolTipText, getViewFactory, getViewIndex, insert, insertUpdate, isVisible, modelToView, remove, removeAll, removeUpdate, updateChildren, updateLayout, viewToModel
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

firstOnPage

protected int firstOnPage
Index of first view to be rendered on current page.


firstOnPageOffset

protected int firstOnPageOffset
Offset into first view to be rendered on current page.


pageState

protected PrintView.PageState pageState
Current page state.


pageStateMap

protected java.util.HashMap pageStateMap
Hashmap holds page state for each printed page.


currentPageIndex

protected int currentPageIndex
Index of current page.


headerAndFooter

protected PrintHeaderFooter headerAndFooter
Header and footer.

Constructor Detail

PrintView

public PrintView(javax.swing.text.Element elem,
                 javax.swing.text.View root,
                 int width,
                 int height,
                 PrintHeaderFooter headerAndFooter)
Create PrintView.

Parameters:
elem - Root element of document to print
root - Root view of document to print
width - Printing width
height - Print height
headerAndFooter - Specifies header and footer for each page

PrintView

public PrintView(javax.swing.text.Element elem,
                 javax.swing.text.View root,
                 int width,
                 int height)
Create PrintView.

Parameters:
elem - Root element of document to print
root - Root view of document to print
width - Printing width
height - Print height
Method Detail

printChildView

public void printChildView(java.awt.Graphics2D graphics2D,
                           java.awt.Rectangle rect,
                           javax.swing.text.View view,
                           double scaleFactor)
Paint child view row using a specified scale factor.

Parameters:
graphics2D - Graphics context in which to render a page.
rect - Page location and unscaled view size.
view - View to print.
scaleFactor - Scale factor to use when printing.

getScaledVerticalSpan

public int getScaledVerticalSpan(javax.swing.text.View view,
                                 int printerPageWidth,
                                 int printerPageHeight)
Get scaled vertical span used by view.

Parameters:
view - The view whose vertical span is desired.
printerPageWidth - Width of printable printer page area.
printerPageHeight - Height of printable printer page area.
Returns:
The vertical span. For most views this is just the usual maximum vertical span. For views which need to be scaled to fit the printer page, this is the usual vertical span multiplied by the scale factor for the view.

printPage

public boolean printPage(java.awt.Graphics graphics,
                         int pageHeight,
                         int pageIndex)
Print one page.

Parameters:
graphics - Graphics context in which to render a page.
pageHeight - The page height.
pageIndex - Index of page to render.
Returns:
True if given page is rendered successfully, False if end of document reached.