Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alejandro-du/ef3f696484861cf7c2256903a989dbae to your computer and use it in GitHub Desktop.
Save alejandro-du/ef3f696484861cf7c2256903a989dbae to your computer and use it in GitHub Desktop.
package com.example;
import com.vaadin.data.Binder;
import com.vaadin.server.VaadinRequest;
import com.vaadin.spring.annotation.SpringUI;
import com.vaadin.ui.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
@SpringUI
public class VaadinUI extends UI {
@Autowired
private CustomerService service;
private Customer customer;
private Binder<Customer> binder = new Binder<>(Customer.class);
private Grid<Customer> grid = new Grid(Customer.class);
private TextField firstName = new TextField("First name");
private TextField lastName = new TextField("Last name");
private Button save = new Button("Save", e -> saveCustomer());
@Override
protected void init(VaadinRequest request) {
updateGrid();
grid.setColumns("firstName", "lastName");
grid.addSelectionListener(e -> updateForm());
binder.bindInstanceFields(this);
VerticalLayout form = new VerticalLayout(firstName, lastName, save);
HorizontalLayout layout = new HorizontalLayout(grid, form);
setContent(layout);
}
private void updateGrid() {
List<Customer> customers = service.findAll();
grid.setItems(customers);
setFormVisible(false);
}
private void updateForm() {
if (grid.asSingleSelect().isEmpty()) {
setFormVisible(false);
} else {
customer = grid.asSingleSelect().getValue();
binder.setBean(customer);
setFormVisible(true);
}
}
private void setFormVisible(boolean visible) {
firstName.setVisible(visible);
lastName.setVisible(visible);
save.setVisible(visible);
}
private void saveCustomer() {
service.update(customer);
updateGrid();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment