Search in sources :

Example 1 with Converter

use of com.vaadin.v7.data.util.converter.Converter in project cuba by cuba-platform.

the class WebAbstractTable method formatCellValue.

protected String formatCellValue(Object rowId, Object colId, @Nullable Property<?> property) {
    TableItems<E> tableItems = getItems();
    if (tableItems == null || tableItems.getState() == BindingState.INACTIVE) {
        return null;
    }
    Column<E> column = columns.get(colId);
    if (column != null && column.getValueProvider() != null) {
        E item = tableItems.getItem(rowId);
        Object generatedValue = column.getValueProvider().apply(item);
        Function<Object, String> formatter = column.getFormatter();
        if (formatter != null) {
            return column.getFormatter().apply(generatedValue);
        }
        return metadataTools.format(generatedValue);
    }
    Object cellValue;
    if (ignoreUnfetchedAttributes && colId instanceof MetaPropertyPath) {
        E item = tableItems.getItem(rowId);
        cellValue = getValueExIgnoreUnfetched(item, ((MetaPropertyPath) colId).getPath());
    } else if (property != null) {
        cellValue = property.getValue();
    } else {
        cellValue = null;
    }
    if (colId instanceof MetaPropertyPath) {
        MetaPropertyPath propertyPath = (MetaPropertyPath) colId;
        if (column != null) {
            if (column.getFormatter() != null) {
                return column.getFormatter().apply(cellValue);
            } else if (column.getXmlDescriptor() != null) {
                // vaadin8 move to Column
                String captionProperty = column.getXmlDescriptor().attributeValue("captionProperty");
                if (StringUtils.isNotEmpty(captionProperty)) {
                    E item = getItems().getItemNN(rowId);
                    Object captionValue = item.getValueEx(captionProperty);
                    return captionValue != null ? String.valueOf(captionValue) : null;
                }
            }
        }
        return metadataTools.format(cellValue, propertyPath.getMetaProperty());
    }
    if (cellValue == null) {
        return "";
    }
    if (!(cellValue instanceof Component)) {
        return metadataTools.format(cellValue);
    }
    // fallback to Vaadin formatting
    UI ui = component.getUI();
    VaadinSession session = ui != null ? ui.getSession() : null;
    Converter converter = ConverterUtil.getConverter(String.class, property.getType(), session);
    if (converter != null) {
        return (String) converter.convertToPresentation(cellValue, String.class, locale);
    }
    return cellValue.toString();
}
Also used : AppUI(com.haulmont.cuba.web.AppUI) CubaUI(com.haulmont.cuba.web.widgets.CubaUI) VaadinSession(com.vaadin.server.VaadinSession) Converter(com.vaadin.v7.data.util.converter.Converter) Component(com.vaadin.ui.Component)

Aggregations

AppUI (com.haulmont.cuba.web.AppUI)1 CubaUI (com.haulmont.cuba.web.widgets.CubaUI)1 VaadinSession (com.vaadin.server.VaadinSession)1 Component (com.vaadin.ui.Component)1 Converter (com.vaadin.v7.data.util.converter.Converter)1