|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.nocrala.tools.texttablefmt.Table
public class Table
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 CellStyle
s.
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 |
---|
public Table(int totalColumns)
BorderStyle.CLASSIC
and
ShownBorders.SURROUND_HEADER_AND_COLUMNS
, no XML
escaping and no left margin.
totalColumns
- Total columns of this table.public Table(int totalColumns, BorderStyle borderStyle)
ShownBorders.SURROUND_HEADER_AND_COLUMNS
, no XML
escaping and no left margin.
totalColumns
- Total columns of this table.borderStyle
- The border style to use when rendering the table.public Table(int totalColumns, BorderStyle borderStyle, ShownBorders shownBorders)
totalColumns
- Total columns of this table.borderStyle
- The border style to use when rendering the table.shownBorders
- Specifies which borders will be rendered.public Table(int totalColumns, BorderStyle borderStyle, ShownBorders shownBorders, boolean escapeXml)
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.public Table(int totalColumns, BorderStyle borderStyle, ShownBorders shownBorders, boolean escapeXml, int leftMargin)
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.public Table(int totalColumns, BorderStyle borderStyle, ShownBorders shownBorders, boolean escapeXml, java.lang.String prompt)
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 |
---|
public void setColumnWidth(int col, int minWidth, int maxWidth)
col
- Column whose desired widths will be set. First column is 0
(zero).minWidth
- Minimum desired width.maxWidth
- Maximum desired width.public void addCell(java.lang.String content)
CellStyle
for details
on its default characteristics.
content
- Cell text.public void addCell(java.lang.String content, int colSpan)
content
- Cell text.colSpan
- Columns this cell will span through.public void addCell(java.lang.String content, CellStyle style)
content
- Cell text.style
- Cell style to use when rendering the cell content.public void addCell(java.lang.String content, CellStyle style, int colSpan)
content
- Cell text.style
- Cell style to use when rendering the cell content.colSpan
- Columns this cell will span through.public java.lang.String render()
public java.lang.String[] renderAsStringArray()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |