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

org.bklab.flow.util.dialog.ErrorMessageDialogBuilder Maven / Gradle / Ivy

There is a newer version: 22.0.1
Show newest version
/*
 * Copyright (c) 2008 - 2021. - Broderick Labs.
 * Author: Broderick Johansson
 * E-mail: [email protected]
 * Modify date:2021-04-13 14:13:33
 * _____________________________
 * Project name: fluent-vaadin-flow
 * Class name:org.bklab.flow.util.dialog.ErrorMessageDialogBuilder
 * Copyright (c) 2008 - 2021. - Broderick Labs.
 */

package org.bklab.flow.util.dialog;

import org.bklab.flow.dialog.ErrorDialog;

import java.util.*;
import java.util.function.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class ErrorMessageDialogBuilder implements Collector {

    private final List messages = new ArrayList<>();
    private final List noErrorListeners = new ArrayList<>();
    private final List hasErrorListeners = new ArrayList<>();

    public ErrorMessageDialogBuilder add(String message) {
        this.messages.add(message);
        List collect = Stream.of("").collect(Collectors.toList());
        return this;
    }

    public ErrorMessageDialogBuilder add(boolean add, String message) {
        return add ? add(message) : this;
    }

    public ErrorMessageDialogBuilder add(String... messages) {
        this.messages.addAll(Arrays.asList(messages));
        return this;
    }

    public ErrorMessageDialogBuilder add(Collection messages) {
        this.messages.addAll(messages);
        return this;
    }

    public ErrorMessageDialogBuilder add(boolean add, String... messages) {
        return add ? add(messages) : this;
    }

    public  ErrorMessageDialogBuilder add(Predicate predicate, E test, String... messages) {
        return predicate.test(test) ? add(messages) : this;
    }

    public boolean isValid() {
        return messages.isEmpty();
    }

    public ErrorMessageDialogBuilder whenNoError(NoErrorListener noErrorListener) {
        if (noErrorListener != null) this.noErrorListeners.add(noErrorListener);
        return this;
    }

    public ErrorMessageDialogBuilder whenHasError(HasErrorListener hasErrorListener) {
        if (hasErrorListener != null) this.hasErrorListeners.add(hasErrorListener);
        return this;
    }

    public boolean computeAndOpen() {
        return computeAndOpen(null);
    }

    public boolean computeAndOpen(String header) {
        if (messages.isEmpty()) {
            noErrorListeners.forEach(NoErrorListener::onNoErrors);
            return true;
        }
        hasErrorListeners.forEach(a -> a.onHasErrors(this));
        ErrorDialog errorDialog = new ErrorDialog(IntStream.range(0, messages.size())
                .mapToObj(i -> (i + 1) + ". " + messages.get(i)).collect(Collectors.toList()));
        if (header != null && !header.isBlank()) {
            errorDialog.header(header);
        }
        errorDialog.build().open();
        return false;
    }

    public List getMessages() {
        return messages;
    }

    @Override
    public Supplier supplier() {
        return () -> this;
    }

    @Override
    public BiConsumer accumulator() {
        return ErrorMessageDialogBuilder::add;
    }

    @Override
    public BinaryOperator combiner() {
        return (left, right) -> left.add(right.messages);
    }

    @Override
    public Function finisher() {
        return Function.identity();
    }

    @Override
    public Set characteristics() {
        return Set.of(Characteristics.IDENTITY_FINISH);
    }

    public interface NoErrorListener {
        void onNoErrors();
    }

    public interface HasErrorListener {
        void onHasErrors(ErrorMessageDialogBuilder builder);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy