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

org.panteleyev.fx.BaseDialog Maven / Gradle / Ivy

The newest version!
/*
 Copyright © 2020-2021 Petr Panteleyev 
 SPDX-License-Identifier: BSD-2-Clause
 */
package org.panteleyev.fx;

import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;

import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;

/**
 * Base class for dialogs.
 *
 * @param  The return type of the dialog, via the result property.
 */
public class BaseDialog extends Dialog {
    /**
     * Creates dialog instance with no owner and no style sheet.
     */
    public BaseDialog() {
    }

    /**
     * Creates dialog instance with specified owner.
     *
     * @param owner owner
     */
    public BaseDialog(Controller owner) {
        this();
        if (owner != null) {
            initOwner(owner.getStage());
        }
    }

    /**
     * Creates dialog instance with no owner and specified style sheet.
     *
     * @param cssUrl style sheet url
     */
    @Deprecated(forRemoval = true)
    public BaseDialog(URL cssUrl) {
        this(null, cssUrl);
    }

    /**
     * Creates dialog instance with no owner and specified style sheet.
     *
     * @param cssUrl style sheet url
     */
    public BaseDialog(String cssUrl) {
        this(null, cssUrl);
    }

    /**
     * Creates dialog instance with specified owner and style sheet.
     *
     * @param owner  owner
     * @param cssUrl stye sheet url
     */
    @Deprecated(forRemoval = true)
    public BaseDialog(Controller owner, URL cssUrl) {
        this(owner);
        if (cssUrl != null) {
            getDialogPane().getStylesheets().add(cssUrl.toString());
        }
    }

    /**
     * Creates dialog instance with specified owner and style sheet.
     *
     * @param owner  owner
     * @param cssUrl style sheet url
     */
    public BaseDialog(Controller owner, String cssUrl) {
        this(owner);
        if (cssUrl != null) {
            getDialogPane().getStylesheets().add(cssUrl);
        }
    }

    /**
     * Creates OK and Cancel buttons. Supplied resource bundle must define "button.Cancel"
     * resource for Cancel translation.
     *
     * @param rb resource bundle
     */
    protected void createDefaultButtons(ResourceBundle rb) {
        createDefaultButtons(rb, null);
    }

    /**
     * Creates OK and Cancel buttons. Supplied resource bundle must define "button.Cancel"
     * resource for Cancel translation.
     *
     * @param rb                        resource bundle
     * @param validationInvalidProperty invalid property for validation support
     */
    protected void createDefaultButtons(ResourceBundle rb, ReadOnlyBooleanProperty validationInvalidProperty) {
        getDialogPane().getButtonTypes().addAll(
                ButtonType.OK,
                ButtonType.CANCEL);

        if (validationInvalidProperty != null) {
            var btOk = (Button) getDialogPane().lookupButton(ButtonType.OK);
            btOk.disableProperty().bind(validationInvalidProperty);
        }

        var btCancel = (Button) getDialogPane().lookupButton(ButtonType.CANCEL);
        btCancel.setText(rb == null ? "Cancel" : rb.getString("button.Cancel"));
    }

    /**
     * Returns button of the specified type.
     *
     * @param type button type
     * @return button if exists
     */
    protected Optional




© 2015 - 2024 Weber Informatics LLC | Privacy Policy