Search in sources :

Example 1 with RendererCellReference

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);
            }
        }
    };
}
Also used : Element(com.google.gwt.user.client.Element) TableCellElement(com.google.gwt.dom.client.TableCellElement) SpanElement(com.google.gwt.dom.client.SpanElement) RendererCellReference(com.vaadin.client.widget.grid.RendererCellReference) HtmlRenderer(com.vaadin.client.renderers.HtmlRenderer) JsonObject(elemental.json.JsonObject) TableCellElement(com.google.gwt.dom.client.TableCellElement) Nullable(javax.annotation.Nullable)

Aggregations

SpanElement (com.google.gwt.dom.client.SpanElement)1 TableCellElement (com.google.gwt.dom.client.TableCellElement)1 Element (com.google.gwt.user.client.Element)1 HtmlRenderer (com.vaadin.client.renderers.HtmlRenderer)1 RendererCellReference (com.vaadin.client.widget.grid.RendererCellReference)1 JsonObject (elemental.json.JsonObject)1 Nullable (javax.annotation.Nullable)1