Vaadin 8 Binding data from pojo to form created by designer

I am working on a project where the UI is being generated from the Vaadin designer. I have the html generated and the corresponding .java file.

I extend the java file and bind my data to the view of the design. The basic code to display my data is working as each field on the form has a corresponding pojo variable. Now I want to display two fields together on the form much like you would combine first and last name to show a full name.

My pojo has a toString method that returns the two values combined but I can’t figure out how to set the label in the design ( named propertyNoName() ) to the value of aPropertyProfile.toString() .

Below is the relevant code…

public class PropertyProfileDesign extends VerticalLayout
{

    private VerticalLayout  propertyProfileHeader;
    private Label           propertyNoName;
    private TextField       propertyMgr;
    private TextField       propertyMgrPhone;
    private TextField       propertyMgrCellular;
    private TextField       propertyMgrHome;
    private TextField       regionalMgr;
    private TextField       regionalMgrPhone;
    private TextField       regionalMgrCellular;
    private TextField       regionalMgrHome;
    private VerticalLayout  propertyProfileBodyFooter;

    public PropertyProfileDesign()
    {
        Design.read( this );
    }

    public VerticalLayout getPropertyProfileHeader()
    {
        return propertyProfileHeader;
    }

    public Label getPropertyNoName()
    {
        return propertyNoName;
    }

    public TextField getPropertyMgr()
    {
        return propertyMgr;
    }

    public TextField getPropertyMgrPhone()
    {
        return propertyMgrPhone;
    }

    public TextField getPropertyMgrCellular()
    {
        return propertyMgrCellular;
    }

    public TextField getPropertyMgrHome()
    {
        return propertyMgrHome;
    }

    public TextField getRegionalMgr()
    {
        return regionalMgr;
    }

    public TextField getRegionalMgrPhone()
    {
        return regionalMgrPhone;
    }

    public TextField getRegionalMgrCellular()
    {
        return regionalMgrCellular;
    }

    public TextField getRegionalMgrHome()
    {
        return regionalMgrHome;
    }

    public VerticalLayout getPropertyProfileBodyFooter()
    {
        return propertyProfileBodyFooter;
    }
}

public class PropertyProfileView extends PropertyProfileDesign
{

    private static final long serialVersionUID = 7454308982717559970L;

    private Binder<PropertyProfile> binder = new Binder<>( PropertyProfile.class );

    public PropertyProfileView()
    {
    }

    public PropertyProfileView( final PropertyProfile aPropertyProfile )
    {
        this();

        binder.bindInstanceFields( this );
        binder.readBean( aPropertyProfile );
        //  How to update the label field propertyNoName with aPropertyProfile.toString()?

    }
}

well, you can’t bind label with binder.

simply experiment, try

Label label = new Label();
Binder<PropertyProfile> binder = new Binder<>();
binder.forField(label);

the binder.forField needs HasValue and Label just does not have it.

Simply solution would be a oneLiner.

propertyNoName.setValue(aPropertyProfile.getNoName());

Thank you so much Villius, that makes perfect sense. Sometimes it’s hard to see the forest through the trees.

Another question if I could. I have another field named propertyMgr that contains a name in hierarchical format:CN=Steven Rieger/O=MyOrg

I want to always strip that out. This works getPropertyMgr().setValue( getPropertyMgr().getValue().replaceAll( “CN=”, “” ).replaceAll( “/O=Lieberman”, “” ) ); but I was hoping to have the binder do it in a withconverter…

I can’t figure out the syntax to get this to work.

I’ve tried a couple variations of this such as :
binder.forField( getPropertyMgr() ).withConverter( value → value.replaceAll( “CN=”, “” ), getPropertyMgr().setValue( value ) ).bind( getPropertyMgr() );

Any help you can offer on the correct syntax for the converter would be greatly appreciated!

Hey Steven.

this should work for the Presentation.

binder
	.forField(propertyMgr)
	 .withConverter(n -> n,  n -> n.replaceAll("CN=", "" ).replaceAll("/O=.*", "" ))
	 .bind(...)

The withConverter has one overloaded method, which accepts first parameter as “toModel” and second as “toPresentation”

But i see a possible Problem with this.

The n -> n part may overwrite your property with the converted “clear” value, without CN= and /O=Company. I havent testet it tough :slight_smile:

Also, do you have access to Model ? If you do, can you simply write a method

public String getNamesOnly() {
   return name + " " + lastname;
}

And bind this method to the field, so you do not need to parse the string.

Many ways :slight_smile: