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
|
/*
* Copyright 2000-2014 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.tests.fieldgroup;
import java.util.Iterator;
import java.util.Map;
import com.vaadin.annotations.Theme;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.fieldgroup.BeanFieldGroup;
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.data.fieldgroup.PropertyId;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.data.validator.IntegerRangeValidator;
import com.vaadin.server.VaadinRequest;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.shared.util.SharedUtil;
import com.vaadin.tests.components.AbstractTestUIWithLog;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.Field;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Notification.Type;
import com.vaadin.ui.TextField;
import com.vaadin.ui.themes.ValoTheme;
@Theme("valo")
public abstract class AbstractBasicCrud extends AbstractTestUIWithLog {
protected AbstractForm form;
protected static String[] columns = new String[] { "firstName", "lastName",
"gender", "birthDate", "age", "alive", "address.streetAddress",
"address.postalCode", "address.city", "address.country" };
protected BeanItemContainer<ComplexPerson> container = ComplexPerson
.createContainer(100);;
{
container.addNestedContainerBean("address");
}
protected ComboBox formType;
/*
* (non-Javadoc)
*
* @see com.vaadin.tests.components.AbstractTestUI#setup(com.vaadin.server.
* VaadinRequest)
*/
@Override
protected void setup(VaadinRequest request) {
getLayout().setSizeFull();
getLayout().setSpacing(true);
getContent().setSizeFull();
form = new CustomForm();
formType = new ComboBox();
formType.setNullSelectionAllowed(false);
formType.setWidth("300px");
formType.addItem(form);
formType.setValue(form);
formType.addItem(new AutoGeneratedForm(TextField.class));
formType.addItem(new AutoGeneratedForm(Field.class));
Iterator<?> iterator = formType.getItemIds().iterator();
formType.setItemCaption(iterator.next(), "TextField based form");
formType.setItemCaption(iterator.next(),
"Auto generated form (TextFields)");
formType.setItemCaption(iterator.next(),
"Auto generated form (Any fields)");
formType.addValueChangeListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
AbstractForm oldForm = form;
form = (AbstractForm) formType.getValue();
replaceComponent(oldForm, form);
}
});
addComponent(formType);
}
public class CustomForm extends AbstractForm {
private TextField firstName = new TextField("First name");
private TextField lastName = new TextField("Last name");
private TextField gender = new TextField("Gender");
private TextField birthDate = new TextField("Birth date");
private TextField age = new TextField("Age");
private CheckBox alive = new CheckBox("Alive");
private Label errorLabel = new Label((String) null, ContentMode.HTML);
@PropertyId("address.streetAddress")
private TextField address_streetAddress = new TextField(
"Street address");
@PropertyId("address.postalCode")
private TextField address_postalCode = new TextField("Postal code");
@PropertyId("address.city")
private TextField address_city = new TextField("City");
@PropertyId("address.country")
private TextField address_country = new TextField("Country");
public CustomForm() {
fieldGroup.bindMemberFields(this);
address_postalCode.setNullRepresentation("");
gender.setNullRepresentation("");
age.setNullRepresentation("");
address_country.setNullRepresentation("");
// Last name editing is disabled through property readonly.
// Postal code editing is disabled through disabling field.
/*
* Currently only sets the initial state because of
* https://dev.vaadin.com/ticket/17847
*
* Must set lastName state initially as BeanFieldGroup can't tell it
* should be read-only before setting an item data source
*/
lastName.setReadOnly(true);
address_postalCode.setEnabled(false);
birthDate.setNullRepresentation("");
age.addValidator(new IntegerRangeValidator(
"Must be between 0 and 100", 0, 100));
setDefaultComponentAlignment(Alignment.MIDDLE_LEFT);
addComponents(firstName, lastName, gender, birthDate, age, alive,
address_streetAddress, address_postalCode, address_city,
address_country);
errorLabel.addStyleName(ValoTheme.LABEL_COLORED);
setRows(3);
addComponent(errorLabel, 0, 2, getColumns() - 1, 2);
HorizontalLayout hl = new HorizontalLayout(save, cancel);
hl.setSpacing(true);
addComponent(hl);
}
@Override
protected void handleCommitException(CommitException e) {
String message = "";
// Produce error message in the order in which the fields are in the
// layout
for (Component c : this) {
if (!(c instanceof Field)) {
continue;
}
Field<?> f = (Field<?>) c;
Map<Field<?>, InvalidValueException> exceptions = e
.getInvalidFields();
if (exceptions.containsKey(f)) {
message += f.getCaption() + ": "
+ exceptions.get(f).getLocalizedMessage()
+ "<br/>\n";
}
}
errorLabel.setValue(message);
}
@Override
protected void discard() {
super.discard();
errorLabel.setValue(null);
}
}
protected abstract void deselectAll();
public class AbstractForm extends GridLayout {
protected Button save = new Button("Save");
protected Button cancel = new Button("Cancel");
protected BeanFieldGroup<ComplexPerson> fieldGroup = new BeanFieldGroup<ComplexPerson>(
ComplexPerson.class) {
@Override
protected void configureField(com.vaadin.ui.Field<?> field) {
super.configureField(field);
if (field.getCaption().equals("Postal code")) {
// Last name editing is disabled through property.
// Postal code editing is disabled through field.
/*
* This is needed because of
* https://dev.vaadin.com/ticket/17847
*/
field.setEnabled(false);
}
};
};
public AbstractForm() {
super(5, 1);
setSpacing(true);
setId("form");
save.addClickListener(new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
try {
fieldGroup.commit();
log("Saved " + fieldGroup.getItemDataSource());
} catch (CommitException e) {
handleCommitException(e);
log("Commit failed: " + e.getMessage());
}
}
});
cancel.addClickListener(new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
log("Discarded " + fieldGroup.getItemDataSource());
discard();
}
});
}
protected void discard() {
deselectAll();
}
protected void handleCommitException(CommitException e) {
String message = "";
for (Object propertyId : e.getInvalidFields().keySet()) {
Field<?> f = e.getFieldGroup().getField(propertyId);
message += f.getCaption() + ": "
+ e.getInvalidFields().get(propertyId);
}
if (!message.isEmpty()) {
Notification.show(message, Type.ERROR_MESSAGE);
}
}
public void edit(BeanItem<ComplexPerson> item) {
fieldGroup.setItemDataSource(item);
}
}
public class AutoGeneratedForm extends AbstractForm {
public AutoGeneratedForm(Class<? extends Field> class1) {
for (String p : columns) {
Field f = fieldGroup.getFieldFactory().createField(
container.getType(p), class1);
f.setCaption(SharedUtil.propertyIdToHumanFriendly(p));
fieldGroup.bind(f, p);
addComponent(f);
}
HorizontalLayout hl = new HorizontalLayout(save, cancel);
hl.setSpacing(true);
addComponent(hl);
}
}
}
|