fix:修复重命名同样的文件没有提示的bug
This commit is contained in:
parent
b8c98328e3
commit
9cdf93a913
@ -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.CenterTabPane;
|
||||
import org.jcnc.jnotepad.tool.LogUtil;
|
||||
import org.jcnc.jnotepad.tool.PopUpUtil;
|
||||
import org.jcnc.jnotepad.tool.UiUtil;
|
||||
import org.jcnc.jnotepad.ui.dialog.factory.impl.BasicFileChooserFactory;
|
||||
import org.slf4j.Logger;
|
||||
@ -53,28 +54,42 @@ public class RenameFile implements EventHandler<ActionEvent> {
|
||||
private void handleRenameTab(CenterTab centerTab) {
|
||||
TextField textField = new TextField(centerTab.getText());
|
||||
textField.getStyleClass().add("tab-title-editable");
|
||||
|
||||
// 清空标签页名称
|
||||
centerTab.setText("");
|
||||
|
||||
// 监听 Enter 键,完成编辑
|
||||
textField.setOnKeyPressed(event -> {
|
||||
if (event.getCode() == KeyCode.ENTER) {
|
||||
centerTab.setText(textField.getText());
|
||||
// 可选:移除 TextField 的图形
|
||||
centerTab.setGraphic(null);
|
||||
// 可选:恢复标签页的关闭按钮
|
||||
centerTab.setClosable(true);
|
||||
String newTabName = textField.getText();
|
||||
|
||||
// 检查是否存在相同名称的标签页
|
||||
if (isTabNameExists(newTabName)) {
|
||||
// 显示弹窗并提示用户更换名称
|
||||
showDuplicateNameAlert(newTabName);
|
||||
} else {
|
||||
centerTab.setText(newTabName);
|
||||
// 可选:移除 TextField 的图形
|
||||
centerTab.setGraphic(null);
|
||||
// 可选:恢复标签页的关闭按钮
|
||||
centerTab.setClosable(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 监听失去焦点事件,完成编辑
|
||||
textField.focusedProperty().addListener((observable, oldValue, newValue) -> {
|
||||
if (Boolean.FALSE.equals(newValue)) {
|
||||
centerTab.setText(textField.getText());
|
||||
String newTabName = textField.getText();
|
||||
centerTab.setText(newTabName);
|
||||
// 可选:移除 TextField 的图形
|
||||
centerTab.setGraphic(null);
|
||||
// 可选:恢复标签页的关闭按钮
|
||||
centerTab.setClosable(true);
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
centerTab.setClosable(false);
|
||||
// 设置 TextField 作为标签页的图形
|
||||
centerTab.setGraphic(textField);
|
||||
@ -83,6 +98,27 @@ public class RenameFile implements EventHandler<ActionEvent> {
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 重命名关联文件。
|
||||
*
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user