Search in sources :

Example 1 with Renderer

use of com.vaadin.flow.data.renderer.Renderer 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)

Example 2 with Renderer

use of com.vaadin.flow.data.renderer.Renderer in project flow-components by vaadin.

the class IronListTestPage method createListWithComponentRenderer.

private void createListWithComponentRenderer() {
    IronList<String> list = new IronList<>();
    list.setHeight("100px");
    List<String> items = IntStream.range(1, 101).mapToObj(i -> "Item " + i).collect(Collectors.toList());
    list.setRenderer(new ComponentRenderer<>(item -> {
        Label label = new Label(item);
        label.addClassName("component-rendered");
        return label;
    }));
    list.setItems(items);
    list.setId("component-renderer");
    add(list);
}
Also used : IntStream(java.util.stream.IntStream) LocalDateRenderer(com.vaadin.flow.data.renderer.LocalDateRenderer) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Arrays(java.util.Arrays) Query(com.vaadin.flow.data.provider.Query) ValueProvider(com.vaadin.flow.function.ValueProvider) LocalDateTime(java.time.LocalDateTime) Div(com.vaadin.flow.component.html.Div) Label(com.vaadin.flow.component.html.Label) NativeButton(com.vaadin.flow.component.html.NativeButton) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Route(com.vaadin.flow.router.Route) NumberRenderer(com.vaadin.flow.data.renderer.NumberRenderer) LocalDateTimeRenderer(com.vaadin.flow.data.renderer.LocalDateTimeRenderer) List(java.util.List) NativeButtonRenderer(com.vaadin.flow.data.renderer.NativeButtonRenderer) Locale(java.util.Locale) DataProvider(com.vaadin.flow.data.provider.DataProvider) TemplateRenderer(com.vaadin.flow.data.renderer.TemplateRenderer) LocalDate(java.time.LocalDate) IronList(com.vaadin.flow.component.ironlist.IronList) HasComponents(com.vaadin.flow.component.HasComponents) Renderer(com.vaadin.flow.data.renderer.Renderer) Label(com.vaadin.flow.component.html.Label) IronList(com.vaadin.flow.component.ironlist.IronList)

Example 3 with Renderer

use of com.vaadin.flow.data.renderer.Renderer in project flow-components by vaadin.

the class VirtualListPage method createTemplateFromRendererWithPeople.

private void createTemplateFromRendererWithPeople() {
    VirtualList<Person> list = new VirtualList<>();
    list.setHeight("100px");
    List<Person> people = createPeople(3);
    DataProvider<Person, ?> dataProvider = DataProvider.ofCollection(people);
    list.setDataProvider(dataProvider);
    list.setRenderer(TemplateRenderer.<Person>of("[[item.name]] - [[item.age]] - [[item.user]]").withProperty("name", Person::getName).withProperty("age", Person::getAge).withProperty("user", person -> person.getName().toLowerCase().replace(" ", "_")));
    NativeButton update = new NativeButton("Update item 1", evt -> {
        Person item = people.get(0);
        item.setName(item.getName() + " Updated");
        list.getDataProvider().refreshItem(item);
    });
    list.setId("template-renderer-with-people");
    update.setId("template-renderer-with-people-update-item");
    add(list, update);
}
Also used : IntStream(java.util.stream.IntStream) Text(com.vaadin.flow.component.Text) LocalDateRenderer(com.vaadin.flow.data.renderer.LocalDateRenderer) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Arrays(java.util.Arrays) Query(com.vaadin.flow.data.provider.Query) ValueProvider(com.vaadin.flow.function.ValueProvider) LocalDateTime(java.time.LocalDateTime) Div(com.vaadin.flow.component.html.Div) NativeButton(com.vaadin.flow.component.html.NativeButton) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Route(com.vaadin.flow.router.Route) NumberRenderer(com.vaadin.flow.data.renderer.NumberRenderer) LocalDateTimeRenderer(com.vaadin.flow.data.renderer.LocalDateTimeRenderer) List(java.util.List) NativeButtonRenderer(com.vaadin.flow.data.renderer.NativeButtonRenderer) Locale(java.util.Locale) DataProvider(com.vaadin.flow.data.provider.DataProvider) TemplateRenderer(com.vaadin.flow.data.renderer.TemplateRenderer) LocalDate(java.time.LocalDate) VirtualList(com.vaadin.flow.component.virtuallist.VirtualList) HasComponents(com.vaadin.flow.component.HasComponents) Renderer(com.vaadin.flow.data.renderer.Renderer) NativeButton(com.vaadin.flow.component.html.NativeButton) VirtualList(com.vaadin.flow.component.virtuallist.VirtualList)

Example 4 with Renderer

use of com.vaadin.flow.data.renderer.Renderer in project flow-components by vaadin.

the class VirtualListPage method createListWithComponentRenderer.

private void createListWithComponentRenderer() {
    VirtualList<String> list = new VirtualList<>();
    list.setHeight("100px");
    List<String> items = IntStream.range(1, 101).mapToObj(i -> "Item " + i).collect(Collectors.toList());
    list.setRenderer(new ComponentRenderer<>(item -> {
        Div text = new Div(new Text(item));
        text.addClassName("component-rendered");
        text.setHeight("18px");
        return text;
    }));
    list.setItems(items);
    list.setId("component-renderer");
    add(list);
}
Also used : IntStream(java.util.stream.IntStream) Text(com.vaadin.flow.component.Text) LocalDateRenderer(com.vaadin.flow.data.renderer.LocalDateRenderer) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Arrays(java.util.Arrays) Query(com.vaadin.flow.data.provider.Query) ValueProvider(com.vaadin.flow.function.ValueProvider) LocalDateTime(java.time.LocalDateTime) Div(com.vaadin.flow.component.html.Div) NativeButton(com.vaadin.flow.component.html.NativeButton) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Route(com.vaadin.flow.router.Route) NumberRenderer(com.vaadin.flow.data.renderer.NumberRenderer) LocalDateTimeRenderer(com.vaadin.flow.data.renderer.LocalDateTimeRenderer) List(java.util.List) NativeButtonRenderer(com.vaadin.flow.data.renderer.NativeButtonRenderer) Locale(java.util.Locale) DataProvider(com.vaadin.flow.data.provider.DataProvider) TemplateRenderer(com.vaadin.flow.data.renderer.TemplateRenderer) LocalDate(java.time.LocalDate) VirtualList(com.vaadin.flow.component.virtuallist.VirtualList) HasComponents(com.vaadin.flow.component.HasComponents) Renderer(com.vaadin.flow.data.renderer.Renderer) Div(com.vaadin.flow.component.html.Div) VirtualList(com.vaadin.flow.component.virtuallist.VirtualList) Text(com.vaadin.flow.component.Text)

Example 5 with Renderer

use of com.vaadin.flow.data.renderer.Renderer in project flow-components by vaadin.

the class IronListTestPage method createListWithComponentRendererWithBeansAndPlaceholder.

private void createListWithComponentRendererWithBeansAndPlaceholder() {
    IronList<Person> list = new IronList<>();
    list.setHeight("100px");
    List<Person> people = createPeople(100);
    list.setRenderer(new ComponentRenderer<Label, Person>(person -> {
        Label label = new Label(person.getName());
        label.addClassName("component-rendered");
        return label;
    }));
    list.setItems(people);
    list.setId("component-renderer-with-beans");
    Person placeholder = new Person();
    placeholder.setName("the-placeholder");
    list.setPlaceholderItem(placeholder);
    add(list);
}
Also used : IntStream(java.util.stream.IntStream) LocalDateRenderer(com.vaadin.flow.data.renderer.LocalDateRenderer) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Arrays(java.util.Arrays) Query(com.vaadin.flow.data.provider.Query) ValueProvider(com.vaadin.flow.function.ValueProvider) LocalDateTime(java.time.LocalDateTime) Div(com.vaadin.flow.component.html.Div) Label(com.vaadin.flow.component.html.Label) NativeButton(com.vaadin.flow.component.html.NativeButton) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Route(com.vaadin.flow.router.Route) NumberRenderer(com.vaadin.flow.data.renderer.NumberRenderer) LocalDateTimeRenderer(com.vaadin.flow.data.renderer.LocalDateTimeRenderer) List(java.util.List) NativeButtonRenderer(com.vaadin.flow.data.renderer.NativeButtonRenderer) Locale(java.util.Locale) DataProvider(com.vaadin.flow.data.provider.DataProvider) TemplateRenderer(com.vaadin.flow.data.renderer.TemplateRenderer) LocalDate(java.time.LocalDate) IronList(com.vaadin.flow.component.ironlist.IronList) HasComponents(com.vaadin.flow.component.HasComponents) Renderer(com.vaadin.flow.data.renderer.Renderer) Label(com.vaadin.flow.component.html.Label) IronList(com.vaadin.flow.component.ironlist.IronList)

Aggregations

DataProvider (com.vaadin.flow.data.provider.DataProvider)7 Query (com.vaadin.flow.data.provider.Query)7 ComponentRenderer (com.vaadin.flow.data.renderer.ComponentRenderer)7 Renderer (com.vaadin.flow.data.renderer.Renderer)7 TemplateRenderer (com.vaadin.flow.data.renderer.TemplateRenderer)7 ValueProvider (com.vaadin.flow.function.ValueProvider)7 ArrayList (java.util.ArrayList)7 Arrays (java.util.Arrays)7 List (java.util.List)7 Collectors (java.util.stream.Collectors)7 IntStream (java.util.stream.IntStream)7 HasComponents (com.vaadin.flow.component.HasComponents)6 Div (com.vaadin.flow.component.html.Div)6 NativeButton (com.vaadin.flow.component.html.NativeButton)6 LocalDateRenderer (com.vaadin.flow.data.renderer.LocalDateRenderer)6 LocalDateTimeRenderer (com.vaadin.flow.data.renderer.LocalDateTimeRenderer)6 NativeButtonRenderer (com.vaadin.flow.data.renderer.NativeButtonRenderer)6 NumberRenderer (com.vaadin.flow.data.renderer.NumberRenderer)6 Route (com.vaadin.flow.router.Route)6 LocalDate (java.time.LocalDate)6