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

com.dua3.fx.controls.AlertBuilder 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 javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.DialogPane;
import javafx.stage.Window;

import java.net.URL;
import java.util.Arrays;

/**
 * Builder for Alert Dialogs.
 * Provides a fluent interface to create Alerts.
 */
public class AlertBuilder
        extends AbstractDialogBuilder {
    private String css = null;
    private String text = null;
    private ButtonType[] buttons;
    private ButtonType defaultButton;

    AlertBuilder(AlertType type, Window parentWindow) {
        super(parentWindow);
        setDialogSupplier(() -> new Alert(type));
    }

    /**
     * Create Alert instance.
     *
     * @return Alert instance
     */
    @Override
    public Alert build() {
        Alert dlg = super.build();

        if (css != null) {
            dlg.getDialogPane().getScene().getStylesheets().add(css);
        }

        if (buttons != null) {
            dlg.getButtonTypes().setAll(buttons);
        }

        if (defaultButton != null) {
            DialogPane pane = dlg.getDialogPane();
            for (ButtonType t : dlg.getButtonTypes()) {
                ((Button) pane.lookupButton(t)).setDefaultButton(t == defaultButton);
            }
        }

        if (text != null) {
            dlg.setContentText(text);
        }

        return dlg;
    }

    /**
     * Set text.
     *
     * @param fmt  the format String as defined by {@link java.util.Formatter}
     * @param args the arguments passed to the formatter
     * @return {@code this}
     */
    public AlertBuilder text(String fmt, Object... args) {
        this.text = format(fmt, args);
        return this;
    }

    /**
     * Define Alert Buttons.
     *
     * @param buttons the buttons to show
     * @return {@code this}
     */
    public AlertBuilder buttons(ButtonType... buttons) {
        this.buttons = Arrays.copyOf(buttons, buttons.length);
        return this;
    }

    /**
     * Define the default Buttons.
     *
     * @param button the button to use as default
     * @return {@code this}
     */
    public AlertBuilder defaultButton(ButtonType button) {
        this.defaultButton = button;
        return this;
    }

    /**
     * Set supplemental CSS.
     *
     * @param css the name of the CSS resource to load ({@link URL#toExternalForm()}
     * @return this
     */
    public AlertBuilder css(String css) {
        this.css = css;
        return this;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy