重构代码

This commit is contained in:
许轲 2023-08-11 03:14:20 +08:00
parent 8144bce921
commit 15c6660a5a
4 changed files with 28 additions and 28 deletions

View File

@ -13,7 +13,7 @@ import org.jcnc.jnotepad.view.View;
import java.util.List;
import java.util.Objects;
import static org.jcnc.jnotepad.ViewManager.tabPane;
import static org.jcnc.jnotepad.viewManager.tabPane;
import static org.jcnc.jnotepad.controller.Controller.updateStatusLabel;
public class MainApp extends Application {
@ -37,7 +37,7 @@ public class MainApp extends Application {
primaryStage.getIcons().add(new Image((Objects.requireNonNull(getClass().getResource(icon))).toString()));
primaryStage.show();
ViewManager viewManager = ViewManager.getInstance(scene);
viewManager viewManager = org.jcnc.jnotepad.viewManager.getInstance(scene);
viewManager.initScreen(scene);
@ -52,7 +52,7 @@ public class MainApp extends Application {
TextArea textArea = Controller.openAssociatedFileAndCreateTextArea(rawParameters);
if (isRelevance) {
// 创建新标签页并添加到标签栏
Tab tab = new Tab("新建文件 " + ++ViewManager.tabIndex);
Tab tab = new Tab("新建文件 " + ++viewManager.tabIndex);
tab.setContent(textArea);
tabPane.getTabs().add(tab);
tabPane.getSelectionModel().select(tab);

View File

@ -11,7 +11,7 @@ import org.jcnc.jnotepad.MainApp;
import java.io.*;
import java.util.List;
import static org.jcnc.jnotepad.ViewManager.*;
import static org.jcnc.jnotepad.viewManager.*;
/**
* 控制器类负责处理与用户界面的交互并实现相关事件处理逻辑
@ -32,7 +32,7 @@ public class Controller {
TextArea textArea = new TextArea(); // 创建新的文本编辑区
updateEncodingLabel(textArea.getText()); // 更新文本编码信息
upDateEncodingLabel(textArea.getText()); // 更新文本编码信息
updateStatusLabel(textArea);
// 添加文本变更监听器
@ -40,7 +40,7 @@ public class Controller {
// 更新状态栏信息
updateStatusLabel(textArea);
});
AutoSave(textArea); // 自动保存
autoSave(textArea); // 自动保存
return textArea;
}
@ -55,7 +55,7 @@ public class Controller {
tabPane.getSelectionModel().select(tab);
updateStatusLabel(textArea);
// 更新编码信息
updateEncodingLabel(textArea.getText()); // 更新文本编码信息
upDateEncodingLabel(textArea.getText()); // 更新文本编码信息
}
}
@ -69,8 +69,8 @@ public class Controller {
Task<Void> openFileTask = new Task<>() {
@Override
protected Void call() throws Exception {
getTXT(file);
updateEncodingLabel(((TextArea) tabPane.getSelectionModel().getSelectedItem().getContent()).getText());
getText(file);
upDateEncodingLabel(((TextArea) tabPane.getSelectionModel().getSelectedItem().getContent()).getText());
return null;
}
};
@ -90,7 +90,7 @@ public class Controller {
}
// 自动保存方法
public static void AutoSave(TextArea textArea) {
public static void autoSave(TextArea textArea) {
// 当文本编辑区内容发生变化时自动保存文本到文件
textArea.textProperty().addListener((observable, oldValue, newValue) -> {
Tab tab = tabPane.getSelectionModel().getSelectedItem();
@ -156,7 +156,7 @@ public class Controller {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
TextArea textArea = (TextArea) selectedTab.getContent(); // 获取当前Tab页的文本编辑区
AutoSave(textArea);// 自动保存
autoSave(textArea);// 自动保存
String text = textArea.getText();
writer.write(text); // 写入文件内容
writer.flush();
@ -186,8 +186,8 @@ public class Controller {
if (file.exists() && file.isFile()) {
try {
MainApp.isRelevance = false;
getTXT(file);// 调用读取文件方法
updateEncodingLabel(((TextArea) tabPane.getSelectionModel().getSelectedItem().getContent()).getText()); // 更新文本编码信息
getText(file);// 调用读取文件方法
upDateEncodingLabel(((TextArea) tabPane.getSelectionModel().getSelectedItem().getContent()).getText()); // 更新文本编码信息
} catch (IOException ignored) {
// 处理异常忽略
}
@ -195,7 +195,7 @@ public class Controller {
}
// 读取文件并创建文本编辑区
public static void getTXT(File file) throws IOException {
public static void getText(File file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
StringBuilder textBuilder = new StringBuilder();
@ -206,7 +206,7 @@ public class Controller {
String text = textBuilder.toString();
TextArea textArea = new TextArea(text); // 创建新的文本编辑区
AutoSave(textArea); // 自动保存
autoSave(textArea); // 自动保存
Tab tab = new Tab(file.getName()); // 创建新的Tab页
tab.setContent(textArea);
@ -217,9 +217,9 @@ public class Controller {
updateStatusLabel(textArea);
}
// 更新文本编码标签信息
public static void updateEncodingLabel(String text) {
public static void upDateEncodingLabel(String text) {
String encoding = detectEncoding(text);
encodingLabel.setText("\t编码: " + encoding);
enCodingLabel.setText("\t编码: " + encoding);
}
// 判断编码是否有效
public static boolean isEncodingValid(String text, String encoding) {

View File

@ -3,8 +3,8 @@ package org.jcnc.jnotepad.view;
import javafx.scene.control.TextArea;
import org.jcnc.jnotepad.controller.Controller;
import static org.jcnc.jnotepad.ViewManager.*;
import static org.jcnc.jnotepad.controller.Controller.updateEncodingLabel;
import static org.jcnc.jnotepad.viewManager.*;
import static org.jcnc.jnotepad.controller.Controller.upDateEncodingLabel;
import static org.jcnc.jnotepad.controller.Controller.updateStatusLabel;
public class View {
@ -30,7 +30,7 @@ public class View {
textArea.caretPositionProperty().addListener((caretObservable, oldPosition, newPosition) -> updateStatusLabel(textArea));
// 更新编码标签
updateEncodingLabel(textArea.getText());
upDateEncodingLabel(textArea.getText());
}
});
}

View File

@ -9,9 +9,9 @@ import javafx.scene.layout.HBox;
/**
* 该类管理记事本应用程序的视图组件
*/
public class ViewManager {
public class viewManager {
public static Label encodingLabel; // 显示文本编码
public static Label enCodingLabel; // 显示文本编码
public static int tabIndex = 0;
@ -29,7 +29,7 @@ public class ViewManager {
// 状态栏
public static Label statusLabel;
private static ViewManager instance = null;
private static viewManager instance = null;
/**
* 获取ViewManager的实例如果实例不存在则创建一个新实例
@ -37,9 +37,9 @@ public class ViewManager {
* @param scene 与视图相关联的JavaFX场景
* @return ViewManager的实例
*/
public static ViewManager getInstance(Scene scene) {
public static viewManager getInstance(Scene scene) {
if (instance == null) {
instance = new ViewManager(scene);
instance = new viewManager(scene);
}
return instance;
}
@ -49,7 +49,7 @@ public class ViewManager {
*
* @param scene 与视图相关联的JavaFX场景
*/
private ViewManager(Scene scene) {
private viewManager(Scene scene) {
root = new BorderPane();
scene.setRoot(root);
}
@ -81,8 +81,8 @@ public class ViewManager {
// 创建状态栏
statusLabel = new Label("行数1 \t列数1 \t字数0 ");
encodingLabel = new Label(); // 创建新的标签以显示编码信息
HBox statusBox = new HBox(statusLabel, encodingLabel); // 使用HBox放置状态标签和编码标签
enCodingLabel = new Label(); // 创建新的标签以显示编码信息
HBox statusBox = new HBox(statusLabel, enCodingLabel); // 使用HBox放置状态标签和编码标签
root.setBottom(statusBox);
BorderPane.setMargin(statusBox, new Insets(5, 10, 5, 10));