if (chunks.length == 1) {
container.add(processTagsInChunk(chunks[0]));
return;
}
ReadMoreLink readMoreLink = null;
boolean shouldExpand = false;
boolean reachedExpandedChunk = false;
// Create a link that causes everything to be hidden
Link readLessLink = new Link(SHOW_LESS) {
@Override
protected void onClick(ClickEvent e) {
// Hide all the widgets except the first few, as specified by the
// initiallyVisibleChunkCount variable.
for (int i = 1; i < container.getWidgetCount(); i++) {
container.getWidget(i).setVisible(i < initiallyVisibleChunkCount);
}
}
};
readLessLink.setVisible(false);
container.add(readLessLink);
for (int i = chunks.length - 1; i >= 0; i--) {
String chunk = chunks[i].trim();
if (readMoreLink != null) {
// Insert the previously created "read more" link at the beginning.
container.insert(readMoreLink, 0);
}
// If this is the first chunk or if this chunk contains a highlight for new content,
// expand it.
if (i == 0 || (openHighlightedChunks && chunk.contains(HIGHLIGHT_CLASS))) {
shouldExpand = true;
if (!reachedExpandedChunk && readMoreLink != null) {
// If all the previous chunks were hidden, then we should show the
// read more link after this expanded chunk, if there is one.
readMoreLink.setVisible(true);
}
if (i > 0) {
// If this expanded chunk isn't the first chunk, then we should show
// the read less link.
readLessLink.setVisible(true);
}
reachedExpandedChunk = true;
}
// Process this chunk. First, check if this chunk ends with text in paragraph tags
// followed by non-text content.
int position = 0;
int finalParagraphIndex = chunk.toLowerCase().lastIndexOf(PARAGRAPH_END_TAG);
int finalParagraphEndIndex = finalParagraphIndex + PARAGRAPH_END_TAG.length();
if (finalParagraphIndex > 0 && finalParagraphEndIndex < chunk.length()) {
// There's non-text stuff after the last paragraph tag.
// Split this into two chunks for processing,
// and put the 'read more' link before the non-text content.
HTMLPanel html1 = processTagsInChunk(chunk.substring(0, finalParagraphEndIndex + 1));
html1.setVisible(shouldExpand);
container.insert(html1, position++);
if (readMoreLink != null) {
container.insert(readMoreLink, position++);
}
HTMLPanel html2 = processTagsInChunk(chunk.substring(finalParagraphEndIndex));
html2.setVisible(shouldExpand);
container.insert(html2, position++);
// Create a new readMoreLink that will show the text chunk,
// the next read more link, the non-text chunk, and the read less link.
readMoreLink = new ReadMoreLink(trackReadMoreWithAnalytics,
html1, readMoreLink, html2, readLessLink);
initiallyVisibleChunkCount = 3;
} else {
// Otherwise, process this as a single chunk.
HTMLPanel html = processTagsInChunk(chunk);
html.setVisible(shouldExpand);
container.insert(html, position++);
if (readMoreLink != null) {
container.insert(readMoreLink, position++);
}
// Create a new readMoreLink that will show the chunk,
// the next read more link, and the read less link.
readMoreLink = new ReadMoreLink(trackReadMoreWithAnalytics,
html, readMoreLink, readLessLink);
initiallyVisibleChunkCount = 2;
}
}