
org.dellroad.stuff.vaadin24.util.SelfRenderer Maven / Gradle / Ivy
Show all versions of dellroad-stuff-vaadin24 Show documentation
/*
* Copyright (C) 2022 Archie L. Cobbs. All rights reserved.
*/
package org.dellroad.stuff.vaadin24.util;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Text;
import com.vaadin.flow.data.renderer.ComponentRenderer;
import com.vaadin.flow.function.SerializableFunction;
import com.vaadin.flow.function.ValueProvider;
import java.util.Optional;
import org.dellroad.stuff.vaadin24.grid.GridColumn;
import org.dellroad.stuff.vaadin24.grid.GridColumnScanner;
/**
* A "do nothing" {@link ComponentRenderer} used to render values that are already {@link Component}'s.
*
*
*
*
*
*
* This becomes the default {@link GridColumn#renderer renderer()} for {@link GridColumn @GridColumn} annotations
* on methods that return sub-types of {@link Component}:
*
* public class User {
*
* // Render username normally - no special Renderer is used
* @GridColumn(header = "Username", width = "50px")
* public String getUsername() { ... }
*
* // Render status with an Image - as if we had said "renderer = SelfRenderer.class"
* @GridColumn(header = "Status", width = "64px")
* public Image statusColumn() { ... }
* }
*
*
*
* This class includes graceful handling of null values; null values are converted into empty {@link Text} components.
*
* @param the type of the input model object
* @see GridColumn
* @see GridColumnScanner
*/
@SuppressWarnings("serial")
public class SelfRenderer extends ComponentRenderer {
/**
* Constructor.
*
* @param valueProvider creates a component from a model instance
* @throws IllegalArgumentException if {@code valueProvider} is null
*/
public SelfRenderer(final ValueProvider super T, ? extends Component> valueProvider) {
super(new SerializableFunction() {
@Override
public Component apply(T obj) {
return Optional.ofNullable(obj)
.map(valueProvider::apply)
.orElseGet(() -> new Text(""));
}
});
if (valueProvider == null)
throw new IllegalArgumentException("null valueProvider");
}
}