APIdia Launch Partner
Top Description Inners Fields Constructors Methods
sun.swing.text

public Class TextComponentPrintable

extends Object
implements CountingPrintable
Class Inheritance
All Implemented Interfaces
sun.swing.text.CountingPrintable, java.awt.print.Printable
Imports
java.awt.ComponentOrientation, .Dimension, .Font, .FontMetrics, .Graphics, .Graphics2D, .Insets, .Rectangle, .Component, .Container, java.awt.font.FontRenderContext, java.awt.print.PageFormat, .Printable, .PrinterException, java.lang.reflect.InvocationTargetException, java.text.MessageFormat, java.util.ArrayList, .Collections, .List, java.util.concurrent.Callable, .ExecutionException, .FutureTask, javax.swing.BorderFactory, .CellRendererPane, .JEditorPane, .JPasswordField, .JTextArea, .JTextField, .JTextPane, .JViewport, .SwingUtilities, javax.swing.border.Border, .TitledBorder, javax.swing.text.BadLocationException, .JTextComponent, .Document, .EditorKit, .AbstractDocument, javax.swing.text.html.HTMLDocument, .HTML, sun.font.FontDesignMetrics, sun.swing.text.html.FrameEditorPaneTag

An implementation of Printable to print JTextComponent with the header and footer.

Warning

this class is to be used in javax.swing.text.JTextComponent only.

The implementation creates a new JTextComponent (printShell) to print the content using the Document, EditorKit and rendering-affecting properties from the original JTextComponent.

printShell is laid out on the first print invocation.

This class can be used on any thread. Part of the implementation is executed on the EDT though.

Author
Igor Kushnirskiy
Since
1.6

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
TextComponentPrintable.IntegerSegment

Class to represent segment of integers.

Field Summary

Modifier and TypeField and Description
private final Font
private final MessageFormat
private volatile FontRenderContext
private static final float
private final Font
private final MessageFormat
private boolean
private static final int
private boolean
private final List<TextComponentPrintable.IntegerSegment>
pagesMetrics

thread-safe list for storing pages metrics.

private final JTextComponent
printShell

Special text component used to print to the printer.

private final List<TextComponentPrintable.IntegerSegment>
rowsMetrics

stores metrics for the unhandled rows.

private final JTextComponent

Constructor Summary

AccessConstructor and Description
private
TextComponentPrintable(JTextComponent
JTextComponent to print
textComponent
,
MessageFormat
the page header or null for none
headerFormat
,
MessageFormat
the page footer or null for none
footerFormat
)

Constructs TextComponentPrintable to print JTextComponent textComponent with headerFormat and footerFormat.

Method Summary

Modifier and TypeMethod and Description
private void
acquireReadLock()

Tries to acquire document's readLock if it was released in releaseReadLock() method.

private void
calculateRowsMetrics()

Calculates rowsMetrics for the document.

private static void
createFrames(final JEditorPane
the JEditorPane to create frames for
editor
)

Triggers the frames creation for JEditorPane

private JTextComponent

Returns:

the print shell
createPrintShell
(final JTextComponent
JTextComponent to create a printShell for
textComponent
)

creates a printShell.

private JTextComponent
private static List<JEditorPane>

Returns:

list of all frames
getFrames
(final JEditorPane
the JEditorPane to find the frames for
editor
)

Returns frames under the editor.

private static void
getFrames(final Container
the container to find all frames under
container
,
List<JEditorPane>
List to append the results too
list
)

Adds all JEditorPanes under container tagged by FrameEditorPaneTag to the list.

public int

Returns:

the number of pages.
getNumberOfPages
()

Implements sun.swing.text.CountingPrintable.getNumberOfPages.

Returns the number of pages in this printable.

public static Printable

Returns:

TextComponentPrintable to print textComponent
getPrintable
(final JTextComponent
JTextComponent to print
textComponent
,
final MessageFormat
the page header, or null for none
headerFormat
,
final MessageFormat
the page footer, or null for none
footerFormat
)

Returns TextComponentPrintable to print textComponent.

private static boolean

Returns:

true if the document has frames
isFrameSetDocument
(final Document
the Document to check
document
)

Checks whether the document has frames.

private void
layout(final int
width to layout the text for
width
)

Prepares printShell for printing.

private void
layoutOnEDT(final int width)

The EDT part of layout method.

private int
printOnEDT(final Graphics graphics, final PageFormat pf, final int pageIndex)

The EDT part of the print method.

private void
releaseReadLock()

Tries to release document's readlock Note: Not to be called on the EDT.

private void
updatePagesMetrics(final int
the page to update the metrics for
pageIndex
,
final int
the page height
pageHeight
)

Calculates pageMetrics for the pages up to the pageIndex using rowsMetrics.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

footerFontback to summary
private final Font footerFont
footerFormatback to summary
private final MessageFormat footerFormat
frcback to summary
private volatile FontRenderContext frc
HEADER_FONT_SIZEback to summary
private static final float HEADER_FONT_SIZE
headerFontback to summary
private final Font headerFont
headerFormatback to summary
private final MessageFormat headerFormat
isLayoutedback to summary
private boolean isLayouted
LIST_SIZEback to summary
private static final int LIST_SIZE
needReadLockback to summary
private boolean needReadLock
pagesMetricsback to summary
private final List<TextComponentPrintable.IntegerSegment> pagesMetrics

thread-safe list for storing pages metrics. The only metrics we need are yStart and yEnd. It has to be thread-safe since metrics are calculated on the printing thread and accessed on the EDT thread. sorted

printShellback to summary
private final JTextComponent printShell

Special text component used to print to the printer.

rowsMetricsback to summary
private final List<TextComponentPrintable.IntegerSegment> rowsMetrics

stores metrics for the unhandled rows. The only metrics we need are yStart and yEnd when row is handled by updatePagesMetrics it is removed from the list. Thus the head of the list is the fist row to handle. sorted

textComponentToPrintback to summary
private final JTextComponent textComponentToPrint

Constructor Detail

TextComponentPrintableback to summary
private TextComponentPrintable(JTextComponent textComponent, MessageFormat headerFormat, MessageFormat footerFormat)

Constructs TextComponentPrintable to print JTextComponent textComponent with headerFormat and footerFormat.

Parameters
textComponent:JTextComponent

JTextComponent to print

headerFormat:MessageFormat

the page header or null for none

footerFormat:MessageFormat

the page footer or null for none

Method Detail

acquireReadLockback to summary
private void acquireReadLock()

Tries to acquire document's readLock if it was released in releaseReadLock() method.

Note

Not to be called on the EDT.

calculateRowsMetricsback to summary
private void calculateRowsMetrics()

Calculates rowsMetrics for the document. The result is stored in the rowsMetrics. Two steps process. First step is to find yStart and yEnd for the every document position. Second step is to merge all intersected segments ( [yStart, yEnd] ).

Annotations
@SuppressWarnings:deprecation
createFramesback to summary
private static void createFrames(final JEditorPane editor)

Triggers the frames creation for JEditorPane

Parameters
editor:JEditorPane

the JEditorPane to create frames for

createPrintShellback to summary
private JTextComponent createPrintShell(final JTextComponent textComponent)

creates a printShell. It creates closest text component to textComponent which uses frc from the TextComponentPrintable for the getFontMetrics method.

Parameters
textComponent:JTextComponent

JTextComponent to create a printShell for

Returns:JTextComponent

the print shell

createPrintShellOnEDTback to summary
private JTextComponent createPrintShellOnEDT(final JTextComponent textComponent)
getFramesback to summary
private static List<JEditorPane> getFrames(final JEditorPane editor)

Returns frames under the editor. The frames are created if necessary.

Parameters
editor:JEditorPane

the JEditorPane to find the frames for

Returns:List<JEditorPane>

list of all frames

getFramesback to summary
private static void getFrames(final Container container, List<JEditorPane> list)

Adds all JEditorPanes under container tagged by FrameEditorPaneTag to the list. It adds only top level JEditorPanes. For instance if there is a frame inside the frame it will return the top frame only.

Parameters
container:Container

the container to find all frames under

list:List<JEditorPane>

List to append the results too

getNumberOfPagesback to summary
public int getNumberOfPages()

Implements sun.swing.text.CountingPrintable.getNumberOfPages.

Returns the number of pages in this printable.

This number is defined only after print returns NO_SUCH_PAGE.

Returns:int

the number of pages.

getPrintableback to summary
public static Printable getPrintable(final JTextComponent textComponent, final MessageFormat headerFormat, final MessageFormat footerFormat)

Returns TextComponentPrintable to print textComponent.

Parameters
textComponent:JTextComponent

JTextComponent to print

headerFormat:MessageFormat

the page header, or null for none

footerFormat:MessageFormat

the page footer, or null for none

Returns:Printable

TextComponentPrintable to print textComponent

isFrameSetDocumentback to summary
private static boolean isFrameSetDocument(final Document document)

Checks whether the document has frames. Only HTMLDocument might have frames.

Parameters
document:Document

the Document to check

Returns:boolean

true if the document has frames

layoutback to summary
private void layout(final int width)

Prepares printShell for printing. Sets properties from the component to print. Sets width and FontRenderContext. Triggers Views creation for the printShell. There are two parts in the implementation. First part (layout) is to be called on the printing thread. Second part (layoutOnEDT) is to be called on the EDT only. layout triggers layoutOnEDT.

Parameters
width:int

width to layout the text for

layoutOnEDTback to summary
private void layoutOnEDT(final int width)

The EDT part of layout method. This method is to be called on the EDT only.

printOnEDTback to summary
private int printOnEDT(final Graphics graphics, final PageFormat pf, final int pageIndex) throws PrinterException

The EDT part of the print method. This method is to be called on the EDT only. Layout should be done before calling this method.

releaseReadLockback to summary
private void releaseReadLock()

Tries to release document's readlock Note: Not to be called on the EDT.

updatePagesMetricsback to summary
private void updatePagesMetrics(final int pageIndex, final int pageHeight)

Calculates pageMetrics for the pages up to the pageIndex using rowsMetrics. Metrics are stored in the pagesMetrics.

Parameters
pageIndex:int

the page to update the metrics for

pageHeight:int

the page height

sun.swing.text back to summary

private Class TextComponentPrintable.IntegerSegment

extends Object
implements Comparable<TextComponentPrintable.IntegerSegment>
Class Inheritance
All Implemented Interfaces
java.lang.Comparable

Class to represent segment of integers. we do not call it Segment to avoid confusion with javax.swing.text.Segment

Field Summary

Modifier and TypeField and Description
pack-priv final int
pack-priv final int

Constructor Summary

AccessConstructor and Description
pack-priv
IntegerSegment(int start, int end)

Method Summary

Modifier and TypeMethod and Description
public int
compareTo(TextComponentPrintable.IntegerSegment object)

Implements java.lang.Comparable.compareTo.

Compares this object with the specified object for order.

public boolean
equals(Object obj)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

endback to summary
pack-priv final int end
startback to summary
pack-priv final int start

Constructor Detail

IntegerSegmentback to summary
pack-priv IntegerSegment(int start, int end)

Method Detail

compareToback to summary
public int compareTo(TextComponentPrintable.IntegerSegment object)

Implements java.lang.Comparable.compareTo.

Doc from java.lang.Comparable.compareTo.

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

The implementor must ensure signum(x.compareTo(y)) == -signum(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception if and only if y.compareTo(x) throws an exception.)

The implementor must also ensure that the relation is transitive: (x.compareTo(y) > 0 && y.compareTo(z) > 0) implies x.compareTo(z) > 0.

Finally, the implementor must ensure that x.compareTo(y)==0 implies that signum(x.compareTo(z)) == signum(y.compareTo(z)), for all z.

equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override