All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vaadin.flow.data.renderer.TextRenderer Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2000-2024 Vaadin Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.vaadin.flow.data.renderer;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.ItemLabelGenerator;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.function.ValueProvider;

/**
 * A renderer that renders each item as a text using provided
 * {@link ItemLabelGenerator}.
 *
 * @author Vaadin Ltd
 *
 * @param 
 *            the type of the input object that can be used by the rendered
 *            component
 *
 */
public class TextRenderer extends BasicRenderer {

    private static class TextRendererComponent extends Component {

        TextRendererComponent(Element element) {
            super(element);
        }
    }

    private final ItemLabelGenerator itemLabelGenerator;

    /**
     * Creates a new renderer instance using the default
     * {@link ItemLabelGenerator}: String::valueOf.
     */
    public TextRenderer() {
        this(String::valueOf);
    }

    /**
     * Creates a new renderer instance using the provided
     * {@code itemLabelGenerator}.
     *
     * @param itemLabelGenerator
     *            the item label generator
     */
    public TextRenderer(ItemLabelGenerator itemLabelGenerator) {
        super(ValueProvider.identity());
        this.itemLabelGenerator = itemLabelGenerator;
    }

    @Override
    public Component createComponent(ITEM item) {
        String text = getFormattedValue(item);
        if (text == null) {
            throw new IllegalStateException(String.format(
                    "Got 'null' as a label value for the item '%s'. "
                            + "'%s' instance may not return 'null' values",
                    item, ItemLabelGenerator.class.getSimpleName()));
        }
        return new TextRendererComponent(createElement(text));
    }

    /**
     * Creates a new {@link Element} that represent the rendered {@code item}.
     * 

* By default the text is wrapped inside a {@code } element. * Subclasses may override this method to return some other {@link Element}. * * @param item * the item to render * @return the element representing rendered item */ protected Element createElement(String item) { return new Element("span").setText(item); } @Override protected String getFormattedValue(ITEM item) { return itemLabelGenerator.apply(item); } @Override protected String getTemplateExpression() { return "${item.label}"; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy