Created
May 4, 2017 11:55
-
-
Save alejandro-du/ef3f696484861cf7c2256903a989dbae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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