From 6033e13c20b3d6e8b6f5add0f786d5ab2e1bb3fe Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Sun, 18 Sep 2016 17:35:34 +0300 Subject: [PATCH] Make initially disabled grid work when enabled (#20240) Adds API to allow grid to ask its data source if it is waiting for data. The previous tracking inside grid was not always correct as it relied on dataAvailable being called. Change-Id: I0bdb448d9b720155940b0834e118f0eca27a3bfc --- .../connectors/RpcDataSourceConnector.java | 5 ++ .../client/data/AbstractRemoteDataSource.java | 16 ++++- .../com/vaadin/client/data/DataSource.java | 10 ++++ .../grid/datasources/ListDataSource.java | 5 ++ .../java/com/vaadin/client/widgets/Grid.java | 20 ++----- .../grid/InitiallyDisabledGrid.java | 58 +++++++------------ .../grid/GridCellFocusOnResetSizeWidget.java | 5 ++ .../GridClientColumnRendererConnector.java | 12 +++- .../grid/InitiallyDisabledGridTest.java | 13 +++++ 9 files changed, 88 insertions(+), 56 deletions(-) diff --git a/client/src/main/java/com/vaadin/client/connectors/RpcDataSourceConnector.java b/client/src/main/java/com/vaadin/client/connectors/RpcDataSourceConnector.java index a9837fa06b..10466d0641 100644 --- a/client/src/main/java/com/vaadin/client/connectors/RpcDataSourceConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/RpcDataSourceConnector.java @@ -240,6 +240,11 @@ public class RpcDataSourceConnector extends AbstractExtensionConnector { droppedRowKeys.set(droppedRowKeys.length(), getRowKey(row)); } } + + @Override + protected boolean canFetchData() { + return isEnabled(); + } } private final RpcDataSource dataSource = new RpcDataSource(); diff --git a/client/src/main/java/com/vaadin/client/data/AbstractRemoteDataSource.java b/client/src/main/java/com/vaadin/client/data/AbstractRemoteDataSource.java index 34342ebce9..4a9ced7308 100644 --- a/client/src/main/java/com/vaadin/client/data/AbstractRemoteDataSource.java +++ b/client/src/main/java/com/vaadin/client/data/AbstractRemoteDataSource.java @@ -308,6 +308,7 @@ public abstract class AbstractRemoteDataSource implements DataSource { * @return true if waiting for data; otherwise * false */ + @Override public boolean isWaitingForData() { return currentRequestCallback != null; } @@ -366,7 +367,7 @@ public abstract class AbstractRemoteDataSource implements DataSource { } private void handleMissingRows(Range range) { - if (range.isEmpty()) { + if (range.isEmpty() || !canFetchData()) { return; } currentRequestCallback = new RequestRowsCallback(this, range); @@ -776,4 +777,17 @@ public abstract class AbstractRemoteDataSource implements DataSource { protected boolean isPinned(T row) { return pinnedRows.containsKey(getRowKey(row)); } + + /** + * Checks if it is possible to currently fetch data from the remote data + * source. + * + * @return true if it is ok to try to fetch data, + * false if it is known that fetching data will fail + * and should not be tried right now. + * @since 7.7.2 + */ + protected boolean canFetchData() { + return true; + } } diff --git a/client/src/main/java/com/vaadin/client/data/DataSource.java b/client/src/main/java/com/vaadin/client/data/DataSource.java index 5e4cbc1b40..96837ffe1a 100644 --- a/client/src/main/java/com/vaadin/client/data/DataSource.java +++ b/client/src/main/java/com/vaadin/client/data/DataSource.java @@ -194,4 +194,14 @@ public interface DataSource { * means that the row is not currently in this data source's cache. */ public RowHandle getHandle(T row); + + /** + * Checks whether this data source is currently waiting for more rows to + * become available. + * + * @return true if waiting for data; otherwise + * false + * @since 7.7.2 + */ + public boolean isWaitingForData(); } diff --git a/client/src/main/java/com/vaadin/client/widget/grid/datasources/ListDataSource.java b/client/src/main/java/com/vaadin/client/widget/grid/datasources/ListDataSource.java index cff544ac94..f4207fad25 100644 --- a/client/src/main/java/com/vaadin/client/widget/grid/datasources/ListDataSource.java +++ b/client/src/main/java/com/vaadin/client/widget/grid/datasources/ListDataSource.java @@ -475,4 +475,9 @@ public class ListDataSource implements DataSource { } }; } + + @Override + public boolean isWaitingForData() { + return false; + } } diff --git a/client/src/main/java/com/vaadin/client/widgets/Grid.java b/client/src/main/java/com/vaadin/client/widgets/Grid.java index 8c08748213..2b193a55db 100644 --- a/client/src/main/java/com/vaadin/client/widgets/Grid.java +++ b/client/src/main/java/com/vaadin/client/widgets/Grid.java @@ -3188,7 +3188,7 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, Scheduler.get().scheduleDeferred(this); } } else if (currentDataAvailable.isEmpty() - && dataIsBeingFetched) { + && dataSource.isWaitingForData()) { // No data available yet but something is incoming soon Scheduler.get().scheduleDeferred(this); } else { @@ -3224,8 +3224,8 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, private void calculate() { isScheduled = false; rescheduleCount = 0; - assert !(currentDataAvailable.isEmpty() - && dataIsBeingFetched) : "Trying to calculate column widths without data while data is still being fetched."; + assert !(currentDataAvailable.isEmpty() && dataSource + .isWaitingForData()) : "Trying to calculate column widths without data while data is still being fetched."; if (columnsAreGuaranteedToBeWiderThanGrid()) { applyColumnWidths(); @@ -4086,8 +4086,6 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, private final Editor editor = GWT.create(Editor.class); - private boolean dataIsBeingFetched = false; - /** * The cell a click event originated from *

@@ -5955,7 +5953,6 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, public void onRowVisibilityChange( RowVisibilityChangeEvent event) { if (dataSource != null && dataSource.size() != 0) { - dataIsBeingFetched = true; dataSource.ensureAvailability( event.getFirstVisibleRow(), event.getVisibleRowCount()); @@ -5995,12 +5992,6 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, } }); - addDataAvailableHandler(new DataAvailableHandler() { - @Override - public void onDataAvailable(DataAvailableEvent event) { - dataIsBeingFetched = false; - } - }); } @Override @@ -6775,7 +6766,6 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, } if (newSize > 0) { - dataIsBeingFetched = true; Range visibleRowRange = escalator.getVisibleRowRange(); dataSource.ensureAvailability(visibleRowRange.getStart(), visibleRowRange.length()); @@ -7915,7 +7905,7 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, Scheduler.get().scheduleFinally(new ScheduledCommand() { @Override public void execute() { - if (!dataIsBeingFetched) { + if (!dataSource.isWaitingForData()) { handler.onDataAvailable( new DataAvailableEvent(currentDataAvailable)); } @@ -8236,7 +8226,7 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, @Override public boolean isWorkPending() { - return escalator.isWorkPending() || dataIsBeingFetched + return escalator.isWorkPending() || dataSource.isWaitingForData() || autoColumnWidthsRecalculator.isScheduled() || editor.isWorkPending(); } diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/InitiallyDisabledGrid.java b/uitest/src/main/java/com/vaadin/tests/components/grid/InitiallyDisabledGrid.java index 41f8eb0914..d91297d234 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/grid/InitiallyDisabledGrid.java +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/InitiallyDisabledGrid.java @@ -5,43 +5,16 @@ import java.util.Collection; import com.vaadin.data.util.BeanItemContainer; import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.data.bean.Person; import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.Button.ClickListener; import com.vaadin.ui.Grid; import com.vaadin.ui.UI; import com.vaadin.ui.VerticalLayout; public class InitiallyDisabledGrid extends UI { - public static class NotAPersonJustStringAndInt { - private String name; - - public NotAPersonJustStringAndInt() { - } - - public NotAPersonJustStringAndInt(String string, int i) { - name = string; - age = i; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - - private int age; - - public void setName(String name) { - this.name = name; - } - - public String getName() { - return name; - } - } - @Override protected void init(VaadinRequest request) { VerticalLayout layout = new VerticalLayout(); @@ -49,13 +22,18 @@ public class InitiallyDisabledGrid extends UI { layout.setSizeFull(); layout.setWidth("600px"); layout.setHeight("600px"); - final Grid g = createGrid(); - Button button = new Button("Sample button"); + final Grid grid = createGrid(); + Button button = new Button("Enable/Disable", new ClickListener() { + @Override + public void buttonClick(ClickEvent event) { + grid.setEnabled(!grid.isEnabled()); + } + }); layout.addComponent(button); VerticalLayout l = new VerticalLayout(); l.setSizeFull(); - l.addComponent(g); + l.addComponent(grid); layout.addComponent(l); layout.setExpandRatio(l, 1.0f); @@ -63,18 +41,22 @@ public class InitiallyDisabledGrid extends UI { private Grid createGrid() { // Have some data - Collection people = new ArrayList(); + Collection people = new ArrayList(); for (int i = 0; i < 100; i++) { - people.add(new NotAPersonJustStringAndInt("A " + i, i)); + Person person = new Person(); + person.setFirstName("First " + i); + person.setLastName("Last " + i); + people.add(person); + } // Have a container of some type to contain the data - BeanItemContainer container = new BeanItemContainer( - NotAPersonJustStringAndInt.class, people); + BeanItemContainer container = new BeanItemContainer( + Person.class, people); // Create a grid bound to the container Grid grid = new Grid(container); grid.setSizeFull(); - grid.setColumnOrder("name", "age"); + grid.setColumns("firstName", "lastName"); grid.setEnabled(false); diff --git a/uitest/src/main/java/com/vaadin/tests/widgetset/client/grid/GridCellFocusOnResetSizeWidget.java b/uitest/src/main/java/com/vaadin/tests/widgetset/client/grid/GridCellFocusOnResetSizeWidget.java index 1d71de65fe..06101e3ca5 100644 --- a/uitest/src/main/java/com/vaadin/tests/widgetset/client/grid/GridCellFocusOnResetSizeWidget.java +++ b/uitest/src/main/java/com/vaadin/tests/widgetset/client/grid/GridCellFocusOnResetSizeWidget.java @@ -78,6 +78,11 @@ public class GridCellFocusOnResetSizeWidget } handler.resetDataAndSize(size); } + + @Override + public boolean isWaitingForData() { + return false; + } } private class Col extends Grid.Column { diff --git a/uitest/src/main/java/com/vaadin/tests/widgetset/client/grid/GridClientColumnRendererConnector.java b/uitest/src/main/java/com/vaadin/tests/widgetset/client/grid/GridClientColumnRendererConnector.java index c5b08204ae..03a7193155 100644 --- a/uitest/src/main/java/com/vaadin/tests/widgetset/client/grid/GridClientColumnRendererConnector.java +++ b/uitest/src/main/java/com/vaadin/tests/widgetset/client/grid/GridClientColumnRendererConnector.java @@ -68,6 +68,7 @@ public class GridClientColumnRendererConnector private int numberOfRows; private DataChangeHandler dataChangeHandler; private int latency; + private Timer timer; public DelayedDataSource(DataSource ds, int latency) { this.ds = ds; @@ -77,7 +78,7 @@ public class GridClientColumnRendererConnector @Override public void ensureAvailability(final int firstRowIndex, final int numberOfRows) { - new Timer() { + timer = new Timer() { @Override public void run() { @@ -86,8 +87,10 @@ public class GridClientColumnRendererConnector dataChangeHandler.dataUpdated(firstRowIndex, numberOfRows); dataChangeHandler.dataAvailable(firstRowIndex, numberOfRows); + timer = null; } - }.schedule(latency); + }; + timer.schedule(latency); } @Override @@ -114,6 +117,11 @@ public class GridClientColumnRendererConnector // TODO Auto-generated method stub (henrik paul: 17.6.) return null; } + + @Override + public boolean isWaitingForData() { + return timer != null; + } } @Override diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/InitiallyDisabledGridTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/InitiallyDisabledGridTest.java index 1f9aad8eb2..4c7c9c119c 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/grid/InitiallyDisabledGridTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/InitiallyDisabledGridTest.java @@ -3,6 +3,7 @@ package com.vaadin.tests.components.grid; import org.junit.Assert; import org.junit.Test; +import com.vaadin.testbench.elements.ButtonElement; import com.vaadin.testbench.elements.GridElement; import com.vaadin.testbench.elements.GridElement.GridCellElement; import com.vaadin.tests.tb3.SingleBrowserTest; @@ -19,4 +20,16 @@ public class InitiallyDisabledGridTest extends SingleBrowserTest { Assert.assertTrue(col0.getSize().getWidth() > 250); Assert.assertTrue(col1.getSize().getWidth() > 250); } + + @Test + public void worksWhenEnabled() { + openTestURL(); + $(ButtonElement.class).first().click(); + + GridElement grid = $(GridElement.class).first(); + grid.scrollToRow(80); + GridCellElement col0 = grid.getCell(80, 0); + Assert.assertEquals("First 80", col0.getText()); + } + } -- 2.39.5