smoreno
unread,May 29, 2009, 6:01:46 AM5/29/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to vogella
Hello
I am following the JFace TableViewer tutorial and I have noticed a
strange behaviour when highlighting text.
My view consist of a TableViewer and a text box for searching. As you
type a search string in the text box, any match in the grid is
highlighted. Similarly, as you clear characters of the search string,
the hihghlighted text changes accordingly. But, when you clear
completely the text box the grid keeps highlighted the last match
(i.e., the first letter of your search string).
I have fixed this issue adding another condition in the if block of
the update method of the StyledLabelProvider, to take into account the
possibility of the searchText being empty (see code below).
Nevertheless, I am wondering if there is a more elegant way of fixing
this issue. Thank you very much in advanced.
Regards
Sandra
MAIN PART OF MY STYLED CELL LABEL PROVIDER
public class RotStyledLabelProvider extends StyledCellLabelProvider{
private String searchText;
private Color systemColor;
private Color colorFondo;
public RotStyledLabelProvider() {
systemColor = Display.getCurrent().getSystemColor(SWT.COLOR_YELLOW);
colorFondo = Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
}
public void setSearchText(String searchText) {
this.searchText = searchText;
}
@Override
public void update(ViewerCell cell) {
Rot element = (Rot) cell.getElement();
int index = cell.getColumnIndex();
String columnText = getColumnText(element, index);
cell.setText(columnText);
cell.setImage(getColumnImage(element, index));
if (searchText != null && searchText.length() > 0) {
int intRangesCorrectSize[] = SearchUtil.getSearchTermOccurrences(
searchText, columnText);
List<StyleRange> styleRange = new ArrayList<StyleRange>();
for (int i = 0; i < intRangesCorrectSize.length / 2; i++) {
StyleRange myStyleRange = new StyleRange(0, 0, null,
systemColor);
myStyleRange.start = intRangesCorrectSize[i];
myStyleRange.length = intRangesCorrectSize[++i];
styleRange.add(myStyleRange);
}
cell.setStyleRanges((StyleRange[]) styleRange.toArray
(new StyleRange[styleRange.size()]));
} else if (searchText=="") {
List<StyleRange> styleRange = new ArrayList<StyleRange>();
styleRange.add(new StyleRange(0, 10, null,colorFondo));
cell.setStyleRanges((StyleRange[]) styleRange.toArray
(new StyleRange[styleRange.size()]));
}
super.update(cell);
}
...