* that are not in the given collection will be cleaned up and
* {@link DataGenerator#destroyData(Object)} will be called for them.
*/
- protected class ActiveDataHandler
- implements DataGenerator<T> {
+ protected class ActiveDataHandler implements DataGenerator<T> {
/**
* Set of key strings for currently active data objects
private void attachDataProviderListener() {
dataProviderUpdateRegistration = getDataProvider()
.addDataProviderListener(event -> {
- getUI().access(() -> {
- if (event instanceof DataRefreshEvent) {
- T item = ((DataRefreshEvent<T>) event).getItem();
- getKeyMapper().refresh(item);
- generators.forEach(g -> g.refreshData(item));
- refresh(item);
- } else {
- hardReset();
- }
- });
+ if (event instanceof DataRefreshEvent) {
+ T item = ((DataRefreshEvent<T>) event).getItem();
+ getKeyMapper().refresh(item);
+ generators.forEach(g -> g.refreshData(item));
+ getUI().access(() -> refresh(item));
+ } else {
+ getUI().access(this::hardReset);
+ }
});
}
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
+import java.util.Optional;
import java.util.stream.Stream;
import com.vaadin.data.provider.AbstractDataProvider;
import com.vaadin.data.provider.Query;
import com.vaadin.server.VaadinRequest;
-import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.tests.components.AbstractTestUIWithLog;
import com.vaadin.ui.Button;
import com.vaadin.ui.Grid;
-public class DataProviderRefresh extends AbstractTestUI {
+public class DataProviderRefresh extends AbstractTestUIWithLog {
public static class Bean implements Serializable {
}
}
+ private Grid<Bean> grid = new Grid<>();
+
@Override
protected void setup(VaadinRequest request) {
- Grid<Bean> grid = new Grid<>();
List<Bean> arrayList = new ArrayList<>();
Bean foo = new Bean("Foo", 10);
arrayList.add(foo);
grid.setDataProvider(dataProvider);
grid.addColumn(Object::toString).setCaption("toString");
addComponent(grid);
- addComponent(new Button("Replace item",
- event -> dataProvider.refreshItem(new Bean("Bar", 10))));
+ addComponent(new Button("Replace item", event -> {
+ dataProvider.refreshItem(new Bean("Bar", 10));
+ logSelectedItem(grid.asSingleSelect().getValue());
+ }));
addComponent(new Button("Select old", event -> grid.select(foo)));
+ grid.asSingleSelect().addValueChangeListener(e -> {
+ logSelectedItem(e.getValue());
+ });
}
+ private void logSelectedItem(Bean bean) {
+ Optional.ofNullable(bean)
+ .map(b -> "Currently selected: " + b.toString())
+ .ifPresent(this::log);
+ }
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
+import static org.hamcrest.CoreMatchers.*;
+
import org.junit.Test;
import com.vaadin.testbench.elements.ButtonElement;
// Select item before replace
$(ButtonElement.class).caption("Select old").first().click();
assertTrue("Row should be selected", grid.getRow(0).isSelected());
+ assertThat(getLogRow(0), containsString("{ Foo, 10 }"));
$(ButtonElement.class).caption("Replace item").first().click();
assertTrue("Row should still be selected after item replace",
grid.getRow(0).isSelected());
assertEquals("Grid content was not updated.", "{ Bar, 10 }",
grid.getCell(0, 0).getText());
+ assertThat(getLogRow(0), containsString("{ Bar, 10 }"));
// Deselect row
grid.getCell(0, 0).click();