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

com.dua3.fx.controls.AbstractDialogPaneBuilder Maven / Gradle / Ivy

The newest version!
// Copyright 2019 Axel Howind
// 
// 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.dua3.fx.controls;

import com.dua3.cabe.annotations.Nullable;
import javafx.scene.control.ButtonType;

import java.util.function.BiConsumer;
import java.util.function.Supplier;

/**
 * Abstract base class for DialogPane builders.
 * 

* Provides a fluent interface to create Dialog panes. * * @param the type of the dialog or pane to build * @param the type of the builder * @param the result type */ public abstract class AbstractDialogPaneBuilder, R> { private final BiConsumer headerSetter; private Supplier dialogSupplier; private String header = null; private ResultHandler resultHandler = (b, r) -> true; AbstractDialogPaneBuilder( BiConsumer headerSetter ) { this.dialogSupplier = () -> {throw new IllegalStateException("call setDialogSupplier() first");}; this.headerSetter = headerSetter; } protected final void setDialogSupplier(Supplier dialogSupplier) { this.dialogSupplier = dialogSupplier; } /** * Create Alert instance. * * @return Alert instance */ public D build() { D dlg = dialogSupplier.get(); applyIfNotNull(headerSetter, dlg, header); return dlg; } protected static void applyIfNotNull(BiConsumer consumer, @Nullable C a, @Nullable D b) { if (a != null && b != null) { consumer.accept(a, b); } } /** * Set Alert header text. * * @param fmt the format String as defined by {@link java.util.Formatter} * @param args the arguments passed to the formatter * @return {@code this} */ @SuppressWarnings("unchecked") public B header(String fmt, Object... args) { this.header = format(fmt, args); return (B) this; } protected static String format(String fmt, Object... args) { return String.format(fmt, args); } @SuppressWarnings("unchecked") public B resultHandler(ResultHandler resultHandler) { this.resultHandler = resultHandler; return (B) this; } public ResultHandler getResultHandler() { return resultHandler; } /** * Dialog(Pane) result handler. * * @param the result type */ @FunctionalInterface public interface ResultHandler { /** * Handle result. * * @param btn the button that was pressed * @param result the dialog/pane result as returned by the result converter * @return true, if it's ok to proceed (the current page should be left) * false otherwise */ boolean handleResult(ButtonType btn, R result); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy