org.nocrala.tools.texttablefmt
Class Table

java.lang.Object
  extended by org.nocrala.tools.texttablefmt.Table

public class Table
extends java.lang.Object

In-memory text table generator. This class will generate text tables like:

 
  +---------+-----------+----------+--------+
  |Country  | Population|Area (km2)| Density|
  +---------+-----------+----------+--------+
  |Chile    | 17 000 000| 1 250 000|   13.60|
  |Argentina| 50 000 000| 3 000 000|   16.67|
  |Brasil   | 80 000 000| 5 000 000|   16.00|
  +---------+-----------+----------+--------+
  |Total    |147 000 000| 9 250 000|   15.89|
  +---------+-----------+----------+--------+
  

where the border style, shown borders/separators, column widths, alignments and other are configurable.

Cells are added using the addCell() method and finally rendered as a String using the render() method.

The entire table is built in memory, so this class is not intended for a massive numbers of rows/cells. Although the maximum size of the in-memory table depends on the amount of available memory the JVM has, as a rule of thumb, don't exceed 10.000 total cells. If you need to render a bigger table, use the StreamingTable class instead.

If no widths are specified for a column, its width will adjusted to the wider cell in the column.

As an example, the following code:

 
  CellStyle cs = new CellStyle(HorizontalAlign.left, AbbreviationStyle.crop,
      NullStyle.emptyString);
  Table t = new Table(2, BorderStyle.CLASSIC, ShownBorders.ALL, false, "");
  t.addCell("abcdef", cs);
  t.addCell("123456", cs);
  t.addCell("mno", cs);
  t.addCell("45689", cs);
  t.addCell("xyztuvw", cs);
  t.addCell("01234567", cs);
  System.out.println(t.render());
  

will generate the table:

  
  +-------+--------+
  |abcdef |123456  | 
  +-------+--------+ 
  |mno    |45689   | 
  +-------+--------+ 
  |xyztuvw|01234567| 
  +-------+--------+
  

The generated table can be customized using a BorderStyle, ShownBorders and cell widths. Besides, cell rendering can be customized on a cell basis using CellStyles.

Author:
valarcon

Constructor Summary
Table(int totalColumns)
          Creates a table using BorderStyle.CLASSIC and ShownBorders.SURROUND_HEADER_AND_COLUMNS, no XML escaping and no left margin.
Table(int totalColumns, BorderStyle borderStyle)
          Creates a table using the specified border style and ShownBorders.SURROUND_HEADER_AND_COLUMNS, no XML escaping and no left margin.
Table(int totalColumns, BorderStyle borderStyle, ShownBorders shownBorders)
          Creates a table using the specified border style and shown borders, with no XML escaping and no left margin.
Table(int totalColumns, BorderStyle borderStyle, ShownBorders shownBorders, boolean escapeXml)
          Creates a table using the specified border style and shown borders, XML escaping and no left margin.
Table(int totalColumns, BorderStyle borderStyle, ShownBorders shownBorders, boolean escapeXml, int leftMargin)
          Creates a table using the specified border style and shown borders, XML escaping and left margin.
Table(int totalColumns, BorderStyle borderStyle, ShownBorders shownBorders, boolean escapeXml, java.lang.String prompt)
          Creates a table using the specified border style and shown borders, XML escaping and left margin.
 
Method Summary
 void addCell(java.lang.String content)
          Adds a cell with the default CellStyle.
 void addCell(java.lang.String content, CellStyle style)
          Adds a cell with a specific cell style.
 void addCell(java.lang.String content, CellStyle style, int colSpan)
          Adds a cell with a specific cell style and colspan.
 void addCell(java.lang.String content, int colSpan)
          Adds a cell with a colspan and the default CellStyle.
 java.lang.String render()
          Renders the table as a multi-line String.
 java.lang.String[] renderAsStringArray()
          Renders the table as a String array.
 void setColumnWidth(int col, int minWidth, int maxWidth)
          Sets the minimum and maximum desired column widths of a specific column.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Table

public Table(int totalColumns)
Creates a table using BorderStyle.CLASSIC and ShownBorders.SURROUND_HEADER_AND_COLUMNS, no XML escaping and no left margin.

Parameters:
totalColumns - Total columns of this table.

Table

public Table(int totalColumns,
             BorderStyle borderStyle)
Creates a table using the specified border style and ShownBorders.SURROUND_HEADER_AND_COLUMNS, no XML escaping and no left margin.

Parameters:
totalColumns - Total columns of this table.
borderStyle - The border style to use when rendering the table.

Table

public Table(int totalColumns,
             BorderStyle borderStyle,
             ShownBorders shownBorders)
Creates a table using the specified border style and shown borders, with no XML escaping and no left margin.

Parameters:
totalColumns - Total columns of this table.
borderStyle - The border style to use when rendering the table.
shownBorders - Specifies which borders will be rendered.

Table

public Table(int totalColumns,
             BorderStyle borderStyle,
             ShownBorders shownBorders,
             boolean escapeXml)
Creates a table using the specified border style and shown borders, XML escaping and no left margin.

Parameters:
totalColumns - Total columns of this table.
borderStyle - The border style to use when rendering the table.
shownBorders - Specifies which borders will be rendered.
escapeXml - Specifies if the rendered text should be escaped using XML entities.

Table

public Table(int totalColumns,
             BorderStyle borderStyle,
             ShownBorders shownBorders,
             boolean escapeXml,
             int leftMargin)
Creates a table using the specified border style and shown borders, XML escaping and left margin.

Parameters:
totalColumns - Total columns of this table.
borderStyle - The border style to use when rendering the table.
shownBorders - Specifies which borders will be rendered.
escapeXml - Specifies if the rendered text should be escaped using XML entities.
leftMargin - Specifies how many blank spaces to use as a left margin for the table.

Table

public Table(int totalColumns,
             BorderStyle borderStyle,
             ShownBorders shownBorders,
             boolean escapeXml,
             java.lang.String prompt)
Creates a table using the specified border style and shown borders, XML escaping and left margin.

Parameters:
totalColumns - Total columns of this table.
borderStyle - The border style to use when rendering the table.
shownBorders - Specifies which borders will be rendered.
escapeXml - Specifies if the rendered text should be escaped using XML entities.
prompt - Text to use as left margin for the table.
Method Detail

setColumnWidth

public void setColumnWidth(int col,
                           int minWidth,
                           int maxWidth)
Sets the minimum and maximum desired column widths of a specific column. If no width range is specified for a column, its width will be adjusted to the wider cell in the column.

Parameters:
col - Column whose desired widths will be set. First column is 0 (zero).

minWidth - Minimum desired width.
maxWidth - Maximum desired width.

addCell

public void addCell(java.lang.String content)
Adds a cell with the default CellStyle. See CellStyle for details on its default characteristics.

Parameters:
content - Cell text.

addCell

public void addCell(java.lang.String content,
                    int colSpan)
Adds a cell with a colspan and the default CellStyle.

Parameters:
content - Cell text.
colSpan - Columns this cell will span through.

addCell

public void addCell(java.lang.String content,
                    CellStyle style)
Adds a cell with a specific cell style.

Parameters:
content - Cell text.
style - Cell style to use when rendering the cell content.

addCell

public void addCell(java.lang.String content,
                    CellStyle style,
                    int colSpan)
Adds a cell with a specific cell style and colspan.

Parameters:
content - Cell text.
style - Cell style to use when rendering the cell content.
colSpan - Columns this cell will span through.

render

public java.lang.String render()
Renders the table as a multi-line String.

Returns:
rendered table.

renderAsStringArray

public java.lang.String[] renderAsStringArray()
Renders the table as a String array.

Returns:
rendered table


Nocrala tools.