Skip to content

Commit

Permalink
feat: add a constructor with autocreatecolumns option to treegrid (#6501
Browse files Browse the repository at this point in the history
)

* feat: add a constructor with autocreatecolumns option to treegrid

* refactor: reuse configurebeantype in grid constructor

* chore: remove javadoc from private constructor
  • Loading branch information
ugur-vaadin authored Sep 12, 2024
1 parent 5633d44 commit 76ad7b0
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1624,15 +1624,47 @@ protected <U extends GridArrayUpdater, B extends DataCommunicatorBuilder<T, U>>
Class<T> beanType,
SerializableBiFunction<UpdateQueueData, Integer, UpdateQueue> updateQueueBuilder,
B dataCommunicatorBuilder) {
this(beanType, updateQueueBuilder, 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 updateQueueBuilder
* the builder for new {@link UpdateQueue} instance
* @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,
SerializableBiFunction<UpdateQueueData, Integer, UpdateQueue> updateQueueBuilder,
B dataCommunicatorBuilder, boolean autoCreateColumns) {
this(50, updateQueueBuilder, dataCommunicatorBuilder);
Objects.requireNonNull(beanType, "Bean type can't be null");
Objects.requireNonNull(dataCommunicatorBuilder,
"Data communicator builder can't be null");
this.beanType = beanType;
propertySet = BeanPropertySet.get(beanType);
propertySet.getProperties()
.filter(property -> !property.isSubProperty())
.forEach(this::addColumn);
configureBeanType(beanType, autoCreateColumns);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import com.vaadin.flow.data.provider.hierarchy.TreeData;
import com.vaadin.flow.data.renderer.ComponentRenderer;
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.SerializableBiFunction;
Expand Down Expand Up @@ -268,7 +269,32 @@ private void addItemHasChildrenPathGenerator() {
* the bean type to use, not {@code null}
*/
public TreeGrid(Class<T> beanType) {
this(beanType, new TreeDataCommunicatorBuilder<T>());
this(beanType, true);
}

/**
* 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}.
* <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 #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, e.g.
* <code>"property.nestedProperty"</code>.
*
* @param beanType
* the bean type to use, not <code>null</code>
* @param autoCreateColumns
* when <code>true</code>, columns are created automatically for
* the properties of the beanType
*/
public TreeGrid(Class<T> beanType, boolean autoCreateColumns) {
this(beanType, new TreeDataCommunicatorBuilder<>(), autoCreateColumns);
}

/**
Expand All @@ -286,7 +312,14 @@ public TreeGrid(Class<T> beanType) {
*/
protected TreeGrid(Class<T> beanType,
DataCommunicatorBuilder<T, TreeGridArrayUpdater> dataCommunicatorBuilder) {
super(beanType, TreeGridUpdateQueue::new, dataCommunicatorBuilder);
this(beanType, dataCommunicatorBuilder, true);
}

private TreeGrid(Class<T> beanType,
DataCommunicatorBuilder<T, TreeGridArrayUpdater> dataCommunicatorBuilder,
boolean autoCreateColumns) {
super(beanType, TreeGridUpdateQueue::new, dataCommunicatorBuilder,
autoCreateColumns);

setUniqueKeyProperty("key");
getArrayUpdater().getUpdateQueueData()
Expand Down

0 comments on commit 76ad7b0

Please sign in to comment.