!70 ♻️ 重构代码 重构对话框创建与其工具类的初步封装 ➕ 添加第三方图标库
Merge pull request !70 from 格物方能致知/refactor-I7Y2O0
This commit is contained in:
commit
6035919fb5
29
pom.xml
29
pom.xml
@ -16,6 +16,16 @@
|
||||
<javafx.version>17.0.1</javafx.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.kordamp.ikonli</groupId>
|
||||
<artifactId>ikonli-javafx</artifactId>
|
||||
<version>12.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.kordamp.ikonli</groupId>
|
||||
<artifactId>ikonli-antdesignicons-pack</artifactId>
|
||||
<version>12.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.mkpaz</groupId>
|
||||
<artifactId>atlantafx-base</artifactId>
|
||||
@ -111,6 +121,25 @@
|
||||
</bundlesList>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.2.4</version>
|
||||
<configuration>
|
||||
<transformers>
|
||||
<transformer
|
||||
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
|
||||
</transformers>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
@ -12,7 +12,11 @@ module org.jcnc.jnotepad {
|
||||
requires ch.qos.logback.core;
|
||||
requires ch.qos.logback.classic;
|
||||
requires com.ibm.icu;
|
||||
requires org.kordamp.ikonli.core;
|
||||
requires org.kordamp.ikonli.javafx;
|
||||
requires org.kordamp.ikonli.antdesignicons;
|
||||
exports org.jcnc.jnotepad;
|
||||
exports org.jcnc.jnotepad.ui.dialog.alert;
|
||||
exports org.jcnc.jnotepad.app.config;
|
||||
exports org.jcnc.jnotepad.app.i18n;
|
||||
exports org.jcnc.jnotepad.constants;
|
||||
|
||||
@ -32,6 +32,7 @@ public class LunchApp extends Application {
|
||||
private static final Scene SCENE;
|
||||
|
||||
static {
|
||||
Application.setUserAgentStylesheet(new PrimerLight().getUserAgentStylesheet());
|
||||
double width = AppConstants.SCREEN_WIDTH;
|
||||
double length = AppConstants.SCREEN_LENGTH;
|
||||
SCENE = new Scene(ROOT, width, length);
|
||||
@ -62,7 +63,7 @@ public class LunchApp extends Application {
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) {
|
||||
Application.setUserAgentStylesheet(new PrimerLight().getUserAgentStylesheet());
|
||||
|
||||
SCENE.getStylesheets().add(Objects.requireNonNull(getClass().getResource("/css/styles.css")).toExternalForm());
|
||||
initUiComponents();
|
||||
UiResourceBundle.bindStringProperty(primaryStage.titleProperty(), TextConstants.TITLE);
|
||||
@ -70,7 +71,7 @@ public class LunchApp extends Application {
|
||||
primaryStage.setScene(SCENE);
|
||||
primaryStage.setWidth(SCENE.getWidth());
|
||||
primaryStage.setHeight(SCENE.getHeight());
|
||||
primaryStage.getIcons().add(UiUtil.getIcon());
|
||||
primaryStage.getIcons().add(UiUtil.getAppIcon());
|
||||
primaryStage.show();
|
||||
}
|
||||
|
||||
|
||||
@ -4,7 +4,6 @@ import org.jcnc.jnotepad.app.config.AppConfig;
|
||||
import org.jcnc.jnotepad.exception.AppException;
|
||||
import org.jcnc.jnotepad.tool.JsonUtil;
|
||||
import org.jcnc.jnotepad.tool.LogUtil;
|
||||
import org.jcnc.jnotepad.tool.PopUpUtil;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
@ -86,7 +85,7 @@ public class AppConfigController {
|
||||
writer.write(JsonUtil.toJsonString(appConfig));
|
||||
} catch (Exception e) {
|
||||
logger.error("", e);
|
||||
PopUpUtil.errorAlert("错误", "读写错误", "配置文件读写错误!");
|
||||
// PopUpUtil.errorAlert("错误", "读写错误", "配置文件读写错误!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,38 +0,0 @@
|
||||
package org.jcnc.jnotepad.interfaces;
|
||||
|
||||
import javafx.scene.image.Image;
|
||||
|
||||
/**
|
||||
* 自定义提示框的接口。
|
||||
*
|
||||
* <p>此接口定义了自定义提示框的基本行为,包括设置图标、处理确认和取消按钮操作以及获取提示框类型。</p>
|
||||
*
|
||||
* @author luke
|
||||
*/
|
||||
public interface CustomDialogAble {
|
||||
|
||||
/**
|
||||
* 设置提示框的图标。
|
||||
*
|
||||
* @param iconImage 图标图像
|
||||
*/
|
||||
void setIconImage(Image iconImage);
|
||||
|
||||
/**
|
||||
* 处理确认按钮的操作。子类必须实现此方法以定义确认按钮的行为。
|
||||
*/
|
||||
void handleConfirmAction();
|
||||
|
||||
/**
|
||||
* 获取提示框类型。
|
||||
*
|
||||
* @return 提示框类型的描述
|
||||
*/
|
||||
String getAlertType();
|
||||
|
||||
/**
|
||||
* 处理取消按钮的操作。默认情况下,此方法为空,子类可以选择性地实现它。
|
||||
*/
|
||||
default void handleCancelAction() {
|
||||
}
|
||||
}
|
||||
@ -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 信息
|
||||
*
|
||||
* <p>用于创建一个错误类型的弹窗,显示给用户以指示发生了错误。</p>
|
||||
* @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();
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
@ -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();
|
||||
}
|
||||
@ -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);
|
||||
|
||||
@ -1,50 +0,0 @@
|
||||
package org.jcnc.jnotepad.ui.module.alert;
|
||||
|
||||
/**
|
||||
* 错误对话框
|
||||
*
|
||||
* <p>用于显示错误信息的自定义对话框,包含图标、消息文本和确认按钮。</p>
|
||||
*
|
||||
* @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() {
|
||||
}
|
||||
}
|
||||
@ -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() {
|
||||
}
|
||||
}
|
||||
@ -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();
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user