]> source.dussan.org Git - vaadin-framework.git/commitdiff
Make initially disabled grid work when enabled (#20240)
authorArtur Signell <artur@vaadin.com>
Sun, 18 Sep 2016 14:35:34 +0000 (17:35 +0300)
committerArtur Signell <artur@vaadin.com>
Tue, 20 Sep 2016 06:48:52 +0000 (09:48 +0300)
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

client/src/main/java/com/vaadin/client/connectors/RpcDataSourceConnector.java
client/src/main/java/com/vaadin/client/data/AbstractRemoteDataSource.java
client/src/main/java/com/vaadin/client/data/DataSource.java
client/src/main/java/com/vaadin/client/widget/grid/datasources/ListDataSource.java
client/src/main/java/com/vaadin/client/widgets/Grid.java
uitest/src/main/java/com/vaadin/tests/components/grid/InitiallyDisabledGrid.java
uitest/src/main/java/com/vaadin/tests/widgetset/client/grid/GridCellFocusOnResetSizeWidget.java
uitest/src/main/java/com/vaadin/tests/widgetset/client/grid/GridClientColumnRendererConnector.java
uitest/src/test/java/com/vaadin/tests/components/grid/InitiallyDisabledGridTest.java

index a9837fa06b12f7ef69b8a92d2447c94d1a8d778b..10466d0641cb509bc9135285350d37cdace754bc 100644 (file)
@@ -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();
index 34342ebce9d50e0ee46b5c116c660c56ecb4b0b1..4a9ced730881cacec39f9a55b25b15a608891fa4 100644 (file)
@@ -308,6 +308,7 @@ public abstract class AbstractRemoteDataSource<T> implements DataSource<T> {
      * @return <code>true</code> if waiting for data; otherwise
      *         <code>false</code>
      */
+    @Override
     public boolean isWaitingForData() {
         return currentRequestCallback != null;
     }
@@ -366,7 +367,7 @@ public abstract class AbstractRemoteDataSource<T> implements DataSource<T> {
     }
 
     private void handleMissingRows(Range range) {
-        if (range.isEmpty()) {
+        if (range.isEmpty() || !canFetchData()) {
             return;
         }
         currentRequestCallback = new RequestRowsCallback<T>(this, range);
@@ -776,4 +777,17 @@ public abstract class AbstractRemoteDataSource<T> implements DataSource<T> {
     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 <code>true</code> if it is ok to try to fetch data,
+     *         <code>false</code> 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;
+    }
 }
index 5e4cbc1b40e1d0f5d0c02d2b118c3f90ef079b03..96837ffe1ad312a637312fae9fff5b456c440c2d 100644 (file)
@@ -194,4 +194,14 @@ public interface DataSource<T> {
      *        means that the row is not currently in this data source's cache.
      */
     public RowHandle<T> getHandle(T row);
+
+    /**
+     * Checks whether this data source is currently waiting for more rows to
+     * become available.
+     *
+     * @return <code>true</code> if waiting for data; otherwise
+     *         <code>false</code>
+     * @since 7.7.2
+     */
+    public boolean isWaitingForData();
 }
index cff544ac94fc97fb2d3ccfd3c9c48185dada01d6..f4207fad25835a966bae469266486b5d078b6516 100644 (file)
@@ -475,4 +475,9 @@ public class ListDataSource<T> implements DataSource<T> {
             }
         };
     }
+
+    @Override
+    public boolean isWaitingForData() {
+        return false;
+    }
 }
index 8c087482136830f4b210d5ca54b813fef2f95439..2b193a55db3e55661795056bc08f1d0e8c378193 100644 (file)
@@ -3188,7 +3188,7 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,
                         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<T> extends ResizeComposite implements HasSelectionHandlers<T>,
         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<T> extends ResizeComposite implements HasSelectionHandlers<T>,
 
     private final Editor<T> editor = GWT.create(Editor.class);
 
-    private boolean dataIsBeingFetched = false;
-
     /**
      * The cell a click event originated from
      * <p>
@@ -5955,7 +5953,6 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,
                     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<T> extends ResizeComposite implements HasSelectionHandlers<T>,
             }
         });
 
-        addDataAvailableHandler(new DataAvailableHandler() {
-            @Override
-            public void onDataAvailable(DataAvailableEvent event) {
-                dataIsBeingFetched = false;
-            }
-        });
     }
 
     @Override
@@ -6775,7 +6766,6 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,
                 }
 
                 if (newSize > 0) {
-                    dataIsBeingFetched = true;
                     Range visibleRowRange = escalator.getVisibleRowRange();
                     dataSource.ensureAvailability(visibleRowRange.getStart(),
                             visibleRowRange.length());
@@ -7915,7 +7905,7 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,
         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<T> extends ResizeComposite implements HasSelectionHandlers<T>,
 
     @Override
     public boolean isWorkPending() {
-        return escalator.isWorkPending() || dataIsBeingFetched
+        return escalator.isWorkPending() || dataSource.isWaitingForData()
                 || autoColumnWidthsRecalculator.isScheduled()
                 || editor.isWorkPending();
     }
index 41f8eb0914785794bb3701f17704be536b33059f..d91297d23466aef1b9634a670b5e963a54caa95f 100644 (file)
@@ -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<NotAPersonJustStringAndInt> people = new ArrayList<NotAPersonJustStringAndInt>();
+        Collection<Person> people = new ArrayList<Person>();
         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<NotAPersonJustStringAndInt> container = new BeanItemContainer<NotAPersonJustStringAndInt>(
-                NotAPersonJustStringAndInt.class, people);
+        BeanItemContainer<Person> container = new BeanItemContainer<Person>(
+                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);
 
index 1d71de65fe4be0f07ae66b910e3b7d9fc90cca6a..06101e3ca516fb4bf2dd584f7f39021dddb08774 100644 (file)
@@ -78,6 +78,11 @@ public class GridCellFocusOnResetSizeWidget
             }
             handler.resetDataAndSize(size);
         }
+
+        @Override
+        public boolean isWaitingForData() {
+            return false;
+        }
     }
 
     private class Col extends Grid.Column<String, String[]> {
index c5b08204aeb2582f2f1dd3882d1c351865d586f0..03a71931556420b194aafe42cb0638c8b1c3d475 100644 (file)
@@ -68,6 +68,7 @@ public class GridClientColumnRendererConnector
         private int numberOfRows;
         private DataChangeHandler dataChangeHandler;
         private int latency;
+        private Timer timer;
 
         public DelayedDataSource(DataSource<String> 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
index 1f9aad8eb273ee95b0f5a80b88bbdf57a03b97b6..4c7c9c119ca396f8b241218952e21f783606641e 100644 (file)
@@ -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());
+    }
+
 }