此接口定义了自定义提示框的基本行为,包括设置图标、处理确认和取消按钮操作以及获取提示框类型。
- * - * @author luke - */ -public interface CustomDialogAble { - - /** - * 设置提示框的图标。 - * - * @param iconImage 图标图像 - */ - void setIconImage(Image iconImage); - - /** - * 处理确认按钮的操作。子类必须实现此方法以定义确认按钮的行为。 - */ - void handleConfirmAction(); - - /** - * 获取提示框类型。 - * - * @return 提示框类型的描述 - */ - String getAlertType(); - - /** - * 处理取消按钮的操作。默认情况下,此方法为空,子类可以选择性地实现它。 - */ - default void handleCancelAction() { - } -} diff --git a/src/main/java/org/jcnc/jnotepad/tool/PopUpUtil.java b/src/main/java/org/jcnc/jnotepad/tool/PopUpUtil.java index ada80e319f72354f3f6d2a455c7ffcd4cfb5af38..3ab9af0e5b67b14b14d28cf8576d2ee2fc92a488 100644 --- a/src/main/java/org/jcnc/jnotepad/tool/PopUpUtil.java +++ b/src/main/java/org/jcnc/jnotepad/tool/PopUpUtil.java @@ -1,6 +1,7 @@ package org.jcnc.jnotepad.tool; -import javafx.scene.control.Alert; +import org.jcnc.jnotepad.ui.dialog.alert.AlertDialog; +import org.jcnc.jnotepad.ui.dialog.alert.AlertDialogButtonAction; /** * 弹窗工具类 @@ -13,19 +14,16 @@ public class PopUpUtil { } /** - * 获取错误弹窗。 + * 获取错误弹窗 * * @param title 弹窗标题 * @param headerText 头文本 - * @param message 信息 - * - *用于创建一个错误类型的弹窗,显示给用户以指示发生了错误。
+ * @param message 消息文本 + * @param action 按钮的事件类 + * @apiNote + * @since 2023/9/3 11:54 */ - public static void errorAlert(String title, String headerText, String message) { - Alert alert = new Alert(Alert.AlertType.ERROR); - alert.setTitle(title); - alert.setHeaderText(headerText); - alert.setContentText(message); - alert.showAndWait(); + public static void errorAlert(String title, String headerText, String message, AlertDialogButtonAction action) { + new AlertDialog(title, headerText, message, AlertDialog.DialogType.ERROR, action).showAndWait(); } } diff --git a/src/main/java/org/jcnc/jnotepad/tool/UiUtil.java b/src/main/java/org/jcnc/jnotepad/tool/UiUtil.java index 5918bdf110b586ebb9f4bb84db1d3c43bab3be7e..c06660c22492903d7ab174e1c0e8be2eb395d6ef 100644 --- a/src/main/java/org/jcnc/jnotepad/tool/UiUtil.java +++ b/src/main/java/org/jcnc/jnotepad/tool/UiUtil.java @@ -1,12 +1,16 @@ package org.jcnc.jnotepad.tool; import javafx.scene.image.Image; +import javafx.scene.paint.Color; import javafx.stage.Window; import org.jcnc.jnotepad.LunchApp; import org.jcnc.jnotepad.constants.AppConstants; +import org.kordamp.ikonli.javafx.FontIcon; import java.util.Objects; +import static org.kordamp.ikonli.antdesignicons.AntDesignIconsFilled.*; + /** * UI工具 * @@ -18,7 +22,25 @@ public class UiUtil { /** * 应用程序图标 */ - private static final Image ICON = new Image(Objects.requireNonNull(UiUtil.class.getResource(AppConstants.APP_ICON)).toString()); + private static final Image APP_ICON = new Image(Objects.requireNonNull(UiUtil.class.getResource(AppConstants.APP_ICON)).toString()); + + /** + * 错误图标 + */ + private static final FontIcon ERROR_ICON = FontIcon.of(EXCLAMATION_CIRCLE, 64, Color.RED); + + /** + * 信息图标 + */ + private static final FontIcon INFO_ICON = FontIcon.of(INFO_CIRCLE, 64, Color.BLUE); + /** + * 警告图标 + */ + private static final FontIcon WARNING_ICON = FontIcon.of(WARNING, 64, Color.ORANGE); + /** + * 问题图标 + */ + private static final FontIcon QUESTION_ICON = FontIcon.of(QUESTION_CIRCLE, 64, Color.YELLOW); private UiUtil() { } @@ -29,8 +51,45 @@ public class UiUtil { * * @return javafx.scene.image.Image 应用程序图标对象 */ - public static Image getIcon() { - return ICON; + public static Image getAppIcon() { + return APP_ICON; + } + + /** + * Retrieves the information icon. + * + * @return the information icon + */ + + public static FontIcon getInfoIcon() { + return INFO_ICON; + } + + /** + * Returns the error icon. + * + * @return the error icon + */ + public static FontIcon getErrorIcon() { + return ERROR_ICON; + } + + /** + * Retrieves the warning icon. + * + * @return the warning icon + */ + public static FontIcon getWarningIcon() { + return WARNING_ICON; + } + + /** + * Retrieves the question icon. + * + * @return the question icon + */ + public static FontIcon getQuestionIcon() { + return QUESTION_ICON; } /** diff --git a/src/main/java/org/jcnc/jnotepad/ui/module/alert/AbstractDialog.java b/src/main/java/org/jcnc/jnotepad/ui/dialog/alert/AlertDialog.java similarity index 57% rename from src/main/java/org/jcnc/jnotepad/ui/module/alert/AbstractDialog.java rename to src/main/java/org/jcnc/jnotepad/ui/dialog/alert/AlertDialog.java index 5a8a67afc500748f1ddac24bcb99c7582d04843f..68a2af597c7fad2beaa119fc137ee60600934aab 100644 --- a/src/main/java/org/jcnc/jnotepad/ui/module/alert/AbstractDialog.java +++ b/src/main/java/org/jcnc/jnotepad/ui/dialog/alert/AlertDialog.java @@ -1,18 +1,17 @@ -package org.jcnc.jnotepad.ui.module.alert; +package org.jcnc.jnotepad.ui.dialog.alert; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; -import javafx.scene.image.Image; -import javafx.scene.image.ImageView; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Modality; import javafx.stage.Stage; -import org.jcnc.jnotepad.interfaces.CustomDialogAble; +import org.jcnc.jnotepad.tool.UiUtil; +import org.kordamp.ikonli.javafx.FontIcon; /** * 自定义提示框的抽象基类 @@ -21,50 +20,65 @@ import org.jcnc.jnotepad.interfaces.CustomDialogAble; * * @author luke */ -public abstract class AbstractDialog extends Stage implements CustomDialogAble { - - private ImageView iconImageView; - private final String customText; +public class AlertDialog extends Stage { /** * 构造一个自定义提示框。 * + * @param title 提示框的标题 * @param message 提示框中显示的消息文本 * @param customText 自定义的文本 * @param width 提示框的宽度 * @param height 提示框的高度 + * @param type 提示框的类型 + * @param action 按钮的事件类 */ - protected AbstractDialog(String message, String customText, double width, double height) { + public AlertDialog(String title, String message, String customText, double width, double height, DialogType type, AlertDialogButtonAction action) { + // 设置窗口图标 + this.getIcons().add(UiUtil.getAppIcon()); this.customText = customText; - setTitle(getAlertType()); + setTitle(title); setResizable(false); initModality(Modality.APPLICATION_MODAL); - BorderPane borderPane = createLayout(message); + FontIcon icon = switch (type) { + case INFO -> UiUtil.getInfoIcon(); + case WARNING -> UiUtil.getWarningIcon(); + case ERROR -> UiUtil.getErrorIcon(); + default -> UiUtil.getQuestionIcon(); + }; + BorderPane borderPane = createLayout(message, icon, action); Scene scene = new Scene(borderPane, width, height); setScene(scene); } + private final String customText; + /** - * 处理取消按钮的操作。 + * 构造一个自定义提示框,使用默认大小。 + * + * @param title 提示框的标题 + * @param message 提示框中显示的消息文本 + * @param customText 自定义的文本 + * @param type 提示框的类型 + * @param action 按钮的事件类 */ - @Override - public void handleCancelAction() { - close(); + public AlertDialog(String title, String message, String customText, DialogType type, AlertDialogButtonAction action) { + // 使用默认的宽度和高度 + this(title, message, customText, 350, 150, type, action); } /** * 创建提示框的布局。 * * @param message 提示框中显示的消息文本 + * @param icon 提示框中显示的图标 * @return BorderPane 布局容器 */ - private BorderPane createLayout(String message) { + private BorderPane createLayout(String message, FontIcon icon, AlertDialogButtonAction action) { BorderPane borderPane = new BorderPane(); - iconImageView = new ImageView(); - - HBox iconBox = new HBox(iconImageView); + HBox iconBox = new HBox(icon); iconBox.setPadding(new Insets(10, 10, 10, 10)); VBox vbox = new VBox(10); vbox.setAlignment(Pos.CENTER); @@ -74,8 +88,8 @@ public abstract class AbstractDialog extends Stage implements CustomDialogAble { // 自定义文本 Label customTextLabel = new Label(customText); - Button confirmButton = createButton("确认", this::handleConfirmAction); - Button cancelButton = createButton("取消", this::handleCancelAction); + Button confirmButton = createButton("确认", action::handleConfirmAction); + Button cancelButton = createButton("取消", action::handleCancelAction); HBox hBox = new HBox(10, confirmButton, cancelButton); hBox.setAlignment(Pos.CENTER_RIGHT); @@ -89,16 +103,30 @@ public abstract class AbstractDialog extends Stage implements CustomDialogAble { return borderPane; } + /** - * 设置提示框的图标。 - * - * @param iconImage 图标图像 + * 对话框枚举 */ - @Override - public void setIconImage(Image iconImage) { - iconImageView.setImage(iconImage); + public enum DialogType { + /** + * 信息 + */ + INFO, + /** + * 警告 + */ + WARNING, + /** + * 错误 + */ + ERROR, + /** + * 疑问 + */ + QUESTION } + /** * 创建按钮。 * @@ -112,28 +140,5 @@ public abstract class AbstractDialog extends Stage implements CustomDialogAble { return button; } - /** - * 获取提示框类型的抽象方法,子类应该实现该方法以返回具体的提示框类型。 - * - * @return 提示框类型 - */ - @Override - public abstract String getAlertType(); - /** - * 处理确认按钮的操作,子类必须实现。 - */ - @Override - public abstract void handleConfirmAction(); - - /** - * 构造一个自定义提示框,使用默认大小。 - * - * @param message 提示框中显示的消息文本 - * @param customText 自定义的文本 - */ - protected AbstractDialog(String message, String customText) { - // 使用默认的宽度和高度 - this(message, customText, 350, 150); - } } diff --git a/src/main/java/org/jcnc/jnotepad/ui/dialog/alert/AlertDialogButtonAction.java b/src/main/java/org/jcnc/jnotepad/ui/dialog/alert/AlertDialogButtonAction.java new file mode 100644 index 0000000000000000000000000000000000000000..31dd8b5f67a862026d3532209a5aaa707391ffbb --- /dev/null +++ b/src/main/java/org/jcnc/jnotepad/ui/dialog/alert/AlertDialogButtonAction.java @@ -0,0 +1,18 @@ +package org.jcnc.jnotepad.ui.dialog.alert; + +/** + * 对话框按钮事件类 + * + * @author gewuyou + */ +public abstract class AlertDialogButtonAction { + /** + * 处理确认按钮的操作。子类必须实现此方法以定义确认按钮的行为。 + */ + protected abstract void handleConfirmAction(); + + /** + * 处理取消按钮的操作。默认情况下,此方法为空,子类可以选择性地实现它。 + */ + protected abstract void handleCancelAction(); +} diff --git a/src/main/java/org/jcnc/jnotepad/ui/module/LineNumberTextArea.java b/src/main/java/org/jcnc/jnotepad/ui/module/LineNumberTextArea.java index 9cb163a62789c03999174565e171b2bd46189d9b..b93648557f1c55e7ee49489915977e122a401c4f 100644 --- a/src/main/java/org/jcnc/jnotepad/ui/module/LineNumberTextArea.java +++ b/src/main/java/org/jcnc/jnotepad/ui/module/LineNumberTextArea.java @@ -34,7 +34,6 @@ public class LineNumberTextArea extends BorderPane { public LineNumberTextArea() { mainTextArea = new TextArea(); mainTextArea.setWrapText(SingletonUtil.getAppConfigController().getAutoLineConfig()); - lineNumberArea = new TextArea(); lineNumberArea.setEditable(false); lineNumberArea.setPrefWidth(MIN_LINE_NUMBER_WIDTH); diff --git a/src/main/java/org/jcnc/jnotepad/ui/module/alert/ErrorDialogAble.java b/src/main/java/org/jcnc/jnotepad/ui/module/alert/ErrorDialogAble.java deleted file mode 100644 index 49485fbc22132a42f300e939171706a7fcc6068c..0000000000000000000000000000000000000000 --- a/src/main/java/org/jcnc/jnotepad/ui/module/alert/ErrorDialogAble.java +++ /dev/null @@ -1,50 +0,0 @@ -package org.jcnc.jnotepad.ui.module.alert; - -/** - * 错误对话框 - * - *用于显示错误信息的自定义对话框,包含图标、消息文本和确认按钮。
- * - * @author luke - */ -class ErrorDialogAble extends AbstractDialog { - - /** - * 构造一个错误对话框。 - * - * @param title 对话框中显示的错误消息 - * @param customText 自定义文本 - * @param width 对话框的宽度 - * @param height 对话框的高度 - */ - protected ErrorDialogAble(String title, String customText, double width, double height) { - super(title, customText, width, height); - } - - /** - * 构造一个错误对话框,使用默认大小。 - * - * @param title 对话框中显示的错误消息 - * @param customText 自定义文本 - */ - public ErrorDialogAble(String title, String customText) { - super(title, customText); - } - - /** - * 获取对话框类型。 - * - * @return 对话框类型为"错误提示" - */ - @Override - public String getAlertType() { - return "错误提示"; - } - - /** - * 处理确认按钮的操作,子类可以在这里添加具体的处理逻辑。 - */ - @Override - public void handleConfirmAction() { - } -} diff --git a/src/main/java/org/jcnc/jnotepad/ui/module/alert/InformationDialogAble.java b/src/main/java/org/jcnc/jnotepad/ui/module/alert/InformationDialogAble.java deleted file mode 100644 index 13db3aa88a94ea9fd6384536413c9d1a10566f93..0000000000000000000000000000000000000000 --- a/src/main/java/org/jcnc/jnotepad/ui/module/alert/InformationDialogAble.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.jcnc.jnotepad.ui.module.alert; - -/** - * 信息提示对话框类,继承自AbstractCustomAlert,用于显示信息提示。 - * - * @author luke - */ -public class InformationDialogAble extends AbstractDialog { - - /** - * 构造一个信息提示对话框。 - * - * @param title 提示信息 - * @param customText 自定义文本 - * @param width 对话框宽度 - * @param height 对话框高度 - */ - protected InformationDialogAble(String title, String customText, double width, double height) { - super(title, customText, width, height); - } - - /** - * 构造一个信息提示对话框。 - * - * @param title 提示信息 - * @param number 编号 - */ - public InformationDialogAble(String title, String number) { - super(title, number); - } - - /** - * 获取对话框类型。 - * - * @return 对话框类型字符串,此处为"信息提示" - */ - @Override - public String getAlertType() { - return "信息提示"; - } - - /** - * 处理确认操作的方法。 - */ - @Override - public void handleConfirmAction() { - } -} diff --git a/src/main/java/org/jcnc/jnotepad/ui/setstage/SetStage.java b/src/main/java/org/jcnc/jnotepad/ui/setstage/SetStage.java index 758e450a4666bd0e6dad296b46d699669b0940ae..adab40d0bf6b6f857e1623b9369e5b5830c38b77 100644 --- a/src/main/java/org/jcnc/jnotepad/ui/setstage/SetStage.java +++ b/src/main/java/org/jcnc/jnotepad/ui/setstage/SetStage.java @@ -59,7 +59,7 @@ public class SetStage { Stage setStage = new Stage(); setStage.setScene(scene); setStage.setTitle("设置窗口"); - setStage.getIcons().add(UiUtil.getIcon()); + setStage.getIcons().add(UiUtil.getAppIcon()); setStage.show(); }