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
|
package com.vaadin.tests.tickets;
import com.vaadin.data.Validator;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.MethodProperty;
import com.vaadin.data.validator.IntegerValidator;
import com.vaadin.server.LegacyApplication;
import com.vaadin.server.ThemeResource;
import com.vaadin.ui.AbstractComponent;
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.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.LegacyWindow;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
public class Ticket736 extends LegacyApplication {
Address address = new Address();
@Override
public void init() {
final LegacyWindow mainWin = new LegacyWindow("Test app for #736");
setMainWindow(mainWin);
setTheme("runo");
// Create form for editing address
final Form f = new Form();
f.setItemDataSource(new BeanItem<Address>(address, new String[] {
"name", "street", "zip", "city", "state", "country" }));
f.setCaption("Office address");
f.setIcon(new ThemeResource("../runo/icons/16/document.png"));
f.setDescription("Jep jpe, this is form description.");
mainWin.addComponent(f);
// Select to use buffered mode for editing to enable commit and discard
f.setBuffered(true);
Button commit = new Button("Commit", new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
f.commit();
}
});
Button discard = new Button("Discard", new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
f.discard();
}
});
HorizontalLayout ol = new HorizontalLayout();
ol.setHeight("3em");
ol.addComponent(commit);
ol.setComponentAlignment(commit, Alignment.TOP_RIGHT);
ol.addComponent(discard);
f.setFooter(ol);
// Add some validators for the form
f.getField("zip").addValidator(
new IntegerValidator("'{0}' is not a number"));
((AbstractComponent) f.getField("zip")).setDescription("Jepjep");
((AbstractComponent) f.getField("zip")).setIcon(new ThemeResource(
"../runo/icons/16/folder.png"));
f.getField("state").addValidator(new IsValidState());
f.getField("name").setRequired(true);
f.getField("street").setRequired(true);
f.getField("city").setRequired(true);
f.getField("zip").setRequired(true);
// Debug form properties
final VerticalLayout formPropertiesLayout = new VerticalLayout();
formPropertiesLayout.setMargin(true);
final Panel formProperties = new Panel("Form properties",
formPropertiesLayout);
formProperties.setWidth("200px");
final String[] visibleProps = { "required", "invalidAllowed",
"readOnly", "readThrough", "writeThrough", "invalidCommitted",
"validationVisible", "immediate" };
for (int i = 0; i < visibleProps.length; i++) {
CheckBox b = new CheckBox(visibleProps[i],
new MethodProperty<Boolean>(f, visibleProps[i]));
b.setImmediate(true);
formPropertiesLayout.addComponent(b);
}
mainWin.addComponent(formProperties);
// Debug the internal state of the address-object
mainWin.addComponent(new Button("Show state of the address object",
new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
mainWin.showNotification(address.toString());
}
}));
}
/** Address pojo. */
public static class Address {
String name = "";
String street = "";
String zip = "";
String city = "";
String state = "";
String country = "";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return name + "; " + street + "; " + city + " " + zip
+ (state != null ? " " + state : "") + " " + country;
}
}
/** Simple state validator */
static class IsValidState implements Validator {
@Override
public void validate(Object value) throws InvalidValueException {
// Empty and null are accepted values
if (value == null || "".equals("" + value)) {
return;
}
// Otherwise state must be two capital letter combo
if (value.toString().length() != 2
|| !value.toString().equals(("" + value).toUpperCase())) {
throw new InvalidValueException(
"State must be either two capital letter abreviation or left empty");
}
}
}
}
|