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

com.dua3.utility.fx.controls.PromptBuilder Maven / Gradle / Ivy

There is a newer version: 14.0.1
Show 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.utility.fx.controls;

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

import java.util.Optional;
import java.util.function.Predicate;

/**
 * Builder for Alert Dialogs.
 * 

* Provides a fluent interface to create Alerts. */ public class PromptBuilder extends AbstractDialogBuilder { private String defaultValue = ""; private Predicate validate = s -> s != null && !s.isEmpty(); PromptBuilder(@Nullable Window parentWindow) { super(parentWindow); setDialogSupplier(this::createDialog); } public PromptBuilder defaultValue(String fmt, Object... args) { this.defaultValue = args.length == 0 ? fmt : String.format(fmt, args); return this; } public PromptBuilder validate(Predicate validate) { this.validate = validate; return this; } @Override public TextInputDialog build() { TextInputDialog dlg = super.build(); dlg.setGraphic(null); return dlg; } private TextInputDialog createDialog() { TextInputDialog dlg = new TextInputDialog(defaultValue); Optional.ofNullable(dlg.getDialogPane().lookupButton(ButtonType.OK)) .ifPresent(btn -> { btn.setDisable(!validate.test(dlg.getEditor().getText())); dlg.getEditor().textProperty().addListener((observable, oldValue, newValue) -> btn.setDisable(!validate.test(newValue)) ); }); return dlg; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy