public int print(Graphics graphics, PageFormat pageFormat, int page)
throws PrinterException {
if (page < numPages) {
StyledDocument doc = (StyledDocument)getDocument();
Paper paper = pageFormat.getPaper();
// initialize the PRINT_PANE (need this so that wrapping
// can take place)
PRINT_PANE.setDocument(getDocument());
PRINT_PANE.setFont(getFont());
PRINT_SIZE.setSize(paper.getImageableWidth(),
getSize().getHeight());
PRINT_PANE.setSize(PRINT_SIZE);
// translate the graphics origin upwards so the area of the page we
// want to print is in the origin; the clipping region auto set
// will take care of the rest
double y = -(page * paper.getImageableHeight()) + paper.getImageableY();
((Graphics2D)graphics).translate(paper.getImageableX(), y);
// print the text with its own routines
PRINT_PANE.print(graphics);
// translate the graphics object back to reality in the y dimension
// so we can print a page number
((Graphics2D)graphics).translate(0, -y);
Rectangle rect = graphics.getClipBounds();
graphics.setClip(rect.x, 0, rect.width, (int)paper.getHeight() + 100);
// get the name of the pane (or user name) and the time for the header
Calendar cal = Calendar.getInstance();
String header = cal.getTime().toString().trim();
String name = getName() == null ?
System.getProperty("user.name").trim() : getName().trim();
String pageStr = String.valueOf(page + 1);
Font font = Font.decode("Monospaced 8");
graphics.setFont(font);
FontMetrics fm = graphics.getFontMetrics(font);
int width = SwingUtilities.computeStringWidth(fm, header);
((Graphics2D)graphics).drawString(header,
(float)(paper.getImageableWidth()/2 - width/2),
(float)paper.getImageableY()/2 + fm.getHeight());
((Graphics2D)graphics).translate(0, paper.getImageableY() - fm.getHeight());
double height = paper.getImageableHeight() + paper.getImageableY()/2;
width = SwingUtilities.computeStringWidth(fm, name);
((Graphics2D)graphics).drawString(name,
(float)(paper.getImageableWidth()/2 - width/2),
(float)height - fm.getHeight()/2);
((Graphics2D)graphics).translate(0, fm.getHeight());
width = SwingUtilities.computeStringWidth(fm, pageStr);
((Graphics2D)graphics).drawString(pageStr,
(float)(paper.getImageableWidth()/2 - width/2),
(float)height - fm.getHeight()/2);
return Printable.PAGE_EXISTS;
}
return Printable.NO_SUCH_PAGE;