!33 修改项目文件结构 ⚰️ 删除无用代码 修复 BUG 当无打开文件时打开文件程序抛空指针异常问题

Merge pull request !33 from 格物方能致知/master
This commit is contained in:
Luke 2023-08-26 06:45:38 +00:00 committed by Gitee
commit 0a4fb7150d
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
14 changed files with 34 additions and 202 deletions

View File

@ -56,6 +56,7 @@
<artifactId>logback-classic</artifactId> <artifactId>logback-classic</artifactId>
<version>1.4.11</version> <version>1.4.11</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.ibm.icu</groupId> <groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId> <artifactId>icu4j</artifactId>

View File

@ -22,4 +22,5 @@ module org.jcnc.jnotepad {
exports org.jcnc.jnotepad.view.manager; exports org.jcnc.jnotepad.view.manager;
exports org.jcnc.jnotepad.constants; exports org.jcnc.jnotepad.constants;
exports org.jcnc.jnotepad.ui; exports org.jcnc.jnotepad.ui;
exports org.jcnc.jnotepad.app.init;
} }

View File

@ -1,5 +1,6 @@
package org.jcnc.jnotepad; package org.jcnc.jnotepad;
import atlantafx.base.theme.PrimerLight; import atlantafx.base.theme.PrimerLight;
import javafx.application.Application; import javafx.application.Application;
import javafx.application.Platform; import javafx.application.Platform;
@ -7,10 +8,10 @@ import javafx.scene.Scene;
import javafx.scene.image.Image; import javafx.scene.image.Image;
import javafx.scene.layout.Pane; import javafx.scene.layout.Pane;
import javafx.stage.Stage; import javafx.stage.Stage;
import org.jcnc.jnotepad.app.config.LoadJnotepadConfig;
import org.jcnc.jnotepad.app.config.LoadLanguageConfig;
import org.jcnc.jnotepad.app.config.LoadShortcutKeyConfig;
import org.jcnc.jnotepad.app.config.LocalizationConfig; import org.jcnc.jnotepad.app.config.LocalizationConfig;
import org.jcnc.jnotepad.app.init.LoadJnotepadConfig;
import org.jcnc.jnotepad.app.init.LoadLanguageConfig;
import org.jcnc.jnotepad.app.init.LoadShortcutKeyConfig;
import org.jcnc.jnotepad.constants.AppConstants; import org.jcnc.jnotepad.constants.AppConstants;
import org.jcnc.jnotepad.controller.manager.Controller; import org.jcnc.jnotepad.controller.manager.Controller;
import org.jcnc.jnotepad.ui.LineNumberTextArea; import org.jcnc.jnotepad.ui.LineNumberTextArea;
@ -24,7 +25,6 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
/** /**
* 启动程序 * 启动程序
* *
@ -53,11 +53,9 @@ public class LunchApp extends Application {
@Override @Override
public void start(Stage primaryStage) { public void start(Stage primaryStage) {
Pane root = new Pane(); Pane root = new Pane();
double width = AppConstants.SCREEN_WIDTH; double width = AppConstants.SCREEN_WIDTH;
double length = AppConstants.SCREEN_LENGTH; double length = AppConstants.SCREEN_LENGTH;
String icon = AppConstants.APP_ICON; String icon = AppConstants.APP_ICON;
scene = new Scene(root, width, length); scene = new Scene(root, width, length);
Application.setUserAgentStylesheet(new PrimerLight().getUserAgentStylesheet()); Application.setUserAgentStylesheet(new PrimerLight().getUserAgentStylesheet());
scene.getStylesheets().add(Objects.requireNonNull(getClass().getResource("/css/styles.css")).toExternalForm()); scene.getStylesheets().add(Objects.requireNonNull(getClass().getResource("/css/styles.css")).toExternalForm());

View File

@ -1,53 +0,0 @@
package org.jcnc.jnotepad.app.config;
import org.jcnc.jnotepad.app.entity.ShortcutKey;
import org.jcnc.jnotepad.app.entity.Style;
import java.util.List;
/**
* 应用程序配置类
*
* @author gewuyou 一个大转盘
* @see [相关类/方法]
*/
public class JnotepadConfig {
/**
* 快捷键列表
*/
private List<ShortcutKey> shortcutKeyList;
/**
* 样式列表 TODO
*/
private List<Style> styleList;
/**
* 单例模式
*/
private JnotepadConfig() {
}
private static final JnotepadConfig INSTANCE = new JnotepadConfig();
public static JnotepadConfig getInstance() {
return INSTANCE;
}
public List<ShortcutKey> getShortcutKeyList() {
return shortcutKeyList;
}
public void setShortcutKeyList(List<ShortcutKey> shortcutKeyList) {
this.shortcutKeyList = shortcutKeyList;
}
public List<Style> getStyleList() {
return styleList;
}
public void setStyleList(List<Style> styleList) {
this.styleList = styleList;
}
}

View File

@ -1,5 +1,6 @@
package org.jcnc.jnotepad.app.config; package org.jcnc.jnotepad.app.config;
import org.jcnc.jnotepad.LunchApp;
import org.jcnc.jnotepad.tool.LogUtil; import org.jcnc.jnotepad.tool.LogUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -11,8 +12,9 @@ import static org.jcnc.jnotepad.constants.AppConstants.APP_NAME;
import static org.jcnc.jnotepad.constants.TextConstants.*; import static org.jcnc.jnotepad.constants.TextConstants.*;
/** /**
* 本地化配置文件 * 本地化配置文件<br>
* * 注意该配置文件必须先于快捷键配置文件加载
* @see LunchApp
* @author gewuyou * @author gewuyou
*/ */
public class LocalizationConfig { public class LocalizationConfig {

View File

@ -1,35 +0,0 @@
package org.jcnc.jnotepad.app.entity;
/**
* 快捷键
*
* @author gewuyou
*/
public class ShortcutKey {
private String buttonName;
private String shortcutKeyValue;
public ShortcutKey() {
}
public ShortcutKey(String buttonName, String shortcutKeyValue) {
this.buttonName = buttonName;
this.shortcutKeyValue = shortcutKeyValue;
}
public String getButtonName() {
return buttonName;
}
public void setButtonName(String buttonName) {
this.buttonName = buttonName;
}
public String getShortcutKeyValue() {
return shortcutKeyValue;
}
public void setShortcutKeyValue(String shortcutKeyValue) {
this.shortcutKeyValue = shortcutKeyValue;
}
}

View File

@ -1,35 +0,0 @@
package org.jcnc.jnotepad.app.entity;
/**
* 样式
*
* @author gewuyou
*/
public class Style {
private String styleName;
private String styleValue;
public Style() {
}
public Style(String styleName, String styleValue) {
this.styleName = styleName;
this.styleValue = styleValue;
}
public String getStyleName() {
return styleName;
}
public void setStyleName(String styleName) {
this.styleName = styleName;
}
public String getStyleValue() {
return styleValue;
}
public void setStyleValue(String styleValue) {
this.styleValue = styleValue;
}
}

View File

@ -1,8 +1,9 @@
package org.jcnc.jnotepad.app.config; package org.jcnc.jnotepad.app.init;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.jcnc.jnotepad.app.config.LocalizationConfig;
import org.jcnc.jnotepad.exception.AppException; import org.jcnc.jnotepad.exception.AppException;
import org.jcnc.jnotepad.tool.LogUtil; import org.jcnc.jnotepad.tool.LogUtil;
import org.jcnc.jnotepad.tool.PopUpUtil; import org.jcnc.jnotepad.tool.PopUpUtil;
@ -61,6 +62,7 @@ public abstract class LoadJnotepadConfig<T> {
// 不存在则创建 // 不存在则创建
createConfig(); createConfig();
try { try {
// 创建后重新加载
loadConfig(new FileInputStream(CONFIG_NAME)); loadConfig(new FileInputStream(CONFIG_NAME));
} catch (FileNotFoundException ex) { } catch (FileNotFoundException ex) {
throw new AppException(ex.getMessage()); throw new AppException(ex.getMessage());
@ -72,24 +74,12 @@ public abstract class LoadJnotepadConfig<T> {
* 解析配置文件 * 解析配置文件
* *
* @param inputStream 输入流 * @param inputStream 输入流
* @return java.util.List<java.util.LinkedHashMap < java.lang.String, java.lang.String>> * @return T
* @since 2023/8/25 15:18 * @since 2023/8/25 15:18
*/ */
protected abstract T parseConfig(InputStream inputStream); protected abstract T parseConfig(InputStream inputStream);
private void createConfig() { private void createConfig() {
// List<ShortcutKey> shortcutKeyList = getShortcutKeys();
// JnotepadConfig.getInstance().setShortcutKeyList(shortcutKeyList);
// for (ShortcutKey shortcutKey : shortcutKeyList) {
// // 保证json的key必须和变量名一致
// MenuItem menuItem = JNotepadMenuBar.getMenuBar().getItemMap().get(shortcutKey.getButtonName());
// String shortKeyValue = shortcutKey.getShortcutKeyValue();
// if ("".equals(shortKeyValue) || Objects.isNull(menuItem)) {
// continue;
// }
// // 动态添加快捷键
// menuItem.setAccelerator(KeyCombination.keyCombination(shortKeyValue));
// }
try (BufferedWriter writer = new BufferedWriter(new FileWriter(CONFIG_NAME))) { try (BufferedWriter writer = new BufferedWriter(new FileWriter(CONFIG_NAME))) {
writer.write(JNOTEPAD_CONFIG); writer.write(JNOTEPAD_CONFIG);
} catch (IOException e) { } catch (IOException e) {
@ -98,36 +88,6 @@ public abstract class LoadJnotepadConfig<T> {
LocalizationConfig.getLocalizationConfig().initLocalizationConfig(); LocalizationConfig.getLocalizationConfig().initLocalizationConfig();
} }
// /**
// * 获取快捷键集合
// *
// * @return java.util.List<org.jcnc.jnotepad.app.entity.ShortcutKey> 快捷键集合
// * @since 2023/8/24 14:19
// */
// private static List<ShortcutKey> getShortcutKeys() {
// List<ShortcutKey> shortcutKeyList = new ArrayList<>();
// // 打开文件
// ShortcutKey shortcutKeyOfOpen = new ShortcutKey("openItem", "ctrl+o");
//
// // 新建
// ShortcutKey shortcutKeyOfNew = new ShortcutKey("newItem", "ctrl+n");
//
// // 保存
// ShortcutKey shortcutKeyOfSave = new ShortcutKey("saveItem", "ctrl+s");
//
// // 保存作为
// ShortcutKey shortcutKeyOfSaveAs = new ShortcutKey("saveAsItem", "ctrl+shift+s");
//
// // 打开配置文件
// ShortcutKey shortcutKeyOfOpenConfig = new ShortcutKey("openConfigItem", "alt+s");
//
// shortcutKeyList.add(shortcutKeyOfOpen);
// shortcutKeyList.add(shortcutKeyOfNew);
// shortcutKeyList.add(shortcutKeyOfSave);
// shortcutKeyList.add(shortcutKeyOfSaveAs);
// shortcutKeyList.add(shortcutKeyOfOpenConfig);
// return shortcutKeyList;
// }
/** /**
* 加载配置文件 * 加载配置文件

View File

@ -1,5 +1,6 @@
package org.jcnc.jnotepad.app.config; package org.jcnc.jnotepad.app.init;
import org.jcnc.jnotepad.app.config.LocalizationConfig;
import org.jcnc.jnotepad.tool.LogUtil; import org.jcnc.jnotepad.tool.LogUtil;
import org.jcnc.jnotepad.ui.menu.JNotepadMenuBar; import org.jcnc.jnotepad.ui.menu.JNotepadMenuBar;
import org.slf4j.Logger; import org.slf4j.Logger;

View File

@ -1,4 +1,4 @@
package org.jcnc.jnotepad.app.config; package org.jcnc.jnotepad.app.init;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import javafx.scene.control.MenuItem; import javafx.scene.control.MenuItem;

View File

@ -1,16 +1,10 @@
package org.jcnc.jnotepad.constants; package org.jcnc.jnotepad.constants;
/** /**
* 路径常量 * 路径常量<br>
*
* @author gewuyou * @author gewuyou
*/ */
public class PathConstants { public class PathConstants {
private PathConstants() { private PathConstants() {
} }
/**
* 配置文件路径
*/
public static final String CONFIGURATION_FILE_PATH = "../config/";
} }

View File

@ -2,8 +2,8 @@ package org.jcnc.jnotepad.controller.event.handler;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import org.jcnc.jnotepad.app.config.LoadLanguageConfig; import org.jcnc.jnotepad.app.init.LoadLanguageConfig;
import org.jcnc.jnotepad.app.config.LoadShortcutKeyConfig; import org.jcnc.jnotepad.app.init.LoadShortcutKeyConfig;
import org.jcnc.jnotepad.tool.LogUtil; import org.jcnc.jnotepad.tool.LogUtil;
import org.jcnc.jnotepad.ui.LineNumberTextArea; import org.jcnc.jnotepad.ui.LineNumberTextArea;
import org.jcnc.jnotepad.ui.tab.JNotepadTab; import org.jcnc.jnotepad.ui.tab.JNotepadTab;

View File

@ -52,14 +52,10 @@ public class LineNumberTextArea extends BorderPane {
private void initListeners() { private void initListeners() {
// 当主要文本区域的垂直滚动位置发生变化时使行号文本区域的滚动位置保持一致 // 当主要文本区域的垂直滚动位置发生变化时使行号文本区域的滚动位置保持一致
mainTextArea.scrollTopProperty().addListener((observable, oldValue, newValue) -> { mainTextArea.scrollTopProperty().addListener((observable, oldValue, newValue) -> lineNumberArea.setScrollTop(mainTextArea.getScrollTop()));
lineNumberArea.setScrollTop(mainTextArea.getScrollTop());
});
// 当行号文本区域的垂直滚动位置发生变化时使主要文本区域的滚动位置保持一致 // 当行号文本区域的垂直滚动位置发生变化时使主要文本区域的滚动位置保持一致
lineNumberArea.scrollTopProperty().addListener((observable, oldValue, newValue) -> { lineNumberArea.scrollTopProperty().addListener((observable, oldValue, newValue) -> mainTextArea.setScrollTop(lineNumberArea.getScrollTop()));
mainTextArea.setScrollTop(lineNumberArea.getScrollTop());
});
lineNumberArea.textProperty().addListener((observable, oldValue, newValue) -> updateLineNumberWidth()); lineNumberArea.textProperty().addListener((observable, oldValue, newValue) -> updateLineNumberWidth());
@ -79,14 +75,16 @@ public class LineNumberTextArea extends BorderPane {
*/ */
public void save() { public void save() {
JNotepadTab tab = JNotepadTabPane.getInstance().getSelected(); JNotepadTab tab = JNotepadTabPane.getInstance().getSelected();
File file = (File) tab.getUserData(); if (tab != null) {
String newValue = this.mainTextArea.getText(); File file = (File) tab.getUserData();
if (file != null) { String newValue = this.mainTextArea.getText();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file, tab.getCharset()))) { if (file != null) {
writer.write(newValue); try (BufferedWriter writer = new BufferedWriter(new FileWriter(file, tab.getCharset()))) {
LogUtil.getLogger(this.getClass()).info("正在自动保存---"); writer.write(newValue);
} catch (IOException ignored) { LogUtil.getLogger(this.getClass()).info("正在自动保存---");
LogUtil.getLogger(this.getClass()).info("已忽视IO异常!"); } catch (IOException ignored) {
LogUtil.getLogger(this.getClass()).info("已忽视IO异常!");
}
} }
} }
} }
@ -137,7 +135,7 @@ public class LineNumberTextArea extends BorderPane {
// 恢复之前的滚动位置 // 恢复之前的滚动位置
mainTextArea.setScrollTop(mainTextAreaScrollTop); mainTextArea.setScrollTop(mainTextAreaScrollTop);
lineNumberArea.setScrollTop(lineNumberAreaScrollTop-8); lineNumberArea.setScrollTop(lineNumberAreaScrollTop - 8);
} }
public TextArea getMainTextArea() { public TextArea getMainTextArea() {

View File

@ -1,6 +1,6 @@
package org.jcnc.jnotepad.view.init; package org.jcnc.jnotepad.view.init;
import org.jcnc.jnotepad.app.config.LoadJnotepadConfig; import org.jcnc.jnotepad.app.init.LoadJnotepadConfig;
import java.util.List; import java.util.List;