Skip to content

Commit 6e860e1

Browse files
committed
Version updates and using vaadin4spring event push to decouple MainUI from the PersonForm
1 parent 58ba9a9 commit 6e860e1

File tree

6 files changed

+116
-93
lines changed

6 files changed

+116
-93
lines changed

pom.xml

Lines changed: 44 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
<properties>
1313
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1414
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
15-
<version.vaadin>7.7.0</version.vaadin>
15+
<version.vaadin>7.7.3</version.vaadin>
1616
<vaadin.plugin.version>${version.vaadin}</vaadin.plugin.version>
1717
<start-class>crud.Application</start-class>
1818
</properties>
1919

2020
<parent>
2121
<groupId>org.springframework.boot</groupId>
2222
<artifactId>spring-boot-starter-parent</artifactId>
23-
<version>1.4.0.RELEASE</version>
23+
<version>1.4.1.RELEASE</version>
2424
</parent>
2525

2626
<dependencyManagement>
@@ -39,35 +39,53 @@
3939
<dependency>
4040
<groupId>com.vaadin</groupId>
4141
<artifactId>vaadin-spring-boot-starter</artifactId>
42-
<version>1.0.1</version>
42+
<version>1.0.2</version>
4343
</dependency>
4444
<dependency>
4545
<groupId>org.springframework.boot</groupId>
4646
<artifactId>spring-boot-starter-data-jpa</artifactId>
4747
</dependency>
48-
49-
<dependency>
50-
<groupId>org.hibernate</groupId>
51-
<artifactId>hibernate-validator</artifactId>
52-
</dependency>
53-
<dependency>
54-
<groupId>org.vaadin</groupId>
55-
<artifactId>viritin</artifactId>
56-
<version>1.56</version>
57-
</dependency>
58-
<dependency>
59-
<groupId>org.vaadin.teemu</groupId>
60-
<artifactId>switch</artifactId>
61-
<version>2.0.2</version>
62-
</dependency>
63-
48+
<!-- vaadin4spring https://github.com/peholmst/vaadin4spring
49+
contains lots of handy helpers for serious Spring + Vaadin
50+
applications. This example uses event bus to decouple the editor
51+
form from the MainUI class.
52+
-->
53+
<dependency>
54+
<groupId>org.vaadin.spring.extensions</groupId>
55+
<artifactId>vaadin-spring-ext-boot</artifactId>
56+
<version>0.0.7.RELEASE</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.vaadin.spring.addons</groupId>
60+
<artifactId>vaadin-spring-addon-eventbus</artifactId>
61+
<version>0.0.7.RELEASE</version>
62+
<type>jar</type>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.hibernate</groupId>
66+
<artifactId>hibernate-validator</artifactId>
67+
</dependency>
68+
<dependency>
69+
<groupId>org.vaadin</groupId>
70+
<artifactId>viritin</artifactId>
71+
<version>1.57</version>
72+
</dependency>
73+
<!-- Using Vaadin add-ons with client side extensions is easy, switch
74+
as an example in this project, see also vaadin-maven-plugin
75+
configuration.
76+
-->
77+
<dependency>
78+
<groupId>org.vaadin.teemu</groupId>
79+
<artifactId>switch</artifactId>
80+
<version>2.0.2</version>
81+
</dependency>
6482
<dependency>
6583
<groupId>com.h2database</groupId>
6684
<artifactId>h2</artifactId>
67-
<scope>runtime</scope>
85+
<scope>runtime</scope>
6886
</dependency>
6987

70-
<dependency>
88+
<dependency>
7189
<groupId>org.springframework.boot</groupId>
7290
<artifactId>spring-boot-starter-test</artifactId>
7391
<scope>test</scope>
@@ -88,6 +106,11 @@
88106
<target>1.8</target>
89107
</configuration>
90108
</plugin>
109+
<!-- Using client side Vaadin add-ons (Switch in this example)
110+
require a custom built "widgetset". Vaadin maven plugin does
111+
that automatically, here using a handy cloud service with CDN
112+
hosting.
113+
-->
91114
<plugin>
92115
<groupId>com.vaadin</groupId>
93116
<artifactId>vaadin-maven-plugin</artifactId>
@@ -108,41 +131,10 @@
108131
</build>
109132

110133
<repositories>
111-
<repository>
112-
<id>vaadin-snapshots</id>
113-
<url>http://oss.sonatype.org/content/repositories/vaadin-snapshots/</url>
114-
<snapshots>
115-
<enabled>true</enabled>
116-
</snapshots>
117-
</repository>
118-
<repository>
119-
<id>vaadin-prereleases</id>
120-
<name>Vaadin Pre-releases</name>
121-
<url>https://maven.vaadin.com/vaadin-prereleases</url>
122-
</repository>
123134
<repository>
124135
<id>vaadin-addons</id>
125136
<url>http://maven.vaadin.com/vaadin-addons</url>
126137
</repository>
127-
128-
<repository>
129-
<id>sonatype-snapshots</id>
130-
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
131-
<snapshots>
132-
<enabled>true</enabled>
133-
</snapshots>
134-
<releases>
135-
<enabled>false</enabled>
136-
</releases>
137-
</repository>
138-
139138
</repositories>
140-
<pluginRepositories>
141-
<pluginRepository>
142-
<id>vaadin-prereleases</id>
143-
<name>Vaadin Pre-releases</name>
144-
<url>https://maven.vaadin.com/vaadin-prereleases</url>
145-
</pluginRepository>
146-
</pluginRepositories>
147139

148140
</project>

src/main/java/crud/Application.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ public class Application {
99
public static void main(String[] args) {
1010
SpringApplication.run(Application.class, args);
1111
}
12-
12+
1313
}

src/main/java/crud/vaadin/MainUI.java

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
import org.springframework.beans.factory.annotation.Autowired;
1414
import org.springframework.data.domain.PageRequest;
1515
import org.springframework.data.domain.Sort;
16-
import org.vaadin.viritin.SortableLazyList;
16+
import org.vaadin.spring.events.EventBus;
17+
import org.vaadin.spring.events.EventBusListener;
1718
import org.vaadin.viritin.button.ConfirmButton;
1819
import org.vaadin.viritin.button.MButton;
20+
import org.vaadin.viritin.components.DisclosurePanel;
1921
import org.vaadin.viritin.fields.MTable;
2022
import org.vaadin.viritin.label.RichText;
2123
import org.vaadin.viritin.layouts.MHorizontalLayout;
@@ -24,9 +26,11 @@
2426
@Title("PhoneBook CRUD example")
2527
@Theme("valo")
2628
@SpringUI
27-
public class MainUI extends UI {
29+
public class MainUI extends UI implements EventBusListener<PersonModifiedEvent> {
2830

2931
PersonRepository repo;
32+
PersonForm personForm;
33+
EventBus.UIEventBus eventBus;
3034

3135
private MTable<Person> list = new MTable<>(Person.class)
3236
.withProperties("id", "name", "email")
@@ -39,23 +43,28 @@ public class MainUI extends UI {
3943
private Button delete = new ConfirmButton(FontAwesome.TRASH_O,
4044
"Are you sure you want to delete the entry?", this::remove);
4145

42-
4346
@Autowired
44-
public MainUI(PersonRepository r) {
47+
public MainUI(PersonRepository r, PersonForm f, EventBus.UIEventBus b) {
4548
this.repo = r;
49+
this.personForm = f;
50+
this.eventBus = b;
4651
}
4752

4853
@Override
4954
protected void init(VaadinRequest request) {
55+
DisclosurePanel aboutBox = new DisclosurePanel("Spring Boot JPA CRUD example with Vaadin UI", new RichText().withMarkDownResource("/welcome.md"));
5056
setContent(
5157
new MVerticalLayout(
52-
new RichText().withMarkDownResource("/welcome.md"),
58+
aboutBox,
5359
new MHorizontalLayout(addNew, edit, delete),
5460
list
5561
).expand(list)
5662
);
5763
listEntities();
5864
list.addMValueChangeListener(e -> adjustActionButtonState());
65+
66+
// Listen to change events emitted by PersonForm see onEvent method
67+
eventBus.subscribe(this);
5968
}
6069

6170
protected void adjustActionButtonState() {
@@ -70,12 +79,11 @@ private void listEntities() {
7079
// A dead simple in memory listing would be:
7180
// list.setBeans(repo.findAll());
7281

73-
// Lazy binding with SortableLazyList: memory and query efficient
74-
// connection from Vaadin Table to Spring Repository
75-
// Note that fetching strategies can be given to MTable constructor as well.
76-
// Use this approach if you expect you'll have lots of data in your
77-
// table.
78-
list.setBeans(new SortableLazyList<>(
82+
// Lazy binding for better optimized connection from the Vaadin Table to
83+
// Spring Repository. This approach uses less memory and database
84+
// resources. Use this approach if you expect you'll have lots of data
85+
// in your table. There are simpler APIs if you don't need sorting.
86+
list.lazyLoadFrom(
7987
// entity fetching strategy
8088
(firstRow, asc, sortProperty) -> repo.findAllBy(
8189
new PageRequest(
@@ -89,7 +97,7 @@ private void listEntities() {
8997
// count fetching strategy
9098
() -> (int) repo.count(),
9199
PAGESIZE
92-
));
100+
);
93101
adjustActionButtonState();
94102

95103
}
@@ -109,26 +117,14 @@ public void remove(ClickEvent e) {
109117
}
110118

111119
protected void edit(final Person phoneBookEntry) {
112-
PhoneBookEntryForm phoneBookEntryForm = new PhoneBookEntryForm(
113-
phoneBookEntry);
114-
phoneBookEntryForm.openInModalPopup();
115-
phoneBookEntryForm.setSavedHandler(this::saveEntry);
116-
phoneBookEntryForm.setResetHandler(this::resetEntry);
120+
personForm.setEntity(phoneBookEntry);
121+
personForm.openInModalPopup();
117122
}
118123

119-
public void saveEntry(Person entry) {
120-
repo.save(entry);
121-
listEntities();
122-
closeWindow();
123-
}
124-
125-
public void resetEntry(Person entry) {
124+
@Override
125+
public void onEvent(org.vaadin.spring.events.Event<PersonModifiedEvent> event) {
126126
listEntities();
127-
closeWindow();
128-
}
129-
130-
protected void closeWindow() {
131-
getWindows().stream().forEach(w -> removeWindow(w));
127+
personForm.closePopup();
132128
}
133129

134130
}

src/main/java/crud/vaadin/PhoneBookEntryForm.java renamed to src/main/java/crud/vaadin/PersonForm.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
package crud.vaadin;
22

3+
import com.vaadin.spring.annotation.SpringComponent;
4+
import com.vaadin.spring.annotation.UIScope;
35
import com.vaadin.ui.Component;
46
import com.vaadin.ui.DateField;
57
import com.vaadin.ui.TextField;
68
import crud.backend.Person;
9+
import crud.backend.PersonRepository;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.vaadin.spring.events.EventBus;
12+
import org.vaadin.spring.events.EventBus.UIEventBus;
713
import org.vaadin.teemu.switchui.Switch;
814
import org.vaadin.viritin.fields.MTextField;
915
import org.vaadin.viritin.form.AbstractForm;
1016
import org.vaadin.viritin.layouts.MFormLayout;
1117
import org.vaadin.viritin.layouts.MVerticalLayout;
1218

13-
public class PhoneBookEntryForm extends AbstractForm<Person> {
19+
@UIScope
20+
@SpringComponent
21+
public class PersonForm extends AbstractForm<Person> {
22+
23+
EventBus.UIEventBus eventBus;
24+
PersonRepository repo;
1425

1526
TextField name = new MTextField("Name");
1627
TextField email = new MTextField("Email");
@@ -20,9 +31,21 @@ public class PhoneBookEntryForm extends AbstractForm<Person> {
2031
// the awesome extendions by the community: http://vaadin.com/directory
2132
Switch colleague = new Switch("Colleague");
2233

23-
PhoneBookEntryForm(Person phoneBookEntry) {
34+
@Autowired
35+
PersonForm(PersonRepository r, EventBus.UIEventBus b) {
36+
this.repo = r;
37+
this.eventBus = b;
38+
39+
// On save & cancel, publish events that other parts of the UI can listen
40+
setSavedHandler(person -> {
41+
// persist changes
42+
repo.save(person);
43+
// send the event for other parts of the application
44+
eventBus.publish(this, new PersonModifiedEvent(person));
45+
});
46+
setResetHandler(p -> eventBus.publish(this, new PersonModifiedEvent(p)));
47+
2448
setSizeUndefined();
25-
setEntity(phoneBookEntry);
2649
}
2750

2851
@Override
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package crud.vaadin;
2+
3+
import crud.backend.Person;
4+
import java.io.Serializable;
5+
6+
public class PersonModifiedEvent implements Serializable {
7+
8+
private final Person person;
9+
10+
public PersonModifiedEvent(Person p) {
11+
this.person = p;
12+
}
13+
14+
public Person getPerson() {
15+
return person;
16+
}
17+
18+
}

src/main/resources/welcome.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
### Spring Boot JPA CRUD example with Vaadin UI
2-
31
This is a [simple CRUD app](https://github.com/mstahv/spring-data-vaadin-crud)
4-
built with Spring Boot + Vaadin. The artifact of Spring Bootprojects is by default
5-
a jar file, which may sound bit weird for Java web developers. How to deploy the
6-
jar file when your server expects war files? I already had an account and
7-
was logged into [IBM Bluemix](http://www.bluemix.net) with CLI tools, so deploying needed
8-
actually only this command (after building with the usual 'mvn install'):
2+
built with Spring Boot + Vaadin. It uses Spring Data JPA and in memory H2
3+
database by default. See the GitHub project page for more details.
94

10-
*cf push bootexample -p target/\*.jar*
115

0 commit comments

Comments
 (0)