Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
import com.vaadin.flow.data.event.SortEvent;
import com.vaadin.flow.data.event.SortEvent.SortNotifier;
import com.vaadin.flow.data.provider.AbstractDataView;
import com.vaadin.flow.data.provider.ArrayUpdater;
import com.vaadin.flow.data.provider.ArrayUpdater.Update;
import com.vaadin.flow.data.provider.BackEndDataProvider;
import com.vaadin.flow.data.provider.CallbackDataProvider;
Expand Down Expand Up @@ -1491,7 +1490,30 @@ public Grid(Collection<T> items) {
* the page size. Must be greater than zero.
*/
public Grid(int pageSize) {
this(pageSize, new DataCommunicatorBuilder<>());
arrayUpdater = createDefaultArrayUpdater();
gridDataGenerator = new CompositeDataGenerator<>();
gridDataGenerator.addDataGenerator(this::generateUniqueKeyData);
gridDataGenerator.addDataGenerator(this::generatePartData);
gridDataGenerator.addDataGenerator(this::generateTooltipTextData);
gridDataGenerator.addDataGenerator(this::generateRowsDragAndDropAccess);
gridDataGenerator.addDataGenerator(this::generateDragData);
gridDataGenerator.addDataGenerator(this::generateSelectableData);

dataCommunicator = createDataCommunicator();

detailsManager = new DetailsManager(this);
setPageSize(pageSize);
setSelectionModel(SelectionMode.SINGLE.createModel(this),
SelectionMode.SINGLE);

columnLayers.add(new ColumnLayer(this));

addDragStartListener(this::onDragStart);
addDragEndListener(this::onDragEnd);

updateMultiSortPriority(defaultMultiSortPriority);

initSelectionPreservationHandler();
}

/**
Expand Down Expand Up @@ -1545,120 +1567,6 @@ public Grid(Class<T> beanType) {
this(beanType, true);
}

/**
* Creates a new grid with an initial set of columns for each of the bean's
* properties. The property-values of the bean will be converted to Strings.
* Full names of the properties will be used as the
* {@link Column#setKey(String) column keys} and the property captions will
* be used as the {@link Column#setHeader(String) column headers}.
* <p>
* You can add columns for nested properties of the bean with
* {@link #addColumn(String)}.
*
* @param beanType
* the bean type to use, not <code>null</code>
* @param dataCommunicatorBuilder
* Builder for {@link DataCommunicator} implementation this Grid
* uses to handle all data communication.
* @param <B>
* the data communicator builder type
* @param <U>
* the GridArrayUpdater type
*/
protected <U extends GridArrayUpdater, B extends DataCommunicatorBuilder<T, U>> Grid(
Class<T> beanType, B dataCommunicatorBuilder) {
this(beanType, dataCommunicatorBuilder, true);
}

/**
* Creates a new grid with an initial set of columns for each of the bean's
* properties. The property-values of the bean will be converted to Strings.
* Full names of the properties will be used as the
* {@link Column#setKey(String) column keys} and the property captions will
* be used as the {@link Column#setHeader(String) column headers}.
* <p>
* When autoCreateColumns is <code>true</code>, only the direct properties
* of the bean are included and they will be in alphabetical order. Use
* {@link Grid#setColumns(String...)} to define which properties to include
* and in which order. You can also add a column for an individual property
* with {@link #addColumn(String)}. Both of these methods support also
* sub-properties with dot-notation, eg.
* <code>"property.nestedProperty"</code>.
*
* @param beanType
* the bean type to use, not <code>null</code>
* @param dataCommunicatorBuilder
* Builder for {@link DataCommunicator} implementation this Grid
* uses to handle all data communication.
* @param <B>
* the data communicator builder type
* @param <U>
* the GridArrayUpdater type
* @param autoCreateColumns
* when <code>true</code>, columns are created automatically for
* the properties of the beanType
*/
protected <U extends GridArrayUpdater, B extends DataCommunicatorBuilder<T, U>> Grid(
Class<T> beanType, B dataCommunicatorBuilder,
boolean autoCreateColumns) {
this(50, dataCommunicatorBuilder);
Objects.requireNonNull(dataCommunicatorBuilder,
"Data communicator builder can't be null");
configureBeanType(beanType, autoCreateColumns);
}

/**
* Creates a new instance, with the specified page size and data
* communicator.
* <p>
* The page size influences the {@link Query#getLimit()} sent by the client,
* but it's up to the webcomponent to determine the actual query limit,
* based on the height of the component and scroll position. Usually the
* limit is 3 times the page size (e.g. 150 items with a page size of 50).
*
* @param pageSize
* the page size. Must be greater than zero.
* @param dataCommunicatorBuilder
* Builder for {@link DataCommunicator} implementation this Grid
* uses to handle all data communication.
* @param <B>
* the data communicator builder type
* @param <U>
* the GridArrayUpdater type
*/
@SuppressWarnings("unchecked")
protected <U extends GridArrayUpdater, B extends DataCommunicatorBuilder<T, U>> Grid(
int pageSize, B dataCommunicatorBuilder) {
Objects.requireNonNull(dataCommunicatorBuilder,
"Data communicator builder can't be null");
arrayUpdater = createDefaultArrayUpdater();
gridDataGenerator = new CompositeDataGenerator<>();
gridDataGenerator.addDataGenerator(this::generateUniqueKeyData);
gridDataGenerator.addDataGenerator(this::generatePartData);
gridDataGenerator.addDataGenerator(this::generateTooltipTextData);
gridDataGenerator.addDataGenerator(this::generateRowsDragAndDropAccess);
gridDataGenerator.addDataGenerator(this::generateDragData);
gridDataGenerator.addDataGenerator(this::generateSelectableData);

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

detailsManager = new DetailsManager(this);
setPageSize(pageSize);
setSelectionModel(SelectionMode.SINGLE.createModel(this),
SelectionMode.SINGLE);

columnLayers.add(new ColumnLayer(this));

addDragStartListener(this::onDragStart);
addDragEndListener(this::onDragEnd);

updateMultiSortPriority(defaultMultiSortPriority);

initSelectionPreservationHandler();
}

private void generateUniqueKeyData(T item, ObjectNode jsonObject) {
if (uniqueKeyProperty != null && !jsonObject.has(uniqueKeyProperty)) {
jsonObject.put(uniqueKeyProperty, getUniqueKey(item));
Expand Down Expand Up @@ -1726,39 +1634,14 @@ protected void initConnector() {
}

/**
* Builder for {@link DataCommunicator} object.
* Creates the {@link DataCommunicator} that this Grid uses to handle all
* data communication.
*
* @param <T>
* the grid bean type
*
* @param <U>
* the ArrayUpdater type
* @return the new data communicator
*/
protected static class DataCommunicatorBuilder<T, U extends ArrayUpdater>
implements Serializable {

/**
* Build a new {@link DataCommunicator} object for the given Grid
* instance.
*
* @param element
* the target grid element
* @param dataGenerator
* the {@link CompositeDataGenerator} for the data
* communicator
* @param arrayUpdater
* 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, U arrayUpdater,
SerializableSupplier<ValueProvider<T, String>> uniqueKeyProviderSupplier) {
return new GridDataCommunicator<>(element, dataGenerator,
arrayUpdater);
}
protected DataCommunicator<T> createDataCommunicator() {
return new GridDataCommunicator<>(getElement(),
getCompositeDataGenerator(), getArrayUpdater());
}

protected GridArrayUpdater createDefaultArrayUpdater() {
Expand Down Expand Up @@ -4494,6 +4377,10 @@ protected GridArrayUpdater getArrayUpdater() {
return arrayUpdater;
}

protected CompositeDataGenerator<T> getCompositeDataGenerator() {
return gridDataGenerator;
}

/**
* Callback which is called if a new data provider is set or any change
* happen in the current data provider (an {@link DataChangeEvent} event is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@
import com.vaadin.flow.component.ComponentUtil;
import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.grid.GridArrayUpdater;
import com.vaadin.flow.component.grid.dataview.GridDataView;
import com.vaadin.flow.component.grid.dataview.GridLazyDataView;
import com.vaadin.flow.component.grid.dataview.GridListDataView;
import com.vaadin.flow.component.internal.AllowInert;
import com.vaadin.flow.data.binder.PropertyDefinition;
import com.vaadin.flow.data.provider.BackEndDataProvider;
import com.vaadin.flow.data.provider.CallbackDataProvider;
import com.vaadin.flow.data.provider.CompositeDataGenerator;
import com.vaadin.flow.data.provider.DataCommunicator;
import com.vaadin.flow.data.provider.DataProvider;
import com.vaadin.flow.data.provider.ListDataProvider;
Expand All @@ -53,10 +51,8 @@
import com.vaadin.flow.data.renderer.LitRenderer;
import com.vaadin.flow.data.renderer.Renderer;
import com.vaadin.flow.dom.DisabledUpdateMode;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.function.SerializableComparator;
import com.vaadin.flow.function.SerializablePredicate;
import com.vaadin.flow.function.SerializableSupplier;
import com.vaadin.flow.function.ValueProvider;
import com.vaadin.flow.shared.Registration;

Expand All @@ -82,7 +78,7 @@ public class TreeGrid<T> extends Grid<T>
* automatically sets up columns based on the type of presented data.
*/
public TreeGrid() {
this(50, new TreeDataCommunicatorBuilder<T>());
this(50);
}

/**
Expand All @@ -93,13 +89,9 @@ public TreeGrid() {
*
* @param pageSize
* the page size. Must be greater than zero.
* @param dataCommunicatorBuilder
* Builder for {@link DataCommunicator} implementation this Grid
* uses to handle all data communication.
*/
protected TreeGrid(int pageSize,
DataCommunicatorBuilder<T, GridArrayUpdater> dataCommunicatorBuilder) {
super(pageSize, dataCommunicatorBuilder);
protected TreeGrid(int pageSize) {
super(pageSize);

setUniqueKeyProperty("key");
addTreeDataGenerator();
Expand Down Expand Up @@ -171,31 +163,7 @@ public TreeGrid(Class<T> beanType) {
* the properties of the beanType
*/
public TreeGrid(Class<T> beanType, boolean autoCreateColumns) {
this(beanType, new TreeDataCommunicatorBuilder<>(), autoCreateColumns);
}

/**
* Creates a new {@code TreeGrid} with an initial set of columns for each of
* the bean's properties. The property-values of the bean will be converted
* to Strings. Full names of the properties will be used as the
* {@link Column#setKey(String) column keys} and the property captions will
* be used as the {@link Column#setHeader(String) column headers}.
*
* @param beanType
* the bean type to use, not {@code null}
* @param dataCommunicatorBuilder
* Builder for {@link DataCommunicator} implementation this Grid
* uses to handle all data communication.
*/
protected TreeGrid(Class<T> beanType,
DataCommunicatorBuilder<T, GridArrayUpdater> dataCommunicatorBuilder) {
this(beanType, dataCommunicatorBuilder, true);
}

private TreeGrid(Class<T> beanType,
DataCommunicatorBuilder<T, GridArrayUpdater> dataCommunicatorBuilder,
boolean autoCreateColumns) {
super(beanType, dataCommunicatorBuilder, autoCreateColumns);
super(beanType, autoCreateColumns);

setUniqueKeyProperty("key");
addTreeDataGenerator();
Expand All @@ -216,17 +184,11 @@ public TreeGrid(HierarchicalDataProvider<T, ?> dataProvider) {
setDataProvider(dataProvider);
}

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

@Override
protected DataCommunicator<T> build(Element element,
CompositeDataGenerator<T> dataGenerator,
GridArrayUpdater arrayUpdater,
SerializableSupplier<ValueProvider<T, String>> uniqueKeyProviderSupplier) {
return new TreeGridDataCommunicator<>(element, dataGenerator,
arrayUpdater, uniqueKeyProviderSupplier);
}
@Override
protected DataCommunicator<T> createDataCommunicator() {
return new TreeGridDataCommunicator<>(getElement(),
getCompositeDataGenerator(), getArrayUpdater(),
this::getUniqueKeyProvider);
}

/**
Expand Down
Loading