🐛 修复 BUG 修复错误的监听器设置方法导致的文件数据覆盖问题
This commit is contained in:
parent
3d69d8ac0e
commit
9711e7b2f1
@ -55,7 +55,7 @@ public class SaveFile implements EventHandler<ActionEvent> {
|
||||
} else {
|
||||
logger.info("当前保存文件为关联打开文件,调用自动保存方法");
|
||||
// 调用tab保存方法
|
||||
selectedTab.save();
|
||||
selectedTab.saveSelectedFileTab();
|
||||
// 如果该文件是配置文件,则刷新快捷键
|
||||
if (CONFIG_NAME.equals(selectedTab.getText())) {
|
||||
// 重新加载语言包和快捷键
|
||||
|
||||
@ -41,10 +41,8 @@ public class Controller implements ControllerAble {
|
||||
// 获取上次打开的页面
|
||||
Cache cache = CACHE_MANAGER.getCache("tabs", "centerTabs");
|
||||
List<String> fileTab = (List<String>) cache.getCacheData();
|
||||
// fileTab.forEach(filePath -> new OpenFile().openFile(new File(filePath)));
|
||||
for (String filePath : fileTab) {
|
||||
new OpenFile().openFile(new File(filePath));
|
||||
}
|
||||
fileTab.forEach(filePath -> new OpenFile().openFile(new File(filePath)));
|
||||
|
||||
if (!rawParameters.isEmpty()) {
|
||||
String filePath = rawParameters.get(0);
|
||||
openAssociatedFile(filePath);
|
||||
|
||||
@ -12,18 +12,9 @@ import org.fxmisc.richtext.LineNumberFactory;
|
||||
import org.fxmisc.richtext.model.Paragraph;
|
||||
import org.fxmisc.richtext.model.StyleSpans;
|
||||
import org.fxmisc.richtext.model.StyleSpansBuilder;
|
||||
import org.jcnc.jnotepad.util.LogUtil;
|
||||
import org.jcnc.jnotepad.views.manager.BottomStatusBoxManager;
|
||||
import org.jcnc.jnotepad.views.manager.CenterTabPaneManager;
|
||||
import org.jcnc.jnotepad.views.root.center.main.center.tab.CenterTab;
|
||||
import org.reactfx.Subscription;
|
||||
import org.reactfx.collection.ListModification;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@ -84,11 +75,6 @@ public class LineNumberTextArea extends CodeArea {
|
||||
+ "|(?<COMMENT>" + COMMENT_PATTERN + ")"
|
||||
);
|
||||
|
||||
/**
|
||||
* 用于记录日志的静态Logger对象
|
||||
*/
|
||||
private static final Logger logger = LogUtil.getLogger(LineNumberTextArea.class);
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* <p>
|
||||
@ -128,10 +114,7 @@ public class LineNumberTextArea extends CodeArea {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
initListeners();
|
||||
this.getStylesheets().add(Objects.requireNonNull(getClass().getResource("/css/java_code_styles.css")).toString());
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -243,51 +226,4 @@ public class LineNumberTextArea extends CodeArea {
|
||||
System.out.println(((CodeArea) getOwnerNode()).getText());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化监听器方法
|
||||
*/
|
||||
private void initListeners() {
|
||||
// 监听主要文本区域的文本变化
|
||||
this.textProperty().addListener((observable, oldValue, newValue) -> {
|
||||
BottomStatusBoxManager.getInstance().updateWordCountStatusLabel();
|
||||
save();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存方法
|
||||
*/
|
||||
public void save() {
|
||||
// 获取当前选定的中央标签页(CenterTab对象)
|
||||
CenterTab tab = CenterTabPaneManager.getInstance().getSelected();
|
||||
|
||||
// 如果没有选定标签页,返回,不执行保存操作
|
||||
if (tab == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 从标签页的用户数据中获取文件对象
|
||||
File file = (File) tab.getUserData();
|
||||
|
||||
// 获取主文本区域中的文本内容
|
||||
String newValue = this.getText();
|
||||
|
||||
// 如果文件对象为空,记录警告信息并返回,不执行保存操作
|
||||
if (file == null) {
|
||||
logger.warn("Tab上没有关联文件信息");
|
||||
return;
|
||||
}
|
||||
|
||||
// 尝试使用BufferedWriter写入文件内容
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file, tab.getCharset()))) {
|
||||
// 将新的文本内容写入文件
|
||||
writer.write(newValue);
|
||||
// 记录保存操作的日志信息
|
||||
LogUtil.getLogger(this.getClass()).info("正在自动保存---");
|
||||
} catch (IOException ignored) {
|
||||
// 如果发生IO异常,记录忽视的日志信息,但不中断程序执行
|
||||
LogUtil.getLogger(this.getClass()).info("已忽视IO异常!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -98,6 +98,9 @@ public class CenterTabPaneManager {
|
||||
bottomStatusBoxManager.updateWhenTabSelected();
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存当前所有打开的文件标签页
|
||||
*/
|
||||
public void saveOpenFileTabs() {
|
||||
// 获取当前所有标签页
|
||||
ObservableList<Tab> tabs = centerTabPane.getTabs();
|
||||
|
||||
@ -4,8 +4,15 @@ import javafx.scene.control.Tab;
|
||||
import org.fxmisc.flowless.VirtualizedScrollPane;
|
||||
import org.jcnc.jnotepad.controller.config.AppConfigController;
|
||||
import org.jcnc.jnotepad.ui.module.LineNumberTextArea;
|
||||
import org.jcnc.jnotepad.util.LogUtil;
|
||||
import org.jcnc.jnotepad.views.manager.BottomStatusBoxManager;
|
||||
import org.jcnc.jnotepad.views.manager.CenterTabPaneManager;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
@ -15,7 +22,7 @@ import java.nio.charset.Charset;
|
||||
* @author songdragon
|
||||
*/
|
||||
public class CenterTab extends Tab {
|
||||
|
||||
Logger logger = LogUtil.getLogger(this.getClass());
|
||||
private final LineNumberTextArea lineNumberTextArea;
|
||||
/**
|
||||
* 默认关闭自动换行
|
||||
@ -38,6 +45,7 @@ public class CenterTab extends Tab {
|
||||
public CenterTab(String tabTitle, LineNumberTextArea textArea, Charset charset) {
|
||||
super(tabTitle);
|
||||
lineNumberTextArea = textArea;
|
||||
initTextAreaListeners();
|
||||
this.setContent(new VirtualizedScrollPane<>(lineNumberTextArea));
|
||||
setAutoLine(AppConfigController.getInstance().getAutoLineConfig());
|
||||
this.charset = charset;
|
||||
@ -73,12 +81,50 @@ public class CenterTab extends Tab {
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存文件内容
|
||||
* 保存选中的文件标签页
|
||||
*/
|
||||
public void save() {
|
||||
this.lineNumberTextArea.save();
|
||||
public void saveSelectedFileTab() {
|
||||
// 获取当前选定的中央标签页(CenterTab对象)
|
||||
CenterTab tab = CenterTabPaneManager.getInstance().getSelected();
|
||||
|
||||
// 如果没有选定标签页,返回,不执行保存操作
|
||||
if (tab == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 从标签页的用户数据中获取文件对象
|
||||
File file = (File) tab.getUserData();
|
||||
// 获取主文本区域中的文本内容
|
||||
String newValue = tab.getLineNumberTextArea().getText();
|
||||
|
||||
// 如果文件对象为空,记录警告信息并返回,不执行保存操作
|
||||
if (file == null) {
|
||||
logger.warn("Tab上没有关联文件信息");
|
||||
return;
|
||||
}
|
||||
|
||||
// 尝试使用BufferedWriter写入文件内容
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file, tab.getCharset()))) {
|
||||
// 将新的文本内容写入文件
|
||||
writer.write(newValue);
|
||||
// 记录保存操作的日志信息
|
||||
LogUtil.getLogger(this.getClass()).info("正在自动保存---");
|
||||
} catch (IOException ignored) {
|
||||
// 如果发生IO异常,记录忽视的日志信息,但不中断程序执行
|
||||
LogUtil.getLogger(this.getClass()).info("已忽视IO异常!");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化监听器方法
|
||||
*/
|
||||
private void initTextAreaListeners() {
|
||||
// 监听主要文本区域的文本变化
|
||||
lineNumberTextArea.textProperty().addListener((observable, oldValue, newValue) -> {
|
||||
BottomStatusBoxManager.getInstance().updateWordCountStatusLabel();
|
||||
saveSelectedFileTab();
|
||||
});
|
||||
}
|
||||
/**
|
||||
* 保存为指定文件
|
||||
*
|
||||
@ -89,7 +135,7 @@ public class CenterTab extends Tab {
|
||||
return;
|
||||
}
|
||||
this.setUserData(file);
|
||||
save();
|
||||
saveSelectedFileTab();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user