blob: 955a9c27725905fa3f345ca260882da8ec95a59f (
plain)
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
|
package com.vaadin.tests.components.textfield;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import com.vaadin.data.Item;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanContainer;
import com.vaadin.data.util.BeanItem;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.tests.components.TestBase;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalSplitPanel;
public class OutOfSyncIssueWithKeyboardShortcut extends TestBase {
public static class TestToppingsView extends CustomComponent {
public static class Topping {
private Long id;
private String name = "ham";
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
private Table table = new Table();
private Form form = new Form();
private Map<Long, Topping> toppings = new HashMap<Long, Topping>();
private long index = 1;
private String previousFragment = null;
public TestToppingsView() {
setSizeFull();
VerticalSplitPanel mainLayout = new VerticalSplitPanel();
mainLayout.setSplitPosition(30);
setCompositionRoot(mainLayout);
table.setSizeFull();
table.setImmediate(true);
table.setSelectable(true);
mainLayout.setFirstComponent(table);
mainLayout.setSecondComponent(form);
form.setImmediate(true);
// this is critical for the problem to occur
form.setBuffered(true);
HorizontalLayout footer = new HorizontalLayout();
footer.setSpacing(true);
form.setFooter(footer);
Button saveButton = new Button("Save");
footer.addComponent(saveButton);
// make saving the form the default action on Enter keypress
saveButton.setClickShortcut(KeyCode.ENTER);
table.addListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
Object value = event.getProperty().getValue();
if (value != null) {
String fragment = "edit/"
+ String.valueOf(value)
.replaceAll("[^0-9]", "");
if (!fragment.equals(previousFragment)) {
navigateTo(fragment);
}
}
}
});
saveButton.addListener(new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
form.commit();
Topping entity = getEntityForItem(form.getItemDataSource());
if (entity != null && entity.getId() == null) {
entity.setId(index++);
toppings.put(entity.getId(), entity);
}
refreshTable();
navigateTo(null);
}
});
// create new entity at the beginning
refreshTable();
navigateTo("new");
}
public void navigateTo(String requestedDataId) {
previousFragment = requestedDataId;
if ("new".equals(requestedDataId)) {
table.setValue(null);
form.setVisible(true);
setCurrentEntity(new Topping());
form.focus();
} else if (requestedDataId != null
&& requestedDataId.startsWith("edit/")) {
try {
Long id = Long.valueOf(requestedDataId.substring(5));
setCurrentEntity(getEntityForItem(table.getItem(id)));
form.focus();
} catch (NumberFormatException e) {
setCurrentEntity(null);
}
} else {
setCurrentEntity(null);
}
}
private void refreshTable() {
// refresh table
BeanContainer<Long, Topping> container = new BeanContainer<Long, Topping>(
Topping.class);
container.setBeanIdProperty("id");
for (Topping entity : toppings.values()) {
container.addBean(entity);
}
table.setContainerDataSource(container);
}
protected void setCurrentEntity(Topping entity) {
form.setVisible(entity != null);
if (entity != null) {
Item item = table.getItem(entity.getId());
if (item == null) {
item = new BeanItem<Topping>(entity);
}
form.setItemDataSource(item, Collections.singleton("name"));
} else {
form.setItemDataSource(null);
}
}
public Topping getEntityForItem(Item item) {
if (item != null) {
return ((BeanItem<Topping>) item).getBean();
} else {
return null;
}
}
}
@Override
protected void setup() {
TestToppingsView testToppingsView = new TestToppingsView();
addComponent(testToppingsView);
getLayout().setSizeFull();
getLayout().setExpandRatio(testToppingsView, 1);
}
@Override
protected String getDescription() {
return "Focus the text field and press ENTER.\n"
+ "Click on the table row to edit it, change the text to \"ahm\" using the keyboard and press ENTER again.\n"
+ "Then select the table row again.\n"
+ "This causes an Out of Sync error if the cursor position for the text field is sent too late to a component that is no longer in the layout.";
}
@Override
protected Integer getTicketNumber() {
return 6834;
}
}
|