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