fix:修复重命名同样的文件没有提示的bug

This commit is contained in:
许轲 2023-09-09 00:17:57 +08:00
parent b8c98328e3
commit 9cdf93a913

View File

@ -10,6 +10,7 @@ import org.jcnc.jnotepad.constants.TextConstants;
import org.jcnc.jnotepad.root.center.main.center.tab.CenterTab; import org.jcnc.jnotepad.root.center.main.center.tab.CenterTab;
import org.jcnc.jnotepad.root.center.main.center.tab.CenterTabPane; import org.jcnc.jnotepad.root.center.main.center.tab.CenterTabPane;
import org.jcnc.jnotepad.tool.LogUtil; import org.jcnc.jnotepad.tool.LogUtil;
import org.jcnc.jnotepad.tool.PopUpUtil;
import org.jcnc.jnotepad.tool.UiUtil; import org.jcnc.jnotepad.tool.UiUtil;
import org.jcnc.jnotepad.ui.dialog.factory.impl.BasicFileChooserFactory; import org.jcnc.jnotepad.ui.dialog.factory.impl.BasicFileChooserFactory;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -53,28 +54,42 @@ public class RenameFile implements EventHandler<ActionEvent> {
private void handleRenameTab(CenterTab centerTab) { private void handleRenameTab(CenterTab centerTab) {
TextField textField = new TextField(centerTab.getText()); TextField textField = new TextField(centerTab.getText());
textField.getStyleClass().add("tab-title-editable"); textField.getStyleClass().add("tab-title-editable");
// 清空标签页名称 // 清空标签页名称
centerTab.setText(""); centerTab.setText("");
// 监听 Enter 完成编辑 // 监听 Enter 完成编辑
textField.setOnKeyPressed(event -> { textField.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ENTER) { if (event.getCode() == KeyCode.ENTER) {
centerTab.setText(textField.getText()); String newTabName = textField.getText();
// 可选移除 TextField 的图形
centerTab.setGraphic(null); // 检查是否存在相同名称的标签页
// 可选恢复标签页的关闭按钮 if (isTabNameExists(newTabName)) {
centerTab.setClosable(true); // 显示弹窗并提示用户更换名称
showDuplicateNameAlert(newTabName);
} else {
centerTab.setText(newTabName);
// 可选移除 TextField 的图形
centerTab.setGraphic(null);
// 可选恢复标签页的关闭按钮
centerTab.setClosable(true);
}
} }
}); });
// 监听失去焦点事件完成编辑 // 监听失去焦点事件完成编辑
textField.focusedProperty().addListener((observable, oldValue, newValue) -> { textField.focusedProperty().addListener((observable, oldValue, newValue) -> {
if (Boolean.FALSE.equals(newValue)) { if (Boolean.FALSE.equals(newValue)) {
centerTab.setText(textField.getText()); String newTabName = textField.getText();
centerTab.setText(newTabName);
// 可选移除 TextField 的图形 // 可选移除 TextField 的图形
centerTab.setGraphic(null); centerTab.setGraphic(null);
// 可选恢复标签页的关闭按钮 // 可选恢复标签页的关闭按钮
centerTab.setClosable(true); centerTab.setClosable(true);
} }
}); });
centerTab.setClosable(false); centerTab.setClosable(false);
// 设置 TextField 作为标签页的图形 // 设置 TextField 作为标签页的图形
centerTab.setGraphic(textField); centerTab.setGraphic(textField);
@ -83,6 +98,27 @@ public class RenameFile implements EventHandler<ActionEvent> {
textField.selectAll(); textField.selectAll();
} }
/**
* 判断是否存在具有相同名称的标签页
*
* @param newTabName 要检查的新标签页名称
* @return 如果存在具有相同名称的标签页则返回 true否则返回 false
*/
private boolean isTabNameExists(String newTabName) {
CenterTabPane tabPane = CenterTabPane.getInstance();
return tabPane.getTabs().stream()
.anyMatch(tab -> tab.getText().equals(newTabName));
}
/**
* 显示警告弹窗提示用户更换重复的名称
*/
private void showDuplicateNameAlert(String newTabName) {
PopUpUtil.errorAlert("重命名错误", "\" " + newTabName + "\" 和已有标签页名字重复", "请再次重命名", null, null);
}
/** /**
* 重命名关联文件 * 重命名关联文件
* *