categoryField.setSummaryFunction(new SummaryFunction() {
public Object getSummaryValue(Record[] records, ListGridField field) {
Set<String> uniqueCategories = new HashSet<String>();
for (int i = 0; i < records.length; i++) {
Record record = records[i];
uniqueCategories.add(((OrderItem) record).getCategory());
}
return uniqueCategories.size() + " Categories";
}
});
ListGridField shipDateField = new ListGridField("shipDate");
shipDateField.setWidth(160);
shipDateField.setShowGroupSummary(true);
shipDateField.setShowGridSummary(false);
shipDateField.setSummaryFunction(new SummaryFunction() {
public Object getSummaryValue(Record[] records, ListGridField field) {
Date firstOrder = null;
for (int i = 0; i < records.length; i++) {
Record record = records[i];
Date shipDate = record.getAttributeAsDate("shipDate");
if (firstOrder == null || shipDate.before(firstOrder)) {
firstOrder = shipDate;
}
}
return "First Order " + dtf.format(firstOrder);
}
});
shipDateField.addSummaryFunction(new SummaryFunction() {
public Object getSummaryValue(Record[] records, ListGridField field) {
Date lastOrder = null;
for (int i = 0; i < records.length; i++) {
Record record = records[i];
Date shipDate = record.getAttributeAsDate("shipDate");
if(lastOrder == null || shipDate.after(lastOrder)) {
lastOrder = shipDate;
}
}
return "Last Order " + dtf.format(lastOrder);