Search code examples
vaadinvaadin8

Vaadin: Bind Enum values to String in Vaadin 8


I'm getting some errorI’m working on upgrading our application vaadin version from 7.7.24 to 8.13.3. We’ve completed all the dependency issues and i’m able to start the application in locally.

We have a textbox that is showing up the Event data.

Here is the class file that i'm using:

@Entity
@Table(name = "changelog")
public class ChangelogEvent extends BaseEntity
{
@Column(name = "remote_ip")
private String remoteIp;
@Column(name = "remote_host")
private String remoteHost;
@Column(name = "event")
@Enumerated(EnumType.ORDINAL)
private ChangelogEventType eventType;

@Column(name = "entity_type")
private String entityType;

public ChangelogEvent()
{
}
public ChangelogEvent(String remoteIp, String remoteHost, ChangelogEventType eventType)
{
this.remoteIp = remoteIp;
this.remoteHost = remoteHost;
this.eventType = eventType;
}
public String getRemoteIp()
{
return remoteIp;
}
public void setRemoteIp(String remoteIp)
{
this.remoteIp = remoteIp;
}
public ChangelogEventType getEventType()
{
return eventType;
}
public void setEventType(ChangelogEventType eventType)
{
this.eventType = eventType;
}
public String getRemoteHost()
{
return remoteHost;
}
public void setRemoteHost(String remoteHost)
{
this.remoteHost = remoteHost;
}
public String getEntityType()
{
return entityType;
}
public void setEntityType(String entityType)
{
this.entityType = entityType;
}

}

And here is my ChangelogEventType.java file that defined ChangelogEventType enum:

public enum ChangelogEventType
{
CREATED("Created"),
UPDATED("Updated"),
DELETED("Deleted"),
LOGIN("Login"),
LOGOUT("Logout"),
LOGIN_RETRY("Login Retry"),
ACCOUNT_LOCKED("Account Locked"),
PASSWORD_EXPIRED("Password Expired"),
PASSWORD_CHANGED("Password Changed");
private String text;
ChangelogEventType(String text)
{
this.text = text;
}
public String getText()
{
return text;
}
public static ChangelogEventType fromString(String text)
{
if (text != null)
{
for (ChangelogEventType event : ChangelogEventType.values())
{
if (text.equalsIgnoreCase(event.text))
{
return event;
}
}
}
return null;
}
}

Here is the code that i'm using for binding the values into textfield.

eventType = createTextField("Event", COLUMN_WIDTH);
binder.forField(eventType)
        .withNullRepresentation("None")
        .bind(ChangelogEvent::getEventType, ChangelogEvent::setEventType);

Is there any way to bind the Enum to textbox ?


Solution

  • You need to write custom converter and use it in Binder using withConverter method, in your case something like:

    class StringToChangelogEventTypeConverter implements Converter<String, ChangelogEventType> {
      @Override
      public Result<ChangelogEventType> convertToModel(String fieldValue, ValueContext context) {
        // Produces a converted value or an error
        ChangelogEventType event = ChangelogEventType.fromString(fieldValue);
        if (event != null) {
          // ok is a static helper method that creates a Result
          return Result.ok(ChangelogEventType.fromString(fieldValue));
        } else {
          // error is a static helper method that creates a Result
          return Result.error("Please enter a number");
        }
      }
    
      @Override
      public String convertToPresentation(ChangelogEventType event, ValueContext context) {
        // Converting to the field type should always succeed,
        // so there is no support for returning an error Result.
        return event.getText();
      }
    }