!67 ♻️ 重构代码 移除Ui单例于Ui工具类

Merge pull request !67 from 格物方能致知/develop
This commit is contained in:
格物方能致知 2023-09-02 05:21:10 +00:00 committed by Gitee
commit 6e012e3855
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
10 changed files with 35 additions and 111 deletions

View File

@ -4,8 +4,9 @@ import javafx.event.ActionEvent;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import org.jcnc.jnotepad.app.i18n.UiResourceBundle; import org.jcnc.jnotepad.app.i18n.UiResourceBundle;
import org.jcnc.jnotepad.constants.TextConstants; import org.jcnc.jnotepad.constants.TextConstants;
import org.jcnc.jnotepad.root.center.main.bottom.status.StatusHorizontalBox;
import org.jcnc.jnotepad.root.center.main.center.tab.MainTab; import org.jcnc.jnotepad.root.center.main.center.tab.MainTab;
import org.jcnc.jnotepad.tool.UiUtil; import org.jcnc.jnotepad.root.center.main.center.tab.MainTabPane;
import org.jcnc.jnotepad.ui.module.LineNumberTextArea; import org.jcnc.jnotepad.ui.module.LineNumberTextArea;
import org.jcnc.jnotepad.view.manager.ViewManager; import org.jcnc.jnotepad.view.manager.ViewManager;
@ -35,7 +36,7 @@ public class NewFile implements EventHandler<ActionEvent> {
// 创建一个新的文本编辑区 // 创建一个新的文本编辑区
LineNumberTextArea textArea = new LineNumberTextArea(); LineNumberTextArea textArea = new LineNumberTextArea();
// TODO: refactor统一TextArea新建绑定监听器入口 // TODO: refactor统一TextArea新建绑定监听器入口
ViewManager viewManager = UiUtil.getViewManager(); ViewManager viewManager = ViewManager.getInstance();
// 创建标签页 // 创建标签页
MainTab mainTab = new MainTab( MainTab mainTab = new MainTab(
UiResourceBundle.getContent(TextConstants.NEW_FILE) UiResourceBundle.getContent(TextConstants.NEW_FILE)
@ -44,8 +45,8 @@ public class NewFile implements EventHandler<ActionEvent> {
// 设置当前标签页与本地文件无关联 // 设置当前标签页与本地文件无关联
mainTab.setRelevance(false); mainTab.setRelevance(false);
// 将Tab页添加到TabPane中 // 将Tab页添加到TabPane中
UiUtil.getJnotepadTabPane().addNewTab(mainTab); MainTabPane.getInstance().addNewTab(mainTab);
// 更新编码信息 // 更新编码信息
UiUtil.getStatusBox().updateEncodingLabel(); StatusHorizontalBox.getInstance().updateEncodingLabel();
} }
} }

View File

@ -83,7 +83,7 @@ public class OpenFile implements EventHandler<ActionEvent> {
*/ */
public void openFile(File file) { public void openFile(File file) {
// 获取标签页集合 // 获取标签页集合
MainTabPane jnotepadMainTabPane = UiUtil.getJnotepadTabPane(); MainTabPane jnotepadMainTabPane = MainTabPane.getInstance();
// 遍历标签页查找匹配的标签页 // 遍历标签页查找匹配的标签页
for (Tab tab : jnotepadMainTabPane.getTabs()) { for (Tab tab : jnotepadMainTabPane.getTabs()) {
// 获取绑定的文件 // 获取绑定的文件
@ -123,7 +123,7 @@ public class OpenFile implements EventHandler<ActionEvent> {
// 设置当前标签页关联本地文件 // 设置当前标签页关联本地文件
tab.setRelevance(true); tab.setRelevance(true);
tab.setUserData(file); tab.setUserData(file);
UiUtil.getJnotepadTabPane().addNewTab(tab); MainTabPane.getInstance().addNewTab(tab);
}); });
} catch (IOException ignored) { } catch (IOException ignored) {
LogUtil.getLogger(this.getClass()).info("已忽视IO异常!"); LogUtil.getLogger(this.getClass()).info("已忽视IO异常!");

View File

@ -8,6 +8,7 @@ import javafx.stage.FileChooser;
import org.jcnc.jnotepad.app.i18n.UiResourceBundle; import org.jcnc.jnotepad.app.i18n.UiResourceBundle;
import org.jcnc.jnotepad.constants.TextConstants; import org.jcnc.jnotepad.constants.TextConstants;
import org.jcnc.jnotepad.root.center.main.center.tab.MainTab; import org.jcnc.jnotepad.root.center.main.center.tab.MainTab;
import org.jcnc.jnotepad.root.center.main.center.tab.MainTabPane;
import org.jcnc.jnotepad.tool.LogUtil; import org.jcnc.jnotepad.tool.LogUtil;
import org.jcnc.jnotepad.tool.UiUtil; import org.jcnc.jnotepad.tool.UiUtil;
import org.jcnc.jnotepad.ui.dialog.factory.impl.TextFileChooserFactory; import org.jcnc.jnotepad.ui.dialog.factory.impl.TextFileChooserFactory;
@ -29,7 +30,7 @@ public class RenameFile implements EventHandler<ActionEvent> {
@Override @Override
public void handle(ActionEvent actionEvent) { public void handle(ActionEvent actionEvent) {
// 获取当前标签页 // 获取当前标签页
MainTab jnotepadtab = UiUtil.getJnotepadtab(); MainTab jnotepadtab = MainTabPane.getInstance().getSelected();
if (jnotepadtab == null || jnotepadtab.getText().isEmpty()) { if (jnotepadtab == null || jnotepadtab.getText().isEmpty()) {
return; return;
} }

View File

@ -7,6 +7,8 @@ import org.jcnc.jnotepad.app.i18n.UiResourceBundle;
import org.jcnc.jnotepad.constants.TextConstants; import org.jcnc.jnotepad.constants.TextConstants;
import org.jcnc.jnotepad.controller.i18n.LocalizationController; import org.jcnc.jnotepad.controller.i18n.LocalizationController;
import org.jcnc.jnotepad.root.center.main.center.tab.MainTab; import org.jcnc.jnotepad.root.center.main.center.tab.MainTab;
import org.jcnc.jnotepad.root.center.main.center.tab.MainTabPane;
import org.jcnc.jnotepad.root.top.menu.TopMenuBar;
import org.jcnc.jnotepad.tool.LogUtil; import org.jcnc.jnotepad.tool.LogUtil;
import org.jcnc.jnotepad.tool.SingletonUtil; import org.jcnc.jnotepad.tool.SingletonUtil;
import org.jcnc.jnotepad.tool.UiUtil; import org.jcnc.jnotepad.tool.UiUtil;
@ -38,7 +40,7 @@ public class SaveFile implements EventHandler<ActionEvent> {
@Override @Override
public void handle(ActionEvent actionEvent) { public void handle(ActionEvent actionEvent) {
// 获取当前tab页 // 获取当前tab页
MainTab selectedTab = UiUtil.getJnotepadtab(); MainTab selectedTab = MainTabPane.getInstance().getSelected();
if (selectedTab == null) { if (selectedTab == null) {
return; return;
} }
@ -54,7 +56,7 @@ public class SaveFile implements EventHandler<ActionEvent> {
if (CONFIG_NAME.equals(selectedTab.getText())) { if (CONFIG_NAME.equals(selectedTab.getText())) {
// 重新加载语言包和快捷键 // 重新加载语言包和快捷键
SingletonUtil.getAppConfigController().loadConfig(); SingletonUtil.getAppConfigController().loadConfig();
UiUtil.getMenuBar().initShortcutKeys(); TopMenuBar.getInstance().initShortcutKeys();
LocalizationController.initLocal(); LocalizationController.initLocal();
logger.info("已刷新语言包!"); logger.info("已刷新语言包!");
logger.info("已刷新快捷键!"); logger.info("已刷新快捷键!");
@ -70,7 +72,7 @@ public class SaveFile implements EventHandler<ActionEvent> {
* @see LogUtil * @see LogUtil
*/ */
protected void saveTab(Class<?> currentClass) { protected void saveTab(Class<?> currentClass) {
MainTab selectedTab = UiUtil.getJnotepadtab(); MainTab selectedTab = MainTabPane.getInstance().getSelected();
if (selectedTab == null) { if (selectedTab == null) {
return; return;
} }

View File

@ -2,7 +2,8 @@ package org.jcnc.jnotepad.controller.event.handler.tool;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import org.jcnc.jnotepad.ui.setStage.SetStage; import org.jcnc.jnotepad.ui.setstage.SetStage;
/** /**
* 设置按钮事件的事件处理程序 * 设置按钮事件的事件处理程序

View File

@ -1,8 +1,8 @@
package org.jcnc.jnotepad.root.center.main.center.tab; package org.jcnc.jnotepad.root.center.main.center.tab;
import javafx.scene.control.TabPane; import javafx.scene.control.TabPane;
import org.jcnc.jnotepad.root.center.main.bottom.status.JNotepadStatusBox; import org.jcnc.jnotepad.root.center.main.bottom.status.StatusHorizontalBox;
import org.jcnc.jnotepad.root.top.menu.JNotepadMenuBar; import org.jcnc.jnotepad.root.top.menu.TopMenuBar;
import org.jcnc.jnotepad.tool.SingletonUtil; import org.jcnc.jnotepad.tool.SingletonUtil;
/** /**
@ -31,10 +31,10 @@ public class MainTabPane extends TabPane {
(ov, from, to) -> { (ov, from, to) -> {
if (to != null) { if (to != null) {
// 更新菜单栏中与tab相关设置 // 更新菜单栏中与tab相关设置
JNotepadMenuBar.getInstance().updateMenuStatusBySelectedTab(); TopMenuBar.getInstance().updateMenuStatusBySelectedTab();
} }
// 更新状态标签 // 更新状态标签
JNotepadStatusBox.getInstance().updateWhenTabSelected(); StatusHorizontalBox.getInstance().updateWhenTabSelected();
} }
); );
} }
@ -72,6 +72,6 @@ public class MainTabPane extends TabPane {
public void fireTabSelected() { public void fireTabSelected() {
MainTab selectedTab = getSelected(); MainTab selectedTab = getSelected();
selectedTab.setAutoLine(SingletonUtil.getAppConfigController().getAutoLineConfig()); selectedTab.setAutoLine(SingletonUtil.getAppConfigController().getAutoLineConfig());
JNotepadStatusBox.getInstance().updateWhenTabSelected(); StatusHorizontalBox.getInstance().updateWhenTabSelected();
} }
} }

View File

@ -1,6 +1,6 @@
package org.jcnc.jnotepad.root.top; package org.jcnc.jnotepad.root.top;
import org.jcnc.jnotepad.root.top.menu.JNotepadMenuBar; import org.jcnc.jnotepad.root.top.menu.TopMenuBar;
import org.jcnc.jnotepad.ui.module.AbstractBorderPane; import org.jcnc.jnotepad.ui.module.AbstractBorderPane;
/** /**
@ -23,11 +23,11 @@ public class RootTopBorderPane extends AbstractBorderPane {
/** /**
* 初始化 RootTopBorderPane * 初始化 RootTopBorderPane
* *
* <p>在顶部区域添加了 JNotepadMenuBar 的单例实例</p> * <p>在顶部区域添加了 TopMenuBar 的单例实例</p>
*/ */
private void initRootBorderPane() { private void initRootBorderPane() {
// 在顶部区域添加菜单栏 // 在顶部区域添加菜单栏
setTopComponent(JNotepadMenuBar.getInstance()); setTopComponent(TopMenuBar.getInstance());
} }
/** /**

View File

@ -25,9 +25,9 @@ import static org.jcnc.jnotepad.constants.TextConstants.*;
* *
* @author songdragon * @author songdragon
*/ */
public class JNotepadMenuBar extends MenuBar { public class TopMenuBar extends MenuBar {
private static final JNotepadMenuBar MENU_BAR = new JNotepadMenuBar(); private static final TopMenuBar MENU_BAR = new TopMenuBar();
/** /**
* 按钮集合 * 按钮集合
*/ */
@ -113,11 +113,11 @@ public class JNotepadMenuBar extends MenuBar {
*/ */
private RadioMenuItem englishItem; private RadioMenuItem englishItem;
private JNotepadMenuBar() { private TopMenuBar() {
initMenuBar(); initMenuBar();
} }
public static JNotepadMenuBar getInstance() { public static TopMenuBar getInstance() {
return MENU_BAR; return MENU_BAR;
} }

View File

@ -2,13 +2,8 @@ package org.jcnc.jnotepad.tool;
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.constants.AppConstants; import org.jcnc.jnotepad.constants.AppConstants;
import org.jcnc.jnotepad.root.center.main.bottom.status.StatusHorizontalBox;
import org.jcnc.jnotepad.root.center.main.center.tab.MainTab;
import org.jcnc.jnotepad.root.center.main.center.tab.MainTabPane;
import org.jcnc.jnotepad.root.top.menu.JNotepadMenuBar;
import org.jcnc.jnotepad.ui.setstage.SetStage;
import org.jcnc.jnotepad.view.manager.ViewManager;
import java.util.Objects; import java.util.Objects;
@ -20,43 +15,14 @@ import java.util.Objects;
* @author gewuyou * @author gewuyou
*/ */
public class UiUtil { public class UiUtil {
/**
* 标签页布局组件
*/
private static final MainTabPane TAB_PANE = MainTabPane.getInstance();
/**
* 视图管理组件
*/
private static final ViewManager VIEW_MANAGER = ViewManager.getInstance();
/**
* 状态栏组件
*/
private static final StatusHorizontalBox STATUS_BOX = StatusHorizontalBox.getInstance();
/**
* 菜单栏组件
*/
private static final JNotepadMenuBar MENU_BAR = JNotepadMenuBar.getInstance();
/** /**
* 应用程序图标 * 应用程序图标
*/ */
private static final Image ICON = new Image(Objects.requireNonNull(UiUtil.class.getResource(AppConstants.APP_ICON)).toString()); private static final Image ICON = new Image(Objects.requireNonNull(UiUtil.class.getResource(AppConstants.APP_ICON)).toString());
/**
* 设置窗口
*/
private static final SetStage SET_STAGE = SetStage.getInstance();
private UiUtil() { private UiUtil() {
} }
/**
* 获取设置窗口
*
* @return org.jcnc.jnotepad.ui.setStage.SetStage 设置窗口对象
*/
public static SetStage getSetStage() {
return SET_STAGE;
}
/** /**
* 获取应用程序图标 * 获取应用程序图标
* *
@ -66,28 +32,6 @@ public class UiUtil {
return ICON; return ICON;
} }
/**
* 获取标签页布局组件
*
* @return org.jcnc.jnotepad.root.center.main.center.tab.JNotepadTabPane 标签页布局组件对象
* @apiNote JNotepadTabPane.getInstance()
* @see MainTabPane
*/
public static MainTabPane getJnotepadTabPane() {
return TAB_PANE;
}
/**
* 获取标签页组件
*
* @return org.jcnc.jnotepad.root.center.main.center.tab.JNotepadTab 标签页组件对象
* @apiNote JNotepadTabPane.getInstance().getSelected()<br>获取当前选中的标签页
* @see MainTabPane
*/
public static MainTab getJnotepadtab() {
return TAB_PANE.getSelected();
}
/** /**
* 获取应用窗口 * 获取应用窗口
* *
@ -95,34 +39,8 @@ public class UiUtil {
* @apiNote JNotepadTabPane.getInstance().getSelected().getTabPane().getScene().getWindow() * @apiNote JNotepadTabPane.getInstance().getSelected().getTabPane().getScene().getWindow()
*/ */
public static Window getAppWindow() { public static Window getAppWindow() {
return TAB_PANE.getSelected().getTabPane().getScene().getWindow(); return LunchApp.getWindow();
} }
/**
* 获取视图管理组件
*
* @return org.jcnc.jnotepad.view.manager.ViewManager 视图管理组件对象
* @apiNote ViewManager.getInstance()
*/
public static ViewManager getViewManager() {
return VIEW_MANAGER;
}
/**
* 获取状态栏组件
*
* @return org.jcnc.jnotepad.root.center.main.bottom.status.JNotepadStatusBox 状态栏组件对象
*/
public static StatusHorizontalBox getStatusBox() {
return STATUS_BOX;
}
/**
* 获取菜单栏组件
*
* @return org.jcnc.jnotepad.root.top.menu.JNotepadMenuBar 菜单栏组件对象
*/
public static JNotepadMenuBar getMenuBar() {
return MENU_BAR;
}
} }

View File

@ -3,10 +3,11 @@ package org.jcnc.jnotepad.ui.module;
import javafx.beans.property.StringProperty; import javafx.beans.property.StringProperty;
import javafx.scene.control.TextArea; import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
import org.jcnc.jnotepad.root.center.main.bottom.status.StatusHorizontalBox;
import org.jcnc.jnotepad.root.center.main.center.tab.MainTab; import org.jcnc.jnotepad.root.center.main.center.tab.MainTab;
import org.jcnc.jnotepad.root.center.main.center.tab.MainTabPane;
import org.jcnc.jnotepad.tool.LogUtil; import org.jcnc.jnotepad.tool.LogUtil;
import org.jcnc.jnotepad.tool.SingletonUtil; import org.jcnc.jnotepad.tool.SingletonUtil;
import org.jcnc.jnotepad.tool.UiUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import java.io.BufferedWriter; import java.io.BufferedWriter;
@ -61,12 +62,12 @@ public class LineNumberTextArea extends BorderPane {
lineNumberArea.textProperty().addListener((observable, oldValue, newValue) -> updateLineNumberWidth()); lineNumberArea.textProperty().addListener((observable, oldValue, newValue) -> updateLineNumberWidth());
this.mainTextArea.caretPositionProperty().addListener((caretObservable, oldPosition, newPosition) -> UiUtil.getStatusBox().updateWordCountStatusLabel()); this.mainTextArea.caretPositionProperty().addListener((caretObservable, oldPosition, newPosition) -> StatusHorizontalBox.getInstance().updateWordCountStatusLabel());
this.textProperty().addListener((observable, oldValue, newValue) -> { this.textProperty().addListener((observable, oldValue, newValue) -> {
// 更新行号 // 更新行号
updateLineNumberArea(); updateLineNumberArea();
// 更新状态栏 // 更新状态栏
UiUtil.getStatusBox().updateWordCountStatusLabel(); StatusHorizontalBox.getInstance().updateWordCountStatusLabel();
// 自动保存 // 自动保存
save(); save();
}); });
@ -76,7 +77,7 @@ public class LineNumberTextArea extends BorderPane {
* 以原文件编码格式写回文件 * 以原文件编码格式写回文件
*/ */
public void save() { public void save() {
MainTab tab = UiUtil.getJnotepadtab(); MainTab tab = MainTabPane.getInstance().getSelected();
if (tab == null) { if (tab == null) {
return; return;
} }