|
| 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 | +/** |
| 16 | + * A simple example to introduce building forms. As your real application is probably much |
| 17 | + * more complicated than this example, you could re-use this form in multiple places. This |
| 18 | + * example component is only used in MainView. |
| 19 | + * <p> |
| 20 | + * In a real world application you'll most likely using a common super class for all your |
| 21 | + * forms - less code, better UX. |
| 22 | + */ |
| 23 | +@SpringComponent |
| 24 | +@UIScope |
| 25 | +public class EmployeeEditor extends VerticalLayout implements KeyNotifier { |
| 26 | + |
| 27 | + private final EmployeeRepository repository; |
| 28 | + |
| 29 | + /** |
| 30 | + * The currently edited employee |
| 31 | + */ |
| 32 | + private Employee employee; |
| 33 | + |
| 34 | + /* Fields to edit properties in Employee entity */ |
| 35 | + TextField firstName = new TextField("First name"); |
| 36 | + TextField lastName = new TextField("Last name"); |
| 37 | + |
| 38 | + /* Action buttons */ |
| 39 | + // TODO why more code? |
| 40 | + Button save = new Button("Save", VaadinIcon.CHECK.create()); |
| 41 | + Button cancel = new Button("Cancel"); |
| 42 | + Button delete = new Button("Delete", VaadinIcon.TRASH.create()); |
| 43 | + HorizontalLayout actions = new HorizontalLayout(save, cancel, delete); |
| 44 | + |
| 45 | + Binder<Employee> binder = new Binder<>(Employee.class); |
| 46 | + private ChangeHandler changeHandler; |
| 47 | + |
| 48 | + @Autowired |
| 49 | + public EmployeeEditor(EmployeeRepository repository) { |
| 50 | + this.repository = repository; |
| 51 | + |
| 52 | + add(firstName, lastName, actions); |
| 53 | + |
| 54 | + // bind using naming convention |
| 55 | + binder.bindInstanceFields(this); |
| 56 | + |
| 57 | + // Configure and style components |
| 58 | + setSpacing(true); |
| 59 | + |
| 60 | + save.getElement().getThemeList().add("primary"); |
| 61 | + delete.getElement().getThemeList().add("error"); |
| 62 | + |
| 63 | + addKeyPressListener(Key.ENTER, e -> save()); |
| 64 | + |
| 65 | + // wire action buttons to save, delete and reset |
| 66 | + save.addClickListener(e -> save()); |
| 67 | + delete.addClickListener(e -> delete()); |
| 68 | + cancel.addClickListener(e -> editEmployee(employee)); |
| 69 | + setVisible(false); |
| 70 | + } |
| 71 | + |
| 72 | + void delete() { |
| 73 | + repository.delete(employee); |
| 74 | + changeHandler.onChange(); |
| 75 | + } |
| 76 | + |
| 77 | + void save() { |
| 78 | + repository.save(employee); |
| 79 | + changeHandler.onChange(); |
| 80 | + } |
| 81 | + |
| 82 | + public interface ChangeHandler { |
| 83 | + void onChange(); |
| 84 | + } |
| 85 | + |
| 86 | + public final void editEmployee(Employee c) { |
| 87 | + if (c == null) { |
| 88 | + setVisible(false); |
| 89 | + return; |
| 90 | + } |
| 91 | + final boolean persisted = c.getId() != null; |
| 92 | + if (persisted) { |
| 93 | + // Find fresh entity for editing |
| 94 | + employee = repository.findById(c.getId()).get(); |
| 95 | + } else { |
| 96 | + employee = c; |
| 97 | + } |
| 98 | + cancel.setVisible(persisted); |
| 99 | + |
| 100 | + // Bind employee properties to similarly named fields |
| 101 | + // Could also use annotation or "manual binding" or programmatically |
| 102 | + // moving values from fields to entities before saving |
| 103 | + binder.setBean(employee); |
| 104 | + |
| 105 | + setVisible(true); |
| 106 | + |
| 107 | + // Focus first name initially |
| 108 | + firstName.focus(); |
| 109 | + } |
| 110 | + |
| 111 | + public void setChangeHandler(ChangeHandler h) { |
| 112 | + // ChangeHandler is notified when either save or delete |
| 113 | + // is clicked |
| 114 | + changeHandler = h; |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments