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
|
package com.vaadin.automatedtests.featurebrowser;
import java.io.Serializable;
import com.vaadin.data.Item;
import com.vaadin.data.Validator;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.BaseFieldFactory;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Button.ClickEvent;
/**
* This example demonstrates the most important features of the Form component:
* binding Form to a JavaBean so that form fields are automatically generated
* from the bean properties, creation of custom field editors using a
* FieldFactory, customizing the form without FieldFactory, buffering
* (commit/discard) and validation. Please note that the example is quite a bit
* more complex than real use, as it tries to demonstrate more features than
* needed in general case.
*/
@SuppressWarnings("serial")
public class FormExample extends CustomComponent {
static final String cities[] = { "Amsterdam", "Berlin", "Helsinki",
"Hong Kong", "London", "Luxemburg", "New York", "Oslo", "Paris",
"Rome", "Stockholm", "Tokyo", "Turku" };
/** Compose the demo. */
public FormExample() {
// Example data model
final Address dataModel = new Address();
Button peekDataModelState = new Button("Show the data model state",
new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
getWindow().showNotification(
dataModel.getAddressAsText());
}
});
// Example form
final AddressForm form = new AddressForm("Contact Information");
form.setDataSource(dataModel);
form
.setDescription("Please enter valid name and address. Fields marked with * are required. "
+ "If you try to commit with invalid values, a form error message is displayed. "
+ "(Address is required but failing to give it a value does not display an error.)");
// Layout the example
VerticalLayout root = new VerticalLayout();
root.setMargin(true);
root.setSpacing(true);
root.addComponent(form);
root.addComponent(peekDataModelState);
setCompositionRoot(root);
}
public static class AddressForm extends Form {
public AddressForm(String caption) {
setCaption(caption);
// Use custom field factory to modify the defaults on how the
// components are created
setFieldFactory(new MyFieldFactory());
// Add Commit and Discard controls to the form.
Button commit = new Button("Save", this, "commit");
Button discard = new Button("Reset", this, "discard");
HorizontalLayout footer = new HorizontalLayout();
footer.addComponent(commit);
footer.addComponent(discard);
setFooter(footer);
}
public void setDataSource(Address dataModel) {
// Set the form to edit given datamodel by converting pojo used as
// the datamodel to Item
setItemDataSource(new BeanItem(dataModel));
// Ensure that the fields are shown in correct order as the
// datamodel does not force any specific order.
setVisibleItemProperties(new String[] { "name", "streetAddress",
"postalCode", "city" });
// For examples sake, customize some of the form fields directly
// here. The alternative way is to use custom field factory as shown
// above.
getField("name").setRequired(true);
getField("name").setRequiredError("Name is missing");
getField("streetAddress").setRequired(true); // No error message
getField("postalCode").setRequired(true); // No error message
replaceWithSelect("city", cities, cities).setNewItemsAllowed(true);
// Set the form to act immediately on user input. This is
// automatically transports data between the client and the server
// to do server-side validation.
setImmediate(true);
// Enable buffering so that commit() must be called for the form
// before input is written to the data. (Form input is not written
// immediately through to the underlying object.)
setWriteThrough(false);
}
}
/**
* This is example on how to customize field creation. Any kind of field
* components could be created on the fly.
*/
static class MyFieldFactory extends BaseFieldFactory implements
Serializable {
@Override
public Field createField(Item item, Object propertyId,
Component uiContext) {
Field field = super.createField(item, propertyId, uiContext);
if ("postalCode".equals(propertyId)) {
((TextField) field).setColumns(5);
field.addValidator(new PostalCodeValidator());
}
return field;
}
}
/**
* This is an example of how to create a custom validator for automatic
* input validation.
*/
static class PostalCodeValidator implements Validator {
public boolean isValid(Object value) {
if (value == null || !(value instanceof String)) {
return false;
}
return ((String) value).matches("[0-9]{5}");
}
public void validate(Object value) throws InvalidValueException {
if (!isValid(value)) {
throw new InvalidValueException(
"Postal code must be a five digit number.");
}
}
}
/**
* Contact information data model created as POJO. Note that in many cases
* it would be a good idea to implement Item -interface for the datamodel to
* make it directly bindable to form (without BeanItem wrapper)
*/
public static class Address implements Serializable {
String name = "";
String streetAddress = "";
String postalCode = "";
String city;
public String getAddressAsText() {
return name + "\n" + streetAddress + "\n" + postalCode + " "
+ (city == null ? "" : city);
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setStreetAddress(String address) {
streetAddress = address;
}
public String getStreetAddress() {
return streetAddress;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getPostalCode() {
return postalCode;
}
public void setCity(String city) {
this.city = city;
}
public String getCity() {
return city;
}
}
}
|