* 用于创建 LineNumberTextArea 对象 */ public LineNumberTextArea() { - // 设置主文本区域是否自动换行,根据应用配置决定 - mainTextArea.setWrapText(AppConfigController.getInstance().getAutoLineConfig()); - - // 设置行号区域不可编辑 - lineNumberArea.setEditable(false); - - // 设置行号区域的首选宽度和最小宽度为最小行号宽度 - lineNumberArea.setPrefWidth(MIN_LINE_NUMBER_WIDTH); - lineNumberArea.setMinWidth(MIN_LINE_NUMBER_WIDTH); - - // 为行号区域和主文本区域添加CSS样式类 - lineNumberArea.getStyleClass().add("text-line-number"); - mainTextArea.getStyleClass().add("main-text-area"); - // 设置 LineNumberTextArea 的样式,包括边框和背景颜色 this.setStyle( "-fx-border-color:white;" + "-fx-background-color:white" ); - - // 初始化监听器,用于处理事件 + this.setParagraphGraphicFactory(LineNumberFactory.get(this)); initListeners(); - // 将主文本区域设置为中央内容,将行号区域设置为左侧内容 - setCenter(mainTextArea); - setLeft(lineNumberArea); - } + } /** * 初始化监听器方法 */ private void initListeners() { - // 监听主要文本区域的滚动位置变化 - mainTextArea.scrollTopProperty().addListener((observable, oldValue, newValue) -> lineNumberArea.setScrollTop(mainTextArea.getScrollTop())); - // 监听行号文本区域的滚动位置变化 - lineNumberArea.scrollTopProperty().addListener((observable, oldValue, newValue) -> mainTextArea.setScrollTop(lineNumberArea.getScrollTop())); - // 监听行号文本区域的文本变化 - lineNumberArea.textProperty().addListener((observable, oldValue, newValue) -> updateLineNumberWidth()); - // 监听主要文本区域的光标位置变化 - this.mainTextArea.caretPositionProperty().addListener((caretObservable, oldPosition, newPosition) -> BottomStatusBox.getInstance().updateWordCountStatusLabel()); // 监听主要文本区域的文本变化 this.textProperty().addListener((observable, oldValue, newValue) -> { - updateLineNumberArea(); BottomStatusBox.getInstance().updateWordCountStatusLabel(); save(); }); @@ -121,7 +71,7 @@ public class LineNumberTextArea extends BorderPane { File file = (File) tab.getUserData(); // 获取主文本区域中的文本内容 - String newValue = this.mainTextArea.getText(); + String newValue = this.getText(); // 如果文件对象为空,记录警告信息并返回,不执行保存操作 if (file == null) { @@ -140,90 +90,4 @@ public class LineNumberTextArea extends BorderPane { LogUtil.getLogger(this.getClass()).info("已忽视IO异常!"); } } - - - /** - * 更新行号宽度方法 - */ - private void updateLineNumberWidth() { - // 获取主文本区域的段落数量,即文本的行数 - int numOfLines = mainTextArea.getParagraphs().size(); - - // 初始化一个计数器,用于确定适合的行号宽度 - int count = 1; - - // 遍历行号宽度大小的表格 - for (int i = 0; i < SIZE_TABLE.length; i++) { - // 检查文本行数是否在当前表格项的范围内 - if (numOfLines <= SIZE_TABLE[i]) { - // 如果是,设置计数器为当前索引+1并退出循环 - count = i + 1; - break; - } - } - - // 计算实际的行号区域宽度,确保不小于一个最小宽度值 - int actualWidth = Math.max(count * 10 + 11, MIN_LINE_NUMBER_WIDTH); - - // 检查实际宽度是否与当前行号区域的宽度不同 - if (actualWidth != lineNumberArea.getWidth()) { - // 如果不同,设置行号区域的首选宽度为实际宽度 - lineNumberArea.setPrefWidth(actualWidth); - } - } - - - /** - * 获取主要文本区域的text属性 - * - * @return 主要文本区域的text属性 - */ - public StringProperty textProperty() { - return mainTextArea.textProperty(); - } - - /** - * 更新行号区域方法 - */ - private void updateLineNumberArea() { - // 获取主文本区域的垂直滚动位置 - double mainTextAreaScrollTop = mainTextArea.getScrollTop(); - - // 获取行号区域的垂直滚动位置 - double lineNumberAreaScrollTop = lineNumberArea.getScrollTop(); - - // 获取主文本区域的段落数量,即文本的行数 - int numOfLines = mainTextArea.getParagraphs().size(); - - // 用于构建行号文本的字符串构建器 - StringBuilder lineNumberText = new StringBuilder(); - - // 循环迭代,生成行号文本, - for (int i = 1; i <= numOfLines; i++) { - // 将行号和换行符添加到字符串中 - lineNumberText.append(i); - if (i != numOfLines) { - lineNumberText.append("\n"); - } - } - - // 将生成的行号文本设置到 行号区域 - lineNumberArea.setText(lineNumberText.toString()); - - // 恢复主文本区域的垂直滚动位置 - mainTextArea.setScrollTop(mainTextAreaScrollTop); - - // 恢复行号区域的垂直滚动位置 - lineNumberArea.setScrollTop(lineNumberAreaScrollTop); - } - - - /** - * 获取主要文本区域 - * - * @return 主要文本区域 - */ - public TextArea getMainTextArea() { - return mainTextArea; - } } \ No newline at end of file diff --git a/src/main/java/org/jcnc/jnotepad/views/root/center/main/bottom/status/BottomStatusBox.java b/src/main/java/org/jcnc/jnotepad/views/root/center/main/bottom/status/BottomStatusBox.java index eeee545..4a064dc 100644 --- a/src/main/java/org/jcnc/jnotepad/views/root/center/main/bottom/status/BottomStatusBox.java +++ b/src/main/java/org/jcnc/jnotepad/views/root/center/main/bottom/status/BottomStatusBox.java @@ -3,10 +3,10 @@ package org.jcnc.jnotepad.views.root.center.main.bottom.status; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.control.Label; -import javafx.scene.control.TextArea; import org.jcnc.jnotepad.app.i18n.UiResourceBundle; import org.jcnc.jnotepad.common.constants.TextConstants; import org.jcnc.jnotepad.ui.module.AbstractHorizontalBox; +import org.jcnc.jnotepad.ui.module.LineNumberTextArea; import org.jcnc.jnotepad.views.root.center.main.center.tab.CenterTab; import org.jcnc.jnotepad.views.root.center.main.center.tab.CenterTabPane; @@ -94,7 +94,7 @@ public class BottomStatusBox extends AbstractHorizontalBox { if (instance.getSelected() == null) { return; } - TextArea textArea = instance.getSelected().getLineNumberTextArea().getMainTextArea(); + LineNumberTextArea textArea = instance.getSelected().getLineNumberTextArea(); int caretPosition = textArea.getCaretPosition(); int row = getRow(caretPosition, textArea.getText()); int column = getColumn(caretPosition, textArea.getText()); diff --git a/src/main/java/org/jcnc/jnotepad/views/root/center/main/center/tab/CenterTab.java b/src/main/java/org/jcnc/jnotepad/views/root/center/main/center/tab/CenterTab.java index 1cb56db..db93f42 100644 --- a/src/main/java/org/jcnc/jnotepad/views/root/center/main/center/tab/CenterTab.java +++ b/src/main/java/org/jcnc/jnotepad/views/root/center/main/center/tab/CenterTab.java @@ -56,7 +56,7 @@ public class CenterTab extends Tab { public void setAutoLine(boolean autoLine) { this.autoLine = autoLine; - lineNumberTextArea.getMainTextArea().setWrapText(autoLine); + lineNumberTextArea.setWrapText(autoLine); } public LineNumberTextArea getLineNumberTextArea() {