|
| 1 | +package com.baeldung; |
| 2 | + |
| 3 | +import com.vaadin.flow.component.Key; |
| 4 | +import com.vaadin.flow.component.KeyNotifier; |
| 5 | +import com.vaadin.flow.component.button.Button; |
| 6 | +import com.vaadin.flow.component.icon.VaadinIcon; |
| 7 | +import com.vaadin.flow.component.orderedlayout.HorizontalLayout; |
| 8 | +import com.vaadin.flow.component.orderedlayout.VerticalLayout; |
| 9 | +import com.vaadin.flow.component.textfield.TextField; |
| 10 | +import com.vaadin.flow.data.binder.Binder; |
| 11 | +import com.vaadin.flow.spring.annotation.SpringComponent; |
| 12 | +import com.vaadin.flow.spring.annotation.UIScope; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | + |
| 15 | +@SpringComponent |
| 16 | +@UIScope |
| 17 | +public class EmployeeEditor extends VerticalLayout implements KeyNotifier { |
| 18 | + |
| 19 | + private final EmployeeRepository repository; |
| 20 | + |
| 21 | + private Employee employee; |
| 22 | + |
| 23 | + TextField firstName = new TextField("First name"); |
| 24 | + TextField lastName = new TextField("Last name"); |
| 25 | + |
| 26 | + Button save = new Button("Save", VaadinIcon.CHECK.create()); |
| 27 | + Button cancel = new Button("Cancel"); |
| 28 | + Button delete = new Button("Delete", VaadinIcon.TRASH.create()); |
| 29 | + HorizontalLayout actions = new HorizontalLayout(save, cancel, delete); |
| 30 | + |
| 31 | + Binder<Employee> binder = new Binder<>(Employee.class); |
| 32 | + private ChangeHandler changeHandler; |
| 33 | + |
| 34 | + @Autowired |
| 35 | + public EmployeeEditor(EmployeeRepository repository) { |
| 36 | + this.repository = repository; |
| 37 | + |
| 38 | + add(firstName, lastName, actions); |
| 39 | + |
| 40 | + binder.bindInstanceFields(this); |
| 41 | + |
| 42 | + setSpacing(true); |
| 43 | + |
| 44 | + save.getElement().getThemeList().add("primary"); |
| 45 | + delete.getElement().getThemeList().add("error"); |
| 46 | + |
| 47 | + addKeyPressListener(Key.ENTER, e -> save()); |
| 48 | + |
| 49 | + save.addClickListener(e -> save()); |
| 50 | + delete.addClickListener(e -> delete()); |
| 51 | + cancel.addClickListener(e -> editEmployee(employee)); |
| 52 | + setVisible(false); |
| 53 | + } |
| 54 | + |
| 55 | + void delete() { |
| 56 | + repository.delete(employee); |
| 57 | + changeHandler.onChange(); |
| 58 | + } |
| 59 | + |
| 60 | + void save() { |
| 61 | + repository.save(employee); |
| 62 | + changeHandler.onChange(); |
| 63 | + } |
| 64 | + |
| 65 | + public interface ChangeHandler { |
| 66 | + void onChange(); |
| 67 | + } |
| 68 | + |
| 69 | + public final void editEmployee(Employee c) { |
| 70 | + if (c == null) { |
| 71 | + setVisible(false); |
| 72 | + return; |
| 73 | + } |
| 74 | + final boolean persisted = c.getId() != null; |
| 75 | + if (persisted) { |
| 76 | + employee = repository.findById(c.getId()).get(); |
| 77 | + } else { |
| 78 | + employee = c; |
| 79 | + } |
| 80 | + |
| 81 | + cancel.setVisible(persisted); |
| 82 | + binder.setBean(employee); |
| 83 | + setVisible(true); |
| 84 | + firstName.focus(); |
| 85 | + } |
| 86 | + |
| 87 | + public void setChangeHandler(ChangeHandler h) { |
| 88 | + changeHandler = h; |
| 89 | + } |
| 90 | +} |
0 commit comments