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