♻️ 重构代码

This commit is contained in:
gewuyou 2023-09-02 13:19:45 +08:00
parent 71dfbb76d4
commit c18941c0ef
10 changed files with 35 additions and 111 deletions

View File

@ -4,8 +4,9 @@ import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import org.jcnc.jnotepad.app.i18n.UiResourceBundle;
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.tool.UiUtil;
import org.jcnc.jnotepad.root.center.main.center.tab.MainTabPane;
import org.jcnc.jnotepad.ui.module.LineNumberTextArea;
import org.jcnc.jnotepad.view.manager.ViewManager;
@ -35,7 +36,7 @@ public class NewFile implements EventHandler<ActionEvent> {
// 创建一个新的文本编辑区
LineNumberTextArea textArea = new LineNumberTextArea();
// TODO: refactor统一TextArea新建绑定监听器入口
ViewManager viewManager = UiUtil.getViewManager();
ViewManager viewManager = ViewManager.getInstance();
// 创建标签页
MainTab mainTab = new MainTab(
UiResourceBundle.getContent(TextConstants.NEW_FILE)
@ -44,8 +45,8 @@ public class NewFile implements EventHandler<ActionEvent> {
// 设置当前标签页与本地文件无关联
mainTab.setRelevance(false);
// 将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) {
// 获取标签页集合
MainTabPane jnotepadMainTabPane = UiUtil.getJnotepadTabPane();
MainTabPane jnotepadMainTabPane = MainTabPane.getInstance();
// 遍历标签页查找匹配的标签页
for (Tab tab : jnotepadMainTabPane.getTabs()) {
// 获取绑定的文件
@ -123,7 +123,7 @@ public class OpenFile implements EventHandler<ActionEvent> {
// 设置当前标签页关联本地文件
tab.setRelevance(true);
tab.setUserData(file);
UiUtil.getJnotepadTabPane().addNewTab(tab);
MainTabPane.getInstance().addNewTab(tab);
});
} catch (IOException ignored) {
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.constants.TextConstants;
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.UiUtil;
import org.jcnc.jnotepad.ui.dialog.factory.impl.TextFileChooserFactory;
@ -29,7 +30,7 @@ public class RenameFile implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent actionEvent) {
// 获取当前标签页
MainTab jnotepadtab = UiUtil.getJnotepadtab();
MainTab jnotepadtab = MainTabPane.getInstance().getSelected();
if (jnotepadtab == null || jnotepadtab.getText().isEmpty()) {
return;
}

View File

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

View File

@ -2,7 +2,8 @@ package org.jcnc.jnotepad.controller.event.handler.tool;
import javafx.event.ActionEvent;
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;
import javafx.scene.control.TabPane;
import org.jcnc.jnotepad.root.center.main.bottom.status.JNotepadStatusBox;
import org.jcnc.jnotepad.root.top.menu.JNotepadMenuBar;
import org.jcnc.jnotepad.root.center.main.bottom.status.StatusHorizontalBox;
import org.jcnc.jnotepad.root.top.menu.TopMenuBar;
import org.jcnc.jnotepad.tool.SingletonUtil;
/**
@ -31,10 +31,10 @@ public class MainTabPane extends TabPane {
(ov, from, to) -> {
if (to != null) {
// 更新菜单栏中与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() {
MainTab selectedTab = getSelected();
selectedTab.setAutoLine(SingletonUtil.getAppConfigController().getAutoLineConfig());
JNotepadStatusBox.getInstance().updateWhenTabSelected();
StatusHorizontalBox.getInstance().updateWhenTabSelected();
}
}

View File

@ -1,6 +1,6 @@
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;
/**
@ -23,11 +23,11 @@ public class RootTopBorderPane extends AbstractBorderPane {
/**
* 初始化 RootTopBorderPane
*
* <p>在顶部区域添加了 JNotepadMenuBar 的单例实例</p>
* <p>在顶部区域添加了 TopMenuBar 的单例实例</p>
*/
private void initRootBorderPane() {
// 在顶部区域添加菜单栏
setTopComponent(JNotepadMenuBar.getInstance());
setTopComponent(TopMenuBar.getInstance());
}
/**

View File

@ -25,9 +25,9 @@ import static org.jcnc.jnotepad.constants.TextConstants.*;
*
* @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 JNotepadMenuBar() {
private TopMenuBar() {
initMenuBar();
}
public static JNotepadMenuBar getInstance() {
public static TopMenuBar getInstance() {
return MENU_BAR;
}

View File

@ -2,13 +2,8 @@ package org.jcnc.jnotepad.tool;
import javafx.scene.image.Image;
import javafx.stage.Window;
import org.jcnc.jnotepad.LunchApp;
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;
@ -20,43 +15,14 @@ import java.util.Objects;
* @author gewuyou
*/
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 SetStage SET_STAGE = SetStage.getInstance();
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 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()
*/
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.scene.control.TextArea;
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.MainTabPane;
import org.jcnc.jnotepad.tool.LogUtil;
import org.jcnc.jnotepad.tool.SingletonUtil;
import org.jcnc.jnotepad.tool.UiUtil;
import org.slf4j.Logger;
import java.io.BufferedWriter;
@ -61,12 +62,12 @@ public class LineNumberTextArea extends BorderPane {
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) -> {
// 更新行号
updateLineNumberArea();
// 更新状态栏
UiUtil.getStatusBox().updateWordCountStatusLabel();
StatusHorizontalBox.getInstance().updateWordCountStatusLabel();
// 自动保存
save();
});
@ -76,7 +77,7 @@ public class LineNumberTextArea extends BorderPane {
* 以原文件编码格式写回文件
*/
public void save() {
MainTab tab = UiUtil.getJnotepadtab();
MainTab tab = MainTabPane.getInstance().getSelected();
if (tab == null) {
return;
}