1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
---
title: Creating A Master Details View For Editing Persons
order: 28
layout: page
---
[[creating-a-master-details-view-for-editing-persons]]
Creating a master details view for editing persons
--------------------------------------------------
[[set-up]]
Set-up
~~~~~~
In this tutorial we go through a standard use case where you have a bean
and a backend ready with create, read, update and delete capabilities on
that bean. You want to create a view where you can view all the beans
and edit them. This example is an address book where you edit person
information. The bean and the backend that we're going to use looks like
this:
[[person]]
Person
^^^^^^
[source,java]
....
public class Person {
private int id = -1;
private String firstName = "";
private String lastName = "";
private Address address = new Address();
private String phoneNumber = "";
private String email = "";
private Date dateOfBirth = null;
private String comments = "";
....
[[ibackend]]
IBackend
^^^^^^^^
[source,java]
....
public interface IBackend {
public List<Person> getPersons();
public void storePerson(Person person);
public void deletePerson(Person person);
}
....
The view will contain a table, with all the persons in it, and a form
for editing a single person. Additionally the table will have buttons
too add or remove persons and the form will have buttons to save and
discard changes. The UI wireframe looks like this:
image:img/master%20detail%20wireframe.jpg[Master detail UI wireframe]
[[building-the-basic-ui]]
Building the basic UI
~~~~~~~~~~~~~~~~~~~~~
We start off with creating a basic UIfor our application
[source,java]
....
public class AddressFormsUI extends UI {
@Override
protected void init(VaadinRequest request) {
VerticalLayout mainLayout = new VerticalLayout();
mainLayout.setSpacing(true);
mainLayout.setMargin(true);
mainLayout.addComponent(new Label("Hello Vaadiners!"));
setContent(mainLayout);
}
}
....
The first things that we want to add to it is the table and the form.
The table should be selectable and immediate so that we're able to pass
person objects from it to the form. I will create all the fields for our
person editor by hand to get more flexibility in how the fields will be
built and laid out. You could also let Vaadin `FieldGroup` take care of
creating the standard fields with the `buildAndBind` -methods if you don't
need to customize them.
[source,java]
....
package com.example.addressforms;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.Component;
import com.vaadin.ui.DateField;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.UI;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
public class AddressFormsUI extends UI {
private GridLayout form;
private Table table;
@Override
protected void init(VaadinRequest request) {
VerticalLayout mainLayout = new VerticalLayout();
mainLayout.setSpacing(true);
mainLayout.setMargin(true);
mainLayout.addComponent(buildTable());
mainLayout.addComponent(buildForm());
setContent(mainLayout);
}
private Component buildTable() {
table = new Table(null);
table.setWidth("500px");
table.setSelectable(true);
table.setImmediate(true);
return table;
}
private Component buildForm() {
form = new GridLayout(2, 3);
TextField firstName = new TextField("First name:");
TextField lastName = new TextField("Last name:");
TextField phoneNumber = new TextField("Phone Number:");
TextField email = new TextField("E-mail address:");
DateField dateOfBirth = new DateField("Date of birth:");
TextArea comments = new TextArea("Comments:");
form.addComponent(firstName);
form.addComponent(lastName);
form.addComponent(phoneNumber);
form.addComponent(email);
form.addComponent(dateOfBirth);
form.addComponent(comments);
return form;
}
}
....
image:img/table%20and%20form.png[Address form]
We also want the add, remove, save and discard buttons so let's create
them as well. I've positioned the add and remove above the table and
save and discard below the form.
[source,java]
....
private GridLayout form;
private HorizontalLayout tableControls;
private Table table;
private HorizontalLayout formControls;
@Override
protected void init(VaadinRequest request) {
VerticalLayout mainLayout = new VerticalLayout();
mainLayout.setSpacing(true);
mainLayout.setMargin(true);
mainLayout.addComponent(buildTableControls());
mainLayout.addComponent(buildTable());
mainLayout.addComponent(buildForm());
mainLayout.addComponent(buildFormControls());
setContent(mainLayout);
}
...
private Component buildTableControls() {
tableControls = new HorizontalLayout();
Button add = new Button("Add");
Button delete = new Button("Delete");
tableControls.addComponent(add);
tableControls.addComponent(delete);
return tableControls;
}
private Component buildFormControls() {
formControls = new HorizontalLayout();
Button save = new Button("Save");
Button discard = new Button("Discard");
formControls.addComponent(save);
formControls.addComponent(discard);
return formControls;
}
....
The buttons doesn't do anything yet but we have all the components that
we need in the view now.
image:img/buttons%20added.png[Address form with add, delete, save and discard buttons]
[[connecting-the-backend-to-the-view]]
Connecting the backend to the view
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The backend reference is store as a field so that all methods have
access to it.
[source,java]
....
...
private IBackend backend;
@Override
protected void init(VaadinRequest request) {
backend = new Backend();
...
....
Then we have to build a container for the table. I will do it in a
separate method from the table building so that it can be rebuilt for
refreshing the table after the initial rendering. We call this method
once in the initial rendering as well on every button click that
modifies the list of persons. A good choice of container in this case is
the `BeanItemContainer` where we specify to the table which columns we
want to show, and sort the table based on the name.
[source,java]
....
...
private Component buildTable() {
table = new Table(null);
table.setSelectable(true);
table.setImmediate(true);
updateTableData();
return table;
}
...
private void updateTableData() {
List<Person> persons = backend.getPersons();
BeanItemContainer<Person> container = new BeanItemContainer<Person>(
Person.class, persons);
table.setContainerDataSource(container);
table.setVisibleColumns(new String[] { "firstName", "lastName",
"phoneNumber", "email", "dateOfBirth" });
table.setColumnHeaders(new String[] { "First name", "Last name",
"Phone number", "E-mail address", "Date of birth" });
table.sort(new Object[] { "firstName", "lastName" }, new boolean[] {
true, true });
}
...
....
To get the data from the selected person's data into the fields, and the
changes back into the bean, we will use a FieldGroup. The `FieldGroup`
should be defined as class variable and it should bind the fields that
is initialized in `buildForm()`.
[source,java]
....
...
private FieldGroup fieldGroup = new FieldGroup();
...
private Component buildForm() {
form = new GridLayout(2, 3);
TextField firstName = new TextField("First name:");
TextField lastName = new TextField("Last name:");
TextField phoneNumber = new TextField("Phone Number:");
TextField email = new TextField("E-mail address:");
DateField dateOfBirth = new DateField("Date of birth:");
TextArea comments = new TextArea("Comments:");
fieldGroup.bind(firstName, "firstName");
fieldGroup.bind(lastName, "lastName");
fieldGroup.bind(phoneNumber, "phoneNumber");
fieldGroup.bind(email, "email");
fieldGroup.bind(dateOfBirth, "dateOfBirth");
fieldGroup.bind(comments, "comments");
form.addComponent(firstName);
form.addComponent(lastName);
form.addComponent(phoneNumber);
form.addComponent(email);
form.addComponent(dateOfBirth);
form.addComponent(comments);
return form;
}
....
Additionally the table requires a value change listener and the
currently selected person in the table has to be passed to the
`FieldGroup`.
[source,java]
....
private Component buildTable() {
...
table.addValueChangeListener(new ValueChangeListener() {
public void valueChange(ValueChangeEvent event) {
editPerson((Person) table.getValue());
}
});
...
}
...
private void editPerson(Person person) {
if (person == null) {
person = new Person();
}
BeanItem<Person> item = new BeanItem<Person>(person);
fieldGroup.setItemDataSource(item);
}
....
[[putting-the-buttons-in-use]]
Putting the buttons in use
~~~~~~~~~~~~~~~~~~~~~~~~~~
Last thing we have to do is implement all the buttons that we have in
the application. Add should create a new Person object and give it to
the form. Delete should tell the backend to remove the selected person
and update the table. Save should store the changes into the bean and
the bean into the backend and update the table. Discard should reset the
form.
[source,java]
....
private Component buildTableControls() {
tableControls = new HorizontalLayout();
Button add = new Button("Add", new ClickListener() {
public void buttonClick(ClickEvent event) {
editPerson(new Person());
}
});
Button delete = new Button("Delete", new ClickListener() {
public void buttonClick(ClickEvent event) {
backend.deletePerson((Person) table.getValue());
updateTableData();
}
});
tableControls.addComponent(add);
tableControls.addComponent(delete);
return tableControls;
}
private Component buildFormControls() {
formControls = new HorizontalLayout();
Button save = new Button("Save", new ClickListener() {
public void buttonClick(ClickEvent event) {
try {
fieldGroup.commit();
backend.storePerson(((BeanItem<Person>) fieldGroup
.getItemDataSource()).getBean());
updateTableData();
editPerson(null);
} catch (CommitException e) {
e.printStackTrace();
}
}
});
Button discard = new Button("Discard", new ClickListener() {
public void buttonClick(ClickEvent event) {
fieldGroup.discard();
}
});
formControls.addComponent(save);
formControls.addComponent(discard);
return formControls;
}
....
image:img/database%20connected.png[Form with database connected]
That's it! Now you have a full working CRUD view with total control over
the components and layouts. A little theming and layout adjustments and
it is ready for production.
You might have noticed that the person bean contains a reference to
another bean, a address, which is not editable here. The tutorial
link:CreatingACustomFieldForEditingTheAddressOfAPerson.asciidoc[Creating a custom field for editing the address of a person] goes
through on how to edit beans within beans with a `CustomField`, which can
be used directly as a field for the `FieldGroup`.
|