com.dua3.utility.fx.controls.AbstractDialogBuilder 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.utility.fx.controls;
import com.dua3.cabe.annotations.Nullable;
import javafx.scene.control.Dialog;
import javafx.stage.Stage;
import javafx.stage.Window;
import java.util.Optional;
import java.util.function.BiConsumer;
/**
* Abstract class for building dialog boxes.
*
* @param the type of dialog box to build
* @param the type of the builder class itself
* @param the type of the result returned by the dialog box
*/
public abstract class AbstractDialogBuilder, B extends AbstractDialogBuilder, R>
extends AbstractDialogPaneBuilder {
private final BiConsumer titleSetter;
private final Window parentWindow;
private String title;
protected AbstractDialogBuilder(@Nullable Window parentWindow) {
super(Dialog::setHeaderText);
this.parentWindow = parentWindow;
this.titleSetter = Dialog::setTitle;
}
/**
* Set dialog title.
*
* @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 title(String fmt, Object... args) {
this.title = format(fmt, args);
return (B) this;
}
/**
* Build and show the dialog.
*
* This is equivalent to calling build().showAndWait().
*
* @return Optional containing the result as defined by the dialog
*/
public Optional showAndWait() {
return build().showAndWait();
}
/**
* Create Dialog instance.
*
* @return Dialog instance
*/
@Override
public D build() {
D dlg = super.build();
// copy stage icons from parent
if (parentWindow != null) {
Stage stage = (Stage) dlg.getDialogPane().getScene().getWindow();
stage.getIcons().addAll(((Stage) parentWindow).getIcons());
}
// set title
applyIfNotNull(titleSetter, dlg, title);
return dlg;
}
}