重构代码结构
This commit is contained in:
parent
76dc73da84
commit
571b8dd56a
@ -5,7 +5,6 @@ package org.jcnc.jnotepad;
|
||||
*/
|
||||
|
||||
public class Constants {
|
||||
|
||||
public static final double SCREEN_WIDTH = 800; //宽度
|
||||
public static final double SCREEN_LENGTH = 600; //高度
|
||||
public static final String APP_NAME = "JNotepad"; //名字
|
||||
|
||||
@ -16,14 +16,17 @@ import java.util.Objects;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import static org.jcnc.jnotepad.view.View.initItem;
|
||||
import static org.jcnc.jnotepad.view.View.initTabPane;
|
||||
|
||||
public class LunchApp extends Application {
|
||||
private static final ExecutorService threadPool = Executors.newCachedThreadPool();
|
||||
public static boolean isRelevance = true;
|
||||
|
||||
Controller controller=new Controller();
|
||||
Controller controller = new Controller();
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) {
|
||||
View view=new View();
|
||||
Pane root = new Pane();
|
||||
|
||||
double width = Constants.SCREEN_WIDTH;
|
||||
@ -44,8 +47,8 @@ public class LunchApp extends Application {
|
||||
viewManager.initScreen(scene);
|
||||
|
||||
// 初始化菜单项和标签栏
|
||||
view.initItem();
|
||||
View.initTabPane();
|
||||
initItem();
|
||||
initTabPane();
|
||||
|
||||
if (isRelevance) {
|
||||
// 使用线程池加载关联文件并创建文本区域
|
||||
|
||||
@ -127,7 +127,7 @@ public class Controller implements ControllerInterface {
|
||||
autoSave(textArea);
|
||||
});
|
||||
} catch (IOException ignored) {
|
||||
// 适当地处理异常
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -10,7 +10,6 @@ public class LineFeedEventHandler implements EventHandler<ActionEvent> {
|
||||
public LineFeedEventHandler(TextArea textArea) {
|
||||
this.textArea = textArea;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(ActionEvent event) {
|
||||
String text = textArea.getText();
|
||||
|
||||
@ -9,11 +9,6 @@ import org.jcnc.jnotepad.ViewManager;
|
||||
import static org.jcnc.jnotepad.ViewManager.tabPane;
|
||||
|
||||
public class NewFileEventHandler implements EventHandler<ActionEvent> {
|
||||
|
||||
|
||||
public NewFileEventHandler() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(ActionEvent event) {
|
||||
Controller controller = new Controller();
|
||||
|
||||
@ -11,10 +11,10 @@ import java.io.File;
|
||||
import static org.jcnc.jnotepad.ViewManager.tabPane;
|
||||
|
||||
// 打开文件事件处理器
|
||||
public class OpenFileEventHandler implements EventHandler<ActionEvent> {
|
||||
public class OpenFileEventHandler implements EventHandler<ActionEvent> {
|
||||
@Override
|
||||
public void handle(ActionEvent event) {
|
||||
Controller controller=new Controller();
|
||||
Controller controller = new Controller();
|
||||
FileChooser fileChooser = new FileChooser();
|
||||
File file = fileChooser.showOpenDialog(null);
|
||||
if (file != null) {
|
||||
|
||||
@ -14,7 +14,7 @@ import static org.jcnc.jnotepad.ViewManager.tabPane;
|
||||
public class SaveFileEventHandler implements EventHandler<ActionEvent> {
|
||||
@Override
|
||||
public void handle(ActionEvent event) {
|
||||
Controller controller=new Controller();
|
||||
Controller controller = new Controller();
|
||||
Tab selectedTab = tabPane.getSelectionModel().getSelectedItem();
|
||||
if (selectedTab != null) {
|
||||
File file = (File) selectedTab.getUserData();
|
||||
|
||||
@ -1,47 +1,48 @@
|
||||
package org.jcnc.jnotepad.tool;
|
||||
|
||||
import javafx.scene.control.TextArea;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class EncodingDetector {
|
||||
|
||||
public static String detectEncoding(TextArea textArea) {
|
||||
String text = textArea.getText();
|
||||
|
||||
return detectEncoding(text);
|
||||
}
|
||||
public static String detectEncoding(TextArea textArea) {
|
||||
String text = textArea.getText();
|
||||
|
||||
public static String detectEncoding(String text) {
|
||||
// 尝试常见的编码
|
||||
for (Charset charset : commonCharsets()) {
|
||||
if (isValidEncoding(text, charset)) {
|
||||
System.out.println(isValidEncoding(text, charset));
|
||||
return charset.name();
|
||||
}
|
||||
return detectEncoding(text);
|
||||
}
|
||||
|
||||
return "UNKNOWN";
|
||||
}
|
||||
|
||||
private static Charset[] commonCharsets() {
|
||||
return new Charset[] {
|
||||
StandardCharsets.UTF_8,
|
||||
StandardCharsets.UTF_16,
|
||||
StandardCharsets.UTF_16LE,
|
||||
StandardCharsets.UTF_16BE,
|
||||
StandardCharsets.ISO_8859_1
|
||||
};
|
||||
}
|
||||
public static String detectEncoding(String text) {
|
||||
// 尝试常见的编码
|
||||
for (Charset charset : commonCharsets()) {
|
||||
if (isValidEncoding(text, charset)) {
|
||||
System.out.println(isValidEncoding(text, charset));
|
||||
return charset.name();
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isValidEncoding(String text, Charset encoding) {
|
||||
// 尝试使用指定编码解码
|
||||
byte[] bytes = text.getBytes(encoding);
|
||||
String decoded = new String(bytes, encoding);
|
||||
return "UNKNOWN";
|
||||
}
|
||||
|
||||
private static Charset[] commonCharsets() {
|
||||
return new Charset[]{
|
||||
StandardCharsets.UTF_8,
|
||||
StandardCharsets.UTF_16,
|
||||
StandardCharsets.UTF_16LE,
|
||||
StandardCharsets.UTF_16BE,
|
||||
StandardCharsets.ISO_8859_1
|
||||
};
|
||||
}
|
||||
|
||||
private static boolean isValidEncoding(String text, Charset encoding) {
|
||||
// 尝试使用指定编码解码
|
||||
byte[] bytes = text.getBytes(encoding);
|
||||
String decoded = new String(bytes, encoding);
|
||||
|
||||
|
||||
// 解码后的文本相同表示编码有效
|
||||
return text.equals(decoded);
|
||||
}
|
||||
// 解码后的文本相同表示编码有效
|
||||
return text.equals(decoded);
|
||||
}
|
||||
|
||||
}
|
||||
@ -19,7 +19,7 @@ public class View {
|
||||
}
|
||||
|
||||
public static void initTabPane() {
|
||||
Controller controller =new Controller();
|
||||
Controller controller = new Controller();
|
||||
|
||||
tabPane.getSelectionModel().selectedItemProperty().addListener((observable, oldTab, newTab) -> {
|
||||
if (newTab != null) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user