Browse Source

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
tags/7.7.2
Artur Signell 7 years ago
parent
commit
6033e13c20

+ 5
- 0
client/src/main/java/com/vaadin/client/connectors/RpcDataSourceConnector.java View 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();

+ 15
- 1
client/src/main/java/com/vaadin/client/data/AbstractRemoteDataSource.java View 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;
}
}

+ 10
- 0
client/src/main/java/com/vaadin/client/data/DataSource.java View 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();
}

+ 5
- 0
client/src/main/java/com/vaadin/client/widget/grid/datasources/ListDataSource.java View File

@@ -475,4 +475,9 @@ public class ListDataSource<T> implements DataSource<T> {
}
};
}

@Override
public boolean isWaitingForData() {
return false;
}
}

+ 5
- 15
client/src/main/java/com/vaadin/client/widgets/Grid.java View 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();
}

+ 20
- 38
uitest/src/main/java/com/vaadin/tests/components/grid/InitiallyDisabledGrid.java View 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);


+ 5
- 0
uitest/src/main/java/com/vaadin/tests/widgetset/client/grid/GridCellFocusOnResetSizeWidget.java View 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[]> {

+ 10
- 2
uitest/src/main/java/com/vaadin/tests/widgetset/client/grid/GridClientColumnRendererConnector.java View 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

+ 13
- 0
uitest/src/test/java/com/vaadin/tests/components/grid/InitiallyDisabledGridTest.java View 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());
}

}

Loading…
Cancel
Save