!79 修复 BUG 修复删除已有的文本后新建名字不会重新重置的bug

Merge pull request !79 from 格物方能致知/fix-I7ZT6X
This commit is contained in:
Luke 2023-09-08 16:22:05 +00:00 committed by Gitee
commit be1baff33a
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 38 additions and 15 deletions

View File

@ -63,7 +63,6 @@ public class LunchApp extends Application {
@Override
public void start(Stage primaryStage) {
SCENE.getStylesheets().add(Objects.requireNonNull(getClass().getResource("/css/styles.css")).toExternalForm());
initUiComponents();
UiResourceBundle.bindStringProperty(primaryStage.titleProperty(), TextConstants.TITLE);

View File

@ -1,5 +1,9 @@
package org.jcnc.jnotepad.constants;
import org.jcnc.jnotepad.app.i18n.UiResourceBundle;
import java.util.regex.Pattern;
/**
* 应用常量类用于存放应用程序中的常量值
*
@ -19,6 +23,11 @@ public class AppConstants {
*/
public static final String APP_ICON = "/img/icon.png";
/**
* 默认标签页的正则
*/
public static final Pattern TABNAME_PATTERN = Pattern.compile("^" + Pattern.quote(UiResourceBundle.getContent(TextConstants.NEW_FILE)) + "\\d+$");
/**
* 私有构造函数防止该类被实例化
*/

View File

@ -2,13 +2,17 @@ package org.jcnc.jnotepad.controller.event.handler.menubar;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Tab;
import org.jcnc.jnotepad.app.i18n.UiResourceBundle;
import org.jcnc.jnotepad.constants.AppConstants;
import org.jcnc.jnotepad.constants.TextConstants;
import org.jcnc.jnotepad.root.center.main.bottom.status.BottomStatusBox;
import org.jcnc.jnotepad.root.center.main.center.tab.CenterTab;
import org.jcnc.jnotepad.root.center.main.center.tab.CenterTabPane;
import org.jcnc.jnotepad.ui.module.LineNumberTextArea;
import org.jcnc.jnotepad.view.manager.ViewManager;
import java.util.Comparator;
import java.util.List;
/**
* 新建文件事件的事件处理程序
@ -36,11 +40,32 @@ public class NewFile implements EventHandler<ActionEvent> {
// 创建一个新的文本编辑区
LineNumberTextArea textArea = new LineNumberTextArea();
// TODO: refactor统一TextArea新建绑定监听器入口
ViewManager viewManager = ViewManager.getInstance();
// 设定初始索引
int index = 1;
StringBuilder tabTitle = new StringBuilder();
// 获取当前默认创建标签页集合
List<Tab> tabs = CenterTabPane.getInstance()
.getTabs()
.stream()
// 排除不属于默认创建的标签页
.filter(tab -> AppConstants.TABNAME_PATTERN.matcher(tab.getText()).matches())
// 对默认创建的标签页进行排序
.sorted(Comparator.comparing(Tab::getText))
// 转为List集合
.toList();
// 构建初始标签页名称
tabTitle.append(UiResourceBundle.getContent(TextConstants.NEW_FILE)).append(index);
for (Tab tab : tabs) {
if (tab.getText().contentEquals(tabTitle)) {
tabTitle.setLength(0);
tabTitle.append(UiResourceBundle.getContent(TextConstants.NEW_FILE)).append(++index);
} else {
break;
}
}
// 创建标签页
CenterTab centerTab = new CenterTab(
UiResourceBundle.getContent(TextConstants.NEW_FILE)
+ viewManager.selfIncreaseAndGetTabIndex(),
tabTitle.toString(),
textArea);
// 设置当前标签页与本地文件无关联
centerTab.setRelevance(false);

View File

@ -16,7 +16,6 @@ public class ViewManager {
private static ViewManager instance = null;
private int tabIndex = 0;
/**
* 主布局
*/
@ -59,15 +58,6 @@ public class ViewManager {
}
}
/**
* 自增并获取标签页索引
*
* @return 自增后的标签页索引
* @apiNote ++tabIndex
*/
public int selfIncreaseAndGetTabIndex() {
return ++tabIndex;
}
/**
* 初始化屏幕组件