From ee7567c93250b206820fbf6e53d7c33391d6bbe8 Mon Sep 17 00:00:00 2001 From: Gilberto Torrezan Date: Mon, 23 Jul 2018 13:51:49 +0300 Subject: [PATCH 1/2] Update Grid to work with the new abstraction of HierarchicalArrayUpdater --- .../com/vaadin/flow/component/grid/Grid.java | 15 ---- .../flow/component/grid/GridArrayUpdater.java | 3 +- .../flow/component/treegrid/TreeGrid.java | 85 +++++++++++++------ .../treegrid/TreeGridArrayUpdater.java | 30 +++++++ 4 files changed, 90 insertions(+), 43 deletions(-) create mode 100644 src/main/java/com/vaadin/flow/component/treegrid/TreeGridArrayUpdater.java diff --git a/src/main/java/com/vaadin/flow/component/grid/Grid.java b/src/main/java/com/vaadin/flow/component/grid/Grid.java index 3c2355e5..e035cada 100755 --- a/src/main/java/com/vaadin/flow/component/grid/Grid.java +++ b/src/main/java/com/vaadin/flow/component/grid/Grid.java @@ -156,21 +156,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 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."); diff --git a/src/main/java/com/vaadin/flow/component/grid/GridArrayUpdater.java b/src/main/java/com/vaadin/flow/component/grid/GridArrayUpdater.java index a1303934..14c23852 100644 --- a/src/main/java/com/vaadin/flow/component/grid/GridArrayUpdater.java +++ b/src/main/java/com/vaadin/flow/component/grid/GridArrayUpdater.java @@ -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; @@ -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; diff --git a/src/main/java/com/vaadin/flow/component/treegrid/TreeGrid.java b/src/main/java/com/vaadin/flow/component/treegrid/TreeGrid.java index 5ae9bc5d..78c5a59d 100644 --- a/src/main/java/com/vaadin/flow/component/treegrid/TreeGrid.java +++ b/src/main/java/com/vaadin/flow/component/treegrid/TreeGrid.java @@ -37,12 +37,15 @@ 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; +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; @@ -65,18 +68,17 @@ public class TreeGrid extends Grid implements HasHierarchicalDataProvider { - private static final class UpdateQueue extends Grid.UpdateQueue { + private static final class UpdateQueue extends Grid.UpdateQueue + implements HierarchicalUpdate { private UpdateQueue(UpdateQueueData data, int size) { super(data, size); } @Override - public void set(int start, List items, - String parentKey) { + public void set(int start, List items, String parentKey) { enqueue("$connector.set", start, - items.stream().collect(JsonUtils.asArray()), - parentKey); + items.stream().collect(JsonUtils.asArray()), parentKey); } @Override @@ -92,11 +94,9 @@ 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(); } } @@ -111,8 +111,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, - new TreeDataCommunicatorBuilder()); + super(50, UpdateQueue::new, new TreeDataCommunicatorBuilder()); setUniqueKeyProperty("key"); getArrayUpdater().getUpdateQueueData() @@ -137,6 +136,37 @@ public TreeGrid(Class beanType) { .setHasExpandedItems(getDataCommunicator()::hasExpandedItems); } + @Override + protected GridArrayUpdater createDefaultArrayUpdater( + SerializableBiFunction updateQueueFactory) { + + return new TreeGridArrayUpdater() { + + private UpdateQueueData data; + + @Override + public UpdateQueue startUpdate(int sizeChange) { + return (UpdateQueue) 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; + } + }; + } + /** * Creates a new {@code TreeGrid} using the given * {@code HierarchicalDataProvider}, without support for creating columns @@ -151,7 +181,7 @@ public TreeGrid(HierarchicalDataProvider dataProvider) { this(); setDataProvider(dataProvider); } - + private static class TreeDataCommunicatorBuilder extends DataCommunicatorBuilder { @@ -161,11 +191,15 @@ protected DataCommunicator build(Element element, GridArrayUpdater arrayUpdater, SerializableSupplier> uniqueKeyProviderSupplier) { + if (!(arrayUpdater instanceof HierarchicalArrayUpdater)) { + throw new IllegalArgumentException( + "The ArrayUpdater should implement the HierarchicalArrayUpdater interface for TreeGrid"); + } + return new HierarchicalDataCommunicator<>(dataGenerator, - arrayUpdater, + (HierarchicalArrayUpdater) arrayUpdater, data -> element.callFunction("$connector.updateData", data), - element.getNode(), - uniqueKeyProviderSupplier); + element.getNode(), uniqueKeyProviderSupplier); } } @@ -425,8 +459,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); } } @@ -537,8 +570,8 @@ public void expandRecursively(Stream items, int depth) { * @since 8.4 */ public void expandRecursively(Collection items, int depth) { - getDataCommunicator().expand( - getItemsWithChildrenRecursively(items, depth)); + getDataCommunicator() + .expand(getItemsWithChildrenRecursively(items, depth)); } /** @@ -648,15 +681,13 @@ protected Collection getItemsWithChildrenRecursively(Collection 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; } diff --git a/src/main/java/com/vaadin/flow/component/treegrid/TreeGridArrayUpdater.java b/src/main/java/com/vaadin/flow/component/treegrid/TreeGridArrayUpdater.java new file mode 100644 index 00000000..f6f67e3c --- /dev/null +++ b/src/main/java/com/vaadin/flow/component/treegrid/TreeGridArrayUpdater.java @@ -0,0 +1,30 @@ +/* + * Copyright 2000-2018 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.flow.component.treegrid; + +import com.vaadin.flow.component.grid.GridArrayUpdater; +import com.vaadin.flow.data.provider.hierarchy.HierarchicalArrayUpdater; + +/** + * Array update strategy aware class for TreeGrid. + * + * @author Vaadin Ltd + * + */ +public interface TreeGridArrayUpdater + extends GridArrayUpdater, HierarchicalArrayUpdater { + +} From af63e18ae96c56cb60ce7893170c52535813957f Mon Sep 17 00:00:00 2001 From: Gilberto Torrezan Date: Tue, 24 Jul 2018 08:39:23 +0300 Subject: [PATCH 2/2] Remove type casts and anonymous classes --- .../com/vaadin/flow/component/grid/Grid.java | 88 +++++++++++-------- .../flow/component/treegrid/TreeGrid.java | 83 ++++++++--------- 2 files changed, 94 insertions(+), 77 deletions(-) diff --git a/src/main/java/com/vaadin/flow/component/grid/Grid.java b/src/main/java/com/vaadin/flow/component/grid/Grid.java index e035cada..4fa6a674 100755 --- a/src/main/java/com/vaadin/flow/component/grid/Grid.java +++ b/src/main/java/com/vaadin/flow/component/grid/Grid.java @@ -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; @@ -156,11 +157,6 @@ public void enqueue(String name, Serializable... arguments) { queue.add(() -> getElement().callFunction(name, arguments)); } - private void onlySupportedOnTreeGrid() { - throw new UnsupportedOperationException( - "This method can't be used for a Grid. Use TreeGrid instead."); - } - protected Element getElement() { return data.getElement(); } @@ -841,6 +837,38 @@ private void setDetailsVisibleFromClient(Set items) { } } + private class GridArrayUpdaterImpl implements GridArrayUpdater { + private UpdateQueueData data; + private SerializableBiFunction updateQueueFactory; + + public GridArrayUpdaterImpl( + SerializableBiFunction 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 gridDataGenerator; @@ -943,8 +971,11 @@ public Grid(Class beanType) { * uses to handle all data communication. * @param * the data communicator builder type + * @param + * the GridArrayUpdater type */ - protected > Grid(Class beanType, + protected > Grid( + Class beanType, SerializableBiFunction updateQueueBuidler, B dataCommunicatorBuilder) { this(50, updateQueueBuidler, dataCommunicatorBuilder); @@ -975,9 +1006,12 @@ protected > Grid(Class beanType, * uses to handle all data communication. * @param * the data communicator builder type + * @param + * the GridArrayUpdater type * */ - protected > Grid(int pageSize, + protected > Grid( + int pageSize, SerializableBiFunction updateQueueBuidler, B dataCommunicatorBuilder) { Objects.requireNonNull(dataCommunicatorBuilder, @@ -991,7 +1025,8 @@ protected > 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); @@ -1024,8 +1059,12 @@ protected void initConnector() { * * @param * the grid bean type + * + * @param + * the ArrayUpdater type */ - protected static class DataCommunicatorBuilder implements Serializable { + protected static class DataCommunicatorBuilder + implements Serializable { /** * Build a new {@link DataCommunicator} object for the given Grid @@ -1037,15 +1076,14 @@ protected static class DataCommunicatorBuilder 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 build(Element element, - CompositeDataGenerator dataGenerator, - GridArrayUpdater arrayUpdater, + CompositeDataGenerator dataGenerator, U arrayUpdater, SerializableSupplier> uniqueKeyProviderSupplier) { return new DataCommunicator<>(dataGenerator, arrayUpdater, data -> element.callFunction("$connector.updateData", data), @@ -1055,31 +1093,7 @@ protected DataCommunicator build(Element element, protected GridArrayUpdater createDefaultArrayUpdater( SerializableBiFunction 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); } /** diff --git a/src/main/java/com/vaadin/flow/component/treegrid/TreeGrid.java b/src/main/java/com/vaadin/flow/component/treegrid/TreeGrid.java index 78c5a59d..56548c2c 100644 --- a/src/main/java/com/vaadin/flow/component/treegrid/TreeGrid.java +++ b/src/main/java/com/vaadin/flow/component/treegrid/TreeGrid.java @@ -37,7 +37,6 @@ 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; 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; @@ -68,10 +67,10 @@ public class TreeGrid extends Grid implements HasHierarchicalDataProvider { - 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); } @@ -101,6 +100,38 @@ public void commit(int updateId, String parentKey, int levelSize) { } } + private class TreeGridArrayUpdaterImpl implements TreeGridArrayUpdater { + private UpdateQueueData data; + private SerializableBiFunction updateQueueFactory; + + public TreeGridArrayUpdaterImpl( + SerializableBiFunction 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 defaultUniqueKeyProvider = item -> String .valueOf(item.hashCode()); @@ -111,7 +142,8 @@ 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, new TreeDataCommunicatorBuilder()); + super(50, TreeGridUpdateQueue::new, + new TreeDataCommunicatorBuilder()); setUniqueKeyProperty("key"); getArrayUpdater().getUpdateQueueData() @@ -129,7 +161,8 @@ public TreeGrid() { * the bean type to use, not {@code null} */ public TreeGrid(Class beanType) { - super(beanType, UpdateQueue::new, new TreeDataCommunicatorBuilder()); + super(beanType, TreeGridUpdateQueue::new, + new TreeDataCommunicatorBuilder()); setUniqueKeyProperty("key"); getArrayUpdater().getUpdateQueueData() @@ -138,33 +171,8 @@ public TreeGrid(Class beanType) { @Override protected GridArrayUpdater createDefaultArrayUpdater( - SerializableBiFunction updateQueueFactory) { - - return new TreeGridArrayUpdater() { - - private UpdateQueueData data; - - @Override - public UpdateQueue startUpdate(int sizeChange) { - return (UpdateQueue) 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; - } - }; + SerializableBiFunction updateQueueFactory) { + return new TreeGridArrayUpdaterImpl(updateQueueFactory); } /** @@ -183,21 +191,16 @@ public TreeGrid(HierarchicalDataProvider dataProvider) { } private static class TreeDataCommunicatorBuilder - extends DataCommunicatorBuilder { + extends DataCommunicatorBuilder { @Override protected DataCommunicator build(Element element, CompositeDataGenerator dataGenerator, - GridArrayUpdater arrayUpdater, + TreeGridArrayUpdater arrayUpdater, SerializableSupplier> uniqueKeyProviderSupplier) { - if (!(arrayUpdater instanceof HierarchicalArrayUpdater)) { - throw new IllegalArgumentException( - "The ArrayUpdater should implement the HierarchicalArrayUpdater interface for TreeGrid"); - } - return new HierarchicalDataCommunicator<>(dataGenerator, - (HierarchicalArrayUpdater) arrayUpdater, + arrayUpdater, data -> element.callFunction("$connector.updateData", data), element.getNode(), uniqueKeyProviderSupplier); }