Class TableModel

  • public class TableModel
    extends java.lang.Object
    A simple model of table data.

    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.

    • Constructor Summary

      Constructor Description
      TableModel​(java.lang.String... columnIds)
      Constructs a table (devoid of data) with the given column IDs.
    • Constructor Detail

      • TableModel

        public TableModel​(java.lang.String... columnIds)
        Constructs a table (devoid of data) with the given column IDs.
        columnIds - column identifiers
    • Method Detail

      • rowCount

        public int rowCount()
        Returns the number of rows in this table model.
        number of rows
      • columnCount

        public int columnCount()
        Returns the number of columns in this table model.
        number of columns
      • getColumnIds

        public java.lang.String[] getColumnIds()
        Returns the array of column IDs for this table model.

        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!

        the column identifiers
      • getRows

        public TableModel.Row[] getRows()
        Returns the raw TableModel.Row representation of the rows in this table.
        raw table rows
      • addAnnotation

        public void addAnnotation​(java.lang.String key,
                                  java.lang.Object value)
        Inserts a new annotation.
        key - key of annotation
        value - value of annotation
      • getAnnotations

        public java.util.Collection<TableModel.Annot> getAnnotations()
        Returns the annotations in this table.
      • setComparator

        public void setComparator​(java.lang.String columnId,
                                  CellComparator comparator)
        Sets a cell comparator for the specified column.
        columnId - column identifier
        comparator - comparator to use
      • setFormatter

        public void setFormatter​(java.lang.String columnId,
                                 CellFormatter formatter)
        Sets a cell formatter for the specified column.
        columnId - column identifier
        formatter - formatter to use
      • getFormatter

        public CellFormatter getFormatter​(java.lang.String columnId)
        Returns the cell formatter to use on values in the specified column.
        columnId - column identifier
        an appropriate cell formatter
      • addRow

        public TableModel.Row addRow()
        Adds a row to the table model.
        the row, for chaining
      • sort

        public void sort​(java.lang.String id1,
                         TableModel.SortDir dir1,
                         java.lang.String id2,
                         TableModel.SortDir dir2)
        Sorts the table rows based on the specified columns, in the specified directions. The second column is optional, and can be disregarded by passing null into id2 and dir2.
        id1 - first column identifier
        dir1 - first column sort direction
        id2 - second column identifier (may be null)
        dir2 - second column sort direction (may be null)
      • sortDir

        public static TableModel.SortDir sortDir​(java.lang.String s)
        Returns the appropriate sort direction for the given string.

        The expected strings are "asc" for ascending and "desc" for descending. Any other value will default to ascending.

        s - sort direction string encoding
        sort direction