pref: 修改Alert工具

This commit is contained in:
octopus_yan 2024-09-08 01:30:51 +08:00
parent 6858dfcca8
commit edf574c0af
5 changed files with 145 additions and 1 deletions

View File

@ -82,7 +82,7 @@ public class Application extends javafx.application.Application {
private void showErrorDialog(Thread t, Throwable e) {
logger.error("", e);
AlertUtil.exceptionAlert(new Exception(e)).show();
AlertUtil.exception(new Exception(e)).show();
}
@Override

View File

@ -0,0 +1,39 @@
package cn.octopusyan.alistgui.util.alert;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.stage.Window;
import java.util.Optional;
/**
* @author octopus_yan
*/
public class AlertBuilder extends BaseBuilder<Alert, ButtonType> {
public AlertBuilder(Alert alert, Window owner) {
super(alert, owner);
}
/**
* AlertUtil.confirm
*/
public void show(AlertUtil.OnClickListener listener) {
Optional<ButtonType> result = alert.showAndWait();
result.ifPresent(r -> listener.onClicked(r.getText()));
}
/**
* AlertUtil.confirm
*/
public void show(AlertUtil.OnChoseListener listener) {
Optional<ButtonType> result = alert.showAndWait();
result.ifPresent(r -> {
if (r == ButtonType.OK) {
listener.confirm();
} else {
listener.cancelOrClose(r);
}
});
}
}

View File

@ -0,0 +1,57 @@
package cn.octopusyan.alistgui.util.alert;
import javafx.scene.control.Dialog;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.stage.Window;
import java.util.Objects;
/**
* @author octopus_yan
*/
public abstract class BaseBuilder<T extends Dialog<R>, R> {
T alert;
public BaseBuilder(T alert, Window mOwner) {
this.alert = alert;
if (mOwner != null)
this.alert.initOwner(mOwner);
}
public BaseBuilder<T, R> title(String title) {
alert.setTitle(title);
return this;
}
public BaseBuilder<T, R> header(String header) {
alert.setHeaderText(header);
return this;
}
public BaseBuilder<T, R> content(String content) {
alert.setContentText(content);
return this;
}
public BaseBuilder<T, R> icon(String path) {
return icon(new Image(Objects.requireNonNull(this.getClass().getResource(path)).toString()));
}
public BaseBuilder<T, R> icon(Image image) {
getStage().getIcons().add(image);
return this;
}
public void show() {
if (alert.isShowing()) {
if (!Objects.equals(alert.getContentText(), alert.getContentText()))
alert.setOnHidden(_ -> show());
}
alert.showAndWait();
}
private Stage getStage() {
return (Stage) alert.getDialogPane().getScene().getWindow();
}
}

View File

@ -0,0 +1,24 @@
package cn.octopusyan.alistgui.util.alert;
import javafx.scene.control.ChoiceDialog;
import javafx.stage.Window;
import java.util.Collection;
import java.util.Optional;
/**
* @author octopus_yan
*/
public class ChoiceBuilder<R> extends BaseBuilder<ChoiceDialog<R>, R> {
public ChoiceBuilder(ChoiceDialog<R> alert, Window mOwner) {
super(alert, mOwner);
}
/**
* AlertUtil.choices
*/
public R getChoice(Collection<R> choices) {
Optional<R> result = alert.showAndWait();
return result.orElse(null);
}
}

View File

@ -0,0 +1,24 @@
package cn.octopusyan.alistgui.util.alert;
import javafx.scene.control.TextInputDialog;
import javafx.stage.Window;
import java.util.Optional;
/**
* @author octopus_yan
*/
public class TextInputBuilder extends BaseBuilder<TextInputDialog, String>{
public TextInputBuilder(TextInputDialog alert, Window mOwner) {
super(alert, mOwner);
}
/**
* AlertUtil.input
* 如果用户点击了取消按钮,将会返回null
*/
public String getInput() {
Optional<String> result = alert.showAndWait();
return result.orElse(null);
}
}