Skip to content

Commit 381908a

Browse files
committed
BAEL-8841 Merge vaadin and vaadin-spring
- Moved vaadin-spring code to vaadin module
1 parent 2449866 commit 381908a

8 files changed

Lines changed: 290 additions & 19 deletions

File tree

‎pom.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@
579579
<module>testing-modules/mockserver</module>
580580
<module>testing-modules/test-containers</module>
581581
<module>undertow</module>
582+
<module>vaadin</module>
582583
<module>vertx-and-rxjava</module>
583584
<module>saas</module>
584585
<module>deeplearning4j</module>
@@ -1126,6 +1127,7 @@
11261127
<module>testing-modules/mockserver</module>
11271128
<module>testing-modules/test-containers</module>
11281129
<module>undertow</module>
1130+
<module>vaadin</module>
11291131
<module>vertx-and-rxjava</module>
11301132
<module>saas</module>
11311133
<module>deeplearning4j</module>

‎vaadin/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
## Relevant articles:
22

33
- [Introduction to Vaadin](http://www.baeldung.com/vaadin)
4+
- [Sample Application with Spring Boot and Vaadin](https://www.baeldung.com/spring-boot-vaadin)

‎vaadin/pom.xml‎

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,30 @@
88
<packaging>war</packaging>
99
<version>1.0-SNAPSHOT</version>
1010
<name>Vaadin</name>
11-
<prerequisites>
12-
<maven>3</maven>
13-
</prerequisites>
14-
11+
1512
<parent>
13+
<artifactId>parent-boot-2</artifactId>
1614
<groupId>com.baeldung</groupId>
17-
<artifactId>parent-modules</artifactId>
18-
<version>1.0.0-SNAPSHOT</version>
15+
<version>0.0.1-SNAPSHOT</version>
16+
<relativePath>../parent-boot-2</relativePath>
1917
</parent>
2018

19+
<dependencyManagement>
20+
<dependencies>
21+
<dependency>
22+
<groupId>com.vaadin</groupId>
23+
<artifactId>vaadin-bom</artifactId>
24+
<version>${vaadin.version}</version>
25+
<type>pom</type>
26+
<scope>import</scope>
27+
</dependency>
28+
</dependencies>
29+
</dependencyManagement>
30+
2131
<dependencies>
2232
<dependency>
2333
<groupId>javax.servlet</groupId>
2434
<artifactId>javax.servlet-api</artifactId>
25-
<version>${servlet.version}</version>
2635
<scope>provided</scope>
2736
</dependency>
2837
<dependency>
@@ -41,6 +50,25 @@
4150
<groupId>com.vaadin</groupId>
4251
<artifactId>vaadin-themes</artifactId>
4352
</dependency>
53+
54+
<dependency>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-starter-data-jpa</artifactId>
57+
</dependency>
58+
<dependency>
59+
<groupId>com.vaadin</groupId>
60+
<artifactId>vaadin-spring-boot-starter</artifactId>
61+
<version>${vaadin-spring-boot-starter.version}</version>
62+
</dependency>
63+
<dependency>
64+
<groupId>com.h2database</groupId>
65+
<artifactId>h2</artifactId>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.springframework.boot</groupId>
69+
<artifactId>spring-boot-starter-test</artifactId>
70+
<scope>test</scope>
71+
</dependency>
4472
</dependencies>
4573

4674
<build>
@@ -97,21 +125,13 @@
97125
<scanIntervalSeconds>2</scanIntervalSeconds>
98126
</configuration>
99127
</plugin>
128+
<plugin>
129+
<groupId>org.springframework.boot</groupId>
130+
<artifactId>spring-boot-maven-plugin</artifactId>
131+
</plugin>
100132
</plugins>
101133
</build>
102134

103-
<dependencyManagement>
104-
<dependencies>
105-
<dependency>
106-
<groupId>com.vaadin</groupId>
107-
<artifactId>vaadin-bom</artifactId>
108-
<version>${vaadin.version}</version>
109-
<type>pom</type>
110-
<scope>import</scope>
111-
</dependency>
112-
</dependencies>
113-
</dependencyManagement>
114-
115135
<repositories>
116136
<repository>
117137
<id>vaadin-addons</id>
@@ -166,6 +186,7 @@
166186
<servlet.version>3.0.1</servlet.version>
167187
<vaadin.version>7.7.10</vaadin.version>
168188
<vaadin.plugin.version>8.0.6</vaadin.plugin.version>
189+
<vaadin-spring-boot-starter.version>10.0.1</vaadin-spring-boot-starter.version>
169190
<jetty.plugin.version>9.3.9.v20160517</jetty.plugin.version>
170191
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
171192
<maven.compiler.source>1.8</maven.compiler.source>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import org.springframework.boot.CommandLineRunner;
7+
import org.springframework.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
import org.springframework.context.annotation.Bean;
10+
11+
@SpringBootApplication
12+
public class Application {
13+
14+
private static final Logger log = LoggerFactory.getLogger(Application.class);
15+
16+
public static void main(String[] args) {
17+
SpringApplication.run(Application.class);
18+
}
19+
20+
@Bean
21+
public CommandLineRunner loadData(EmployeeRepository repository) {
22+
return (args) -> {
23+
repository.save(new Employee("Bill", "Gates"));
24+
repository.save(new Employee("Mark", "Zuckerberg"));
25+
repository.save(new Employee("Sundar", "Pichai"));
26+
repository.save(new Employee("Jeff", "Bezos"));
27+
};
28+
}
29+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.Id;
6+
7+
@Entity
8+
public class Employee {
9+
10+
@Id
11+
@GeneratedValue
12+
private Long id;
13+
14+
private String firstName;
15+
16+
private String lastName;
17+
18+
protected Employee() {
19+
}
20+
21+
public Employee(String firstName, String lastName) {
22+
this.firstName = firstName;
23+
this.lastName = lastName;
24+
}
25+
26+
public Long getId() {
27+
return id;
28+
}
29+
30+
public String getFirstName() {
31+
return firstName;
32+
}
33+
34+
public void setFirstName(String firstName) {
35+
this.firstName = firstName;
36+
}
37+
38+
public String getLastName() {
39+
return lastName;
40+
}
41+
42+
public void setLastName(String lastName) {
43+
this.lastName = lastName;
44+
}
45+
46+
@Override
47+
public String toString() {
48+
return String.format("Employee[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
49+
}
50+
51+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung;
2+
3+
import java.util.List;
4+
5+
import org.springframework.data.jpa.repository.JpaRepository;
6+
7+
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
8+
9+
List<Employee> findByLastNameStartsWithIgnoreCase(String lastName);
10+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.baeldung;
2+
3+
import org.springframework.util.StringUtils;
4+
5+
import com.vaadin.flow.component.button.Button;
6+
import com.vaadin.flow.component.grid.Grid;
7+
import com.vaadin.flow.component.icon.VaadinIcon;
8+
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
9+
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
10+
import com.vaadin.flow.component.textfield.TextField;
11+
import com.vaadin.flow.data.value.ValueChangeMode;
12+
import com.vaadin.flow.router.Route;
13+
14+
@Route
15+
public class MainView extends VerticalLayout {
16+
17+
private final EmployeeRepository employeeRepository;
18+
19+
private final EmployeeEditor editor;
20+
21+
final Grid<Employee> grid;
22+
23+
final TextField filter;
24+
25+
private final Button addNewBtn;
26+
27+
public MainView(EmployeeRepository repo, EmployeeEditor editor) {
28+
this.employeeRepository = repo;
29+
this.editor = editor;
30+
this.grid = new Grid<>(Employee.class);
31+
this.filter = new TextField();
32+
this.addNewBtn = new Button("New employee", VaadinIcon.PLUS.create());
33+
34+
HorizontalLayout actions = new HorizontalLayout(filter, addNewBtn);
35+
add(actions, grid, editor);
36+
37+
grid.setHeight("200px");
38+
grid.setColumns("id", "firstName", "lastName");
39+
grid.getColumnByKey("id").setWidth("50px").setFlexGrow(0);
40+
41+
filter.setPlaceholder("Filter by last name");
42+
43+
filter.setValueChangeMode(ValueChangeMode.EAGER);
44+
filter.addValueChangeListener(e -> listEmployees(e.getValue()));
45+
46+
grid.asSingleSelect().addValueChangeListener(e -> {
47+
editor.editEmployee(e.getValue());
48+
});
49+
50+
addNewBtn.addClickListener(e -> editor.editEmployee(new Employee("", "")));
51+
52+
editor.setChangeHandler(() -> {
53+
editor.setVisible(false);
54+
listEmployees(filter.getValue());
55+
});
56+
57+
listEmployees(null);
58+
}
59+
60+
void listEmployees(String filterText) {
61+
if (StringUtils.isEmpty(filterText)) {
62+
grid.setItems(employeeRepository.findAll());
63+
} else {
64+
grid.setItems(employeeRepository.findByLastNameStartsWithIgnoreCase(filterText));
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)