!107 ♻️ 重构代码 重构LunchApp,将应用资源加载与初始化与启动类剥离
Merge pull request !107 from 格物方能致知/refactor-I831QL
This commit is contained in:
commit
4da14c06cc
@ -1,26 +1,9 @@
|
|||||||
package org.jcnc.jnotepad;
|
package org.jcnc.jnotepad;
|
||||||
|
|
||||||
import atlantafx.base.theme.PrimerLight;
|
|
||||||
import javafx.application.Application;
|
import javafx.application.Application;
|
||||||
import javafx.scene.Scene;
|
|
||||||
import javafx.scene.layout.Pane;
|
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
import javafx.stage.Window;
|
import org.jcnc.jnotepad.app.manager.ApplicationManager;
|
||||||
import org.jcnc.jnotepad.app.i18n.UiResourceBundle;
|
|
||||||
import org.jcnc.jnotepad.common.constants.AppConstants;
|
|
||||||
import org.jcnc.jnotepad.common.constants.TextConstants;
|
|
||||||
import org.jcnc.jnotepad.common.manager.ThreadPoolManager;
|
|
||||||
import org.jcnc.jnotepad.controller.ResourceController;
|
|
||||||
import org.jcnc.jnotepad.controller.config.PluginConfigController;
|
|
||||||
import org.jcnc.jnotepad.controller.i18n.LocalizationController;
|
|
||||||
import org.jcnc.jnotepad.controller.manager.Controller;
|
|
||||||
import org.jcnc.jnotepad.plugin.PluginManager;
|
|
||||||
import org.jcnc.jnotepad.util.UiUtil;
|
|
||||||
import org.jcnc.jnotepad.views.manager.ViewManager;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 启动程序类
|
* 启动程序类
|
||||||
@ -30,21 +13,7 @@ import java.util.concurrent.ExecutorService;
|
|||||||
* @author 许轲
|
* @author 许轲
|
||||||
*/
|
*/
|
||||||
public class LunchApp extends Application {
|
public class LunchApp extends Application {
|
||||||
|
private static final ApplicationManager APPLICATION_MANAGER = ApplicationManager.getInstance();
|
||||||
private static final Pane ROOT = new Pane();
|
|
||||||
private static final Scene SCENE;
|
|
||||||
|
|
||||||
static {
|
|
||||||
Application.setUserAgentStylesheet(new PrimerLight().getUserAgentStylesheet());
|
|
||||||
double width = AppConstants.SCREEN_WIDTH;
|
|
||||||
double length = AppConstants.SCREEN_LENGTH;
|
|
||||||
SCENE = new Scene(ROOT, width, length);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 线程池
|
|
||||||
*/
|
|
||||||
private final ExecutorService threadPool = ThreadPoolManager.getThreadPool();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 应用程序的入口点,启动 JavaFX 应用程序。
|
* 应用程序的入口点,启动 JavaFX 应用程序。
|
||||||
@ -55,56 +24,22 @@ public class LunchApp extends Application {
|
|||||||
launch(args);
|
launch(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取当前窗口。
|
|
||||||
*
|
|
||||||
* @return 当前窗口
|
|
||||||
*/
|
|
||||||
public static Window getWindow() {
|
|
||||||
return SCENE.getWindow();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(Stage primaryStage) {
|
public void start(Stage primaryStage) {
|
||||||
SCENE.getStylesheets().add(Objects.requireNonNull(getClass().getResource("/css/styles.css")).toExternalForm());
|
// 设置参数
|
||||||
// 初始化UI组件
|
APPLICATION_MANAGER.setApplication(this);
|
||||||
initUiComponents();
|
APPLICATION_MANAGER.setPrimaryStage(primaryStage);
|
||||||
UiResourceBundle.bindStringProperty(primaryStage.titleProperty(), TextConstants.TITLE);
|
// 加载应用程序资源
|
||||||
|
APPLICATION_MANAGER.loadAppResources();
|
||||||
primaryStage.setScene(SCENE);
|
// 初始化应用程序
|
||||||
primaryStage.setWidth(SCENE.getWidth());
|
APPLICATION_MANAGER.initializeApp();
|
||||||
primaryStage.setHeight(SCENE.getHeight());
|
// 初始化默认操作
|
||||||
primaryStage.getIcons().add(UiUtil.getAppIcon());
|
APPLICATION_MANAGER.initializeDefaultAction();
|
||||||
|
|
||||||
primaryStage.show();
|
primaryStage.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initUiComponents() {
|
|
||||||
|
|
||||||
// 1. 加载语言
|
|
||||||
LocalizationController.initLocal();
|
|
||||||
// 2. 加载资源
|
|
||||||
ResourceController.getInstance().loadResources();
|
|
||||||
// 3. 初始化插件
|
|
||||||
PluginManager.getInstance().initializePlugins();
|
|
||||||
// 3. 加载组件
|
|
||||||
ViewManager viewManager = ViewManager.getInstance(SCENE);
|
|
||||||
viewManager.initScreen(SCENE);
|
|
||||||
|
|
||||||
// 使用线程池加载关联文件并创建文本区域
|
|
||||||
List<String> rawParameters = getParameters().getRaw();
|
|
||||||
Controller.getInstance().openAssociatedFileAndCreateTextArea(rawParameters);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop() {
|
public void stop() {
|
||||||
PluginConfigController instance = PluginConfigController.getInstance();
|
APPLICATION_MANAGER.stopApp();
|
||||||
// 刷新插件配置文件
|
|
||||||
instance.getConfig().setPlugins(PluginManager.getInstance().getPluginInfos());
|
|
||||||
instance.writeConfig();
|
|
||||||
// 销毁插件可能申请的资源
|
|
||||||
PluginManager.getInstance().destroyPlugins();
|
|
||||||
// 关闭线程池
|
|
||||||
threadPool.shutdownNow();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,177 @@
|
|||||||
|
package org.jcnc.jnotepad.app.manager;
|
||||||
|
|
||||||
|
import atlantafx.base.theme.PrimerLight;
|
||||||
|
import javafx.application.Application;
|
||||||
|
import javafx.scene.Scene;
|
||||||
|
import javafx.scene.layout.Pane;
|
||||||
|
import javafx.stage.Stage;
|
||||||
|
import javafx.stage.Window;
|
||||||
|
import org.jcnc.jnotepad.app.i18n.UiResourceBundle;
|
||||||
|
import org.jcnc.jnotepad.common.constants.AppConstants;
|
||||||
|
import org.jcnc.jnotepad.common.constants.TextConstants;
|
||||||
|
import org.jcnc.jnotepad.common.manager.ThreadPoolManager;
|
||||||
|
import org.jcnc.jnotepad.controller.ResourceController;
|
||||||
|
import org.jcnc.jnotepad.controller.config.PluginConfigController;
|
||||||
|
import org.jcnc.jnotepad.controller.manager.Controller;
|
||||||
|
import org.jcnc.jnotepad.plugin.PluginManager;
|
||||||
|
import org.jcnc.jnotepad.util.UiUtil;
|
||||||
|
import org.jcnc.jnotepad.views.manager.ViewManager;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 应用程序管理类<br/>
|
||||||
|
* 此类负责管理应用程序的生命周期等操作
|
||||||
|
*
|
||||||
|
* @author gewuyou
|
||||||
|
*/
|
||||||
|
public class ApplicationManager {
|
||||||
|
private static final ApplicationManager INSTANCE = new ApplicationManager();
|
||||||
|
/**
|
||||||
|
* 线程池
|
||||||
|
*/
|
||||||
|
private final ExecutorService threadPool = ThreadPoolManager.getThreadPool();
|
||||||
|
private Pane root = new Pane();
|
||||||
|
private Scene scene;
|
||||||
|
private Stage primaryStage;
|
||||||
|
private Application application;
|
||||||
|
|
||||||
|
|
||||||
|
private ApplicationManager() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ApplicationManager getInstance() {
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化程序
|
||||||
|
*
|
||||||
|
* @apiNote
|
||||||
|
* @since 2023/9/20 17:26
|
||||||
|
*/
|
||||||
|
public void initializeApp() {
|
||||||
|
// 设置应用程序主题
|
||||||
|
Application.setUserAgentStylesheet(new PrimerLight().getUserAgentStylesheet());
|
||||||
|
// 初始化scene
|
||||||
|
initScene();
|
||||||
|
// 初始化插件
|
||||||
|
PluginManager.getInstance().initializePlugins();
|
||||||
|
// 初始化ui组件
|
||||||
|
initUiComponents();
|
||||||
|
// 初始化primaryStage
|
||||||
|
initPrimaryStage();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initializeDefaultAction() {
|
||||||
|
// 使用线程池加载关联文件并创建文本区域
|
||||||
|
List<String> rawParameters = application.getParameters().getRaw();
|
||||||
|
Controller.getInstance().openAssociatedFileAndCreateTextArea(rawParameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initScene() {
|
||||||
|
// 初始化scene
|
||||||
|
double width = AppConstants.SCREEN_WIDTH;
|
||||||
|
double length = AppConstants.SCREEN_LENGTH;
|
||||||
|
scene = new Scene(root, width, length);
|
||||||
|
scene.getStylesheets().add(Objects.requireNonNull(application.getClass().getResource("/css/styles.css")).toExternalForm());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initPrimaryStage() {
|
||||||
|
primaryStage.setScene(scene);
|
||||||
|
primaryStage.setWidth(scene.getWidth());
|
||||||
|
primaryStage.setHeight(scene.getHeight());
|
||||||
|
primaryStage.getIcons().add(UiUtil.getAppIcon());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加载资源
|
||||||
|
*
|
||||||
|
* @apiNote
|
||||||
|
* @since 2023/9/20 18:29
|
||||||
|
*/
|
||||||
|
public void loadAppResources() {
|
||||||
|
// 加载资源
|
||||||
|
ResourceController.getInstance().loadResources();
|
||||||
|
// 绑定快捷键
|
||||||
|
UiResourceBundle.bindStringProperty(primaryStage.titleProperty(), TextConstants.TITLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止程序
|
||||||
|
*
|
||||||
|
* @apiNote 在停止程序之前会执行此操作
|
||||||
|
*/
|
||||||
|
public void stopApp() {
|
||||||
|
PluginConfigController instance = PluginConfigController.getInstance();
|
||||||
|
// 刷新插件配置文件
|
||||||
|
instance.getConfig().setPlugins(PluginManager.getInstance().getPluginInfos());
|
||||||
|
instance.writeConfig();
|
||||||
|
// 销毁插件可能申请的资源
|
||||||
|
PluginManager.getInstance().destroyPlugins();
|
||||||
|
// 关闭线程池
|
||||||
|
threadPool.shutdownNow();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前窗口。
|
||||||
|
*
|
||||||
|
* @return 当前窗口
|
||||||
|
*/
|
||||||
|
public Window getWindow() {
|
||||||
|
return scene.getWindow();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前窗口的场景
|
||||||
|
*
|
||||||
|
* @return javafx.scene.Scene
|
||||||
|
* @since 2023/9/20 18:21
|
||||||
|
*/
|
||||||
|
public Scene getScene() {
|
||||||
|
return scene;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setScene(Scene scene) {
|
||||||
|
this.scene = scene;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加载ui组件
|
||||||
|
*
|
||||||
|
* @apiNote
|
||||||
|
* @since 2023/9/20 17:25
|
||||||
|
*/
|
||||||
|
public void initUiComponents() {
|
||||||
|
// 加载组件
|
||||||
|
ViewManager viewManager = ViewManager.getInstance(scene);
|
||||||
|
viewManager.initScreen(scene);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pane getRoot() {
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRoot(Pane root) {
|
||||||
|
this.root = root;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Application getApplication() {
|
||||||
|
return application;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setApplication(Application application) {
|
||||||
|
this.application = application;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Stage getPrimaryStage() {
|
||||||
|
return primaryStage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrimaryStage(Stage primaryStage) {
|
||||||
|
this.primaryStage = primaryStage;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,6 +1,7 @@
|
|||||||
package org.jcnc.jnotepad.controller;
|
package org.jcnc.jnotepad.controller;
|
||||||
|
|
||||||
import org.jcnc.jnotepad.controller.config.PluginConfigController;
|
import org.jcnc.jnotepad.controller.config.PluginConfigController;
|
||||||
|
import org.jcnc.jnotepad.controller.i18n.LocalizationController;
|
||||||
import org.jcnc.jnotepad.exception.AppException;
|
import org.jcnc.jnotepad.exception.AppException;
|
||||||
import org.jcnc.jnotepad.plugin.PluginLoader;
|
import org.jcnc.jnotepad.plugin.PluginLoader;
|
||||||
import org.jcnc.jnotepad.util.LogUtil;
|
import org.jcnc.jnotepad.util.LogUtil;
|
||||||
@ -29,6 +30,9 @@ public class ResourceController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void loadResources() {
|
public void loadResources() {
|
||||||
|
// 1. 加载语言
|
||||||
|
LocalizationController.initLocal();
|
||||||
|
// 2. 加载插件
|
||||||
loadPlugins();
|
loadPlugins();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -208,7 +208,7 @@ public class PluginLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
LogUtil.getLogger(this.getClass()).info("PluginDescriptor file not found");
|
logger.info("PluginDescriptor file not found");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -227,7 +227,6 @@ public class PluginLoader {
|
|||||||
URLClassLoader classLoader = new URLClassLoader(new URL[]{pluginJar.toURI().toURL()});
|
URLClassLoader classLoader = new URLClassLoader(new URL[]{pluginJar.toURI().toURL()});
|
||||||
JarFile jar = new JarFile(pluginJar)
|
JarFile jar = new JarFile(pluginJar)
|
||||||
) {
|
) {
|
||||||
logger.info("{}", pluginDescriptor.getMainClass());
|
|
||||||
// 加载插件所需的依赖类
|
// 加载插件所需的依赖类
|
||||||
Enumeration<JarEntry> entries = jar.entries();
|
Enumeration<JarEntry> entries = jar.entries();
|
||||||
while (entries.hasMoreElements()) {
|
while (entries.hasMoreElements()) {
|
||||||
@ -241,6 +240,7 @@ public class PluginLoader {
|
|||||||
}
|
}
|
||||||
pluginClass = classLoader.loadClass(pluginDescriptor.getMainClass());
|
pluginClass = classLoader.loadClass(pluginDescriptor.getMainClass());
|
||||||
}
|
}
|
||||||
|
logger.info("已加载插件:{}", pluginDescriptor.getName());
|
||||||
return pluginClass;
|
return pluginClass;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,7 +3,7 @@ package org.jcnc.jnotepad.util;
|
|||||||
import atlantafx.base.theme.Styles;
|
import atlantafx.base.theme.Styles;
|
||||||
import javafx.scene.image.Image;
|
import javafx.scene.image.Image;
|
||||||
import javafx.stage.Window;
|
import javafx.stage.Window;
|
||||||
import org.jcnc.jnotepad.LunchApp;
|
import org.jcnc.jnotepad.app.manager.ApplicationManager;
|
||||||
import org.jcnc.jnotepad.common.constants.AppConstants;
|
import org.jcnc.jnotepad.common.constants.AppConstants;
|
||||||
import org.kordamp.ikonli.javafx.FontIcon;
|
import org.kordamp.ikonli.javafx.FontIcon;
|
||||||
|
|
||||||
@ -118,7 +118,7 @@ public class UiUtil {
|
|||||||
* @apiNote LunchApp.getWindow()
|
* @apiNote LunchApp.getWindow()
|
||||||
*/
|
*/
|
||||||
public static Window getAppWindow() {
|
public static Window getAppWindow() {
|
||||||
return LunchApp.getWindow();
|
return ApplicationManager.getInstance().getWindow();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -28,7 +28,7 @@ public class RootBottomSideBarVerticalBox extends AbstractVerticalBox {
|
|||||||
return INSTANCE;
|
return INSTANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
public RootBottomSideBarVerticalBox() {
|
private RootBottomSideBarVerticalBox() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user