️ 优化:优化代码逻辑

This commit is contained in:
gewuyou 2023-08-24 09:23:30 +08:00
parent f0731aa259
commit c3408b9d3f
6 changed files with 59 additions and 35 deletions

View File

@ -11,7 +11,7 @@ public class PathConstants {
/** /**
* 快捷键配置文件路径 * 快捷键配置文件路径
* todo这里这个配置可以通过配置文件读取 * todo这里这个配置应当可以通过配置文件读取
*/ */
public static final String SHORTCUT_KEY_CONFIGURATION_FILE_PATH = "/config/shortcutKey.json"; public static final String SHORTCUT_KEY_CONFIGURATION_FILE_PATH = "/config/shortcutKey.json";
} }

View File

@ -2,6 +2,7 @@ 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.tool.LogUtil;
import static org.jcnc.jnotepad.tool.FileUtil.saveTab; import static org.jcnc.jnotepad.tool.FileUtil.saveTab;
@ -24,6 +25,7 @@ public class SaveAsFile implements EventHandler<ActionEvent> {
*/ */
@Override @Override
public void handle(ActionEvent event) { public void handle(ActionEvent event) {
LogUtil.getLogger(SaveAsFile.class).info("已调用另存为功能");
saveTab(this.getClass()); saveTab(this.getClass());
} }
} }

View File

@ -8,6 +8,7 @@ import javafx.scene.input.KeyCombination;
import org.jcnc.jnotepad.Interface.ShortcutKeyInterface; import org.jcnc.jnotepad.Interface.ShortcutKeyInterface;
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 java.io.*; import java.io.*;
import java.util.Map; import java.util.Map;
@ -19,39 +20,22 @@ import static org.jcnc.jnotepad.constants.PathConstants.SHORTCUT_KEY_CONFIGURATI
* @author 一个大转盘<br> * @author 一个大转盘<br>
*/ */
public class ShortcutKey implements ShortcutKeyInterface { public class ShortcutKey implements ShortcutKeyInterface {
Logger logger = LogUtil.getLogger(this.getClass());
/**
* 按配置创建快捷键
*
* @apiNote 此方法通过配置json文件初始化快捷键
* @since 2023/8/23 21:56
*/
@Override @Override
public void createShortcutKeyByConfig() { public void createShortcutKeyByConfig() {
String rootPath = System.getProperty("user.dir"); String jsonData = getJsonPathDataBasedOnJson();
// 构建JSON文件路径
String jsonFilePath = rootPath + SHORTCUT_KEY_CONFIGURATION_FILE_PATH;
InputStream inputStream = getClass().getResourceAsStream(SHORTCUT_KEY_CONFIGURATION_FILE_PATH);
StringBuffer jsonData = new StringBuffer();
File file = new File(jsonFilePath);
if (file.exists()) {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
jsonData.append(line);
}
} catch (IOException e) {
LogUtil.getLogger(this.getClass()).error("读取配置失败!", e);
}
} else {
// todo new InputStreamReader(inputStream) 实参 'inputStream' 可能为null
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = reader.readLine()) != null) {
jsonData.append(line);
}
} catch (IOException e) {
LogUtil.getLogger(this.getClass()).error("读取配置失败!", e);
}
}
// 转json对象 // 转json对象
GsonBuilder gsonBuilder = new GsonBuilder(); GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create(); Gson gson = gsonBuilder.create();
Map<String, String> shortcutKeyConfig = gson.fromJson(jsonData.toString(), new TypeToken<Map<String, String>>() { Map<String, String> shortcutKeyConfig = gson.fromJson(jsonData, new TypeToken<Map<String, String>>() {
}.getType()); }.getType());
for (Map.Entry<String, String> stringObjectEntry : shortcutKeyConfig.entrySet()) { for (Map.Entry<String, String> stringObjectEntry : shortcutKeyConfig.entrySet()) {
// 保证json的key必须和变量名一致 // 保证json的key必须和变量名一致
@ -60,8 +44,45 @@ public class ShortcutKey implements ShortcutKeyInterface {
if ("".equals(shortKeyValue) || Objects.isNull(menuItem)) { if ("".equals(shortKeyValue) || Objects.isNull(menuItem)) {
continue; continue;
} }
logger.info("快捷键对象:{}->已分配快捷键:{}", menuItem.getText(), shortKeyValue);
// 动态添加快捷键 // 动态添加快捷键
menuItem.setAccelerator(KeyCombination.keyCombination(shortKeyValue)); menuItem.setAccelerator(KeyCombination.keyCombination(shortKeyValue));
} }
} }
/**
* 根据json路径返回解析的Json数据
*
* @return java.lang.String Json数据
*/
private String getJsonPathDataBasedOnJson() {
String rootPath = System.getProperty("user.dir");
// 构建JSON文件路径
String jsonFilePath = rootPath + SHORTCUT_KEY_CONFIGURATION_FILE_PATH;
StringBuilder jsonData = new StringBuilder();
InputStream inputStream = getClass().getResourceAsStream(SHORTCUT_KEY_CONFIGURATION_FILE_PATH);
File file = new File(jsonFilePath);
if (file.exists()) {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
jsonData.append(line);
}
} catch (IOException e) {
logger.error("读取配置失败!", e);
}
} else {
if (inputStream != null) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = reader.readLine()) != null) {
jsonData.append(line);
}
} catch (IOException e) {
logger.error("读取配置失败!", e);
}
}
}
return jsonData.toString();
}
} }

View File

@ -16,7 +16,8 @@ public class FileUtil {
} }
/** /**
* 把一个文件中的内容读取成一个String字符串 * 把一个文件中的内容读取成一个String字符串<br>
* 注意该方法不支持多线程操作
* *
* @param jsonFile json文件 * @param jsonFile json文件
* @return String * @return String
@ -28,7 +29,7 @@ public class FileUtil {
) { ) {
int ch; int ch;
StringBuffer sb = new StringBuffer(); StringBuilder sb = new StringBuilder();
while ((ch = reader.read()) != -1) { while ((ch = reader.read()) != -1) {
sb.append((char) ch); sb.append((char) ch);
} }

View File

@ -2,7 +2,7 @@
"newItem": "ctrl+n", "newItem": "ctrl+n",
"openItem": "ctrl+o", "openItem": "ctrl+o",
"saveItem": "ctrl+s", "saveItem": "ctrl+s",
"saveAsItem": "ctrl+shift+s", "saveAsItem": "ctrl+alt+s",
"lineFeedItem": "", "lineFeedItem": "",
"addItem": "", "addItem": "",

View File

@ -3,15 +3,15 @@
<!-- scanPeriod属性设置监测配置文件修改的时间间隔默认单位为毫秒在scan为true时才生效 --> <!-- scanPeriod属性设置监测配置文件修改的时间间隔默认单位为毫秒在scan为true时才生效 -->
<!-- debug属性如果为true时会打印出logback内部的日志信息 --> <!-- debug属性如果为true时会打印出logback内部的日志信息 -->
<configuration scan="true" scanPeriod="60 seconds" debug="false"> <configuration scan="true" scanPeriod="60 seconds" debug="false">
<!-- 定义参数常量 --> <!-- 定义参数常量 -->JJ
<!-- 日志级别TRACE<DEBUG<INFO<WARN<ERROR其中常用的是DEBUG、INFO和ERROR --> <!-- 日志级别TRACE<DEBUG<INFO<WARN<ERROR其中常用的是DEBUG、INFO和ERROR -->
<property name="log.level" value="debug"/> <property name="log.level" value="debug"/>
<!-- 文件保留时间 --> <!-- 文件保留时间 -->
<property name="log.maxHistory" value="30"/> <property name="log.maxHistory" value="30"/>
<!-- 日志存储路径 --> <!-- 日志存储路径 -->
<property name="log.filePath" value="logs"/> <!--<property name="log.filePath" value="logs"/>-->
<!--打包时,请使用下方的路径--> <!--打包时,请使用下方的路径-->
<!--<property name="log.filePath" value="../logs"/>--> <property name="log.filePath" value="../logs"/>
<!-- 日志的显式格式 --> <!-- 日志的显式格式 -->
<property name="log.pattern" <property name="log.pattern"
value="时间:[%d{yyyy-MM-dd HH:mm:ss.SSS}] 线程:[%thread] 日志级别:[%-5level] 调用位置:[%logger{50} 参见:[\(%F:%L\)]] - 日志信息:[%msg]%n"> value="时间:[%d{yyyy-MM-dd HH:mm:ss.SSS}] 线程:[%thread] 日志级别:[%-5level] 调用位置:[%logger{50} 参见:[\(%F:%L\)]] - 日志信息:[%msg]%n">