突出显示SWT树节点中的特定字符串 [英] Highlight particular string in SWT Tree node
问题描述
我有一个要求,首先加载树,树包含4个级别。
I have a requirement like first of all tree will be loaded, tree contains 4 levels.
有一个文本字段,用户可以输入filterText和他可以按搜索按钮。
There is a text field, where user can enter "filterText" and he can press search button.
在树中,如果与过滤文本匹配,则在四个级别中的任意一个级别,那个特定的字符串只能用黄色突出显示但是不会扩展整个节点及其对应的树。
In tree, in any of four levels if there is a match with filter text, then that particular string only should be highlighted with yellow color but not whole node and its corresponding tree will be expanded.
不匹配的树节点不应该展开。
Unmatched tree nodes should not expand.
推荐答案
我假设您正在使用 TreeViewer
。
您可以使用 StyledCellLabelProvider
为标签字符串的各个部分设置不同的样式。 DelegatingStyledCellLabelProvider
类派生自此类,使事情变得更容易一些。使用以下方式设置标签提供者:
You can use a StyledCellLabelProvider
to set different styles for parts of the label string. The DelegatingStyledCellLabelProvider
class is derived from this class to make things a bit easier. Set up the label provider using:
viewer.setLabelProvider(new DelegatingStyledCellLabelProvider(myLabelProvider));
其中 myLabelProvider
是一个实现<$的类C $ C> DelegatingStyledCellLabelProvider.IStyledLabelProvider 。提供者像往常一样有 getImage
方法加上:
where myLabelProvider
is a class implementing DelegatingStyledCellLabelProvider.IStyledLabelProvider
. The provider has a getImage
method as usual plus:
public StyledString getStyledText(Object element)
使用 StyledString
它允许您对文本应用不同的样式。类似于:
which uses a StyledString
which allows you to apply different styles to the text. Something like:
StyledString text = new StyledString();
text.append("unstyled text");
text.append("styled text with decorations style", StyledString.DECORATIONS_STYLER);
以及预定义的 StyledString.Styler
您可以定义自己的值。 DefaultStyler
类允许您使用JFace颜色注册表中定义的颜色。
as well as the predefined StyledString.Styler
values you can define your own. The DefaultStyler
class lets you use colors defined in the JFace color registry.
要设置的样式器的简单版本黄色的背景是:
A simple version of a styler to set the background to yellow would be:
class HighlightStyler extends Styler
{
@Override
public void applyStyles(final TextStyle textStyle)
{
textStyle.background = Display.getDefault().getSystemColor(SWT.COLOR_YELLOW);
}
}
这篇关于突出显示SWT树节点中的特定字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!