ColumnSortInfo sortedInfo = (sortList.size() == 0) ? null : sortList.get(0);
Column<?, ?> sortedColumn = (sortedInfo == null) ? null : sortedInfo.getColumn();
boolean isSortAscending = (sortedInfo == null) ? false : sortedInfo.isAscending();
// Get the common style names.
Style style = getTable().getResources().style();
String className = isBuildingFooter() ? style.footer() : style.header();
String sortableStyle = " " + style.sortableHeader();
String sortedStyle =
" " + (isSortAscending ? style.sortedHeaderAscending() : style.sortedHeaderDescending());
// Setup the first column.
Header<?> prevHeader = getHeader(0);
Column<T, ?> column = getTable().getColumn(0);
int prevColspan = 1;
boolean isSortable = false;
boolean isSorted = false;
StringBuilder classesBuilder = new StringBuilder(className);
classesBuilder.append(" " + (isFooter ? style.firstColumnFooter() : style.firstColumnHeader()));
if (!isFooter && column.isSortable()) {
isSortable = true;
isSorted = (column == sortedColumn);
}
// Loop through all column headers.
TableRowBuilder tr = startRow();
int curColumn;
for (curColumn = 1; curColumn < columnCount; curColumn++) {
Header<?> header = getHeader(curColumn);
if (header != prevHeader) {
// The header has changed, so append the previous one.
if (isSortable) {
classesBuilder.append(sortableStyle);
}
if (isSorted) {
classesBuilder.append(sortedStyle);
}
appendExtraStyles(prevHeader, classesBuilder);
// Render the header.
TableCellBuilder th =
tr.startTH().colSpan(prevColspan).className(classesBuilder.toString());
enableColumnHandlers(th, column);
if (prevHeader != null) {
// Build the header.
Context context = new Context(0, curColumn - prevColspan, prevHeader.getKey());
renderSortableHeader(th, context, prevHeader, isSorted, isSortAscending);
}
th.endTH();
// Reset the previous header.
prevHeader = header;
prevColspan = 1;
classesBuilder = new StringBuilder(className);
isSortable = false;
isSorted = false;
} else {
// Increment the colspan if the headers == each other.
prevColspan++;
}
// Update the sorted state.
column = table.getColumn(curColumn);
if (!isFooter && column.isSortable()) {
isSortable = true;
isSorted = (column == sortedColumn);
}
}
// Append the last header.
if (isSortable) {
classesBuilder.append(sortableStyle);
}
if (isSorted) {
classesBuilder.append(sortedStyle);
}
// The first and last columns could be the same column.
classesBuilder.append(" ").append(
isFooter ? style.lastColumnFooter() : style.lastColumnHeader());
appendExtraStyles(prevHeader, classesBuilder);
// Render the last header.
TableCellBuilder th = tr.startTH().colSpan(prevColspan).className(classesBuilder.toString());
enableColumnHandlers(th, column);