Highlighting text in JFace TableViewer tutorial

641 views
Skip to first unread message

smoreno

unread,
May 29, 2009, 6:01:46 AM5/29/09
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);

}
...

Lars Vogel

unread,
Jun 2, 2009, 4:16:27 AM6/2/09
to vog...@googlegroups.com
Hi,

thanks. I'll have a look.

Best regards, Lars

2009/5/29 smoreno <fisik...@gmail.com>



--
Lars
http://www.vogella.de - Tutorial about Java, Eclipse and Web programming

Lars....@googlemail.com

unread,
Jul 27, 2009, 5:02:32 PM7/27/09
to vog...@googlegroups.com
Hi,

sorry for taking so long. I have fixed my example http://www.vogella.de/articles/EclipseJFaceTable/article.html

I used cell.setStyleRanges(null);

Best regards and thank you for the tip, Lars
>                        List styleRange = new ArrayList();

>
>
>
>                        for (int i = 0; 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 = new ArrayList();
Reply all
Reply to author
Forward
0 new messages