jimmc.roots
Class MultiColumnPrintStream

java.lang.Object
  |
  +--java.io.Writer
        |
        +--java.io.PrintWriter
              |
              +--jimmc.roots.MultiColumnPrintStream

public class MultiColumnPrintStream
extends java.io.PrintWriter

Provides support for multi-column output, assuming a fixed-width font. After instantiating, use the addColumn method to define the print columns and their characteristics. Use selectColumn to select a column, then just call the normal PrintWriter methods such as println to add data to that column. Use flushColumns to get all columns back into alignment, and use selectDirect to print directly to the output (e.g. to print data that spans all columns).


Fields inherited from class java.io.PrintWriter
out
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
MultiColumnPrintStream(java.io.OutputStream out)
          Create a new MultiColumnPrintStream.
MultiColumnPrintStream(java.io.Writer out)
          Create a new MultiColumnPrintStream.
 
Method Summary
 int addColumn(int width, int spacing)
          Add an output column.
 boolean anyColumnHasData(int col)
          See if any column starting after a given point has any data.
 void flush()
          Flush all columns, then flush the underlying stream.
 void flushColumns()
          Flush out the data from all columns.
 void outputCompleteLines()
          Output as many complete lines from all columns as possible.
 void printlnc(java.lang.String s)
          Conditional println: print text to the output, add a newline if the last character is not already a newline.
 void selectColumn(int col)
          Select the column to which output is directed.
 void selectDirect()
          Flush and deselect all columns.
 void write(char[] b, int off, int len)
          Write a sub array of chars to the currently selected column.
 void write(int b)
          Write a char to the currently selected column.
 void write(java.lang.String s, int off, int len)
          Write a substring to the currently selected column.
 
Methods inherited from class java.io.PrintWriter
checkError, close, print, print, print, print, print, print, print, print, print, println, println, println, println, println, println, println, println, println, println, setError, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MultiColumnPrintStream

public MultiColumnPrintStream(java.io.OutputStream out)
Create a new MultiColumnPrintStream.
Parameters:
out - the output stream

MultiColumnPrintStream

public MultiColumnPrintStream(java.io.Writer out)
Create a new MultiColumnPrintStream.
Parameters:
out - the output stream
Method Detail

addColumn

public int addColumn(int width,
                     int spacing)
Add an output column.
Parameters:
width - the width of the column in print positions
spacing - the number of blank characters between this column and the next column.
Returns:
the index of this column, to be passed to selectColumn. The first column created is 0, the next is 1, etc.

selectColumn

public void selectColumn(int col)
Select the column to which output is directed.
Parameters:
col - the column index as returned by addColumn.

selectDirect

public void selectDirect()
Flush and deselect all columns. Output goes directly to the underlying stream.

flushColumns

public void flushColumns()
Flush out the data from all columns. If any column has a partial line, a newline is added to that column. Then all columns are output to the underlying stream. If one column has more lines of data than other columns, then blank lines are output in those other columns until all columns are fully output. This has the effect of vertically synchronizing or aligning all columns.

outputCompleteLines

public void outputCompleteLines()
Output as many complete lines from all columns as possible.

anyColumnHasData

public boolean anyColumnHasData(int col)
See if any column starting after a given point has any data.
Parameters:
colnum - index of the first column to check
Returns:
true if that column or any past it has any data.

flush

public void flush()
Flush all columns, then flush the underlying stream.
Overrides:
flush in class java.io.PrintWriter

printlnc

public void printlnc(java.lang.String s)
Conditional println: print text to the output, add a newline if the last character is not already a newline.

write

public void write(int b)
Write a char to the currently selected column. Data is buffered in memory until a newline character is written, at which time output to the underlying stream may occur (or may not, depending on whether all columns have data). If output does occur, the write may block.
Overrides:
write in class java.io.PrintWriter
Parameters:
b - the char

write

public void write(char[] b,
                  int off,
                  int len)
Write a sub array of chars to the currently selected column. Equivalent to calling write(int) for each char in the array.
Overrides:
write in class java.io.PrintWriter
Parameters:
b - the data to be written
off - the start offset in the data
len - the number of chars to write

write

public void write(java.lang.String s,
                  int off,
                  int len)
Write a substring to the currently selected column. Equivalent to calling write(int) for each char in the substring.
Overrides:
write in class java.io.PrintWriter
Parameters:
s - the string to be written
off - the start offset in the string
len - the number of chars to write