🐛 修复 BUG 当没有配置文件打开软件报错问题,解决合并冲突,✏️ 修正错别字
This commit is contained in:
parent
631c7b82f2
commit
1bd9e17b60
@ -1,17 +1,14 @@
|
|||||||
package org.jcnc.jnotepad.app.config;
|
package org.jcnc.jnotepad.app.config;
|
||||||
|
|
||||||
import javafx.scene.control.MenuItem;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import javafx.scene.input.KeyCombination;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import org.jcnc.jnotepad.app.entity.ShortcutKey;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
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;
|
||||||
import org.jcnc.jnotepad.ui.menu.JNotepadMenuBar;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
import static org.jcnc.jnotepad.constants.AppConstants.CONFIG_NAME;
|
import static org.jcnc.jnotepad.constants.AppConstants.CONFIG_NAME;
|
||||||
import static org.jcnc.jnotepad.constants.TextConstants.JNOTEPAD_CONFIG;
|
import static org.jcnc.jnotepad.constants.TextConstants.JNOTEPAD_CONFIG;
|
||||||
@ -63,6 +60,11 @@ public abstract class LoadJnotepadConfig<T> {
|
|||||||
logger.info("未检测到配置文件!");
|
logger.info("未检测到配置文件!");
|
||||||
// 不存在则创建
|
// 不存在则创建
|
||||||
createConfig();
|
createConfig();
|
||||||
|
try {
|
||||||
|
loadConfig(new FileInputStream(CONFIG_NAME));
|
||||||
|
} catch (FileNotFoundException ex) {
|
||||||
|
throw new AppException(ex.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,55 +78,56 @@ public abstract class LoadJnotepadConfig<T> {
|
|||||||
protected abstract T parseConfig(InputStream inputStream);
|
protected abstract T parseConfig(InputStream inputStream);
|
||||||
|
|
||||||
private void createConfig() {
|
private void createConfig() {
|
||||||
List<ShortcutKey> shortcutKeyList = getShortcutKeys();
|
// List<ShortcutKey> shortcutKeyList = getShortcutKeys();
|
||||||
JnotepadConfig.getInstance().setShortcutKeyList(shortcutKeyList);
|
// JnotepadConfig.getInstance().setShortcutKeyList(shortcutKeyList);
|
||||||
for (ShortcutKey shortcutKey : shortcutKeyList) {
|
// for (ShortcutKey shortcutKey : shortcutKeyList) {
|
||||||
// 保证json的key必须和变量名一致
|
// // 保证json的key必须和变量名一致
|
||||||
MenuItem menuItem = JNotepadMenuBar.getMenuBar().getItemMap().get(shortcutKey.getButtonName());
|
// MenuItem menuItem = JNotepadMenuBar.getMenuBar().getItemMap().get(shortcutKey.getButtonName());
|
||||||
String shortKeyValue = shortcutKey.getShortcutKeyValue();
|
// String shortKeyValue = shortcutKey.getShortcutKeyValue();
|
||||||
if ("".equals(shortKeyValue) || Objects.isNull(menuItem)) {
|
// if ("".equals(shortKeyValue) || Objects.isNull(menuItem)) {
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
// 动态添加快捷键
|
// // 动态添加快捷键
|
||||||
menuItem.setAccelerator(KeyCombination.keyCombination(shortKeyValue));
|
// 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) {
|
||||||
PopUpUtil.errorAlert("错误", "读写错误", "配置文件读写错误!");
|
PopUpUtil.errorAlert("错误", "读写错误", "配置文件读写错误!");
|
||||||
}
|
}
|
||||||
|
LocalizationConfig.getLocalizationConfig().initLocalizationConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// /**
|
||||||
* 获取快捷键集合
|
// * 获取快捷键集合
|
||||||
*
|
// *
|
||||||
* @return java.util.List<org.jcnc.jnotepad.app.entity.ShortcutKey> 快捷键集合
|
// * @return java.util.List<org.jcnc.jnotepad.app.entity.ShortcutKey> 快捷键集合
|
||||||
* @since 2023/8/24 14:19
|
// * @since 2023/8/24 14:19
|
||||||
*/
|
// */
|
||||||
private static List<ShortcutKey> getShortcutKeys() {
|
// private static List<ShortcutKey> getShortcutKeys() {
|
||||||
List<ShortcutKey> shortcutKeyList = new ArrayList<>();
|
// List<ShortcutKey> shortcutKeyList = new ArrayList<>();
|
||||||
// 打开文件
|
// // 打开文件
|
||||||
ShortcutKey shortcutKeyOfOpen = new ShortcutKey("openItem", "ctrl+o");
|
// ShortcutKey shortcutKeyOfOpen = new ShortcutKey("openItem", "ctrl+o");
|
||||||
|
//
|
||||||
// 新建
|
// // 新建
|
||||||
ShortcutKey shortcutKeyOfNew = new ShortcutKey("newItem", "ctrl+n");
|
// ShortcutKey shortcutKeyOfNew = new ShortcutKey("newItem", "ctrl+n");
|
||||||
|
//
|
||||||
// 保存
|
// // 保存
|
||||||
ShortcutKey shortcutKeyOfSave = new ShortcutKey("saveItem", "ctrl+s");
|
// ShortcutKey shortcutKeyOfSave = new ShortcutKey("saveItem", "ctrl+s");
|
||||||
|
//
|
||||||
// 保存作为
|
// // 保存作为
|
||||||
ShortcutKey shortcutKeyOfSaveAs = new ShortcutKey("saveAsItem", "ctrl+shift+s");
|
// ShortcutKey shortcutKeyOfSaveAs = new ShortcutKey("saveAsItem", "ctrl+shift+s");
|
||||||
|
//
|
||||||
// 打开配置文件
|
// // 打开配置文件
|
||||||
ShortcutKey shortcutKeyOfOpenConfig = new ShortcutKey("openConfigItem", "alt+s");
|
// ShortcutKey shortcutKeyOfOpenConfig = new ShortcutKey("openConfigItem", "alt+s");
|
||||||
|
//
|
||||||
shortcutKeyList.add(shortcutKeyOfOpen);
|
// shortcutKeyList.add(shortcutKeyOfOpen);
|
||||||
shortcutKeyList.add(shortcutKeyOfNew);
|
// shortcutKeyList.add(shortcutKeyOfNew);
|
||||||
shortcutKeyList.add(shortcutKeyOfSave);
|
// shortcutKeyList.add(shortcutKeyOfSave);
|
||||||
shortcutKeyList.add(shortcutKeyOfSaveAs);
|
// shortcutKeyList.add(shortcutKeyOfSaveAs);
|
||||||
shortcutKeyList.add(shortcutKeyOfOpenConfig);
|
// shortcutKeyList.add(shortcutKeyOfOpenConfig);
|
||||||
return shortcutKeyList;
|
// return shortcutKeyList;
|
||||||
}
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 加载配置文件
|
* 加载配置文件
|
||||||
|
|||||||
@ -7,9 +7,8 @@ import java.io.*;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import static org.jcnc.jnotepad.constants.AppConstants.CH_LANGUAGE_PACK_NAME;
|
import static org.jcnc.jnotepad.constants.AppConstants.APP_NAME;
|
||||||
import static org.jcnc.jnotepad.constants.AppConstants.EN_LANGUAGE_PACK_NAME;
|
import static org.jcnc.jnotepad.constants.TextConstants.*;
|
||||||
import static org.jcnc.jnotepad.constants.TextConstants.TEXT_WRAP;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 本地化配置文件
|
* 本地化配置文件
|
||||||
@ -42,33 +41,34 @@ public class LocalizationConfig {
|
|||||||
InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
|
InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
|
||||||
properties.load(reader);
|
properties.load(reader);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
logger.info("未检测到本地化语言包!");
|
||||||
// 如果读取出错,则调用初始化方法
|
// 如果读取出错,则调用初始化方法
|
||||||
initializePropertiesFile();
|
initializePropertiesFile();
|
||||||
}
|
}
|
||||||
logger.info("初始化本地化语言包成功!");
|
logger.info("初始化本地化语言包成功!");
|
||||||
title = properties.getProperty("title", "JNotepad");
|
title = properties.getProperty(TITLE, APP_NAME);
|
||||||
sava = properties.getProperty("SAVA");
|
sava = properties.getProperty(SAVE);
|
||||||
file = properties.getProperty("FILE");
|
file = properties.getProperty(FILE);
|
||||||
newly = properties.getProperty("NEW");
|
newly = properties.getProperty(NEW);
|
||||||
open = properties.getProperty("OPEN");
|
open = properties.getProperty(OPEN);
|
||||||
savaAs = properties.getProperty("SAVA_AS");
|
savaAs = properties.getProperty(SAVE_AS);
|
||||||
set = properties.getProperty("SET");
|
set = properties.getProperty(SET);
|
||||||
wordWrap = properties.getProperty("WORD_WRAP");
|
wordWrap = properties.getProperty(WORD_WRAP);
|
||||||
plugin = properties.getProperty("PLUGIN");
|
plugin = properties.getProperty(PLUGIN);
|
||||||
addPlugin = properties.getProperty("ADD_PLUGIN");
|
addPlugin = properties.getProperty(ADD_PLUGIN);
|
||||||
statistics = properties.getProperty("STATISTICS");
|
statistics = properties.getProperty(STATISTICS);
|
||||||
openConfigurationFile = properties.getProperty("OPEN_CONFIGURATION_FILE");
|
openConfigurationFile = properties.getProperty(OPEN_CONFIGURATION_FILE);
|
||||||
top = properties.getProperty("TOP");
|
top = properties.getProperty(TOP);
|
||||||
language = properties.getProperty("LANGUAGE");
|
language = properties.getProperty(LANGUAGE);
|
||||||
chinese = properties.getProperty("CHINESE");
|
chinese = properties.getProperty(UPPER_CHINESE);
|
||||||
english = properties.getProperty("ENGLISH");
|
english = properties.getProperty(UPPER_ENGLISH);
|
||||||
textWrap = properties.getProperty(TEXT_WRAP, "true");
|
textWrap = properties.getProperty(TEXT_WRAP, "true");
|
||||||
newFile = properties.getProperty("NEW_FILE");
|
newFile = properties.getProperty(NEW_FILE);
|
||||||
unknown = properties.getProperty("UNKNOWN");
|
unknown = properties.getProperty(UNKNOWN);
|
||||||
row = properties.getProperty("ROW");
|
row = properties.getProperty(ROW);
|
||||||
column = properties.getProperty("COLUMN");
|
column = properties.getProperty(COLUMN);
|
||||||
wordCount = properties.getProperty("WORD_COUNT");
|
wordCount = properties.getProperty(WORD_COUNT);
|
||||||
encode = properties.getProperty("ENCODE");
|
encode = properties.getProperty(ENCODE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -103,52 +103,52 @@ public class LocalizationConfig {
|
|||||||
private void setChineseLanguagePack() {
|
private void setChineseLanguagePack() {
|
||||||
properties.clear();
|
properties.clear();
|
||||||
// 设置默认属性
|
// 设置默认属性
|
||||||
properties.setProperty("TITLE", "JNotepad");
|
properties.setProperty(TITLE, APP_NAME);
|
||||||
properties.setProperty("NEW_FILE", "新建文件");
|
properties.setProperty(NEW_FILE, "新建文件");
|
||||||
properties.setProperty("SAVA", "保存");
|
properties.setProperty(SAVE, "保存");
|
||||||
properties.setProperty("FILE", "文件");
|
properties.setProperty(FILE, "文件");
|
||||||
properties.setProperty("NEW", "新建");
|
properties.setProperty(NEW, "新建");
|
||||||
properties.setProperty("OPEN", "打开");
|
properties.setProperty(OPEN, "打开");
|
||||||
properties.setProperty("SAVA_AS", "另存为");
|
properties.setProperty(SAVE_AS, "另存为");
|
||||||
properties.setProperty("SET", "设置");
|
properties.setProperty(SET, "设置");
|
||||||
properties.setProperty("WORD_WRAP", "自动换行");
|
properties.setProperty(WORD_WRAP, "自动换行");
|
||||||
properties.setProperty("OPEN_CONFIGURATION_FILE", "打开配置文件");
|
properties.setProperty(OPEN_CONFIGURATION_FILE, "打开配置文件");
|
||||||
properties.setProperty("PLUGIN", "插件");
|
properties.setProperty(PLUGIN, "插件");
|
||||||
properties.setProperty("ADD_PLUGIN", "增加插件");
|
properties.setProperty(ADD_PLUGIN, "增加插件");
|
||||||
properties.setProperty("STATISTICS", "统计字数");
|
properties.setProperty(STATISTICS, "统计字数");
|
||||||
properties.setProperty("ROW", "行数");
|
properties.setProperty(ROW, "行数");
|
||||||
properties.setProperty("COLUMN", "列数");
|
properties.setProperty(COLUMN, "列数");
|
||||||
properties.setProperty("WORD_COUNT", "字数");
|
properties.setProperty(WORD_COUNT, "字数");
|
||||||
properties.setProperty("ENCODE", "编码");
|
properties.setProperty(ENCODE, "编码");
|
||||||
properties.setProperty("TOP", "窗口置顶");
|
properties.setProperty(TOP, "窗口置顶");
|
||||||
properties.setProperty("LANGUAGE", "语言");
|
properties.setProperty(LANGUAGE, "语言");
|
||||||
properties.setProperty("CHINESE", "中文");
|
properties.setProperty(UPPER_CHINESE, "中文");
|
||||||
properties.setProperty("ENGLISH", "英文");
|
properties.setProperty(UPPER_ENGLISH, "英文");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setEnglishLanguagePack() {
|
private void setEnglishLanguagePack() {
|
||||||
properties.clear();
|
properties.clear();
|
||||||
properties.setProperty("TITLE", "JNotepad");
|
properties.setProperty(TITLE, APP_NAME);
|
||||||
properties.setProperty("NEW_FILE", "New File");
|
properties.setProperty(NEW_FILE, "New File");
|
||||||
properties.setProperty("SAVA", "Save");
|
properties.setProperty(SAVE, "Save");
|
||||||
properties.setProperty("FILE", "File");
|
properties.setProperty(FILE, "File");
|
||||||
properties.setProperty("NEW", "New");
|
properties.setProperty(NEW, "New");
|
||||||
properties.setProperty("OPEN", "Open");
|
properties.setProperty(OPEN, "Open");
|
||||||
properties.setProperty("SAVA_AS", "Save As");
|
properties.setProperty(SAVE_AS, "Save As");
|
||||||
properties.setProperty("SET", "Settings");
|
properties.setProperty(SET, "Settings");
|
||||||
properties.setProperty("WORD_WRAP", "Word Wrap");
|
properties.setProperty(WORD_WRAP, "Word Wrap");
|
||||||
properties.setProperty("OPEN_CONFIGURATION_FILE", "Open Configuration File");
|
properties.setProperty(OPEN_CONFIGURATION_FILE, "Open Configuration File");
|
||||||
properties.setProperty("PLUGIN", "Plugins");
|
properties.setProperty(PLUGIN, "Plugins");
|
||||||
properties.setProperty("ADD_PLUGIN", "Add Plugin");
|
properties.setProperty(ADD_PLUGIN, "Add Plugin");
|
||||||
properties.setProperty("STATISTICS", "Word Count");
|
properties.setProperty(STATISTICS, "Word Count");
|
||||||
properties.setProperty("ROW", "Row");
|
properties.setProperty(ROW, "Row");
|
||||||
properties.setProperty("COLUMN", "Column");
|
properties.setProperty(COLUMN, "Column");
|
||||||
properties.setProperty("WORD_COUNT", "Word Count");
|
properties.setProperty(WORD_COUNT, "Word Count");
|
||||||
properties.setProperty("ENCODE", "Encoding");
|
properties.setProperty(ENCODE, "Encoding");
|
||||||
properties.setProperty("TOP", "Window Top");
|
properties.setProperty(TOP, "Window Top");
|
||||||
properties.setProperty("LANGUAGE", "Language");
|
properties.setProperty(LANGUAGE, "Language");
|
||||||
properties.setProperty("CHINESE", "Chinese");
|
properties.setProperty(UPPER_CHINESE, "Chinese");
|
||||||
properties.setProperty("ENGLISH", "English");
|
properties.setProperty(UPPER_ENGLISH, "English");
|
||||||
}
|
}
|
||||||
|
|
||||||
private String textWrap;
|
private String textWrap;
|
||||||
@ -192,6 +192,9 @@ public class LocalizationConfig {
|
|||||||
|
|
||||||
private String newFile;
|
private String newFile;
|
||||||
/// EncodingDetector 文本常量
|
/// EncodingDetector 文本常量
|
||||||
|
/**
|
||||||
|
* 未知
|
||||||
|
*/
|
||||||
private String unknown;
|
private String unknown;
|
||||||
|
|
||||||
/// JNotepadStatusBox
|
/// JNotepadStatusBox
|
||||||
|
|||||||
@ -18,20 +18,12 @@ public class AppConstants {
|
|||||||
* 初始高度
|
* 初始高度
|
||||||
*/
|
*/
|
||||||
public static final double SCREEN_LENGTH = 600;
|
public static final double SCREEN_LENGTH = 600;
|
||||||
|
public static final String APP_NAME = "JNotepad";
|
||||||
/**
|
/**
|
||||||
* logo地址
|
* logo地址
|
||||||
*/
|
*/
|
||||||
public static final String APP_ICON = "/img/icon.png";
|
public static final String APP_ICON = "/img/icon.png";
|
||||||
|
|
||||||
/**
|
|
||||||
* 中文语言包
|
|
||||||
*/
|
|
||||||
public static final String CH_LANGUAGE_PACK_NAME = "ch_language_pack.txt";
|
|
||||||
/**
|
|
||||||
* 英文语言包
|
|
||||||
*/
|
|
||||||
public static final String EN_LANGUAGE_PACK_NAME = "en_language_pack.txt";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 配置文件名
|
* 配置文件名
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -24,6 +24,28 @@ public class TextConstants {
|
|||||||
*/
|
*/
|
||||||
public static final String TEXT_WRAP = "text.wrap";
|
public static final String TEXT_WRAP = "text.wrap";
|
||||||
|
|
||||||
|
public static final String TITLE = "title";
|
||||||
|
public static final String SAVE = "SAVE";
|
||||||
|
public static final String FILE = "FILE";
|
||||||
|
public static final String NEW = "NEW";
|
||||||
|
public static final String OPEN = "OPEN";
|
||||||
|
public static final String SAVE_AS = "SAVE_AS";
|
||||||
|
public static final String SET = "SET";
|
||||||
|
public static final String WORD_WRAP = "WORD_WRAP";
|
||||||
|
public static final String PLUGIN = "PLUGIN";
|
||||||
|
public static final String ADD_PLUGIN = "ADD_PLUGIN";
|
||||||
|
public static final String STATISTICS = "STATISTICS";
|
||||||
|
public static final String OPEN_CONFIGURATION_FILE = "OPEN_CONFIGURATION_FILE";
|
||||||
|
public static final String TOP = "TOP";
|
||||||
|
public static final String LANGUAGE = "LANGUAGE";
|
||||||
|
public static final String UPPER_CHINESE = "CHINESE";
|
||||||
|
public static final String UPPER_ENGLISH = "ENGLISH";
|
||||||
|
public static final String NEW_FILE = "NEW_FILE";
|
||||||
|
public static final String UNKNOWN = "UNKNOWN";
|
||||||
|
public static final String ROW = "ROW";
|
||||||
|
public static final String COLUMN = "COLUMN";
|
||||||
|
public static final String WORD_COUNT = "WORD_COUNT";
|
||||||
|
public static final String ENCODE = "ENCODE";
|
||||||
|
|
||||||
/// Config 文本常量
|
/// Config 文本常量
|
||||||
|
|
||||||
|
|||||||
@ -3,16 +3,13 @@ package org.jcnc.jnotepad.tool;
|
|||||||
|
|
||||||
import com.ibm.icu.text.CharsetDetector;
|
import com.ibm.icu.text.CharsetDetector;
|
||||||
import com.ibm.icu.text.CharsetMatch;
|
import com.ibm.icu.text.CharsetMatch;
|
||||||
import org.jcnc.jnotepad.constants.TextConstants;
|
import org.jcnc.jnotepad.app.config.LocalizationConfig;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
import java.io.BufferedInputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
|
|
||||||
import static org.jcnc.jnotepad.constants.TextConstants.UNKNOWN;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -73,13 +70,10 @@ public class EncodingDetector {
|
|||||||
*/
|
*/
|
||||||
public static Charset detectEncodingCharset(File file) {
|
public static Charset detectEncodingCharset(File file) {
|
||||||
String charset = detectEncoding(file);
|
String charset = detectEncoding(file);
|
||||||
if (charset.equals(localizationConfig.getUnknown())) {
|
|
||||||
try {
|
try {
|
||||||
assert charset != null;
|
|
||||||
return Charset.forName(charset);
|
return Charset.forName(charset);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return Charset.defaultCharset();
|
return Charset.defaultCharset();
|
||||||
}
|
}
|
||||||
return Charset.forName(charset);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user