protected void setup() {
addComponent(log);
Panel confPanel = new ConfigurationPanel();
addComponent(confPanel);
final FieldGroup fieldGroup = new BeanFieldGroup<Person>(Person.class);
fieldGroup.addCommitHandler(new CommitHandler() {
@Override
public void preCommit(CommitEvent commitEvent)
throws CommitException {
if (configuration.preCommitFails) {
throw new CommitException(
"Error in preCommit because first name is "
+ getPerson(commitEvent.getFieldBinder())
.getFirstName());
}
}
@Override
public void postCommit(CommitEvent commitEvent)
throws CommitException {
if (configuration.postCommitFails) {
throw new CommitException(
"Error in postCommit because first name is "
+ getPerson(commitEvent.getFieldBinder())
.getFirstName());
}
}
});
fieldGroup.setBuffered(true);
fieldGroup.buildAndBindMemberFields(this);
addComponent(firstName);
addComponent(lastName);
addComponent(email);
addComponent(age);
addComponent(sex);
addComponent(deceased);
Button commitButton = new Button("Commit", new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
String msg = "Commit succesful";
try {
fieldGroup.commit();
} catch (CommitException e) {
msg = "Commit failed: " + e.getMessage();
}
Notification.show(msg);
log.log(msg);
}
});
Button discardButton = new Button("Discard",
new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
fieldGroup.discard();
log.log("Discarded changes");
}
});
Button showBean = new Button("Show bean values",
new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
log.log(getPerson(fieldGroup).toString());
}
});
addComponent(commitButton);
addComponent(discardButton);
addComponent(showBean);
email.addValidator(new EmailValidator("Must be a valid address"));
lastName.addValidator(new StringLengthValidator("Must be min 5 chars",
5, null, true));
age.addValidator(new IntegerRangeValidator(
"Must be between 0 and 150, {0} is not", 0, 150));
sex.setPageLength(0);
deceased.setConverter(new StringToBooleanConverter() {
@Override
protected String getTrueString() {
return "YAY!";
}
@Override
protected String getFalseString() {
return "NAAAAAH";
}
});
Person p = new Person("John", "Doe", "john@doe.com", 64, Sex.MALE,
new Address("John street", 11223, "John's town", Country.USA));
fieldGroup.setItemDataSource(new BeanItem<Person>(p));
}