// stack does not
// contain the necessary labels at the time the comparator is searched
columnHeaderDataLayer
.setConfigLabelAccumulator(new ColumnLabelAccumulator());
ConfigRegistry configRegistry = new ConfigRegistry();
// add the SortHeaderLayer to the column header layer stack
// as we use GlazedLists, we use the GlazedListsSortModel which
// delegates
// the sorting to the SortedList
final SortHeaderLayer<Person> sortHeaderLayer = new SortHeaderLayer<Person>(
columnHeaderLayer, new GlazedListsSortModel<Person>(sortedList,
accessor, configRegistry, columnHeaderDataLayer));
// build the row header layer
IDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(
bodyDataProvider);
DataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(
rowHeaderDataProvider);
ILayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer,
viewportLayer, selectionLayer);
// build the corner layer
IDataProvider cornerDataProvider = new DefaultCornerDataProvider(
columnHeaderDataProvider, rowHeaderDataProvider);
DataLayer cornerDataLayer = new DataLayer(cornerDataProvider);
ILayer cornerLayer = new CornerLayer(cornerDataLayer, rowHeaderLayer,
sortHeaderLayer);
// build the grid layer
GridLayer gridLayer = new GridLayer(viewportLayer, sortHeaderLayer,
rowHeaderLayer, cornerLayer);
// turn the auto configuration off as we want to add our header menu
// configuration
NatTable natTable = new NatTable(parent, gridLayer, false);
natTable.setConfigRegistry(configRegistry);
// as the autoconfiguration of the NatTable is turned off, we have to
// add the
// DefaultNatTableStyleConfiguration manually
natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
// override the default sort configuration and change the mouse bindings
// to sort on a single click
natTable.addConfiguration(new SingleClickSortConfiguration());
natTable.addConfiguration(new DebugMenuConfiguration(natTable));
// add some custom sort configurations regarding comparators
natTable.addConfiguration(new AbstractRegistryConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
// Register custom comparator for last name column
// when sorting via last name, Simpson will always win
configRegistry.registerConfigAttribute(
SortConfigAttributes.SORT_COMPARATOR,
new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
// check the sort order
boolean sortDesc = sortHeaderLayer
.getSortModel().getSortDirection(1)
.equals(SortDirectionEnum.DESC);
if ("Simpson".equals(o1)
&& !"Simpson".equals(o2)) {
return sortDesc ? 1 : -1;
} else if (!"Simpson".equals(o1)
&& "Simpson".equals(o2)) {
return sortDesc ? -1 : 1;
}
return o1.compareToIgnoreCase(o2);
}
}, DisplayMode.NORMAL,
ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 1);
// Register null comparator to disable sorting for gender column
configRegistry.registerConfigAttribute(
SortConfigAttributes.SORT_COMPARATOR,
new NullComparator(), DisplayMode.NORMAL,
ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 2);
}
});