public class TableModel extends Object
Note that this is not a full MVC type model; the expected usage pattern is to create an empty table, add rows (by consulting the business model), sort rows (based on client request parameters), and finally produce the sorted list of rows.
The table also provides a mechanism for defining how cell values for a particular column should be formatted into strings, to help facilitate the encoding of the table data into a JSON structure.
Note that it is expected that all values for a particular column will be the same class.
Modifier and Type | Class and Description |
---|---|
class |
TableModel.Annot
Model of an annotation.
|
class |
TableModel.Row
Model of a row.
|
static class |
TableModel.SortDir
Designates sorting direction.
|
Constructor and Description |
---|
TableModel(String... columnIds)
Constructs a table (devoid of data) with the given column IDs.
|
Modifier and Type | Method and Description |
---|---|
void |
addAnnotation(String key,
Object value)
Inserts a new annotation.
|
TableModel.Row |
addRow()
Adds a row to the table model.
|
int |
columnCount()
Returns the number of columns in this table model.
|
Collection<TableModel.Annot> |
getAnnotations()
Returns the annotations in this table.
|
String[] |
getColumnIds()
Returns the array of column IDs for this table model.
|
CellFormatter |
getFormatter(String columnId)
Returns the cell formatter to use on values in the specified column.
|
TableModel.Row[] |
getRows()
Returns the raw
TableModel.Row representation of the rows in this table. |
int |
rowCount()
Returns the number of rows in this table model.
|
void |
setComparator(String columnId,
CellComparator comparator)
Sets a cell comparator for the specified column.
|
void |
setFormatter(String columnId,
CellFormatter formatter)
Sets a cell formatter for the specified column.
|
void |
sort(String id1,
TableModel.SortDir dir1,
String id2,
TableModel.SortDir dir2)
Sorts the table rows based on the specified columns, in the
specified directions.
|
static TableModel.SortDir |
sortDir(String s)
Returns the appropriate sort direction for the given string.
|
public TableModel(String... columnIds)
columnIds
- column identifierspublic int rowCount()
public int columnCount()
public String[] getColumnIds()
Implementation note: we are knowingly passing you a reference to our internal array to avoid copying. Don't mess with it. It's your table you'll break if you do!
public TableModel.Row[] getRows()
TableModel.Row
representation of the rows in this table.public void addAnnotation(String key, Object value)
key
- key of annotationvalue
- value of annotationpublic Collection<TableModel.Annot> getAnnotations()
public void setComparator(String columnId, CellComparator comparator)
columnId
- column identifiercomparator
- comparator to usepublic void setFormatter(String columnId, CellFormatter formatter)
columnId
- column identifierformatter
- formatter to usepublic CellFormatter getFormatter(String columnId)
columnId
- column identifierpublic TableModel.Row addRow()
public void sort(String id1, TableModel.SortDir dir1, String id2, TableModel.SortDir dir2)
id1
- first column identifierdir1
- first column sort directionid2
- second column identifier (may be null)dir2
- second column sort direction (may be null)public static TableModel.SortDir sortDir(String s)
The expected strings are "asc" for ascending
and
"desc" for descending
. Any other value will
default to ascending.
s
- sort direction string encoding