Using the Crud UI add-on

Thanks to its open source nature, there are hundreds of third-party components and utilities published available at: https://vaadin.com/directory. One of them does almost all the work we have done in this chapter. The following class shows how to implement a CRUD user interface using the Crud UI add-on available at https://vaadin.com/directory/component/crud-ui-add-on, which is maintained by the author of this book:

public class CrudAddOn extends Composite {

private GridCrud<User> crud = new GridCrud<>(User.class, new HorizontalSplitCrudLayout());

public CrudAddOn() {
initLayout();
initBehavior();
}

private void initLayout() {
crud.getGrid().setColumns("firstName", "lastName", "email", "mainRole");
crud.getCrudFormFactory().setVisibleProperties("firstName", "lastName", "email", "password", "roles", "mainRole", "blocked");

crud.getCrudFormFactory().setFieldType("password", PasswordField.class);
crud.getCrudFormFactory().setFieldProvider("roles", new CheckBoxGroupProvider<>(RoleRepository.findAll()));
crud.getCrudFormFactory().setFieldProvider("mainRole", new ComboBoxProvider<>("Main Role", RoleRepository.findAll()));

VerticalLayout layout = new VerticalLayout(crud);
setCompositionRoot(layout);
setSizeFull();
}

private void initBehavior() {
crud.setFindAllOperation(() -> UserRepository.findAll());
crud.setAddOperation(user -> UserRepository.save(user));
crud.setUpdateOperation(user -> UserRepository.save(user));
crud.setDeleteOperation(user -> UserRepository.delete(user));
crud.getCrudFormFactory().setUseBeanValidation(true);
}
}

The add-on offers several configuration options, such as the possibility to configure a layout, set field providers, and use JavaBean Validation. It also delegates the CRUD operations to your own code, allowing you to use any kind of Java backend technology. The following is a screenshot of the CRUD component created with the Crud UI add-on:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset