Conveter String to Embedded

Hi guys,

i´m trying to implement a converter that converts a string into a coresponding embedded icon object.

everytime i´m getting this fault: The converter presentation type class com.vaadin.ui.Embedded is not compatible with the renderer presentation type class java.lang.String (in Column[propertyId:xxxx)


public class IconTypeConverter implements Converter<Embedded, String>{
    @Override
    public String convertToModel(Embedded value, Class<? extends String> targetType, Locale locale)
            throws com.vaadin.data.util.converter.Converter.ConversionException {
        return null;
    }

    @Override
    public Embedded convertToPresentation(String value, Class<? extends Embedded> targetType, Locale locale)
            throws com.vaadin.data.util.converter.Converter.ConversionException {
       return new Embedded(caption, new ThemeResource(sourcepath))
       
    }

    @Override
    public Class<String> getModelType() {
        return String.class;
    }

    @Override
    public Class<Embedded> getPresentationType() {
        return Embedded.class;
    } 
}

Based on your description you are trying to use this IconTypeConverter in Grid.Column. Embedded is a Component, and hence do not have string presentation that Grid could show. That is why you get this error.

I assume that you are trying to show an icon.

In that case you need two phase approach. You need String to String converter that converts your icon to HTML presentation (i.e. String containing the HTML) and then set HTMLRenderer to that same column.

Thank you for your answer.

That is exactly the way i just solved the problem :smiley:

My only doubt is how i access the icons. Therefore i´m using an absolute path like: “http://localhost:8080/VAADIN/themes/mytheme/icons/icon.png”.

is there a way to generate this path generic?

thanks for your help

Usually the paths are relative to class path. So following this chapter in Book of Vaadin

https://vaadin.com/book/-/page/application.resources.html

In your case the path should be “VAADIN/themes/mytheme/icons/icon.png”

ahhh thank you. works perfect