Search in sources :

Example 1 with JsonSerializer

use of com.vaadin.flow.internal.JsonSerializer in project flow-components by vaadin.

the class Grid method addColumn.

/**
 * Adds a new text column to this {@link Grid} with a template renderer,
 * sorting properties and column factory provided. The values inside the
 * renderer are converted to JSON values by using
 * {@link JsonSerializer#toJson(Object)}.
 * <p>
 * <em>NOTE:</em> You can add component columns easily using the
 * {@link #addComponentColumn(ValueProvider)}, but using
 * {@link ComponentRenderer} is not as efficient as the built in renderers
 * or using {@link TemplateRenderer}.
 * <p>
 * This constructor attempts to automatically configure both in-memory and
 * backend sorting using the given sorting properties and matching those
 * with the property names used in the given renderer.
 * <p>
 * <strong>Note:</strong> if a property of the renderer that is used as a
 * sorting property does not extend Comparable, no in-memory sorting is
 * configured for it.
 *
 * <p>
 * Every added column sends data to the client side regardless of its
 * visibility state. Don't add a new column at all or use
 * {@link Grid#removeColumn(Column)} to avoid sending extra data.
 * </p>
 *
 * @see #addColumn(Renderer, String...)
 * @see #removeColumn(Column)
 *
 * @param renderer
 *            the renderer used to create the grid cell structure
 * @param columnFactory
 *            the method that creates a new column instance for this
 *            {@link Grid} instance.
 * @param sortingProperties
 *            the sorting properties to use for this column
 * @return the created column
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
protected <C extends Column<T>> C addColumn(Renderer<T> renderer, BiFunction<Renderer<T>, String, C> columnFactory, String... sortingProperties) {
    C column = addColumn(renderer, columnFactory);
    Map<String, ValueProvider<T, ?>> valueProviders = renderer.getValueProviders();
    Set<String> valueProvidersKeySet = valueProviders.keySet();
    List<String> matchingSortingProperties = Arrays.stream(sortingProperties).filter(valueProvidersKeySet::contains).collect(Collectors.toList());
    column.setSortProperty(matchingSortingProperties.toArray(new String[matchingSortingProperties.size()]));
    Comparator<T> combinedComparator = (a, b) -> 0;
    Comparator nullsLastComparator = Comparator.nullsLast(Comparator.naturalOrder());
    for (String sortProperty : matchingSortingProperties) {
        ValueProvider<T, ?> provider = valueProviders.get(sortProperty);
        combinedComparator = combinedComparator.thenComparing((a, b) -> {
            Object aa = provider.apply(a);
            if (!(aa instanceof Comparable)) {
                return 0;
            }
            Object bb = provider.apply(b);
            return nullsLastComparator.compare(aa, bb);
        });
    }
    return column;
}
Also used : KeyMapper(com.vaadin.flow.data.provider.KeyMapper) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Arrays(java.util.Arrays) DataView(com.vaadin.flow.data.provider.DataView) NpmPackage(com.vaadin.flow.component.dependency.NpmPackage) ComponentUtil(com.vaadin.flow.component.ComponentUtil) HasListDataView(com.vaadin.flow.data.provider.HasListDataView) JsonArray(elemental.json.JsonArray) PropertySet(com.vaadin.flow.data.binder.PropertySet) DataCommunicator(com.vaadin.flow.data.provider.DataCommunicator) JsonValue(elemental.json.JsonValue) SortEvent(com.vaadin.flow.data.event.SortEvent) Map(java.util.Map) Element(com.vaadin.flow.dom.Element) AttachEvent(com.vaadin.flow.component.AttachEvent) DataGenerator(com.vaadin.flow.data.provider.DataGenerator) UpdateQueueData(com.vaadin.flow.component.grid.GridArrayUpdater.UpdateQueueData) JsonType(elemental.json.JsonType) HasStyle(com.vaadin.flow.component.HasStyle) Editor(com.vaadin.flow.component.grid.editor.Editor) Set(java.util.Set) SerializableSupplier(com.vaadin.flow.function.SerializableSupplier) GridContextMenu(com.vaadin.flow.component.grid.contextmenu.GridContextMenu) Serializable(java.io.Serializable) EditorRenderer(com.vaadin.flow.component.grid.editor.EditorRenderer) Stream(java.util.stream.Stream) DetachEvent(com.vaadin.flow.component.DetachEvent) DataProviderListener(com.vaadin.flow.data.provider.DataProviderListener) DataProviderWrapper(com.vaadin.flow.data.provider.DataProviderWrapper) JsModule(com.vaadin.flow.component.dependency.JsModule) InMemoryDataProvider(com.vaadin.flow.data.provider.InMemoryDataProvider) SingleSelectionListener(com.vaadin.flow.data.selection.SingleSelectionListener) DataViewUtils(com.vaadin.flow.data.provider.DataViewUtils) MultiSelect(com.vaadin.flow.data.selection.MultiSelect) SortDirection(com.vaadin.flow.data.provider.SortDirection) EditorImpl(com.vaadin.flow.component.grid.editor.EditorImpl) Single(com.vaadin.flow.data.selection.SelectionModel.Single) SerializableConsumer(com.vaadin.flow.function.SerializableConsumer) SingleSelect(com.vaadin.flow.data.selection.SingleSelect) QuerySortOrder(com.vaadin.flow.data.provider.QuerySortOrder) SerializableBiFunction(com.vaadin.flow.function.SerializableBiFunction) ArrayList(java.util.ArrayList) Tag(com.vaadin.flow.component.Tag) TemplateRenderer(com.vaadin.flow.data.renderer.TemplateRenderer) DropTarget(com.vaadin.flow.component.dnd.DropTarget) Setter(com.vaadin.flow.data.binder.Setter) Update(com.vaadin.flow.data.provider.ArrayUpdater.Update) SerializablePredicate(com.vaadin.flow.function.SerializablePredicate) DragSource(com.vaadin.flow.component.dnd.DragSource) GridLazyDataView(com.vaadin.flow.component.grid.dataview.GridLazyDataView) LitRenderer(com.vaadin.flow.data.renderer.LitRenderer) CompositeDataGenerator(com.vaadin.flow.data.provider.CompositeDataGenerator) ComponentEvent(com.vaadin.flow.component.ComponentEvent) JsonUtils(com.vaadin.flow.internal.JsonUtils) HasTheme(com.vaadin.flow.component.HasTheme) HasLazyDataView(com.vaadin.flow.data.provider.HasLazyDataView) JsonObject(elemental.json.JsonObject) ClientCallable(com.vaadin.flow.component.ClientCallable) SerializableFunction(com.vaadin.flow.function.SerializableFunction) GridListDataView(com.vaadin.flow.component.grid.dataview.GridListDataView) GridDragStartEvent(com.vaadin.flow.component.grid.dnd.GridDragStartEvent) SerializableComparator(com.vaadin.flow.function.SerializableComparator) Component(com.vaadin.flow.component.Component) BiFunction(java.util.function.BiFunction) Registration(com.vaadin.flow.shared.Registration) Json(elemental.json.Json) LoggerFactory(org.slf4j.LoggerFactory) HasDataView(com.vaadin.flow.data.provider.HasDataView) SortNotifier(com.vaadin.flow.data.event.SortEvent.SortNotifier) DataProvider(com.vaadin.flow.data.provider.DataProvider) GridDragEndEvent(com.vaadin.flow.component.grid.dnd.GridDragEndEvent) Synchronize(com.vaadin.flow.component.Synchronize) BackEndDataProvider(com.vaadin.flow.data.provider.BackEndDataProvider) HasSize(com.vaadin.flow.component.HasSize) Query(com.vaadin.flow.data.provider.Query) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) Objects(java.util.Objects) List(java.util.List) Rendering(com.vaadin.flow.data.renderer.Rendering) Optional(java.util.Optional) ArrayUpdater(com.vaadin.flow.data.provider.ArrayUpdater) Renderer(com.vaadin.flow.data.renderer.Renderer) PropertyDefinition(com.vaadin.flow.data.binder.PropertyDefinition) HasDataGenerators(com.vaadin.flow.data.provider.HasDataGenerators) IntStream(java.util.stream.IntStream) ComponentEventListener(com.vaadin.flow.component.ComponentEventListener) ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) HasValue(com.vaadin.flow.component.HasValue) GridDropEvent(com.vaadin.flow.component.grid.dnd.GridDropEvent) ValueProvider(com.vaadin.flow.function.ValueProvider) Binder(com.vaadin.flow.data.binder.Binder) HashMap(java.util.HashMap) HashSet(java.util.HashSet) GridDropMode(com.vaadin.flow.component.grid.dnd.GridDropMode) MultiSelectionListener(com.vaadin.flow.data.selection.MultiSelectionListener) SelectionModel(com.vaadin.flow.data.selection.SelectionModel) BeanPropertySet(com.vaadin.flow.data.binder.BeanPropertySet) SerializableRunnable(com.vaadin.flow.function.SerializableRunnable) SelectionEvent(com.vaadin.flow.data.selection.SelectionEvent) NoSuchElementException(java.util.NoSuchElementException) DisabledUpdateMode(com.vaadin.flow.dom.DisabledUpdateMode) DataChangeEvent(com.vaadin.flow.data.provider.DataChangeEvent) HasElement(com.vaadin.flow.component.HasElement) Focusable(com.vaadin.flow.component.Focusable) CallbackDataProvider(com.vaadin.flow.data.provider.CallbackDataProvider) ReflectTools(com.vaadin.flow.internal.ReflectTools) SelectionListener(com.vaadin.flow.data.selection.SelectionListener) GridDataView(com.vaadin.flow.component.grid.dataview.GridDataView) JsonSerializer(com.vaadin.flow.internal.JsonSerializer) Comparator(java.util.Comparator) Collections(java.util.Collections) SerializableComparator(com.vaadin.flow.function.SerializableComparator) Comparator(java.util.Comparator) JsonObject(elemental.json.JsonObject) ValueProvider(com.vaadin.flow.function.ValueProvider)

Aggregations

AttachEvent (com.vaadin.flow.component.AttachEvent)1 ClientCallable (com.vaadin.flow.component.ClientCallable)1 Component (com.vaadin.flow.component.Component)1 ComponentEvent (com.vaadin.flow.component.ComponentEvent)1 ComponentEventListener (com.vaadin.flow.component.ComponentEventListener)1 ComponentUtil (com.vaadin.flow.component.ComponentUtil)1 DetachEvent (com.vaadin.flow.component.DetachEvent)1 Focusable (com.vaadin.flow.component.Focusable)1 HasElement (com.vaadin.flow.component.HasElement)1 HasSize (com.vaadin.flow.component.HasSize)1 HasStyle (com.vaadin.flow.component.HasStyle)1 HasTheme (com.vaadin.flow.component.HasTheme)1 HasValue (com.vaadin.flow.component.HasValue)1 Synchronize (com.vaadin.flow.component.Synchronize)1 Tag (com.vaadin.flow.component.Tag)1 JsModule (com.vaadin.flow.component.dependency.JsModule)1 NpmPackage (com.vaadin.flow.component.dependency.NpmPackage)1 DragSource (com.vaadin.flow.component.dnd.DragSource)1 DropTarget (com.vaadin.flow.component.dnd.DropTarget)1 UpdateQueueData (com.vaadin.flow.component.grid.GridArrayUpdater.UpdateQueueData)1