Skip to content
This repository has been archived by the owner on Dec 12, 2022. It is now read-only.

Update Grid to work with the new abstraction of HierarchicalArrayUpdater #275

Merged
merged 2 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 51 additions & 52 deletions src/main/java/com/vaadin/flow/component/grid/Grid.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import com.vaadin.flow.data.binder.PropertySet;
import com.vaadin.flow.data.event.SortEvent;
import com.vaadin.flow.data.event.SortEvent.SortNotifier;
import com.vaadin.flow.data.provider.ArrayUpdater;
import com.vaadin.flow.data.provider.ArrayUpdater.Update;
import com.vaadin.flow.data.provider.CompositeDataGenerator;
import com.vaadin.flow.data.provider.DataCommunicator;
Expand Down Expand Up @@ -156,26 +157,6 @@ public void enqueue(String name, Serializable... arguments) {
queue.add(() -> getElement().callFunction(name, arguments));
}

@Override
public void commit(int updateId, String parentKey, int levelSize) {
onlySupportedOnTreeGrid();
}

@Override
public void set(int start, List<JsonValue> items, String parentKey) {
onlySupportedOnTreeGrid();
}

@Override
public void clear(int start, int length, String parentKey) {
onlySupportedOnTreeGrid();
}

private void onlySupportedOnTreeGrid() {
throw new UnsupportedOperationException(
"This method can't be used for a Grid. Use TreeGrid instead.");
}

protected Element getElement() {
return data.getElement();
}
Expand Down Expand Up @@ -856,6 +837,38 @@ private void setDetailsVisibleFromClient(Set<T> items) {
}
}

private class GridArrayUpdaterImpl implements GridArrayUpdater {
private UpdateQueueData data;
private SerializableBiFunction<UpdateQueueData, Integer, UpdateQueue> updateQueueFactory;

public GridArrayUpdaterImpl(
SerializableBiFunction<UpdateQueueData, Integer, UpdateQueue> updateQueueFactory) {
this.updateQueueFactory = updateQueueFactory;
}

@Override
public UpdateQueue startUpdate(int sizeChange) {
return updateQueueFactory.apply(data, sizeChange);
}

@Override
public void initialize() {
initConnector();
updateSelectionModeOnClient();
}

@Override
public void setUpdateQueueData(UpdateQueueData data) {
this.data = data;
}

@Override
public UpdateQueueData getUpdateQueueData() {
return data;
}

}

private final GridArrayUpdater arrayUpdater;

private final CompositeDataGenerator<T> gridDataGenerator;
Expand Down Expand Up @@ -958,8 +971,11 @@ public Grid(Class<T> beanType) {
* uses to handle all data communication.
* @param <B>
* the data communicator builder type
* @param <U>
* the GridArrayUpdater type
*/
protected <B extends DataCommunicatorBuilder<T>> Grid(Class<T> beanType,
protected <U extends GridArrayUpdater, B extends DataCommunicatorBuilder<T, U>> Grid(
Class<T> beanType,
SerializableBiFunction<UpdateQueueData, Integer, UpdateQueue> updateQueueBuidler,
B dataCommunicatorBuilder) {
this(50, updateQueueBuidler, dataCommunicatorBuilder);
Expand Down Expand Up @@ -990,9 +1006,12 @@ protected <B extends DataCommunicatorBuilder<T>> Grid(Class<T> beanType,
* uses to handle all data communication.
* @param <B>
* the data communicator builder type
* @param <U>
* the GridArrayUpdater type
*
*/
protected <B extends DataCommunicatorBuilder<T>> Grid(int pageSize,
protected <U extends GridArrayUpdater, B extends DataCommunicatorBuilder<T, U>> Grid(
int pageSize,
SerializableBiFunction<UpdateQueueData, Integer, UpdateQueue> updateQueueBuidler,
B dataCommunicatorBuilder) {
Objects.requireNonNull(dataCommunicatorBuilder,
Expand All @@ -1006,7 +1025,8 @@ protected <B extends DataCommunicatorBuilder<T>> Grid(int pageSize,
gridDataGenerator.addDataGenerator(this::generateUniqueKeyData);

dataCommunicator = dataCommunicatorBuilder.build(getElement(),
gridDataGenerator, arrayUpdater, this::getUniqueKeyProvider);
gridDataGenerator, (U) arrayUpdater,
this::getUniqueKeyProvider);

detailsManager = new DetailsManager(this);
setPageSize(pageSize);
Expand Down Expand Up @@ -1039,8 +1059,12 @@ protected void initConnector() {
*
* @param <T>
* the grid bean type
*
* @param <U>
* the ArrayUpdater type
*/
protected static class DataCommunicatorBuilder<T> implements Serializable {
protected static class DataCommunicatorBuilder<T, U extends ArrayUpdater>
implements Serializable {

/**
* Build a new {@link DataCommunicator} object for the given Grid
Expand All @@ -1052,15 +1076,14 @@ protected static class DataCommunicatorBuilder<T> implements Serializable {
* the {@link CompositeDataGenerator} for the data
* communicator
* @param arrayUpdater
* the {@link GridArrayUpdater} for the data communicator
* the {@link ArrayUpdater} for the data communicator
* @param uniqueKeyProviderSupplier
* the unique key value provider supplier for the data
* communicator
* @return the build data communicator object
*/
protected DataCommunicator<T> build(Element element,
CompositeDataGenerator<T> dataGenerator,
GridArrayUpdater arrayUpdater,
CompositeDataGenerator<T> dataGenerator, U arrayUpdater,
SerializableSupplier<ValueProvider<T, String>> uniqueKeyProviderSupplier) {
return new DataCommunicator<>(dataGenerator, arrayUpdater,
data -> element.callFunction("$connector.updateData", data),
Expand All @@ -1070,31 +1093,7 @@ protected DataCommunicator<T> build(Element element,

protected GridArrayUpdater createDefaultArrayUpdater(
SerializableBiFunction<UpdateQueueData, Integer, UpdateQueue> updateQueueFactory) {
return new GridArrayUpdater() {

private UpdateQueueData data;

@Override
public UpdateQueue startUpdate(int sizeChange) {
return updateQueueFactory.apply(data, sizeChange);
}

@Override
public void initialize() {
initConnector();
updateSelectionModeOnClient();
}

@Override
public void setUpdateQueueData(UpdateQueueData data) {
this.data = data;
}

@Override
public UpdateQueueData getUpdateQueueData() {
return data;
}
};
return new GridArrayUpdaterImpl(updateQueueFactory);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.io.Serializable;

import com.vaadin.flow.component.treegrid.TreeGridArrayUpdater;
import com.vaadin.flow.data.provider.ArrayUpdater;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.function.SerializableSupplier;
Expand All @@ -30,7 +31,7 @@
public interface GridArrayUpdater extends ArrayUpdater {

/**
* Data object for {@link GridArrayUpdater}.
* Data object for {@link TreeGridArrayUpdater}.
*/
public static class UpdateQueueData implements Serializable {
private final Element element;
Expand Down
92 changes: 63 additions & 29 deletions src/main/java/com/vaadin/flow/component/treegrid/TreeGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@
import com.vaadin.flow.data.provider.DataCommunicator;
import com.vaadin.flow.data.provider.DataProvider;
import com.vaadin.flow.data.provider.hierarchy.HasHierarchicalDataProvider;
import com.vaadin.flow.data.provider.hierarchy.HierarchicalArrayUpdater.HierarchicalUpdate;
import com.vaadin.flow.data.provider.hierarchy.HierarchicalDataCommunicator;
import com.vaadin.flow.data.provider.hierarchy.HierarchicalDataProvider;
import com.vaadin.flow.data.provider.hierarchy.HierarchicalQuery;
import com.vaadin.flow.data.renderer.TemplateRenderer;
import com.vaadin.flow.dom.DisabledUpdateMode;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.function.SerializableBiFunction;
import com.vaadin.flow.function.SerializablePredicate;
import com.vaadin.flow.function.SerializableSupplier;
import com.vaadin.flow.function.ValueProvider;
Expand All @@ -65,18 +67,17 @@
public class TreeGrid<T> extends Grid<T>
implements HasHierarchicalDataProvider<T> {

private static final class UpdateQueue extends Grid.UpdateQueue {
private static final class TreeGridUpdateQueue extends UpdateQueue
implements HierarchicalUpdate {

private UpdateQueue(UpdateQueueData data, int size) {
private TreeGridUpdateQueue(UpdateQueueData data, int size) {
super(data, size);
}

@Override
public void set(int start, List<JsonValue> items,
String parentKey) {
public void set(int start, List<JsonValue> items, String parentKey) {
enqueue("$connector.set", start,
items.stream().collect(JsonUtils.asArray()),
parentKey);
items.stream().collect(JsonUtils.asArray()), parentKey);
}

@Override
Expand All @@ -92,15 +93,45 @@ public void clear(int start, int length, String parentKey) {
enqueue("$connector.clear", start, length, parentKey);
}


@Override
public void commit(int updateId, String parentKey, int levelSize) {
enqueue("$connector.confirmParent", updateId, parentKey,
levelSize);
enqueue("$connector.confirmParent", updateId, parentKey, levelSize);
commit();
}
}

private class TreeGridArrayUpdaterImpl implements TreeGridArrayUpdater {
private UpdateQueueData data;
private SerializableBiFunction<UpdateQueueData, Integer, UpdateQueue> updateQueueFactory;

public TreeGridArrayUpdaterImpl(
SerializableBiFunction<UpdateQueueData, Integer, UpdateQueue> updateQueueFactory) {
this.updateQueueFactory = updateQueueFactory;
}

@Override
public TreeGridUpdateQueue startUpdate(int sizeChange) {
return (TreeGridUpdateQueue) updateQueueFactory.apply(data,
sizeChange);
}

@Override
public void initialize() {
initConnector();
updateSelectionModeOnClient();
}

@Override
public void setUpdateQueueData(UpdateQueueData data) {
this.data = data;
}

@Override
public UpdateQueueData getUpdateQueueData() {
return data;
}
}

private final ValueProvider<T, String> defaultUniqueKeyProvider = item -> String
.valueOf(item.hashCode());

Expand All @@ -111,7 +142,7 @@ public void commit(int updateId, String parentKey, int levelSize) {
* automatically sets up columns based on the type of presented data.
*/
public TreeGrid() {
super(50, UpdateQueue::new,
super(50, TreeGridUpdateQueue::new,
new TreeDataCommunicatorBuilder<T>());

setUniqueKeyProperty("key");
Expand All @@ -130,13 +161,20 @@ public TreeGrid() {
* the bean type to use, not {@code null}
*/
public TreeGrid(Class<T> beanType) {
super(beanType, UpdateQueue::new, new TreeDataCommunicatorBuilder<T>());
super(beanType, TreeGridUpdateQueue::new,
new TreeDataCommunicatorBuilder<T>());

setUniqueKeyProperty("key");
getArrayUpdater().getUpdateQueueData()
.setHasExpandedItems(getDataCommunicator()::hasExpandedItems);
}

@Override
protected GridArrayUpdater createDefaultArrayUpdater(
SerializableBiFunction<UpdateQueueData, Integer, UpdateQueue> updateQueueFactory) {
return new TreeGridArrayUpdaterImpl(updateQueueFactory);
}

/**
* Creates a new {@code TreeGrid} using the given
* {@code HierarchicalDataProvider}, without support for creating columns
Expand All @@ -151,21 +189,20 @@ public TreeGrid(HierarchicalDataProvider<T, ?> dataProvider) {
this();
setDataProvider(dataProvider);
}

private static class TreeDataCommunicatorBuilder<T>
extends DataCommunicatorBuilder<T> {
extends DataCommunicatorBuilder<T, TreeGridArrayUpdater> {

@Override
protected DataCommunicator<T> build(Element element,
CompositeDataGenerator<T> dataGenerator,
GridArrayUpdater arrayUpdater,
TreeGridArrayUpdater arrayUpdater,
SerializableSupplier<ValueProvider<T, String>> uniqueKeyProviderSupplier) {

return new HierarchicalDataCommunicator<>(dataGenerator,
arrayUpdater,
data -> element.callFunction("$connector.updateData", data),
element.getNode(),
uniqueKeyProviderSupplier);
element.getNode(), uniqueKeyProviderSupplier);
}
}

Expand Down Expand Up @@ -425,8 +462,7 @@ private void setParentRequestedRange(int start, int length,
String parentKey) {
T item = getDataCommunicator().getKeyMapper().get(parentKey);
if (item != null) {
getDataCommunicator().setParentRequestedRange(start, length,
item);
getDataCommunicator().setParentRequestedRange(start, length, item);
}
}

Expand Down Expand Up @@ -537,8 +573,8 @@ public void expandRecursively(Stream<T> items, int depth) {
* @since 8.4
*/
public void expandRecursively(Collection<T> items, int depth) {
getDataCommunicator().expand(
getItemsWithChildrenRecursively(items, depth));
getDataCommunicator()
.expand(getItemsWithChildrenRecursively(items, depth));
}

/**
Expand Down Expand Up @@ -648,15 +684,13 @@ protected Collection<T> getItemsWithChildrenRecursively(Collection<T> items,
}
items.stream().filter(getDataCommunicator()::hasChildren)
.forEach(item -> {
itemsWithChildren.add(item);
itemsWithChildren
.addAll(getItemsWithChildrenRecursively(
getDataProvider()
.fetchChildren(new HierarchicalQuery<>(
null, item))
.collect(Collectors.toList()),
depth - 1));
});
itemsWithChildren.add(item);
itemsWithChildren.addAll(
getItemsWithChildrenRecursively(getDataProvider()
.fetchChildren(
new HierarchicalQuery<>(null, item))
.collect(Collectors.toList()), depth - 1));
});
return itemsWithChildren;
}

Expand Down
Loading