|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object javax.swing.text.View javax.swing.text.CompositeView javax.swing.text.BoxView edu.northwestern.at.utils.swing.printing.PrintView
public class PrintView
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 |
---|
protected int firstOnPage
protected int firstOnPageOffset
protected PrintView.PageState pageState
protected java.util.HashMap pageStateMap
protected int currentPageIndex
protected PrintHeaderFooter headerAndFooter
Constructor Detail |
---|
public PrintView(javax.swing.text.Element elem, javax.swing.text.View root, int width, int height, PrintHeaderFooter headerAndFooter)
elem
- Root element of document to printroot
- Root view of document to printwidth
- Printing widthheight
- Print heightheaderAndFooter
- Specifies header and footer for each page public PrintView(javax.swing.text.Element elem, javax.swing.text.View root, int width, int height)
elem
- Root element of document to printroot
- Root view of document to printwidth
- Printing widthheight
- Print height Method Detail |
---|
public void printChildView(java.awt.Graphics2D graphics2D, java.awt.Rectangle rect, javax.swing.text.View view, double scaleFactor)
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. public int getScaledVerticalSpan(javax.swing.text.View view, int printerPageWidth, int printerPageHeight)
view
- The view whose vertical span is desired.printerPageWidth
- Width of printable printer page area.printerPageHeight
- Height of printable printer page area.
public boolean printPage(java.awt.Graphics graphics, int pageHeight, int pageIndex)
graphics
- Graphics context in which to render a page.pageHeight
- The page height.pageIndex
- Index of page to render.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |