diff --git a/src/main/java/cn/octopusyan/alistgui/util/AlertUtil.java b/src/main/java/cn/octopusyan/alistgui/util/AlertUtil.java deleted file mode 100644 index 1ea3f1d..0000000 --- a/src/main/java/cn/octopusyan/alistgui/util/AlertUtil.java +++ /dev/null @@ -1,228 +0,0 @@ -package cn.octopusyan.alistgui.util; - -import javafx.scene.control.*; -import javafx.scene.image.Image; -import javafx.scene.layout.GridPane; -import javafx.scene.layout.Priority; -import javafx.stage.Stage; -import javafx.stage.Window; - -import java.io.PrintWriter; -import java.io.StringWriter; -import java.util.Arrays; -import java.util.List; -import java.util.Objects; -import java.util.Optional; -import java.util.stream.Collectors; - -/** - * 弹窗工具 - * - * @author octopus_yan@foxmail.com - */ -public class AlertUtil { - private static Window mOwner; - private static Builder builder; - - public static void initOwner(Stage stage) { - AlertUtil.mOwner = stage; - } - - public static class Builder { - T alert; - - public Builder(T alert) { - this.alert = alert; - if (mOwner != null) this.alert.initOwner(mOwner); - } - - public Builder title(String title) { - alert.setTitle(title); - return this; - } - - public Builder header(String header) { - alert.setHeaderText(header); - return this; - } - - public Builder content(String content) { - alert.setContentText(content); - return this; - } - - public Builder icon(String path) { - icon(new Image(Objects.requireNonNull(this.getClass().getResource(path)).toString())); - return this; - } - - public Builder icon(Image image) { - getStage().getIcons().add(image); - return this; - } - - public void show() { - if (AlertUtil.builder == null) { - AlertUtil.builder = this; - } else if (AlertUtil.builder.alert.isShowing()) { - if (!Objects.equals(AlertUtil.builder.alert.getContentText(), alert.getContentText())) - ((Alert) AlertUtil.builder.alert).setOnHidden(event -> { - AlertUtil.builder = null; - show(); - }); - } - alert.showAndWait(); - } - - /** - * AlertUtil.confirm - */ - public void show(OnClickListener listener) { - - Optional result = alert.showAndWait(); - - listener.onClicked(result.get().getText()); - } - - /** - * AlertUtil.confirm - */ - public void show(OnChoseListener listener) { - Optional result = alert.showAndWait(); - if (result.get() == ButtonType.OK) { - listener.confirm(); - } else { - listener.cancelOrClose(result.get()); - } - } - - /** - * AlertUtil.input - * 如果用户点击了取消按钮,将会返回null - */ - public String getInput() { - Optional result = alert.showAndWait(); - if (result.isPresent()) { - return result.get(); - } - return null; - } - - /** - * AlertUtil.choices - */ - public R getChoice(R... choices) { - Optional result = alert.showAndWait(); - return (R) result.get(); - } - - private Stage getStage() { - return (Stage) alert.getDialogPane().getScene().getWindow(); - } - } - - public static Builder info(String content) { - return new Builder(new Alert(Alert.AlertType.INFORMATION)).content(content).header(null); - } - - public static Builder info() { - return new Builder(new Alert(Alert.AlertType.INFORMATION)); - } - - public static Builder error(String message) { - return new Builder(new Alert(Alert.AlertType.ERROR)).header(null).content(message); - } - - public static Builder warning() { - return new Builder(new Alert(Alert.AlertType.WARNING)); - } - - public static Builder exception(Exception ex) { - return new Builder(exceptionAlert(ex)); - } - - public static Alert exceptionAlert(Exception ex) { - Alert alert = new Alert(Alert.AlertType.ERROR); - alert.setTitle("Exception Dialog"); - alert.setHeaderText(ex.getClass().getSimpleName()); - alert.setContentText(ex.getMessage()); - - // 创建可扩展的异常。 - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - ex.printStackTrace(pw); - String exceptionText = sw.toString(); - - Label label = new Label("The exception stacktrace was :"); - - TextArea textArea = new TextArea(exceptionText); - textArea.setEditable(false); - textArea.setWrapText(true); - - textArea.setMaxWidth(Double.MAX_VALUE); - textArea.setMaxHeight(Double.MAX_VALUE); - GridPane.setVgrow(textArea, Priority.ALWAYS); - GridPane.setHgrow(textArea, Priority.ALWAYS); - - GridPane expContent = new GridPane(); - expContent.setMaxWidth(Double.MAX_VALUE); - expContent.add(label, 0, 0); - expContent.add(textArea, 0, 1); - - // 将可扩展异常设置到对话框窗格中。 - alert.getDialogPane().setExpandableContent(expContent); - return alert; - } - - /** - * 确认对话框 - */ - public static Builder confirm() { - Alert alert = new Alert(Alert.AlertType.CONFIRMATION); - alert.setTitle("确认对话框"); - return new Builder(alert); - } - - /** - * 自定义确认对话框

- * "Cancel" OR "取消" 为取消按钮 - */ - public static Builder confirm(String... buttons) { - Alert alert = new Alert(Alert.AlertType.CONFIRMATION); - - List buttonList = Arrays.stream(buttons).map((type) -> { - ButtonBar.ButtonData buttonData = ButtonBar.ButtonData.OTHER; - if ("Cancel".equals(type) || "取消".equals(type)) - buttonData = ButtonBar.ButtonData.CANCEL_CLOSE; - return new ButtonType(type, buttonData); - }).collect(Collectors.toList()); - - alert.getButtonTypes().setAll(buttonList); - - return new Builder(alert); - } - - public static Builder input(String content) { - TextInputDialog dialog = new TextInputDialog(); - dialog.setContentText(content); - return new Builder(dialog); - } - - @SafeVarargs - public static Builder> choices(String hintText, T... choices) { - ChoiceDialog dialog = new ChoiceDialog(choices[0], choices); - dialog.setContentText(hintText); - return new Builder>(dialog); - } - - - public interface OnChoseListener { - void confirm(); - - void cancelOrClose(ButtonType buttonType); - } - - public interface OnClickListener { - void onClicked(String result); - } -} diff --git a/src/main/java/cn/octopusyan/alistgui/util/alert/AlertUtil.java b/src/main/java/cn/octopusyan/alistgui/util/alert/AlertUtil.java new file mode 100644 index 0000000..8c86a48 --- /dev/null +++ b/src/main/java/cn/octopusyan/alistgui/util/alert/AlertUtil.java @@ -0,0 +1,136 @@ +package cn.octopusyan.alistgui.util.alert; + +import javafx.scene.control.*; +import javafx.scene.layout.GridPane; +import javafx.scene.layout.Priority; +import javafx.stage.Stage; +import javafx.stage.Window; +import org.apache.commons.lang3.StringUtils; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +/** + * 弹窗工具 + * + * @author octopus_yan@foxmail.com + */ +public class AlertUtil { + private static Window mOwner; + + public static void initOwner(Stage stage) { + AlertUtil.mOwner = stage; + } + + public static AlertBuilder info(String content) { + AlertBuilder alertBuilder = new AlertBuilder(new Alert(Alert.AlertType.INFORMATION), mOwner); + alertBuilder.content(content).header(null); + return alertBuilder; + } + + public static AlertBuilder info() { + return new AlertBuilder(new Alert(Alert.AlertType.INFORMATION), mOwner); + } + + public static AlertBuilder error(String message) { + AlertBuilder alertBuilder = new AlertBuilder(new Alert(Alert.AlertType.ERROR), mOwner); + alertBuilder.header(null).content(message); + return alertBuilder; + } + + public static AlertBuilder warning() { + return new AlertBuilder(new Alert(Alert.AlertType.WARNING), mOwner); + } + + public static AlertBuilder exception(Exception ex) { + return new AlertBuilder(exceptionAlert(ex), mOwner); + } + + private static Alert exceptionAlert(Exception ex) { + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle("Exception Dialog"); + alert.setHeaderText(ex.getClass().getSimpleName()); + alert.setContentText(ex.getMessage()); + + // 创建可扩展的异常。 + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + ex.printStackTrace(pw); + String exceptionText = sw.toString(); + + Label label = new Label("The exception stacktrace was :"); + + TextArea textArea = new TextArea(exceptionText); + textArea.setEditable(false); + textArea.setWrapText(true); + + textArea.setMaxWidth(Double.MAX_VALUE); + textArea.setMaxHeight(Double.MAX_VALUE); + GridPane.setVgrow(textArea, Priority.ALWAYS); + GridPane.setHgrow(textArea, Priority.ALWAYS); + + GridPane expContent = new GridPane(); + expContent.setMaxWidth(Double.MAX_VALUE); + expContent.add(label, 0, 0); + expContent.add(textArea, 0, 1); + + // 将可扩展异常设置到对话框窗格中。 + alert.getDialogPane().setExpandableContent(expContent); + return alert; + } + + /** + * 确认对话框 + */ + public static AlertBuilder confirm() { + Alert alert = new Alert(Alert.AlertType.CONFIRMATION); + alert.setTitle("确认对话框"); + return new AlertBuilder(alert, mOwner); + } + + /** + * 自定义确认对话框

+ * "Cancel" OR "取消" 为取消按钮 + */ + public static AlertBuilder confirm(String... buttons) { + Alert alert = new Alert(Alert.AlertType.CONFIRMATION); + + List buttonList = Arrays.stream(buttons).map((type) -> { + ButtonBar.ButtonData buttonData = ButtonBar.ButtonData.OTHER; + if ("cancel".equals(StringUtils.lowerCase(type)) || "取消".equals(type)) + buttonData = ButtonBar.ButtonData.CANCEL_CLOSE; + return new ButtonType(type, buttonData); + }).collect(Collectors.toList()); + + alert.getButtonTypes().setAll(buttonList); + + return new AlertBuilder(alert, mOwner); + } + + public static TextInputBuilder input(String content) { + TextInputDialog dialog = new TextInputDialog(); + dialog.setContentText(content); + return new TextInputBuilder(dialog, mOwner); + } + + @SafeVarargs + public static ChoiceBuilder choices(String hintText, T... choices) { + ChoiceDialog dialog = new ChoiceDialog<>(choices[0], choices); + dialog.setContentText(hintText); + return new ChoiceBuilder<>(dialog, mOwner); + } + + + public interface OnChoseListener { + void confirm(); + + void cancelOrClose(ButtonType buttonType); + } + + public interface OnClickListener { + void onClicked(String result); + } +}