♻️ 重构代码 优化 文件选择对话框的创建逻辑
This commit is contained in:
parent
3aec1f93cd
commit
03c2363b1e
@ -12,6 +12,8 @@ public class TextConstants {
|
|||||||
|
|
||||||
|
|
||||||
/// GlobalConfig文本常量
|
/// GlobalConfig文本常量
|
||||||
|
|
||||||
|
|
||||||
public static final String SAVE = "SAVE";
|
public static final String SAVE = "SAVE";
|
||||||
public static final String FILE = "FILE";
|
public static final String FILE = "FILE";
|
||||||
public static final String NEW = "NEW";
|
public static final String NEW = "NEW";
|
||||||
|
|||||||
@ -5,11 +5,15 @@ import javafx.concurrent.Task;
|
|||||||
import javafx.event.ActionEvent;
|
import javafx.event.ActionEvent;
|
||||||
import javafx.event.EventHandler;
|
import javafx.event.EventHandler;
|
||||||
import javafx.stage.FileChooser;
|
import javafx.stage.FileChooser;
|
||||||
|
import org.jcnc.jnotepad.app.i18n.UiResourceBundle;
|
||||||
|
import org.jcnc.jnotepad.constants.TextConstants;
|
||||||
import org.jcnc.jnotepad.manager.ThreadPoolManager;
|
import org.jcnc.jnotepad.manager.ThreadPoolManager;
|
||||||
import org.jcnc.jnotepad.root.center.main.center.tab.JNotepadTab;
|
import org.jcnc.jnotepad.root.center.main.center.tab.JNotepadTab;
|
||||||
import org.jcnc.jnotepad.tool.EncodingDetector;
|
import org.jcnc.jnotepad.tool.EncodingDetector;
|
||||||
import org.jcnc.jnotepad.tool.LogUtil;
|
import org.jcnc.jnotepad.tool.LogUtil;
|
||||||
import org.jcnc.jnotepad.tool.UiUtil;
|
import org.jcnc.jnotepad.tool.UiUtil;
|
||||||
|
import org.jcnc.jnotepad.ui.dialog.factory.FileChooserFactory;
|
||||||
|
import org.jcnc.jnotepad.ui.dialog.factory.impl.TextFileChooserFactory;
|
||||||
import org.jcnc.jnotepad.ui.module.LineNumberTextArea;
|
import org.jcnc.jnotepad.ui.module.LineNumberTextArea;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
@ -36,10 +40,14 @@ public class OpenFile implements EventHandler<ActionEvent> {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void handle(ActionEvent event) {
|
public void handle(ActionEvent event) {
|
||||||
// 创建文件选择器
|
FileChooserFactory factory = new TextFileChooserFactory();
|
||||||
FileChooser fileChooser = new FileChooser();
|
|
||||||
// 显示文件选择对话框,并获取选中的文件
|
// 显示文件选择对话框,并获取选中的文件
|
||||||
File file = fileChooser.showOpenDialog(UiUtil.getAppWindow());
|
File file = factory.createFileChooser(
|
||||||
|
UiResourceBundle.getContent(TextConstants.OPEN),
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
new FileChooser.ExtensionFilter("All types", "*.*"))
|
||||||
|
.showOpenDialog(UiUtil.getAppWindow());
|
||||||
if (file != null) {
|
if (file != null) {
|
||||||
openFile(file);
|
openFile(file);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,9 +5,13 @@ import javafx.event.EventHandler;
|
|||||||
import javafx.scene.control.TextField;
|
import javafx.scene.control.TextField;
|
||||||
import javafx.scene.input.KeyCode;
|
import javafx.scene.input.KeyCode;
|
||||||
import javafx.stage.FileChooser;
|
import javafx.stage.FileChooser;
|
||||||
|
import org.jcnc.jnotepad.app.i18n.UiResourceBundle;
|
||||||
|
import org.jcnc.jnotepad.constants.TextConstants;
|
||||||
import org.jcnc.jnotepad.root.center.main.center.tab.JNotepadTab;
|
import org.jcnc.jnotepad.root.center.main.center.tab.JNotepadTab;
|
||||||
import org.jcnc.jnotepad.tool.LogUtil;
|
import org.jcnc.jnotepad.tool.LogUtil;
|
||||||
import org.jcnc.jnotepad.tool.UiUtil;
|
import org.jcnc.jnotepad.tool.UiUtil;
|
||||||
|
import org.jcnc.jnotepad.ui.dialog.factory.FileChooserFactory;
|
||||||
|
import org.jcnc.jnotepad.ui.dialog.factory.impl.TextFileChooserFactory;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -29,65 +33,83 @@ public class RenameFile implements EventHandler<ActionEvent> {
|
|||||||
}
|
}
|
||||||
// 判断当前是否为关联文件
|
// 判断当前是否为关联文件
|
||||||
if (jnotepadtab.isRelevance()) {
|
if (jnotepadtab.isRelevance()) {
|
||||||
// 创建一个文件窗口
|
// 重命名关联文件
|
||||||
FileChooser fileChooser = new FileChooser();
|
handleRenameRelevanceFile(jnotepadtab);
|
||||||
// 设置窗口名称
|
|
||||||
fileChooser.setTitle("重命名");
|
|
||||||
// 设置原始文件名称
|
|
||||||
fileChooser.setInitialFileName(jnotepadtab.getText());
|
|
||||||
// 获取原始文件对象
|
|
||||||
File file = (File) jnotepadtab.getUserData();
|
|
||||||
// 设置打开的目录
|
|
||||||
fileChooser.setInitialDirectory(new File(file.getParent()));
|
|
||||||
// 设置文件类型
|
|
||||||
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("文本文档", "*.txt"));
|
|
||||||
// 获取应用窗口并绑定
|
|
||||||
File newFile = fileChooser.showSaveDialog(UiUtil.getAppWindow());
|
|
||||||
if (newFile != null) {
|
|
||||||
boolean rename = file.renameTo(newFile);
|
|
||||||
// 设置文件数据
|
|
||||||
jnotepadtab.setUserData(newFile);
|
|
||||||
if (rename) {
|
|
||||||
jnotepadtab.setText(newFile.getName());
|
|
||||||
logger.info("文件重命名成功");
|
|
||||||
} else {
|
|
||||||
logger.debug("文件重命名失败");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// 如果当前不是关联文件则重命名标签页
|
// 如果当前不是关联文件则重命名标签页
|
||||||
else {
|
else {
|
||||||
TextField textField = new TextField(jnotepadtab.getText());
|
handleRenameTab(jnotepadtab);
|
||||||
textField.getStyleClass().add("tab-title-editable");
|
}
|
||||||
// 清空标签页名称
|
}
|
||||||
jnotepadtab.setText("");
|
|
||||||
// 监听 Enter 键,完成编辑
|
|
||||||
textField.setOnKeyPressed(event -> {
|
|
||||||
if (event.getCode() == KeyCode.ENTER) {
|
|
||||||
jnotepadtab.setText(textField.getText());
|
|
||||||
// 可选:移除 TextField 的图形
|
|
||||||
jnotepadtab.setGraphic(null);
|
|
||||||
// 可选:恢复标签页的关闭按钮
|
|
||||||
jnotepadtab.setClosable(true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// 监听失去焦点事件,完成编辑
|
|
||||||
textField.focusedProperty().addListener((observable, oldValue, newValue) -> {
|
|
||||||
if (Boolean.FALSE.equals(newValue)) {
|
|
||||||
jnotepadtab.setText(textField.getText());
|
|
||||||
// 可选:移除 TextField 的图形
|
|
||||||
jnotepadtab.setGraphic(null);
|
|
||||||
// 可选:恢复标签页的关闭按钮
|
|
||||||
jnotepadtab.setClosable(true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
jnotepadtab.setClosable(false);
|
|
||||||
// 设置 TextField 作为标签页的图形
|
|
||||||
jnotepadtab.setGraphic(textField);
|
|
||||||
// 默认获取焦点并选中所有文字
|
|
||||||
textField.requestFocus();
|
|
||||||
textField.selectAll();
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重命名标签页
|
||||||
|
*
|
||||||
|
* @param jnotepadtab 标签页组件
|
||||||
|
* @since 2023/8/31 21:48
|
||||||
|
*/
|
||||||
|
private void handleRenameTab(JNotepadTab jnotepadtab) {
|
||||||
|
TextField textField = new TextField(jnotepadtab.getText());
|
||||||
|
textField.getStyleClass().add("tab-title-editable");
|
||||||
|
// 清空标签页名称
|
||||||
|
jnotepadtab.setText("");
|
||||||
|
// 监听 Enter 键,完成编辑
|
||||||
|
textField.setOnKeyPressed(event -> {
|
||||||
|
if (event.getCode() == KeyCode.ENTER) {
|
||||||
|
jnotepadtab.setText(textField.getText());
|
||||||
|
// 可选:移除 TextField 的图形
|
||||||
|
jnotepadtab.setGraphic(null);
|
||||||
|
// 可选:恢复标签页的关闭按钮
|
||||||
|
jnotepadtab.setClosable(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// 监听失去焦点事件,完成编辑
|
||||||
|
textField.focusedProperty().addListener((observable, oldValue, newValue) -> {
|
||||||
|
if (Boolean.FALSE.equals(newValue)) {
|
||||||
|
jnotepadtab.setText(textField.getText());
|
||||||
|
// 可选:移除 TextField 的图形
|
||||||
|
jnotepadtab.setGraphic(null);
|
||||||
|
// 可选:恢复标签页的关闭按钮
|
||||||
|
jnotepadtab.setClosable(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
jnotepadtab.setClosable(false);
|
||||||
|
// 设置 TextField 作为标签页的图形
|
||||||
|
jnotepadtab.setGraphic(textField);
|
||||||
|
// 默认获取焦点并选中所有文字
|
||||||
|
textField.requestFocus();
|
||||||
|
textField.selectAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重命名关联文件
|
||||||
|
*
|
||||||
|
* @param jnotepadtab 标签页组件
|
||||||
|
* @since 2023/8/31 21:47
|
||||||
|
*/
|
||||||
|
private void handleRenameRelevanceFile(JNotepadTab jnotepadtab) {
|
||||||
|
// 创建文件选择器对话框工厂,选择文本文件对话框工厂
|
||||||
|
FileChooserFactory factory = new TextFileChooserFactory();
|
||||||
|
// 获取原始文件对象
|
||||||
|
File file = (File) jnotepadtab.getUserData();
|
||||||
|
// 获取应用窗口并绑定
|
||||||
|
File newFile = factory
|
||||||
|
.createFileChooser(
|
||||||
|
UiResourceBundle.getContent(TextConstants.RENAME),
|
||||||
|
jnotepadtab.getText(),
|
||||||
|
new File(file.getParent()),
|
||||||
|
new FileChooser.ExtensionFilter("All types", "*.*"))
|
||||||
|
.showSaveDialog(UiUtil.getAppWindow());
|
||||||
|
if (newFile != null) {
|
||||||
|
boolean rename = file.renameTo(newFile);
|
||||||
|
// 设置文件数据
|
||||||
|
jnotepadtab.setUserData(newFile);
|
||||||
|
if (rename) {
|
||||||
|
jnotepadtab.setText(newFile.getName());
|
||||||
|
logger.info("文件重命名成功");
|
||||||
|
} else {
|
||||||
|
logger.debug("文件重命名失败");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,11 +3,15 @@ package org.jcnc.jnotepad.controller.event.handler.menuBar;
|
|||||||
import javafx.event.ActionEvent;
|
import javafx.event.ActionEvent;
|
||||||
import javafx.event.EventHandler;
|
import javafx.event.EventHandler;
|
||||||
import javafx.stage.FileChooser;
|
import javafx.stage.FileChooser;
|
||||||
|
import org.jcnc.jnotepad.app.i18n.UiResourceBundle;
|
||||||
|
import org.jcnc.jnotepad.constants.TextConstants;
|
||||||
import org.jcnc.jnotepad.controller.i18n.LocalizationController;
|
import org.jcnc.jnotepad.controller.i18n.LocalizationController;
|
||||||
import org.jcnc.jnotepad.root.center.main.center.tab.JNotepadTab;
|
import org.jcnc.jnotepad.root.center.main.center.tab.JNotepadTab;
|
||||||
import org.jcnc.jnotepad.tool.LogUtil;
|
import org.jcnc.jnotepad.tool.LogUtil;
|
||||||
import org.jcnc.jnotepad.tool.SingletonUtil;
|
import org.jcnc.jnotepad.tool.SingletonUtil;
|
||||||
import org.jcnc.jnotepad.tool.UiUtil;
|
import org.jcnc.jnotepad.tool.UiUtil;
|
||||||
|
import org.jcnc.jnotepad.ui.dialog.factory.FileChooserFactory;
|
||||||
|
import org.jcnc.jnotepad.ui.dialog.factory.impl.TextFileChooserFactory;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -63,16 +67,15 @@ public class SaveFile implements EventHandler<ActionEvent> {
|
|||||||
* @see LogUtil
|
* @see LogUtil
|
||||||
*/
|
*/
|
||||||
protected void saveTab(Class<?> currentClass) {
|
protected void saveTab(Class<?> currentClass) {
|
||||||
|
FileChooserFactory factory = new TextFileChooserFactory();
|
||||||
JNotepadTab selectedTab = UiUtil.getJnotepadtab();
|
JNotepadTab selectedTab = UiUtil.getJnotepadtab();
|
||||||
if (selectedTab != null) {
|
if (selectedTab != null) {
|
||||||
// 创建一个文件窗口
|
File file = factory.createFileChooser(
|
||||||
FileChooser fileChooser = new FileChooser();
|
UiResourceBundle.getContent(TextConstants.SAVE_AS),
|
||||||
// 设置保存文件名称
|
selectedTab.getText(),
|
||||||
fileChooser.setInitialFileName(selectedTab.getText());
|
null,
|
||||||
// 设置保存文件类型
|
new FileChooser.ExtensionFilter("All types", "*.*"))
|
||||||
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("文本文档", "*.txt"));
|
.showSaveDialog(UiUtil.getAppWindow());
|
||||||
// 获取应用窗口并绑定
|
|
||||||
File file = fileChooser.showSaveDialog(UiUtil.getAppWindow());
|
|
||||||
if (file != null) {
|
if (file != null) {
|
||||||
LogUtil.getLogger(currentClass).info("正在保存文件:{}", file.getName());
|
LogUtil.getLogger(currentClass).info("正在保存文件:{}", file.getName());
|
||||||
selectedTab.save(file);
|
selectedTab.save(file);
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
package org.jcnc.jnotepad.root.center.main.center.tab;
|
package org.jcnc.jnotepad.root.center.main.center.tab;
|
||||||
|
|
||||||
import javafx.scene.control.Tab;
|
import javafx.scene.control.Tab;
|
||||||
import org.jcnc.jnotepad.controller.config.AppConfigController;
|
import org.jcnc.jnotepad.tool.SingletonUtil;
|
||||||
import org.jcnc.jnotepad.ui.module.LineNumberTextArea;
|
import org.jcnc.jnotepad.ui.module.LineNumberTextArea;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -38,7 +38,7 @@ public class JNotepadTab extends Tab {
|
|||||||
super(tabTitle);
|
super(tabTitle);
|
||||||
lineNumberTextArea = textArea;
|
lineNumberTextArea = textArea;
|
||||||
this.setContent(lineNumberTextArea);
|
this.setContent(lineNumberTextArea);
|
||||||
setAutoLine(AppConfigController.getInstance().getAutoLineConfig());
|
setAutoLine(SingletonUtil.getAppConfigController().getAutoLineConfig());
|
||||||
this.charset = charset;
|
this.charset = charset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,33 @@
|
|||||||
|
package org.jcnc.jnotepad.ui.dialog.factory;
|
||||||
|
|
||||||
|
import javafx.stage.FileChooser;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件选择对话框工厂
|
||||||
|
*
|
||||||
|
* @author gewuyou
|
||||||
|
*/
|
||||||
|
public interface FileChooserFactory {
|
||||||
|
/**
|
||||||
|
* 创建原始文件选择对话框
|
||||||
|
*
|
||||||
|
* @return javafx.stage.FileChooser
|
||||||
|
* @since 2023/8/31 20:29
|
||||||
|
*/
|
||||||
|
FileChooser createFileChooser();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建详细的文件选择对话框
|
||||||
|
*
|
||||||
|
* @param title 对话框名字
|
||||||
|
* @param filename 选中的文件名
|
||||||
|
* @param directory 初始目录
|
||||||
|
* @param extensionFilter 文件类型数组
|
||||||
|
* @return javafx.stage.FileChooser
|
||||||
|
* @apiNote
|
||||||
|
* @since 2023/8/31 20:35
|
||||||
|
*/
|
||||||
|
FileChooser createFileChooser(String title, String filename, File directory, FileChooser.ExtensionFilter extensionFilter);
|
||||||
|
}
|
||||||
@ -0,0 +1,55 @@
|
|||||||
|
package org.jcnc.jnotepad.ui.dialog.factory.impl;
|
||||||
|
|
||||||
|
import javafx.stage.FileChooser;
|
||||||
|
import org.jcnc.jnotepad.ui.dialog.factory.FileChooserFactory;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文本文件选择对话框
|
||||||
|
*
|
||||||
|
* @author gewuyou
|
||||||
|
* @see FileChooserFactory
|
||||||
|
*/
|
||||||
|
public class TextFileChooserFactory implements FileChooserFactory {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建原始文件选择对话框
|
||||||
|
*
|
||||||
|
* @return javafx.stage.FileChooser
|
||||||
|
* @since 2023/8/31 20:29
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public FileChooser createFileChooser() {
|
||||||
|
return new FileChooser();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建详细的文件选择对话框
|
||||||
|
*
|
||||||
|
* @param title 对话框名字
|
||||||
|
* @param filename 选中的文件名
|
||||||
|
* @param directory 初始目录
|
||||||
|
* @param extensionFilter 文件类型数组
|
||||||
|
* @return javafx.stage.FileChooser
|
||||||
|
* @apiNote
|
||||||
|
* @since 2023/8/31 20:35
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public FileChooser createFileChooser(String title, String filename, File directory, FileChooser.ExtensionFilter extensionFilter) {
|
||||||
|
FileChooser fileChooser = createFileChooser();
|
||||||
|
// 设置窗口名称
|
||||||
|
fileChooser.setTitle(title);
|
||||||
|
if (filename != null) {
|
||||||
|
// 设置原始文件名称
|
||||||
|
fileChooser.setInitialFileName(filename);
|
||||||
|
}
|
||||||
|
if (directory != null) {
|
||||||
|
// 设置打开的目录
|
||||||
|
fileChooser.setInitialDirectory(directory);
|
||||||
|
}
|
||||||
|
// 设置文件类型
|
||||||
|
fileChooser.getExtensionFilters().add(extensionFilter);
|
||||||
|
return fileChooser;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user