-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathContactForm.java
More file actions
116 lines (99 loc) · 3.96 KB
/
ContactForm.java
File metadata and controls
116 lines (99 loc) · 3.96 KB
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
package com.edurekademo.tutorial.addressbook;
import com.vaadin.event.ShortcutAction;
import com.edurekademo.tutorial.addressbook.backend.Contact;
import com.vaadin.ui.Button;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Notification.Type;
import com.vaadin.ui.themes.ValoTheme;
import com.vaadin.v7.data.fieldgroup.BeanFieldGroup;
import com.vaadin.v7.data.fieldgroup.FieldGroup;
import com.vaadin.v7.ui.DateField;
import com.vaadin.v7.ui.TextField;
/* Create custom UI Components.
*
* Create your own Vaadin components by inheritance and composition.
* This is a form component inherited from VerticalLayout. Use
* Use BeanFieldGroup to bind data fields from DTO to UI fields.
* Similarly named field by naming convention or customized
* with @PropertyId annotation.
*/
public class ContactForm extends FormLayout {
Button save = new Button("Save", this::save);
Button cancel = new Button("Cancel", this::cancel);
TextField firstName = new TextField("First name");
TextField lastName = new TextField("Last name");
TextField phone = new TextField("Phone");
TextField email = new TextField("Email");
DateField birthDate = new DateField("Birth date");
Contact contact;
// Easily bind forms to beans and manage validation and buffering
BeanFieldGroup<Contact> formFieldBindings;
public ContactForm() {
configureComponents();
buildLayout();
}
private void configureComponents() {
/*
* Highlight primary actions.
*
* With Vaadin built-in styles you can highlight the primary save button
* and give it a keyboard shortcut for a better UX.
*/
save.setStyleName(ValoTheme.BUTTON_PRIMARY);
save.setClickShortcut(ShortcutAction.KeyCode.ENTER);
setVisible(false);
}
private void buildLayout() {
setSizeUndefined();
setMargin(true);
HorizontalLayout actions = new HorizontalLayout(save, cancel);
actions.setSpacing(true);
addComponents(actions, firstName, lastName, phone, email, birthDate);
}
/*
* Use any JVM language.
*
* Vaadin supports all languages supported by Java Virtual Machine 1.6+.
* This allows you to program user interface in Java 8, Scala, Groovy or any
* other language you choose. The new languages give you very powerful tools
* for organizing your code as you choose. For example, you can implement
* the listener methods in your compositions or in separate controller
* classes and receive to various Vaadin component events, like button
* clicks. Or keep it simple and compact with Lambda expressions.
*/
public void save(Button.ClickEvent event) {
try {
// Commit the fields from UI to DAO
formFieldBindings.commit();
// Save DAO to backend with direct synchronous service API
getUI().service.save(contact);
String msg = String.format("Saved '%s %s'.", contact.getFirstName(),
contact.getLastName());
Notification.show(msg, Type.TRAY_NOTIFICATION);
getUI().refreshContacts();
} catch (FieldGroup.CommitException e) {
// Validation exceptions could be shown here
}
}
public void cancel(Button.ClickEvent event) {
// Place to call business logic.
Notification.show("Cancelled", Type.TRAY_NOTIFICATION);
getUI().contactList.select(null);
}
void edit(Contact contact) {
this.contact = contact;
if (contact != null) {
// Bind the properties of the contact POJO to fiels in this form
formFieldBindings = BeanFieldGroup.bindFieldsBuffered(contact,
this);
firstName.focus();
}
setVisible(contact != null);
}
@Override
public AddressbookUI getUI() {
return (AddressbookUI) super.getUI();
}
}