Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • For a given table view, the table is identified by a "tag" (in this example, that tag is "sample")
    • request event identifier is derived as: <tag> + "DataRequest"

    • response event identifier is derived as: <tag> + "DataResponse"
    • "root" tag is derived as: <tag> + "s"

 

(2a) comment about optionally override defaultColumnId():

Code Block
languagejava
// if necessary, override defaultColumnId() -- if it isn't "id"

Typically, table rows have a unique value (row key) to identify the row (for example, in the Devices table it is the value of the Device.id() property). The default identifier for the column holding that value the row key is "id". If you want to use a different column identifier for the unique row key, your class should override defaultColumnId(). For example:

Code Block
languagejava
private static final String MAC = "mac";
 
...
 
@Override
protected String defaultColumnId() {
    return MAC;
}

 

(2b) define column identifiers:

Code Block
languagejava
private static final String ID = "id";
private static final String LABEL = "label";
private static final String CODE = "code";

private static final String[] COLUMN_IDS = { ID, LABEL, CODE };


...
 
@Override
protected String[] getColumnIds() {
    return COLUMN_IDS;
}

Note that the column identifiers defined here must match the identifiers defined in the HTML snippet for the view.

 

(2c) optionally override createTableModel() to provide custom cell formatters / comparators. The following example sets both a formatter and a comparator for the "code" column:

Code Block
languagejava
@Override
protected TableModel createTableModel() {
    TableModel tm = super.createTableModel();
    tm.setFormatter(CODE, CodeFormatter.INSTANCE);
    tm.setComparator(CODE, CodeComparator.INSTANCE);
    return tm;
}
Info

See additional details about table models, formatters, and comparators.

 

(2d) implement populateTable() by adding rows to the supplied table model:

Code Block
languagejava
@Override
protected void populateTable(TableModel tm, ObjectNode payload) {
    // ...

    // fake data for demonstration purposes...
    List<Item> items = getItems();
    for (Item item: items) {
        populateRow(tm.addRow(), item);
    }
}
 
private void populateRow(TableModel.Row row, Item item) {
    row.cell(ID, item.id())
            .cell(LABEL, item.label())
            .cell(CODE, item.code());
}

The example uses fake data for demonstration purposes. A more realistic implementation would use one or more services to obtain the required data. For example:

Code Block
languagejava
@Override
protected void populateTable(TableModel tm, ObjectNode payload) {
    DeviceService ds = get(DeviceService.class);
    MastershipService ms = get(MastershipService.class);
    for (Device dev : ds.getDevices()) {
        populateRow(tm.addRow(), dev, ds, ms);
    }
}

private void populateRow(TableModel.Row row, Device dev,
                         DeviceService ds, MastershipService ms) {
    DeviceId id = dev.id();
    String protocol = dev.annotations().value(PROTOCOL);

    row.cell(ID, id)
        .cell(MFR, dev.manufacturer())
        .cell(HW, dev.hwVersion())
        .cell(SW, dev.swVersion())
        .cell(PROTOCOL, protocol != null ? protocol : "")
        .cell(NUM_PORTS, ds.getPorts(id).size())
        .cell(MASTER_ID, ms.getMasterFor(id));
    }
}