增加注释
This commit is contained in:
parent
929ae12fbf
commit
d680623993
@ -6,19 +6,30 @@ import javafx.event.ActionEvent;
|
|||||||
import javafx.event.EventHandler;
|
import javafx.event.EventHandler;
|
||||||
import javafx.scene.control.Tab;
|
import javafx.scene.control.Tab;
|
||||||
import javafx.scene.control.TextArea;
|
import javafx.scene.control.TextArea;
|
||||||
import javafx.stage.FileChooser;
|
|
||||||
import org.jcnc.jnotepad.Interface.ControllerInterface;
|
import org.jcnc.jnotepad.Interface.ControllerInterface;
|
||||||
import org.jcnc.jnotepad.LunchApp;
|
import org.jcnc.jnotepad.LunchApp;
|
||||||
import org.jcnc.jnotepad.controller.event.handler.*;
|
import org.jcnc.jnotepad.controller.event.handler.LineFeed;
|
||||||
|
import org.jcnc.jnotepad.controller.event.handler.NewFile;
|
||||||
|
import org.jcnc.jnotepad.controller.event.handler.OpenFile;
|
||||||
|
import org.jcnc.jnotepad.controller.event.handler.SaveAsFile;
|
||||||
|
import org.jcnc.jnotepad.tool.EncodingDetector;
|
||||||
|
import org.jcnc.jnotepad.view.manager.ViewManager;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.jcnc.jnotepad.view.manager.ViewManager.*;
|
/**
|
||||||
import static org.jcnc.jnotepad.tool.EncodingDetector.detectEncoding;
|
* 控制器类,实现ControllerInterface接口,用于管理文本编辑器的各种操作和事件处理。
|
||||||
|
* 包括打开关联文件、创建文本区域、处理行分隔、新建文件、打开文件、自动保存等功能。
|
||||||
|
*/
|
||||||
public class Controller implements ControllerInterface {
|
public class Controller implements ControllerInterface {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打开关联文件并创建文本区域。
|
||||||
|
*
|
||||||
|
* @param rawParameters 原始参数列表
|
||||||
|
* @return 创建的文本区域
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public TextArea openAssociatedFileAndCreateTextArea(List<String> rawParameters) {
|
public TextArea openAssociatedFileAndCreateTextArea(List<String> rawParameters) {
|
||||||
if (!rawParameters.isEmpty()) {
|
if (!rawParameters.isEmpty()) {
|
||||||
@ -32,25 +43,47 @@ public class Controller implements ControllerInterface {
|
|||||||
return textArea;
|
return textArea;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取行分隔事件处理程序。
|
||||||
|
*
|
||||||
|
* @param textArea 文本区域
|
||||||
|
* @return 行分隔事件处理程序
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public EventHandler<ActionEvent> getLineFeedEventHandler(TextArea textArea) {
|
public EventHandler<ActionEvent> getLineFeedEventHandler(TextArea textArea) {
|
||||||
return new LineFeed(textArea);
|
return new LineFeed(textArea);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取新建文件事件处理程序。
|
||||||
|
*
|
||||||
|
* @param textArea 文本区域
|
||||||
|
* @return 新建文件事件处理程序
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public EventHandler<ActionEvent> getNewFileEventHandler(TextArea textArea) {
|
public EventHandler<ActionEvent> getNewFileEventHandler(TextArea textArea) {
|
||||||
return new NewFile();
|
return new NewFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取打开文件事件处理程序。
|
||||||
|
*
|
||||||
|
* @return 打开文件事件处理程序
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public EventHandler<ActionEvent> getOpenFileEventHandler() {
|
public EventHandler<ActionEvent> getOpenFileEventHandler() {
|
||||||
return new OpenFile();
|
return new OpenFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自动保存文本内容。
|
||||||
|
*
|
||||||
|
* @param textArea 文本区域
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void autoSave(TextArea textArea) {
|
public void autoSave(TextArea textArea) {
|
||||||
textArea.textProperty().addListener((observable, oldValue, newValue) -> {
|
textArea.textProperty().addListener((observable, oldValue, newValue) -> {
|
||||||
Tab tab = tabPane.getSelectionModel().getSelectedItem();
|
Tab tab = ViewManager.tabPane.getSelectionModel().getSelectedItem();
|
||||||
if (tab != null) {
|
if (tab != null) {
|
||||||
File file = (File) tab.getUserData();
|
File file = (File) tab.getUserData();
|
||||||
if (file != null) {
|
if (file != null) {
|
||||||
@ -64,20 +97,35 @@ public class Controller implements ControllerInterface {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取另存为文件事件处理程序。
|
||||||
|
*
|
||||||
|
* @return 另存为文件事件处理程序
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public EventHandler<ActionEvent> getSaveAsFileEventHandler() {
|
public EventHandler<ActionEvent> getSaveAsFileEventHandler() {
|
||||||
return new SaveAsFile();
|
return new SaveAsFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新状态标签。
|
||||||
|
*
|
||||||
|
* @param textArea 文本区域
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void updateStatusLabel(TextArea textArea) {
|
public void updateStatusLabel(TextArea textArea) {
|
||||||
int caretPosition = textArea.getCaretPosition();
|
int caretPosition = textArea.getCaretPosition();
|
||||||
int row = getRow(caretPosition, textArea.getText());
|
int row = getRow(caretPosition, textArea.getText());
|
||||||
int column = getColumn(caretPosition, textArea.getText());
|
int column = getColumn(caretPosition, textArea.getText());
|
||||||
int length = textArea.getLength();
|
int length = textArea.getLength();
|
||||||
statusLabel.setText("行: " + row + " \t列: " + column + " \t字数: " + length);
|
ViewManager.statusLabel.setText("行: " + row + " \t列: " + column + " \t字数: " + length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打开关联文件。
|
||||||
|
*
|
||||||
|
* @param filePath 文件路径
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void openAssociatedFile(String filePath) {
|
public void openAssociatedFile(String filePath) {
|
||||||
File file = new File(filePath);
|
File file = new File(filePath);
|
||||||
@ -87,6 +135,11 @@ public class Controller implements ControllerInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取文本文件的内容。
|
||||||
|
*
|
||||||
|
* @param file 文件对象
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void getText(File file) {
|
public void getText(File file) {
|
||||||
TextArea textArea = createNewTextArea();
|
TextArea textArea = createNewTextArea();
|
||||||
@ -102,8 +155,8 @@ public class Controller implements ControllerInterface {
|
|||||||
textArea.setText(text);
|
textArea.setText(text);
|
||||||
|
|
||||||
Tab tab = createNewTab(file.getName(), textArea);
|
Tab tab = createNewTab(file.getName(), textArea);
|
||||||
tabPane.getTabs().add(tab);
|
ViewManager.tabPane.getTabs().add(tab);
|
||||||
tabPane.getSelectionModel().select(tab);
|
ViewManager.tabPane.getSelectionModel().select(tab);
|
||||||
updateStatusLabel(textArea);
|
updateStatusLabel(textArea);
|
||||||
|
|
||||||
autoSave(textArea);
|
autoSave(textArea);
|
||||||
@ -113,12 +166,24 @@ public class Controller implements ControllerInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新编码标签。
|
||||||
|
*
|
||||||
|
* @param text 文本内容
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void upDateEncodingLabel(String text) {
|
public void upDateEncodingLabel(String text) {
|
||||||
String encoding = detectEncoding(text);
|
String encoding = EncodingDetector.detectEncoding(text);
|
||||||
enCodingLabel.setText("\t编码: " + encoding);
|
ViewManager.enCodingLabel.setText("\t编码: " + encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取光标所在行号。
|
||||||
|
*
|
||||||
|
* @param caretPosition 光标位置
|
||||||
|
* @param text 文本内容
|
||||||
|
* @return 光标所在行号
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int getRow(int caretPosition, String text) {
|
public int getRow(int caretPosition, String text) {
|
||||||
caretPosition = Math.min(caretPosition, text.length());
|
caretPosition = Math.min(caretPosition, text.length());
|
||||||
@ -132,16 +197,26 @@ public class Controller implements ControllerInterface {
|
|||||||
return count + 1;
|
return count + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取光标所在列号。
|
||||||
|
*
|
||||||
|
* @param caretPosition 光标位置
|
||||||
|
* @param text 文本内容
|
||||||
|
* @return 光标所在列号
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int getColumn(int caretPosition, String text) {
|
public int getColumn(int caretPosition, String text) {
|
||||||
return caretPosition - text.lastIndexOf("\n", caretPosition - 1);
|
return caretPosition - text.lastIndexOf("\n", caretPosition - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化标签面板。
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void initTabPane() {
|
public void initTabPane() {
|
||||||
Controller controller = new Controller();
|
Controller controller = new Controller();
|
||||||
|
|
||||||
tabPane.getSelectionModel().selectedItemProperty().addListener((observable, oldTab, newTab) -> {
|
ViewManager.tabPane.getSelectionModel().selectedItemProperty().addListener((observable, oldTab, newTab) -> {
|
||||||
if (newTab != null) {
|
if (newTab != null) {
|
||||||
// 获取新选定的标签页并关联的文本区域
|
// 获取新选定的标签页并关联的文本区域
|
||||||
TextArea textArea = (TextArea) newTab.getContent();
|
TextArea textArea = (TextArea) newTab.getContent();
|
||||||
@ -158,7 +233,11 @@ public class Controller implements ControllerInterface {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置文本区域。
|
||||||
|
*
|
||||||
|
* @param textArea 文本区域
|
||||||
|
*/
|
||||||
private void configureTextArea(TextArea textArea) {
|
private void configureTextArea(TextArea textArea) {
|
||||||
textArea.setWrapText(true);
|
textArea.setWrapText(true);
|
||||||
upDateEncodingLabel(textArea.getText());
|
upDateEncodingLabel(textArea.getText());
|
||||||
@ -169,10 +248,22 @@ public class Controller implements ControllerInterface {
|
|||||||
autoSave(textArea);
|
autoSave(textArea);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建新的文本区域。
|
||||||
|
*
|
||||||
|
* @return 新的文本区域
|
||||||
|
*/
|
||||||
private TextArea createNewTextArea() {
|
private TextArea createNewTextArea() {
|
||||||
return new TextArea();
|
return new TextArea();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建新的标签页。
|
||||||
|
*
|
||||||
|
* @param tabName 标签名
|
||||||
|
* @param textArea 文本区域
|
||||||
|
* @return 新的标签页
|
||||||
|
*/
|
||||||
private Tab createNewTab(String tabName, TextArea textArea) {
|
private Tab createNewTab(String tabName, TextArea textArea) {
|
||||||
Tab tab = new Tab(tabName);
|
Tab tab = new Tab(tabName);
|
||||||
tab.setContent(textArea);
|
tab.setContent(textArea);
|
||||||
@ -180,6 +271,12 @@ public class Controller implements ControllerInterface {
|
|||||||
return tab;
|
return tab;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建打开文件的任务。
|
||||||
|
*
|
||||||
|
* @param file 文件对象
|
||||||
|
* @return 打开文件的任务
|
||||||
|
*/
|
||||||
private Task<Void> createOpenFileTask(File file) {
|
private Task<Void> createOpenFileTask(File file) {
|
||||||
TextArea textArea = createNewTextArea();
|
TextArea textArea = createNewTextArea();
|
||||||
return new Task<>() {
|
return new Task<>() {
|
||||||
@ -192,6 +289,11 @@ public class Controller implements ControllerInterface {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打开文件。
|
||||||
|
*
|
||||||
|
* @param file 文件对象
|
||||||
|
*/
|
||||||
private void openFile(File file) {
|
private void openFile(File file) {
|
||||||
Task<Void> openFileTask = createOpenFileTask(file);
|
Task<Void> openFileTask = createOpenFileTask(file);
|
||||||
Thread thread = new Thread(openFileTask);
|
Thread thread = new Thread(openFileTask);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user