Search in sources :

Example 1 with LitRenderer

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

the class ComboBox method render.

private void render() {
    renderingRegistrations.forEach(Registration::remove);
    renderingRegistrations.clear();
    Rendering<T> rendering;
    if (renderer instanceof LitRenderer) {
        // LitRenderer
        if (template != null && template.getParent() != null) {
            getElement().removeChild(template);
        }
        rendering = renderer.render(getElement(), dataCommunicator.getKeyMapper());
    } else {
        // TemplateRenderer or ComponentRenderer
        if (template == null) {
            template = new Element("template");
        }
        if (template.getParent() == null) {
            getElement().appendChild(template);
        }
        rendering = renderer.render(getElement(), dataCommunicator.getKeyMapper(), template);
    }
    rendering.getDataGenerator().ifPresent(renderingDataGenerator -> {
        Registration renderingDataGeneratorRegistration = dataGenerator.addDataGenerator(renderingDataGenerator);
        renderingRegistrations.add(renderingDataGeneratorRegistration);
    });
    renderingRegistrations.add(rendering.getRegistration());
    reset();
}
Also used : Registration(com.vaadin.flow.shared.Registration) Element(com.vaadin.flow.dom.Element) LitRenderer(com.vaadin.flow.data.renderer.LitRenderer)

Example 2 with LitRenderer

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

the class VirtualList method setRenderer.

/**
 * Sets a renderer for the items in the list.
 * <p>
 * When set, a same renderer is used for the placeholder item. See
 * {@link #setPlaceholderItem(Object)} for details.
 *
 * @param renderer
 *            a renderer for the items in the list, not <code>null</code>
 */
public void setRenderer(Renderer<T> renderer) {
    Objects.requireNonNull(renderer, "The renderer must not be null");
    renderingRegistrations.forEach(Registration::remove);
    renderingRegistrations.clear();
    Rendering<T> rendering;
    if (renderer instanceof LitRenderer) {
        // LitRenderer
        if (template.getParent() != null) {
            getElement().removeChild(template);
        }
        rendering = renderer.render(getElement(), dataCommunicator.getKeyMapper());
    } else {
        // TemplateRenderer or ComponentRenderer
        if (template.getParent() == null) {
            getElement().appendChild(template);
        }
        rendering = renderer.render(getElement(), dataCommunicator.getKeyMapper(), template);
    }
    rendering.getDataGenerator().ifPresent(renderingDataGenerator -> {
        Registration renderingDataGeneratorRegistration = dataGenerator.addDataGenerator(renderingDataGenerator);
        renderingRegistrations.add(renderingDataGeneratorRegistration);
    });
    renderingRegistrations.add(rendering.getRegistration());
    this.renderer = renderer;
    rendererChanged = true;
    registerTemplateUpdate();
    getDataCommunicator().reset();
}
Also used : Registration(com.vaadin.flow.shared.Registration) LitRenderer(com.vaadin.flow.data.renderer.LitRenderer)

Example 3 with LitRenderer

use of com.vaadin.flow.data.renderer.LitRenderer in project komunumo-server by komunumo.

the class MembersView method configureGrid.

private void configureGrid() {
    final var sponsorDomains = databaseService.getActiveSponsorDomains();
    grid.setSelectionMode(Grid.SelectionMode.NONE);
    grid.addThemeVariants(GridVariant.LUMO_NO_BORDER, GridVariant.LUMO_ROW_STRIPES);
    grid.addColumn(LitRenderer.<Member>of("<span style=\"font-weight: bold;\">${item.fullName}</span><br/><span>${item.company}</span>").withProperty("fullName", Member::getFullName).withProperty("company", Member::getCompany)).setHeader("Name").setAutoWidth(true).setFlexGrow(1);
    grid.addColumn(LitRenderer.<Member>of("<a href=\"mailto:${item.email}\" target=\"_blank\">${item.email}</a>").withProperty("email", Member::getEmail)).setHeader("Email").setAutoWidth(true).setKey("email").setFlexGrow(0);
    grid.addColumn(new ComponentRenderer<>(member -> new Text(getMembershipText(member, sponsorDomains)))).setHeader("Membership").setAutoWidth(true).setFlexGrow(0);
    grid.addColumn(new ComponentRenderer<>(member -> new Icon(member.getAdmin() ? VaadinIcon.CHECK : VaadinIcon.MINUS))).setHeader("Admin").setAutoWidth(true).setTextAlign(ColumnTextAlign.CENTER).setFlexGrow(0);
    grid.addColumn(new ComponentRenderer<>(member -> {
        final var icon = new Icon(member.getAccountBlocked() ? VaadinIcon.BAN : VaadinIcon.MINUS);
        icon.getElement().setAttribute("title", member.getAccountBlockedReason());
        return icon;
    })).setHeader("Blocked").setAutoWidth(true).setTextAlign(ColumnTextAlign.CENTER).setFlexGrow(0);
    grid.addColumn(new ComponentRenderer<>(member -> {
        final var editButton = new EnhancedButton(new Icon(VaadinIcon.EDIT), clickEvent -> showMemberDialog(member));
        editButton.setTitle("Edit this member");
        final var deleteButton = new EnhancedButton(new Icon(VaadinIcon.TRASH), clickEvent -> deleteMember(member));
        deleteButton.setTitle("Delete this member");
        return new HorizontalLayout(editButton, deleteButton);
    })).setHeader("Actions").setAutoWidth(true).setFlexGrow(0);
    grid.setHeightFull();
}
Also used : Member(org.komunumo.data.entity.Member) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) HasUrlParameter(com.vaadin.flow.router.HasUrlParameter) AdminLayout(org.komunumo.ui.view.admin.AdminLayout) RolesAllowed(javax.annotation.security.RolesAllowed) CssImport(com.vaadin.flow.component.dependency.CssImport) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) EnhancedButton(org.komunumo.ui.component.EnhancedButton) PageTitle(com.vaadin.flow.router.PageTitle) BeforeEvent(com.vaadin.flow.router.BeforeEvent) FormatterUtil.formatDate(org.komunumo.util.FormatterUtil.formatDate) Route(com.vaadin.flow.router.Route) ByteArrayInputStream(java.io.ByteArrayInputStream) UI(com.vaadin.flow.component.UI) ResizableView(org.komunumo.ui.component.ResizableView) Icon(com.vaadin.flow.component.icon.Icon) TextField(com.vaadin.flow.component.textfield.TextField) StreamRegistration(com.vaadin.flow.server.StreamRegistration) Role(org.komunumo.data.entity.Role) Text(com.vaadin.flow.component.Text) VaadinSession(com.vaadin.flow.server.VaadinSession) Serial(java.io.Serial) Grid(com.vaadin.flow.component.grid.Grid) GridVariant(com.vaadin.flow.component.grid.GridVariant) LitRenderer(com.vaadin.flow.data.renderer.LitRenderer) UTF_8(java.nio.charset.StandardCharsets.UTF_8) StringWriter(java.io.StringWriter) VaadinIcon(com.vaadin.flow.component.icon.VaadinIcon) Set(java.util.Set) CSVWriter(com.opencsv.CSVWriter) OptionalParameter(com.vaadin.flow.router.OptionalParameter) Nullable(org.jetbrains.annotations.Nullable) DatabaseService(org.komunumo.data.service.DatabaseService) List(java.util.List) LocalDate(java.time.LocalDate) ColumnTextAlign(com.vaadin.flow.component.grid.ColumnTextAlign) StreamResource(com.vaadin.flow.server.StreamResource) ConfirmDialog(com.vaadin.flow.component.confirmdialog.ConfirmDialog) NotNull(org.jetbrains.annotations.NotNull) FilterField(org.komunumo.ui.component.FilterField) FormatterUtil.formatDateTime(org.komunumo.util.FormatterUtil.formatDateTime) EnhancedButton(org.komunumo.ui.component.EnhancedButton) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Text(com.vaadin.flow.component.Text) Icon(com.vaadin.flow.component.icon.Icon) VaadinIcon(com.vaadin.flow.component.icon.VaadinIcon) Member(org.komunumo.data.entity.Member) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout)

Example 4 with LitRenderer

use of com.vaadin.flow.data.renderer.LitRenderer in project linkki by linkki-framework.

the class BindComboBoxItemStyleAspectDefinitionTest method testCreateComponentValueSetter.

@Test
void testCreateComponentValueSetter() {
    MockVaadin.setup();
    String styleName = "bar";
    BindComboBoxItemStyleAspectDefinition aspectDefinition = new BindComboBoxItemStyleAspectDefinition(styleName);
    ComboBox<Object> comboBox = spy(ComponentFactory.newComboBox());
    @SuppressWarnings("unchecked") ArgumentCaptor<LitRenderer<Object>> argumentCaptor = ArgumentCaptor.forClass(LitRenderer.class);
    ComponentWrapper componentWrapper = new NoLabelComponentWrapper(comboBox);
    Consumer<Function<Object, String>> componentValueSetter = aspectDefinition.createComponentValueSetter(componentWrapper);
    componentValueSetter.accept(o -> EXPECTED_STYLE);
    verify(comboBox).setRenderer(argumentCaptor.capture());
    LitRenderer<Object> renderer = argumentCaptor.getValue();
    Rendering<Object> render = renderer.render(new Element("div"), new KeyMapper<>());
    JreJsonObject jsonObject = new JreJsonObject(new JreJsonFactory());
    render.getDataGenerator().get().generateData(EXPECTED_LABEL, jsonObject);
    assertThat(jsonObject.get("lr_0_" + BindComboBoxItemStyleAspectDefinition.LABEL).asString(), is(EXPECTED_LABEL));
    assertThat(jsonObject.get("lr_0_" + BindComboBoxItemStyleAspectDefinition.STYLE).asString(), is(EXPECTED_STYLE));
    MockVaadin.tearDown();
}
Also used : JreJsonObject(elemental.json.impl.JreJsonObject) Element(com.vaadin.flow.dom.Element) ComponentWrapper(org.linkki.core.binding.wrapper.ComponentWrapper) NoLabelComponentWrapper(org.linkki.core.ui.wrapper.NoLabelComponentWrapper) Function(java.util.function.Function) JreJsonFactory(elemental.json.impl.JreJsonFactory) NoLabelComponentWrapper(org.linkki.core.ui.wrapper.NoLabelComponentWrapper) LitRenderer(com.vaadin.flow.data.renderer.LitRenderer) JreJsonObject(elemental.json.impl.JreJsonObject) Test(org.junit.jupiter.api.Test)

Example 5 with LitRenderer

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

the class Grid method setItemDetailsRenderer.

/**
 * Set the renderer to use for displaying the item details rows in this
 * grid.
 *
 * @param renderer
 *            the renderer to use for displaying item details rows,
 *            {@code null} to remove the current renderer
 */
public void setItemDetailsRenderer(Renderer<T> renderer) {
    detailsRenderingRegistrations.forEach(Registration::remove);
    detailsRenderingRegistrations.clear();
    if (renderer == null) {
        return;
    }
    Rendering<T> rendering;
    if (renderer instanceof LitRenderer) {
        // LitRenderer
        if (detailsTemplate != null && detailsTemplate.getParent() != null) {
            getElement().removeChild(detailsTemplate);
        }
        rendering = ((LitRenderer<T>) renderer).render(getElement(), dataCommunicator.getKeyMapper(), "rowDetailsRenderer");
    } else {
        // TemplateRenderer or ComponentRenderer
        if (detailsTemplate == null) {
            rendering = renderer.render(getElement(), getDataCommunicator().getKeyMapper());
            detailsTemplate = rendering.getTemplateElement();
            detailsTemplate.setAttribute("class", "row-details");
        } else {
            getElement().appendChild(detailsTemplate);
            rendering = renderer.render(getElement(), getDataCommunicator().getKeyMapper(), detailsTemplate);
        }
    }
    rendering.getDataGenerator().ifPresent(renderingDataGenerator -> {
        itemDetailsDataGenerator = renderingDataGenerator;
        Registration detailsRenderingDataGeneratorRegistration = () -> {
            detailsManager.destroyAllData();
            itemDetailsDataGenerator = null;
        };
        detailsRenderingRegistrations.add(detailsRenderingDataGeneratorRegistration);
    });
    detailsRenderingRegistrations.add(rendering.getRegistration());
}
Also used : Registration(com.vaadin.flow.shared.Registration) LitRenderer(com.vaadin.flow.data.renderer.LitRenderer)

Aggregations

LitRenderer (com.vaadin.flow.data.renderer.LitRenderer)5 Registration (com.vaadin.flow.shared.Registration)3 Element (com.vaadin.flow.dom.Element)2 CSVWriter (com.opencsv.CSVWriter)1 Text (com.vaadin.flow.component.Text)1 UI (com.vaadin.flow.component.UI)1 ConfirmDialog (com.vaadin.flow.component.confirmdialog.ConfirmDialog)1 CssImport (com.vaadin.flow.component.dependency.CssImport)1 ColumnTextAlign (com.vaadin.flow.component.grid.ColumnTextAlign)1 Grid (com.vaadin.flow.component.grid.Grid)1 GridVariant (com.vaadin.flow.component.grid.GridVariant)1 Icon (com.vaadin.flow.component.icon.Icon)1 VaadinIcon (com.vaadin.flow.component.icon.VaadinIcon)1 HorizontalLayout (com.vaadin.flow.component.orderedlayout.HorizontalLayout)1 TextField (com.vaadin.flow.component.textfield.TextField)1 ComponentRenderer (com.vaadin.flow.data.renderer.ComponentRenderer)1 BeforeEvent (com.vaadin.flow.router.BeforeEvent)1 HasUrlParameter (com.vaadin.flow.router.HasUrlParameter)1 OptionalParameter (com.vaadin.flow.router.OptionalParameter)1 PageTitle (com.vaadin.flow.router.PageTitle)1