Search in sources :

Example 1 with ComponentRenderer

use of com.vaadin.flow.data.renderer.ComponentRenderer in project karnak by OsiriX-Foundation.

the class DicomWorkListSelectionDialog method buildDicomNodeRenderer.

private ComponentRenderer<Div, ConfigNode> buildDicomNodeRenderer() {
    return new ComponentRenderer<Div, ConfigNode>(item -> {
        Div div = new Div();
        div.getStyle().set("line-height", "92%");
        Span spanDescription = new Span(item.getName());
        spanDescription.getStyle().set("font-weight", "500");
        HtmlComponent htmlLineBreak = new HtmlComponent("BR");
        Span spanOtherAttributes = new Span(item.getAet() + " | " + item.getHostname() + " | " + item.getPort());
        spanOtherAttributes.getStyle().set("font-size", "75%");
        div.add(spanDescription, htmlLineBreak, spanOtherAttributes);
        return div;
    });
}
Also used : Div(com.vaadin.flow.component.html.Div) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) HtmlComponent(com.vaadin.flow.component.HtmlComponent) Span(com.vaadin.flow.component.html.Span)

Example 2 with ComponentRenderer

use of com.vaadin.flow.data.renderer.ComponentRenderer in project aire-components by aire-ux.

the class ModuleGrid method populateGrid.

private Grid<Module> populateGrid() {
    val grid = new Grid<Module>();
    grid.addThemeVariants(GridVariant.LUMO_NO_BORDER);
    grid.addItemClickListener(event -> {
        setSelectedModule(event.getItem());
    });
    grid.setMultiSort(true);
    grid.setAllRowsVisible(true);
    grid.setColumnReorderingAllowed(true);
    grid.addComponentColumn((ValueProvider<Module, ModuleLifecycleButtonBar>) module -> new ModuleLifecycleButtonBar(getModuleLifecycleDelegate(), getZephyr(), module)).setResizable(true).setHeader("Lifecycle");
    grid.addColumn((ValueProvider<Module, String>) module -> module.getCoordinate().getGroup()).setSortable(true).setResizable(true).setHeader("Group");
    grid.addColumn((ValueProvider<Module, String>) module -> module.getCoordinate().getName()).setSortable(true).setResizable(true).setHeader("Name");
    grid.addColumn((ValueProvider<Module, String>) module -> module.getCoordinate().getVersion().toString()).setHeader("Version");
    grid.addColumn(new ComponentRenderer<>(Span::new, new StatusComponentUpdater())).setHeader("Status");
    grid.setItems(new ListDataProvider<>(getZephyr().getPlugins()));
    grid.getStyle().set("overflow-y", "auto");
    return grid;
}
Also used : lombok.val(lombok.val) ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) MenuBar(com.vaadin.flow.component.menubar.MenuBar) Component(com.vaadin.flow.component.Component) CssImport(com.vaadin.flow.component.dependency.CssImport) MenuBarVariant(com.vaadin.flow.component.menubar.MenuBarVariant) SerializableBiConsumer(com.vaadin.flow.function.SerializableBiConsumer) ValueProvider(com.vaadin.flow.function.ValueProvider) Route(com.vaadin.flow.router.Route) Inject(javax.inject.Inject) Overlays(io.sunshower.zephyr.ui.components.Overlays) ComponentValueChangeEvent(com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent) TextField(com.vaadin.flow.component.textfield.TextField) Notification(com.vaadin.flow.component.notification.Notification) ValueChangeMode(com.vaadin.flow.data.value.ValueChangeMode) Zephyr(io.zephyr.cli.Zephyr) Grid(com.vaadin.flow.component.grid.Grid) GridVariant(com.vaadin.flow.component.grid.GridVariant) NotificationVariant(com.vaadin.flow.component.notification.NotificationVariant) NonNull(lombok.NonNull) Position(com.vaadin.flow.component.notification.Notification.Position) lombok.val(lombok.val) VaadinIcon(com.vaadin.flow.component.icon.VaadinIcon) Breadcrumb(io.sunshower.zephyr.ui.controls.Breadcrumb) Collectors(java.util.stream.Collectors) ValueChangeListener(com.vaadin.flow.component.HasValue.ValueChangeListener) Button(com.vaadin.flow.component.button.Button) MainView(io.sunshower.zephyr.MainView) JsModule(com.vaadin.flow.component.dependency.JsModule) Span(com.vaadin.flow.component.html.Span) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) Module(io.zephyr.kernel.Module) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Grid(com.vaadin.flow.component.grid.Grid) JsModule(com.vaadin.flow.component.dependency.JsModule) Module(io.zephyr.kernel.Module) ValueProvider(com.vaadin.flow.function.ValueProvider) Span(com.vaadin.flow.component.html.Span)

Example 3 with ComponentRenderer

use of com.vaadin.flow.data.renderer.ComponentRenderer 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 4 with ComponentRenderer

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

the class LazyLoadingPage method createDataProviderWithCustomItemFilter.

private void createDataProviderWithCustomItemFilter() {
    addTitle("ListDataProvider with custom item filter");
    ComboBox<Person> comboBox = new ComboBox<>();
    comboBox.setId("custom-filter");
    List<Person> people = IntStream.range(0, 500).mapToObj(i -> new Person("Person", i)).collect(Collectors.toList());
    ListDataProvider<Person> personDataProvider = new ListDataProvider<>(people);
    comboBox.setRenderer(new ComponentRenderer<Div, Person>(person -> {
        return new Div(new H4(person.getName()), new Label("Born: " + person.getBorn()));
    }));
    comboBox.setDataProvider((person, filter) -> String.valueOf(person.getBorn()).startsWith(filter), personDataProvider);
    add(comboBox);
}
Also used : IntStream(java.util.stream.IntStream) ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Div(com.vaadin.flow.component.html.Div) Label(com.vaadin.flow.component.html.Label) NativeButton(com.vaadin.flow.component.html.NativeButton) CallbackDataProvider(com.vaadin.flow.data.provider.CallbackDataProvider) ComboBox(com.vaadin.flow.component.combobox.ComboBox) Collectors(java.util.stream.Collectors) H4(com.vaadin.flow.component.html.H4) Serializable(java.io.Serializable) Route(com.vaadin.flow.router.Route) ComboBoxInATemplate(com.vaadin.flow.component.combobox.test.template.ComboBoxInATemplate) List(java.util.List) Stream(java.util.stream.Stream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DataProvider(com.vaadin.flow.data.provider.DataProvider) Element(com.vaadin.flow.dom.Element) Collections(java.util.Collections) Paragraph(com.vaadin.flow.component.html.Paragraph) Span(com.vaadin.flow.component.html.Span) Div(com.vaadin.flow.component.html.Div) ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) ComboBox(com.vaadin.flow.component.combobox.ComboBox) Label(com.vaadin.flow.component.html.Label) H4(com.vaadin.flow.component.html.H4)

Example 5 with ComponentRenderer

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

the class GridViewUsingComponentsPage method createColumnComponentRenderer.

private void createColumnComponentRenderer() {
    Grid<Person> grid = new Grid<>();
    grid.setItems(createItems());
    // Use the component constructor that accepts an item ->
    // new PersonComponent(Person person)
    grid.addComponentColumn(PersonComponent::new).setHeader("Person");
    // Or you can use an ordinary function to setup the component
    grid.addComponentColumn(item -> new NativeButton("Remove", evt -> {
        ListDataProvider<Person> dataProvider = (ListDataProvider<Person>) grid.getDataProvider();
        dataProvider.getItems().remove(item);
        dataProvider.refreshAll();
    })).setHeader("Actions");
    // Item details can also use components
    grid.setItemDetailsRenderer(new ComponentRenderer<>(PersonCard::new));
    // When items are updated, new components are generated
    TextField idField = new TextField("", "Person id");
    TextField nameField = new TextField("", "New name");
    NativeButton updateButton = new NativeButton("Update person", event -> {
        String id = idField.getValue();
        String name = nameField.getValue();
        ListDataProvider<Person> dataProvider = (ListDataProvider<Person>) grid.getDataProvider();
        dataProvider.getItems().stream().filter(person -> String.valueOf(person.getId()).equals(id)).findFirst().ifPresent(person -> {
            person.setFirstName(name);
            dataProvider.refreshItem(person);
        });
    });
    grid.setSelectionMode(SelectionMode.NONE);
    grid.setId("component-renderer");
    idField.setId("component-renderer-id-field");
    nameField.setId("component-renderer-name-field");
    updateButton.setId("component-renderer-update-button");
    addCard("Using components", "Grid with columns using component renderer", grid, idField, nameField, updateButton);
}
Also used : ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Person(com.vaadin.flow.data.bean.Person) Grid(com.vaadin.flow.component.grid.Grid) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) Div(com.vaadin.flow.component.html.Div) NativeButton(com.vaadin.flow.component.html.NativeButton) SelectionMode(com.vaadin.flow.component.grid.Grid.SelectionMode) TextField(com.vaadin.flow.component.textfield.TextField) Span(com.vaadin.flow.component.html.Span) Route(com.vaadin.flow.router.Route) NativeButton(com.vaadin.flow.component.html.NativeButton) ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) Grid(com.vaadin.flow.component.grid.Grid) TextField(com.vaadin.flow.component.textfield.TextField) Person(com.vaadin.flow.data.bean.Person)

Aggregations

ComponentRenderer (com.vaadin.flow.data.renderer.ComponentRenderer)49 Div (com.vaadin.flow.component.html.Div)33 Route (com.vaadin.flow.router.Route)30 Grid (com.vaadin.flow.component.grid.Grid)23 HorizontalLayout (com.vaadin.flow.component.orderedlayout.HorizontalLayout)20 List (java.util.List)19 Icon (com.vaadin.flow.component.icon.Icon)18 NativeButton (com.vaadin.flow.component.html.NativeButton)14 VaadinIcon (com.vaadin.flow.component.icon.VaadinIcon)14 TemplateRenderer (com.vaadin.flow.data.renderer.TemplateRenderer)14 Collectors (java.util.stream.Collectors)14 Label (com.vaadin.flow.component.html.Label)13 Span (com.vaadin.flow.component.html.Span)12 EnhancedButton (org.komunumo.ui.component.EnhancedButton)12 Component (com.vaadin.flow.component.Component)10 H2 (com.vaadin.flow.component.html.H2)10 ArrayList (java.util.ArrayList)10 Element (com.vaadin.flow.dom.Element)9 Collections (java.util.Collections)9 UI (com.vaadin.flow.component.UI)8