重构代码
This commit is contained in:
parent
4f0b51c735
commit
d5917de0ad
@ -1,6 +1,7 @@
|
|||||||
package org.jcnc.jnotepad;
|
package org.jcnc.jnotepad;
|
||||||
|
|
||||||
import javafx.application.Application;
|
import javafx.application.Application;
|
||||||
|
import javafx.application.Platform;
|
||||||
import javafx.scene.Scene;
|
import javafx.scene.Scene;
|
||||||
import javafx.scene.control.Tab;
|
import javafx.scene.control.Tab;
|
||||||
import javafx.scene.control.TextArea;
|
import javafx.scene.control.TextArea;
|
||||||
@ -12,11 +13,11 @@ import org.jcnc.jnotepad.view.View;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
import static org.jcnc.jnotepad.ViewManager.tabPane;
|
import java.util.concurrent.Executors;
|
||||||
import static org.jcnc.jnotepad.controller.Controller.updateStatusLabel;
|
|
||||||
|
|
||||||
public class MainApp extends Application {
|
public class MainApp extends Application {
|
||||||
|
private static final ExecutorService threadPool = Executors.newCachedThreadPool();
|
||||||
public static boolean isRelevance = true;
|
public static boolean isRelevance = true;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -34,33 +35,38 @@ public class MainApp extends Application {
|
|||||||
primaryStage.setWidth(width);
|
primaryStage.setWidth(width);
|
||||||
primaryStage.setHeight(length);
|
primaryStage.setHeight(length);
|
||||||
primaryStage.setScene(scene);
|
primaryStage.setScene(scene);
|
||||||
primaryStage.getIcons().add(new Image((Objects.requireNonNull(getClass().getResource(icon))).toString()));
|
primaryStage.getIcons().add(new Image(Objects.requireNonNull(getClass().getResource(icon)).toString()));
|
||||||
primaryStage.show();
|
primaryStage.show();
|
||||||
|
|
||||||
ViewManager viewManager = ViewManager.getInstance(scene);
|
ViewManager viewManager = ViewManager.getInstance(scene);
|
||||||
|
|
||||||
viewManager.initScreen(scene);
|
viewManager.initScreen(scene);
|
||||||
|
|
||||||
// 初始化应用程序
|
|
||||||
initApp();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initApp() {
|
|
||||||
List<String> rawParameters = getParameters().getRaw();
|
|
||||||
|
|
||||||
// 打开关联文件并创建文本区域
|
|
||||||
TextArea textArea = Controller.openAssociatedFileAndCreateTextArea(rawParameters);
|
|
||||||
if (isRelevance) {
|
|
||||||
// 创建新标签页并添加到标签栏
|
|
||||||
Tab tab = new Tab("新建文件 " + ++ViewManager.tabIndex);
|
|
||||||
tab.setContent(textArea);
|
|
||||||
tabPane.getTabs().add(tab);
|
|
||||||
tabPane.getSelectionModel().select(tab);
|
|
||||||
updateStatusLabel(textArea);
|
|
||||||
}
|
|
||||||
// 初始化菜单项和标签栏
|
// 初始化菜单项和标签栏
|
||||||
View.initItem();
|
View.initItem();
|
||||||
View.initTabPane();
|
View.initTabPane();
|
||||||
|
|
||||||
|
if (isRelevance) {
|
||||||
|
// 使用线程池加载关联文件并创建文本区域
|
||||||
|
List<String> rawParameters = getParameters().getRaw();
|
||||||
|
threadPool.execute(() -> {
|
||||||
|
TextArea textArea = Controller.openAssociatedFileAndCreateTextArea(rawParameters);
|
||||||
|
Platform.runLater(() -> updateUIWithNewTextArea(textArea));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateUIWithNewTextArea(TextArea textArea) {
|
||||||
|
Tab tab = new Tab("新建文件 " + (++ViewManager.tabIndex));
|
||||||
|
tab.setContent(textArea);
|
||||||
|
ViewManager.tabPane.getTabs().add(tab);
|
||||||
|
ViewManager.tabPane.getSelectionModel().select(tab);
|
||||||
|
Controller.updateStatusLabel(textArea);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stop() {
|
||||||
|
// 关闭线程池
|
||||||
|
threadPool.shutdownNow();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@ -48,6 +48,7 @@ public class Controller {
|
|||||||
public static class NewFileEventHandler implements EventHandler<ActionEvent> {
|
public static class NewFileEventHandler implements EventHandler<ActionEvent> {
|
||||||
@Override
|
@Override
|
||||||
public void handle(ActionEvent event) {
|
public void handle(ActionEvent event) {
|
||||||
|
|
||||||
TextArea textArea = new TextArea(); // 创建新的文本编辑区
|
TextArea textArea = new TextArea(); // 创建新的文本编辑区
|
||||||
Tab tab = new Tab("新建文本 " + ++tabIndex); // 创建新的Tab页
|
Tab tab = new Tab("新建文本 " + ++tabIndex); // 创建新的Tab页
|
||||||
tab.setContent(textArea);
|
tab.setContent(textArea);
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package org.jcnc.jnotepad.view;
|
package org.jcnc.jnotepad.view;
|
||||||
|
|
||||||
import javafx.scene.control.TextArea;
|
import javafx.scene.control.TextArea;
|
||||||
|
import org.jcnc.jnotepad.ViewManager;
|
||||||
import org.jcnc.jnotepad.controller.Controller;
|
import org.jcnc.jnotepad.controller.Controller;
|
||||||
|
|
||||||
import static org.jcnc.jnotepad.ViewManager.*;
|
import static org.jcnc.jnotepad.ViewManager.*;
|
||||||
@ -11,6 +12,7 @@ public class View {
|
|||||||
|
|
||||||
public static void initItem() {
|
public static void initItem() {
|
||||||
// 初始化菜单项的事件处理器
|
// 初始化菜单项的事件处理器
|
||||||
|
ViewManager viewManager;
|
||||||
newItem.setOnAction(new Controller.NewFileEventHandler());
|
newItem.setOnAction(new Controller.NewFileEventHandler());
|
||||||
openItem.setOnAction(new Controller.OpenFileEventHandler());
|
openItem.setOnAction(new Controller.OpenFileEventHandler());
|
||||||
saveItem.setOnAction(new Controller.SaveFileEventHandler());
|
saveItem.setOnAction(new Controller.SaveFileEventHandler());
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user