...
- if they do, delegate to the compareTo() method
- if they do not, convert the objects to strings and compare thosewith toString() and then compare
This has the advantage of using the natural sort order of for objects , where the comparable behavior has already been implemented, by default.
DefaultCellFormatter
The default cell formatter simply invokes the toString() method on each object, to generate the string representation to be displayed in the table cell.
Customizing the Behavior
But what if a particular object does not implement Comparable<T>, and sorting by string representation would produce an undesirable ordering? And what if the toString() of an object is not appropriate for display? Fortunately, you can override the default behavior and provide your own comparators and formatters.
The createTableModel() method in your TableRequestHandler should be overridden to look something like this:
Code Block | ||||
---|---|---|---|---|
| ||||
@Override protected TableModel createTableModel() { TableModel tm = super.createTableModel(); tm.setComparator(BAR, new BarComparator()); // see Comparators section below tm.setFormatter(BAZ, new BazFormatter()); // see Formatters section below return tm; } |
The setComparator() and setFormatter() methods are invoked to explicitly set the cell comparator and cell formatter instances to use for objects in the specific columns. All other columns will fall back to the default comparator and formatter.
Custom Cell Comparator
A custom comparator would be defined something like this, (typically as a private inner class in your table request handler):
Code Block | ||||
---|---|---|---|---|
| ||||
private static class BarComparator extends AbstractCellComparator { @Override protected int nonNullCompare(Object o1, Object o2) { Bar b1 = (Bar) o1; Bar b2 = (Bar) o2; |
...
// get some property from o1
// compare to the same property from o2
// return ...
// * a negative value, if o1 < o2
// * zero, if o1 == o2
// * a positive value, if o1 > o2
return ... ;
}
} |
Note that the AbstractCellComparator takes care of null values and guarantees that o1 and o2 will both be non-null if this method is called.
Note that null values are permitted in the table model cells, and are treated as "smaller" than non-null values.
Custom Cell Formatter
A custom formatter might be defined something like this, (typically as a private inner class in your table request handler):
Code Block | ||||
---|---|---|---|---|
| ||||
private static class BazFormatter extends AbstractCellFormatter {
@Override
protected String nonNullFormat(Object value) {
Baz b = (Baz) value;
return b.toDisplayString();
}
} |
Once again, the value parameter is guaranteed to be non-null, if the AbstractCellFormatter invokes this method.
Out of the Box Formatters
A number of formatters are already defined and available for your use. See the org.onosproject.ui.table.cell package:
Formatter | Value Class | Format Description | Example |
---|---|---|---|
AppIdFormatter | ApplicationId | "(app-id) : (app-name)" | "47 : onos-app-uiref" |
ConnectPointFormatter | ConnectPoint | "(element ID)/(port)" | "00:00:00:00:00:00:00:01/5" |
EnumFormatter | Enum<?> | Replaces _ with space and capitalizes the result | SomeEnum.DO_SOMETHING → "Do Something" |
HexFormatter | Integer | Prefix with "0x" and converted to hex | 20 → "0x14" |
HostLocationFormatter | HostLocation | "(device-id)/(port)" | "00:00:00:00:00:00:00:0e/13" |
NumberFormatter* | Number | Uses a java.text.DecimalFormat with pattern "#,##0.00000" | 1234.56 → "1,234.56000" |
TimeFormatter** | DateTime | Uses a DateTimeFormatter | new DateTime(2015, 5, 4, 15, 30) → "3:30:00 PM UTC" |
*Using the NumberFormatter no-args constructor; a java.text.NumberFormat instance can also be provided.
**Note that Locale and TimeZone can also be set on the formatter.
...