1212import com .vaadin .flow .spring .annotation .UIScope ;
1313import org .springframework .beans .factory .annotation .Autowired ;
1414
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- */
2315@ SpringComponent
2416@ UIScope
2517public class EmployeeEditor extends VerticalLayout implements KeyNotifier {
2618
2719 private final EmployeeRepository repository ;
2820
29- /**
30- * The currently edited employee
31- */
3221 private Employee employee ;
3322
34- /* Fields to edit properties in Employee entity */
3523 TextField firstName = new TextField ("First name" );
3624 TextField lastName = new TextField ("Last name" );
3725
38- /* Action buttons */
39- // TODO why more code?
4026 Button save = new Button ("Save" , VaadinIcon .CHECK .create ());
4127 Button cancel = new Button ("Cancel" );
4228 Button delete = new Button ("Delete" , VaadinIcon .TRASH .create ());
@@ -51,18 +37,15 @@ public EmployeeEditor(EmployeeRepository repository) {
5137
5238 add (firstName , lastName , actions );
5339
54- // bind using naming convention
5540 binder .bindInstanceFields (this );
5641
57- // Configure and style components
5842 setSpacing (true );
5943
6044 save .getElement ().getThemeList ().add ("primary" );
6145 delete .getElement ().getThemeList ().add ("error" );
6246
6347 addKeyPressListener (Key .ENTER , e -> save ());
6448
65- // wire action buttons to save, delete and reset
6649 save .addClickListener (e -> save ());
6750 delete .addClickListener (e -> delete ());
6851 cancel .addClickListener (e -> editEmployee (employee ));
@@ -90,28 +73,18 @@ public final void editEmployee(Employee c) {
9073 }
9174 final boolean persisted = c .getId () != null ;
9275 if (persisted ) {
93- // Find fresh entity for editing
9476 employee = repository .findById (c .getId ()).get ();
9577 } else {
9678 employee = c ;
9779 }
98- cancel .setVisible (persisted );
9980
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
81+ cancel .setVisible (persisted );
10382 binder .setBean (employee );
104-
10583 setVisible (true );
106-
107- // Focus first name initially
10884 firstName .focus ();
10985 }
11086
11187 public void setChangeHandler (ChangeHandler h ) {
112- // ChangeHandler is notified when either save or delete
113- // is clicked
11488 changeHandler = h ;
11589 }
116-
11790}
0 commit comments