From dacf049162a8d898849697811db3d781d2c76e24 Mon Sep 17 00:00:00 2001 From: Ganesh Pagade Date: Mon, 23 Jul 2018 23:38:46 +0530 Subject: [PATCH 1/2] vaadin spring --- vaadin-spring/pom.xml | 63 ++++++++++ .../main/java/com/baeldung/Application.java | 29 +++++ .../src/main/java/com/baeldung/Employee.java | 51 ++++++++ .../java/com/baeldung/EmployeeEditor.java | 117 ++++++++++++++++++ .../java/com/baeldung/EmployeeRepository.java | 10 ++ .../src/main/java/com/baeldung/MainView.java | 78 ++++++++++++ 6 files changed, 348 insertions(+) create mode 100644 vaadin-spring/pom.xml create mode 100644 vaadin-spring/src/main/java/com/baeldung/Application.java create mode 100644 vaadin-spring/src/main/java/com/baeldung/Employee.java create mode 100644 vaadin-spring/src/main/java/com/baeldung/EmployeeEditor.java create mode 100644 vaadin-spring/src/main/java/com/baeldung/EmployeeRepository.java create mode 100644 vaadin-spring/src/main/java/com/baeldung/MainView.java diff --git a/vaadin-spring/pom.xml b/vaadin-spring/pom.xml new file mode 100644 index 0000000000..3f411cc7a1 --- /dev/null +++ b/vaadin-spring/pom.xml @@ -0,0 +1,63 @@ + + + 4.0.0 + + com.baeldung + vaadin-spring + 0.1.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.0.3.RELEASE + + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.vaadin + vaadin-spring-boot-starter + + + com.h2database + h2 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + com.vaadin + vaadin-bom + 10.0.1 + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/vaadin-spring/src/main/java/com/baeldung/Application.java b/vaadin-spring/src/main/java/com/baeldung/Application.java new file mode 100644 index 0000000000..1d3084723a --- /dev/null +++ b/vaadin-spring/src/main/java/com/baeldung/Application.java @@ -0,0 +1,29 @@ +package com.baeldung; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +public class Application { + + private static final Logger log = LoggerFactory.getLogger(Application.class); + + public static void main(String[] args) { + SpringApplication.run(Application.class); + } + + @Bean + public CommandLineRunner loadData(EmployeeRepository repository) { + return (args) -> { + repository.save(new Employee("Bill", "Gates")); + repository.save(new Employee("Mark", "Zuckerberg")); + repository.save(new Employee("Sundar", "Pichai")); + repository.save(new Employee("Jeff", "Bezos")); + }; + } +} diff --git a/vaadin-spring/src/main/java/com/baeldung/Employee.java b/vaadin-spring/src/main/java/com/baeldung/Employee.java new file mode 100644 index 0000000000..726f0838b6 --- /dev/null +++ b/vaadin-spring/src/main/java/com/baeldung/Employee.java @@ -0,0 +1,51 @@ +package com.baeldung; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class Employee { + + @Id + @GeneratedValue + private Long id; + + private String firstName; + + private String lastName; + + protected Employee() { + } + + public Employee(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + public Long getId() { + return id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + @Override + public String toString() { + return String.format("Employee[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName); + } + +} diff --git a/vaadin-spring/src/main/java/com/baeldung/EmployeeEditor.java b/vaadin-spring/src/main/java/com/baeldung/EmployeeEditor.java new file mode 100644 index 0000000000..c10c88978b --- /dev/null +++ b/vaadin-spring/src/main/java/com/baeldung/EmployeeEditor.java @@ -0,0 +1,117 @@ +package com.baeldung; + +import com.vaadin.flow.component.Key; +import com.vaadin.flow.component.KeyNotifier; +import com.vaadin.flow.component.button.Button; +import com.vaadin.flow.component.icon.VaadinIcon; +import com.vaadin.flow.component.orderedlayout.HorizontalLayout; +import com.vaadin.flow.component.orderedlayout.VerticalLayout; +import com.vaadin.flow.component.textfield.TextField; +import com.vaadin.flow.data.binder.Binder; +import com.vaadin.flow.spring.annotation.SpringComponent; +import com.vaadin.flow.spring.annotation.UIScope; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * A simple example to introduce building forms. As your real application is probably much + * more complicated than this example, you could re-use this form in multiple places. This + * example component is only used in MainView. + *

+ * In a real world application you'll most likely using a common super class for all your + * forms - less code, better UX. + */ +@SpringComponent +@UIScope +public class EmployeeEditor extends VerticalLayout implements KeyNotifier { + + private final EmployeeRepository repository; + + /** + * The currently edited employee + */ + private Employee employee; + + /* Fields to edit properties in Employee entity */ + TextField firstName = new TextField("First name"); + TextField lastName = new TextField("Last name"); + + /* Action buttons */ + // TODO why more code? + Button save = new Button("Save", VaadinIcon.CHECK.create()); + Button cancel = new Button("Cancel"); + Button delete = new Button("Delete", VaadinIcon.TRASH.create()); + HorizontalLayout actions = new HorizontalLayout(save, cancel, delete); + + Binder binder = new Binder<>(Employee.class); + private ChangeHandler changeHandler; + + @Autowired + public EmployeeEditor(EmployeeRepository repository) { + this.repository = repository; + + add(firstName, lastName, actions); + + // bind using naming convention + binder.bindInstanceFields(this); + + // Configure and style components + setSpacing(true); + + save.getElement().getThemeList().add("primary"); + delete.getElement().getThemeList().add("error"); + + addKeyPressListener(Key.ENTER, e -> save()); + + // wire action buttons to save, delete and reset + save.addClickListener(e -> save()); + delete.addClickListener(e -> delete()); + cancel.addClickListener(e -> editEmployee(employee)); + setVisible(false); + } + + void delete() { + repository.delete(employee); + changeHandler.onChange(); + } + + void save() { + repository.save(employee); + changeHandler.onChange(); + } + + public interface ChangeHandler { + void onChange(); + } + + public final void editEmployee(Employee c) { + if (c == null) { + setVisible(false); + return; + } + final boolean persisted = c.getId() != null; + if (persisted) { + // Find fresh entity for editing + employee = repository.findById(c.getId()).get(); + } else { + employee = c; + } + cancel.setVisible(persisted); + + // Bind employee properties to similarly named fields + // Could also use annotation or "manual binding" or programmatically + // moving values from fields to entities before saving + binder.setBean(employee); + + setVisible(true); + + // Focus first name initially + firstName.focus(); + } + + public void setChangeHandler(ChangeHandler h) { + // ChangeHandler is notified when either save or delete + // is clicked + changeHandler = h; + } + +} diff --git a/vaadin-spring/src/main/java/com/baeldung/EmployeeRepository.java b/vaadin-spring/src/main/java/com/baeldung/EmployeeRepository.java new file mode 100644 index 0000000000..66b5f329d7 --- /dev/null +++ b/vaadin-spring/src/main/java/com/baeldung/EmployeeRepository.java @@ -0,0 +1,10 @@ +package com.baeldung; + +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface EmployeeRepository extends JpaRepository { + + List findByLastNameStartsWithIgnoreCase(String lastName); +} diff --git a/vaadin-spring/src/main/java/com/baeldung/MainView.java b/vaadin-spring/src/main/java/com/baeldung/MainView.java new file mode 100644 index 0000000000..0233f52781 --- /dev/null +++ b/vaadin-spring/src/main/java/com/baeldung/MainView.java @@ -0,0 +1,78 @@ +package com.baeldung; + +import com.vaadin.flow.component.button.Button; +import com.vaadin.flow.component.grid.Grid; +import com.vaadin.flow.component.icon.VaadinIcon; +import com.vaadin.flow.component.orderedlayout.HorizontalLayout; +import com.vaadin.flow.component.orderedlayout.VerticalLayout; +import com.vaadin.flow.component.textfield.TextField; +import com.vaadin.flow.data.value.ValueChangeMode; +import com.vaadin.flow.router.Route; +import com.vaadin.flow.spring.annotation.UIScope; +import org.springframework.util.StringUtils; + +@Route +public class MainView extends VerticalLayout { + + private final EmployeeRepository employeeRepository; + + private final EmployeeEditor editor; + + final Grid grid; + + final TextField filter; + + private final Button addNewBtn; + + public MainView(EmployeeRepository repo, EmployeeEditor editor) { + this.employeeRepository = repo; + this.editor = editor; + this.grid = new Grid<>(Employee.class); + this.filter = new TextField(); + this.addNewBtn = new Button("New employee", VaadinIcon.PLUS.create()); + + // build layout + HorizontalLayout actions = new HorizontalLayout(filter, addNewBtn); + add(actions, grid, editor); + + grid.setHeight("200px"); + grid.setColumns("id", "firstName", "lastName"); + grid.getColumnByKey("id").setWidth("50px").setFlexGrow(0); + + filter.setPlaceholder("Filter by last name"); + + // Hook logic to components + + // Replace listing with filtered content when user changes filter + filter.setValueChangeMode(ValueChangeMode.EAGER); + filter.addValueChangeListener(e -> listEmployees(e.getValue())); + + // Connect selected Employee to editor or hide if none is selected + grid.asSingleSelect().addValueChangeListener(e -> { + editor.editEmployee(e.getValue()); + }); + + // Instantiate and edit new Employee the new button is clicked + addNewBtn.addClickListener(e -> editor.editEmployee(new Employee("", ""))); + + // Listen changes made by the editor, refresh data from backend + editor.setChangeHandler(() -> { + editor.setVisible(false); + listEmployees(filter.getValue()); + }); + + // Initialize listing + listEmployees(null); + } + + // tag::listEmployees[] + void listEmployees(String filterText) { + if (StringUtils.isEmpty(filterText)) { + grid.setItems(employeeRepository.findAll()); + } else { + grid.setItems(employeeRepository.findByLastNameStartsWithIgnoreCase(filterText)); + } + } + // end::listEmployees[] + +} From 35c92fae5f0519b1f077bca8cbf7dc11004c8b4f Mon Sep 17 00:00:00 2001 From: Ganesh Pagade Date: Mon, 23 Jul 2018 23:43:11 +0530 Subject: [PATCH 2/2] format --- .../java/com/baeldung/EmployeeEditor.java | 29 +------------------ .../java/com/baeldung/EmployeeRepository.java | 4 +-- .../src/main/java/com/baeldung/MainView.java | 15 ++-------- 3 files changed, 5 insertions(+), 43 deletions(-) diff --git a/vaadin-spring/src/main/java/com/baeldung/EmployeeEditor.java b/vaadin-spring/src/main/java/com/baeldung/EmployeeEditor.java index c10c88978b..ee312786d1 100644 --- a/vaadin-spring/src/main/java/com/baeldung/EmployeeEditor.java +++ b/vaadin-spring/src/main/java/com/baeldung/EmployeeEditor.java @@ -12,31 +12,17 @@ import com.vaadin.flow.spring.annotation.SpringComponent; import com.vaadin.flow.spring.annotation.UIScope; import org.springframework.beans.factory.annotation.Autowired; -/** - * A simple example to introduce building forms. As your real application is probably much - * more complicated than this example, you could re-use this form in multiple places. This - * example component is only used in MainView. - *

- * In a real world application you'll most likely using a common super class for all your - * forms - less code, better UX. - */ @SpringComponent @UIScope public class EmployeeEditor extends VerticalLayout implements KeyNotifier { private final EmployeeRepository repository; - /** - * The currently edited employee - */ private Employee employee; - /* Fields to edit properties in Employee entity */ TextField firstName = new TextField("First name"); TextField lastName = new TextField("Last name"); - /* Action buttons */ - // TODO why more code? Button save = new Button("Save", VaadinIcon.CHECK.create()); Button cancel = new Button("Cancel"); Button delete = new Button("Delete", VaadinIcon.TRASH.create()); @@ -51,10 +37,8 @@ public class EmployeeEditor extends VerticalLayout implements KeyNotifier { add(firstName, lastName, actions); - // bind using naming convention binder.bindInstanceFields(this); - // Configure and style components setSpacing(true); save.getElement().getThemeList().add("primary"); @@ -62,7 +46,6 @@ public class EmployeeEditor extends VerticalLayout implements KeyNotifier { addKeyPressListener(Key.ENTER, e -> save()); - // wire action buttons to save, delete and reset save.addClickListener(e -> save()); delete.addClickListener(e -> delete()); cancel.addClickListener(e -> editEmployee(employee)); @@ -90,28 +73,18 @@ public class EmployeeEditor extends VerticalLayout implements KeyNotifier { } final boolean persisted = c.getId() != null; if (persisted) { - // Find fresh entity for editing employee = repository.findById(c.getId()).get(); } else { employee = c; } + cancel.setVisible(persisted); - - // Bind employee properties to similarly named fields - // Could also use annotation or "manual binding" or programmatically - // moving values from fields to entities before saving binder.setBean(employee); - setVisible(true); - - // Focus first name initially firstName.focus(); } public void setChangeHandler(ChangeHandler h) { - // ChangeHandler is notified when either save or delete - // is clicked changeHandler = h; } - } diff --git a/vaadin-spring/src/main/java/com/baeldung/EmployeeRepository.java b/vaadin-spring/src/main/java/com/baeldung/EmployeeRepository.java index 66b5f329d7..044160da78 100644 --- a/vaadin-spring/src/main/java/com/baeldung/EmployeeRepository.java +++ b/vaadin-spring/src/main/java/com/baeldung/EmployeeRepository.java @@ -1,9 +1,9 @@ package com.baeldung; -import org.springframework.data.jpa.repository.JpaRepository; - import java.util.List; +import org.springframework.data.jpa.repository.JpaRepository; + public interface EmployeeRepository extends JpaRepository { List findByLastNameStartsWithIgnoreCase(String lastName); diff --git a/vaadin-spring/src/main/java/com/baeldung/MainView.java b/vaadin-spring/src/main/java/com/baeldung/MainView.java index 0233f52781..6d4c0aaa88 100644 --- a/vaadin-spring/src/main/java/com/baeldung/MainView.java +++ b/vaadin-spring/src/main/java/com/baeldung/MainView.java @@ -1,5 +1,7 @@ package com.baeldung; +import org.springframework.util.StringUtils; + import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.grid.Grid; import com.vaadin.flow.component.icon.VaadinIcon; @@ -8,8 +10,6 @@ import com.vaadin.flow.component.orderedlayout.VerticalLayout; import com.vaadin.flow.component.textfield.TextField; import com.vaadin.flow.data.value.ValueChangeMode; import com.vaadin.flow.router.Route; -import com.vaadin.flow.spring.annotation.UIScope; -import org.springframework.util.StringUtils; @Route public class MainView extends VerticalLayout { @@ -31,7 +31,6 @@ public class MainView extends VerticalLayout { this.filter = new TextField(); this.addNewBtn = new Button("New employee", VaadinIcon.PLUS.create()); - // build layout HorizontalLayout actions = new HorizontalLayout(filter, addNewBtn); add(actions, grid, editor); @@ -41,31 +40,23 @@ public class MainView extends VerticalLayout { filter.setPlaceholder("Filter by last name"); - // Hook logic to components - - // Replace listing with filtered content when user changes filter filter.setValueChangeMode(ValueChangeMode.EAGER); filter.addValueChangeListener(e -> listEmployees(e.getValue())); - // Connect selected Employee to editor or hide if none is selected grid.asSingleSelect().addValueChangeListener(e -> { editor.editEmployee(e.getValue()); }); - // Instantiate and edit new Employee the new button is clicked addNewBtn.addClickListener(e -> editor.editEmployee(new Employee("", ""))); - // Listen changes made by the editor, refresh data from backend editor.setChangeHandler(() -> { editor.setVisible(false); listEmployees(filter.getValue()); }); - // Initialize listing listEmployees(null); } - // tag::listEmployees[] void listEmployees(String filterText) { if (StringUtils.isEmpty(filterText)) { grid.setItems(employeeRepository.findAll()); @@ -73,6 +64,4 @@ public class MainView extends VerticalLayout { grid.setItems(employeeRepository.findByLastNameStartsWithIgnoreCase(filterText)); } } - // end::listEmployees[] - }