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

org.bklab.flow.grid.FluentCommonGrid Maven / Gradle / Ivy

There is a newer version: 22.0.1
Show newest version
package org.bklab.flow.grid;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.grid.ColumnTextAlign;
import com.vaadin.flow.component.grid.Grid;
import org.bklab.flow.components.badge.BadgeTag;
import org.bklab.flow.components.badge.BadgeTagStyle;
import org.bklab.flow.factory.ButtonFactory;
import org.bklab.flow.factory.GridFactory;
import org.bklab.flow.factory.SpanFactory;
import org.bklab.flow.util.number.DigitalFormatter;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Comparator;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.IntStream;

public interface FluentCommonGrid extends Supplier> {

    default Grid.Column addIndexColumn() {
        return get().addColumn(r -> {
            int i = IntStream.range(0, get().getDataCommunicator().getItemCount())
                            .filter(a -> r == get().getDataCommunicator().getItem(a))
                            .findFirst().orElse(-1) + 1;
            return i <= 0 ? "-" : new DigitalFormatter(i).toInteger();
        }).setHeader("#").setWidth("6em").setTextAlign(ColumnTextAlign.END).setKey("INDEX_COLUMN");
    }

    default Grid.Column addStringColumn(Function function, String header, String key) {
        return get().addColumn(function::apply).setHeader(header).setKey(key).setComparator(function::apply);
    }

    default Grid.Column addStringTooltipColumn(Function function, String header, String key) {
        return get().addComponentColumn(t -> {
            String content = function.apply(t);
            return new SpanFactory(content).tooltip(content).textOverflowEllipsis().alignContentBaseline().padding("0").get();
        }).setHeader(header).setKey(key).setComparator(function::apply);
    }

    default Grid.Column addIntColumn(Function function, String suffix, String header, String key) {
        return get().addColumn(r -> new DigitalFormatter(function.apply(r)).toInteger() + " " + suffix)
                .setComparator(Comparator.comparingInt(function::apply))
                .setKey(key).setHeader(header)
                ;
    }

    default Grid.Column addLongColumn(Function function, String suffix, String header, String key) {
        return get().addColumn(r -> new DigitalFormatter(function.apply(r)).toInteger() + " " + suffix)
                .setComparator(Comparator.comparingLong(function::apply))
                .setKey(key).setHeader(header)
                ;
    }

    default Grid.Column addDataColumn(Function function, String header, String key) {
        return get().addColumn(r -> new DigitalFormatter(function.apply(r)).toDataSize())
                .setComparator(Comparator.comparingLong(function::apply))
                .setKey(key).setHeader(header)
                ;
    }

    default Grid.Column addDoubleColumn(Function function, String suffix, String header, String key) {
        return get().addColumn(r -> new DigitalFormatter(function.apply(r)).toFormatted() + " " + suffix)
                .setComparator(Comparator.comparingDouble(function::apply))
                .setKey(key).setHeader(header);
    }

    default Grid.Column addDateTimeColumn(Function function, String header, String key) {
        return get().addColumn(r -> Optional.ofNullable(function.apply(r))
                .map(DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss")::format).orElse(null))
                .setComparator(Comparator.comparingLong(r -> Optional.ofNullable(function.apply(r))
                        .map(t -> t.toEpochSecond(ZoneOffset.ofHours(8))).orElse(0L)))
                .setKey(key).setHeader(header);
    }

    default Grid.Column addDateColumn(Function function, String header, String key) {
        return get().addColumn(r -> Optional.ofNullable(function.apply(r))
                .map(DateTimeFormatter.ofPattern("uuuu-MM-dd")::format).orElse(null))
                .setComparator(Comparator.comparingLong(r -> Optional.ofNullable(function.apply(r))
                        .map(LocalDate::toEpochDay).orElse(0L)))
                .setKey(key).setHeader(header);
    }

    default Grid.Column addTimeColumn(Function function, String header, String key) {
        return get().addColumn(r -> Optional.ofNullable(function.apply(r))
                .map(DateTimeFormatter.ofPattern("HH:mm:ss")::format).orElse(null))
                .setComparator(Comparator.comparingLong(r -> Optional.ofNullable(function.apply(r))
                        .map(LocalTime::toNanoOfDay).orElse(0L)))
                .setKey(key).setHeader(header);
    }

    default Button createDetailRenderVisibilityButton(T entity, Map buttonsMap) {
        Function caption = e -> get().isDetailsVisible(e) ? "收起" : "详情";
        return new ButtonFactory(caption.apply(entity), e -> {
            get().setDetailsVisible(entity, !get().isDetailsVisible(entity));
            buttonsMap.forEach((r, b) -> b.setText(caption.apply(r)));
        }).peek(b -> buttonsMap.put(entity, b)).lumoSmall().lumoTertiaryInline().get();
    }

    default Grid.Column addBadgeTagBooleanGreenWhiteColumn(
            Function valueProvider, String trueValue, String falseValue) {
        return addBadgeTagBooleanColumn(valueProvider, trueValue, falseValue, BadgeTagStyle.GREEN, BadgeTagStyle.GREY);
    }

    default Grid.Column addBadgeTagBooleanBlueWhiteColumn(
            Function valueProvider, String trueValue, String falseValue) {
        return addBadgeTagBooleanColumn(valueProvider, trueValue, falseValue, BadgeTagStyle.BLUE, BadgeTagStyle.GREY);
    }

    default Grid.Column addBadgeTagBooleanBlueGreenColumn(
            Function valueProvider, String trueValue, String falseValue) {
        return addBadgeTagBooleanColumn(valueProvider, trueValue, falseValue, BadgeTagStyle.GREEN, BadgeTagStyle.BLUE);
    }

    default Grid.Column addBadgeTagBooleanColumn(Function valueProvider,
                                                    String trueValue, String falseValue,
                                                    BadgeTagStyle trueStyle, BadgeTagStyle falseStyle) {
        return addBadgeTagColumn(
                t -> valueProvider.apply(t) ? trueValue : falseValue,
                t -> valueProvider.apply(t) ? trueStyle : falseStyle
        ).setComparator(Comparator.comparingInt(t -> valueProvider.apply(t) ? 1 : 0));
    }

    default Grid.Column addBadgeTagColumn(Function valueProvider, Function styleProvider) {
        return get().addComponentColumn(t -> new BadgeTag(valueProvider.apply(t), styleProvider.apply(t)));
    }

    default GridFactory asFactory() {
        return new GridFactory<>(get());
    }

    default GridFactory initCommonGrid() {
        return asFactory().columnAutoWidth().columnAlignCenter().columnReorderingAllowed(true).sizeFull();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy