util
Class StringPrinter

java.lang.Object
  extended by java.lang.Thread
      extended by util.StringPrinter
All Implemented Interfaces:
java.awt.print.Pageable, java.awt.print.Printable, java.lang.Runnable

public class StringPrinter
extends java.lang.Thread
implements java.awt.print.Pageable, java.awt.print.Printable

Implements a simple printing class for a text string in a Thread. Based on a JavaWorld article by Michael Shoffner. Rather inefficient, since it repaginates far more often than it needs to, but would be appropriate for the kind of toy compiler I am writing.

Author:
Daniel Wachsstock

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Fields inherited from interface java.awt.print.Pageable
UNKNOWN_NUMBER_OF_PAGES
 
Fields inherited from interface java.awt.print.Printable
NO_SUCH_PAGE, PAGE_EXISTS
 
Constructor Summary
StringPrinter(java.lang.String text)
          Creates a new instance of StringPrinter with default Font and PageFormat
StringPrinter(java.lang.String text, java.awt.Font font)
          Creates a new instance of StringPrinter with default PageFormat
StringPrinter(java.lang.String text, java.awt.Font font, java.awt.print.PageFormat format)
          Creates a new instance of StringPrinter.
StringPrinter(java.lang.String text, java.awt.print.PageFormat format)
          Creates a new instance of StringPrinter with default Font
 
Method Summary
 int getNumberOfPages()
          calculates the total number of printable pages
 java.awt.print.PageFormat getPageFormat(int pageIndex)
          returns the PageFormat for a given page
 java.awt.print.Printable getPrintable(int pageIndex)
          returns the Printable for a given page
 java.lang.String getTitle()
          returns the title string
 int print(java.awt.Graphics g, java.awt.print.PageFormat format, int pageIndex)
          renders a page
 void run()
          prints the string
 void setTitle(java.lang.String title)
          sets the title string
static java.lang.StringBuffer untabify(java.lang.String s, int tabSize, boolean truncate)
          changes tabs to spaces.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StringPrinter

public StringPrinter(java.lang.String text)
Creates a new instance of StringPrinter with default Font and PageFormat


StringPrinter

public StringPrinter(java.lang.String text,
                     java.awt.Font font)
Creates a new instance of StringPrinter with default PageFormat


StringPrinter

public StringPrinter(java.lang.String text,
                     java.awt.print.PageFormat format)
Creates a new instance of StringPrinter with default Font


StringPrinter

public StringPrinter(java.lang.String text,
                     java.awt.Font font,
                     java.awt.print.PageFormat format)
Creates a new instance of StringPrinter.

Parameters:
text - the text to print
font - the font; uses monospaced 12-point if null
format - the PageFormat to use; uses PrinterJob.getPrinterJob().defaultPage() if null
Method Detail

run

public void run()
prints the string

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

setTitle

public void setTitle(java.lang.String title)
sets the title string

Parameters:
title - the String to use for the header, along with the page number

getTitle

public java.lang.String getTitle()
returns the title string

Returns:
the title string

getNumberOfPages

public int getNumberOfPages()
calculates the total number of printable pages

Specified by:
getNumberOfPages in interface java.awt.print.Pageable
Returns:
the number of pages of the whole document

getPageFormat

public java.awt.print.PageFormat getPageFormat(int pageIndex)
                                        throws java.lang.IndexOutOfBoundsException
returns the PageFormat for a given page

Specified by:
getPageFormat in interface java.awt.print.Pageable
Parameters:
pageIndex - the page to format
Returns:
a PageFormat for that page. All of them are the same
Throws:
java.lang.IndexOutOfBoundsException - if pageIndex is not a valid page

getPrintable

public java.awt.print.Printable getPrintable(int pageIndex)
                                      throws java.lang.IndexOutOfBoundsException
returns the Printable for a given page

Specified by:
getPrintable in interface java.awt.print.Pageable
Parameters:
pageIndex - the page that will be printed
Returns:
this, as a Printable interface
Throws:
java.lang.IndexOutOfBoundsException - if pageIndex is not a valid page

print

public int print(java.awt.Graphics g,
                 java.awt.print.PageFormat format,
                 int pageIndex)
          throws java.awt.print.PrinterException
renders a page

Specified by:
print in interface java.awt.print.Printable
Parameters:
g - the Graphics to render on
format - the PageFormat to use for rendering
pageIndex - the page to print
Returns:
NO_SUCH_PAGE or PAGE_EXISTS, as approprite for pageIndex
Throws:
java.lang.IndexOutOfBoundsException - if pageIndex is not a valid page
java.awt.print.PrinterException

untabify

public static java.lang.StringBuffer untabify(java.lang.String s,
                                              int tabSize,
                                              boolean truncate)
changes tabs to spaces. Inefficient; uses String.split(String) to turn the entire string into an array of Strings, then split's each one of those.

Parameters:
s - the string to detabify. Splits lines on '\n' only, not '\r'.
tabSize - the number of spaces to use for each tab
truncate - true if each tab field should be truncated to fit in tabSize-1 columns (the -1 ensures a space between tabs; false to keep the text intact and move to the next available tab stop. Strings get truncated from the end; numbers are rounded with Num2Str if possible, and replaced with '*'s (a la FORTRAN) if not.