use of com.vaadin.client.widget.grid.RendererCellReference in project cuba by cuba-platform.
the class CubaTreeRendererConnector method createRenderer.
@Override
public Renderer<String> createRenderer() {
return new HtmlRenderer() {
@Override
public void render(RendererCellReference cell, String htmlString) {
String content = getContentString(htmlString);
Element icon = getIconElement(cell);
Element span = findSpan(cell);
if (span == null) {
_render(cell, content, icon);
} else {
String oldContent = span.getInnerHTML();
if (!Objects.equals(content, oldContent)) {
_render(cell, content, icon);
}
}
}
protected Element findSpan(RendererCellReference cell) {
TableCellElement cellEl = cell.getElement();
int childCount = DOM.getChildCount(cellEl);
for (int i = 0; i < childCount; i++) {
Element child = DOM.getChild(cellEl, i);
if (SpanElement.TAG.equalsIgnoreCase(child.getTagName())) {
return child;
}
}
return null;
}
protected Element getIconElement(RendererCellReference cell) {
Element iconEl = null;
JsonObject row = getParent().getParent().getDataSource().getRow(cell.getRowIndex());
if (row != null && row.hasKey(ITEM_ICON)) {
String resourceUrl = getResourceUrl(row.getString(ITEM_ICON));
iconEl = getConnection().getIcon(resourceUrl).getElement();
}
return iconEl;
}
protected void _render(RendererCellReference cell, String content, @Nullable Element icon) {
Element span = DOM.createSpan();
span.addClassName(V_CAPTIONTEXT_STYLENAME);
span.setInnerSafeHtml(SafeHtmlUtils.fromSafeConstant(content));
TableCellElement cellEl = cell.getElement();
cellEl.removeAllChildren();
if (icon != null) {
cellEl.appendChild(icon);
}
cellEl.appendChild(span);
}
private String getContentString(String htmlString) {
switch(getState().mode) {
case HTML:
return htmlString;
case PREFORMATTED:
return "<pre>" + SafeHtmlUtils.htmlEscape(htmlString) + "</pre>";
default:
return SafeHtmlUtils.htmlEscape(htmlString);
}
}
};
}
Aggregations