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

com.databasesandlife.util.wicket.LambdaDisplayValueChoiceRenderer Maven / Gradle / Ivy

The newest version!
package com.databasesandlife.util.wicket;

import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.model.IModel;
import org.danekja.java.util.function.serializable.SerializableFunction;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.List;

public class LambdaDisplayValueChoiceRenderer implements IChoiceRenderer {
    public final @Nonnull SerializableFunction supplier;

    public LambdaDisplayValueChoiceRenderer(@Nonnull SerializableFunction supplier) {
        this.supplier = supplier;
    }

    @Override public String getDisplayValue(@CheckForNull T object) {
        if (object == null) return null;
        else return supplier.apply(object);
    }

    @Override public String getIdValue(T object, int index) {
        return Integer.toString(index);
    }

    @Override public T getObject(String id, IModel> choices) {
        try {
            return choices.getObject().get(Integer.parseInt(id));
        }
        catch (NumberFormatException e) {
            // For example, user selected "Please choose" which returns id ""
            return null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy