}
// Calculate the modified ranges.
List<Range> modifiedRanges = calculateModifiedRanges(modifiedRows,
pageStart, pageEnd);
Range range0 = modifiedRanges.size() > 0 ? modifiedRanges.get(0) : null;
Range range1 = modifiedRanges.size() > 1 ? modifiedRanges.get(1) : null;
int replaceDiff = 0; // The total number of rows to replace.
for (Range range : modifiedRanges) {
replaceDiff += range.getLength();
}
/*
* Check the various conditions that require redraw.
*/
int oldPageStart = oldState.getPageStart();
int oldPageSize = oldState.getPageSize();
int oldRowDataCount = oldState.getRowDataSize();
boolean redrawRequired = pending.redrawRequired;
if (pageStart != oldPageStart) {
// Redraw if pageStart changes.
redrawRequired = true;
} else if (rowDataCount < oldRowDataCount) {
// Redraw if we have trimmed the row data.
redrawRequired = true;
} else if (range1 == null && range0 != null
&& range0.getStart() == pageStart
&& (replaceDiff >= oldRowDataCount || replaceDiff > oldPageSize)) {
// Redraw if the new data completely overlaps the old data.
redrawRequired = true;
} else if (replaceDiff >= REDRAW_MINIMUM
&& replaceDiff > REDRAW_THRESHOLD * oldRowDataCount) {
/*
* Redraw if the number of modified rows represents a large portion of the
* view, defined as greater than 30% of the rows (minimum of 5).
*/
redrawRequired = true;
} else if (replacedEmptyRange && oldRowDataCount == 0) {
/*
* If the user replaced an empty range, pass it to the view. This is a
* useful edge case that provides consistency in the way data is pushed to
* the view.
*/
redrawRequired = true;
}
// Update the loading state in the view.
updateLoadingState();
/*
* Push changes to the view.
*/
if (redrawRequired) {
// Redraw the entire content.
SafeHtmlBuilder sb = new SafeHtmlBuilder();
view.render(sb, pending.rowData, pending.pageStart, selectionModel);
SafeHtml newContents = sb.toSafeHtml();
if (!newContents.equals(lastContents)) {
lastContents = newContents;
view.replaceAllChildren(pending.rowData, newContents,
pending.keyboardStealFocus);
}
view.resetFocus();
} else if (range0 != null) {
// Replace specific rows.
lastContents = null;
// Replace range0.
{
int absStart = range0.getStart();
int relStart = absStart - pageStart;
SafeHtmlBuilder sb = new SafeHtmlBuilder();
List<T> replaceValues = pending.rowData.subList(relStart, relStart
+ range0.getLength());
view.render(sb, replaceValues, absStart, selectionModel);
view.replaceChildren(replaceValues, relStart, sb.toSafeHtml(),
pending.keyboardStealFocus);
}
// Replace range1 if it exists.
if (range1 != null) {
int absStart = range1.getStart();
int relStart = absStart - pageStart;
SafeHtmlBuilder sb = new SafeHtmlBuilder();
List<T> replaceValues = pending.rowData.subList(relStart, relStart
+ range1.getLength());
view.render(sb, replaceValues, absStart, selectionModel);
view.replaceChildren(replaceValues, relStart, sb.toSafeHtml(),
pending.keyboardStealFocus);
}