PDA

View Full Version : Bold items on the tree



piechos
23 Feb 2015, 3:26 AM
Hey, I have a problem. Is it possible to bold some items on the tree? If the answer is yes, how can I do it?

Andreas Samjeske
23 Feb 2015, 5:57 AM
Hey, I have a problem. Is it possible to bold some items on the tree? If the answer is yes, how can I do it?

I didn't bold any items, but greyed them out. I guess you might be able to alter following code for bolding.

// Extension of Tree
@Override
protected void update() {
super.update();
for (M item : this.getStore().getAll()) {
// Put your condition here
if (item.shouldBeBold()) {
// Severe nullchecking is important. Soon after adding items to store, there might not be corresponding nodes. If there are nodes, there might not be Elements or they have no Style yet.
if (this.findNode(item) != null) {
if (this.getView().getIconElement(this.findNode(item)) != null && this.getView().getIconElement(this.findNode(item)).getStyle() != null) {
this.getView().getIconElement(this.findNode(item)).getStyle().setOpacity(0.3);
}
if (this.getView().getTextElement(this.findNode(item)) != null && this.getView().getTextElement(this.findNode(item)).getStyle() != null) {
this.getView().getTextElement(this.findNode(item)).getStyle().setOpacity(0.5);
}
}
}
}
}

piechos
24 Feb 2015, 1:46 AM
I resolved it that way:



final List<String> keys = getFilteredList();


filteringTree.getTree().setCell(new AbstractCell<String>() {
@Override
public void render(Context context, String valueString,
SafeHtmlBuilder sb) {
Object key = context.getKey();
System.out.println(keys.contains(key));
if (context.getKey() != null
&& !context.getKey().equals("null")
&& valueString != null
&& valueString.trim().length() > 0
&& keys.contains(key)) {
sb.appendHtmlConstant("<b>" + valueString + "</b>");


} else {
sb.appendHtmlConstant(valueString);
}
}


where getFilteredList() gets the list of elements which fit to the filter

Andreas Samjeske
24 Feb 2015, 5:38 AM
I resolved it that way:



final List<String> keys = getFilteredList();


filteringTree.getTree().setCell(new AbstractCell<String>() {
@Override
public void render(Context context, String valueString,
SafeHtmlBuilder sb) {
Object key = context.getKey();
System.out.println(keys.contains(key));
if (context.getKey() != null
&& !context.getKey().equals("null")
&& valueString != null
&& valueString.trim().length() > 0
&& keys.contains(key)) {
sb.appendHtmlConstant("<b>" + valueString + "</b>");


} else {
sb.appendHtmlConstant(valueString);
}
}


where getFilteredList() gets the list of elements which fit to the filter

Right. Using setCell() is probably the right way. I couldn't cause I wanted to alter the icon, too.

But be careful! Depending on the source of valueString the use of sb.appendHtmlConstant(valueString); is a security threat! An attacker being able to set valueString may use this for cross-site scripting (http://en.wikipedia.org/wiki/Cross-site_scripting).
And btw, as valueString is not a constant, using appendHtmlConstant seems to be odd anyway. I think it is better and safer this way:

sb.append(SafeHtmlUtils.fromTrustedString("<b>"));
sb.appendEscaped(valueString);
sb.append(SafeHtmlUtils.fromTrustedString("</b>"));

piechos
25 Feb 2015, 4:47 AM
You are right, I didn't think about security. Thanks ;)