commit
abb3633172
@ -41,13 +41,22 @@ public class LunchApp extends Application {
|
||||
/**
|
||||
* 配置文件数组
|
||||
*/
|
||||
static List<LoadJnotepadConfig<?>> loadJnotepadConfigs = new ArrayList<>();
|
||||
private static final List<LoadJnotepadConfig<?>> LOAD_JNOTEPAD_CONFIGS = new ArrayList<>();
|
||||
|
||||
static {
|
||||
// 语言配置文件
|
||||
loadJnotepadConfigs.add(new LoadLanguageConfig());
|
||||
LOAD_JNOTEPAD_CONFIGS.add(new LoadLanguageConfig());
|
||||
// 快捷键配置文件
|
||||
loadJnotepadConfigs.add(new LoadShortcutKeyConfig());
|
||||
LOAD_JNOTEPAD_CONFIGS.add(new LoadShortcutKeyConfig());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置文件数组
|
||||
*
|
||||
* @since 2023/8/26 16:05
|
||||
*/
|
||||
public static List<LoadJnotepadConfig<?>> getLocalizationConfigs() {
|
||||
return LOAD_JNOTEPAD_CONFIGS;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -71,7 +80,7 @@ public class LunchApp extends Application {
|
||||
ViewManager viewManager = ViewManager.getInstance(scene);
|
||||
viewManager.initScreen(scene);
|
||||
// 加载配置文件
|
||||
View.getInstance().initJnotepadConfigs(loadJnotepadConfigs);
|
||||
View.getInstance().initJnotepadConfigs(LOAD_JNOTEPAD_CONFIGS);
|
||||
primaryStage.setTitle(localizationConfig.getTitle());
|
||||
primaryStage.setWidth(width);
|
||||
primaryStage.setHeight(length);
|
||||
|
||||
@ -1,9 +1,10 @@
|
||||
package org.jcnc.jnotepad.constants;
|
||||
|
||||
|
||||
import java.util.Map;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.Map;
|
||||
/**
|
||||
* 文本常量
|
||||
* <p>
|
||||
@ -57,7 +58,14 @@ public class TextConstants {
|
||||
|
||||
public static final String CHINESE = "chinese";
|
||||
|
||||
/// 语言映射
|
||||
public static final String LOWER_LANGUAGE = "language";
|
||||
|
||||
private static final String BUTTON_NAME = "buttonName";
|
||||
|
||||
private static final String SHORTCUT_KEY_VALUE = "shortcutKeyValue";
|
||||
/**
|
||||
* 语言映射
|
||||
*/
|
||||
public static final Map<String, String> LANGUAGE_FILE_MAP = Map.of(
|
||||
CHINESE, CH_LANGUAGE_PACK_NAME,
|
||||
ENGLISH, EN_LANGUAGE_PACK_NAME
|
||||
@ -71,48 +79,48 @@ public class TextConstants {
|
||||
public static JSONObject createShortcutKeyJson() {
|
||||
JSONObject json = new JSONObject();
|
||||
|
||||
json.put("language", "chinese");
|
||||
json.put(LOWER_LANGUAGE, CHINESE);
|
||||
|
||||
JSONArray shortcutKeyArray = new JSONArray();
|
||||
|
||||
JSONObject newItem = new JSONObject();
|
||||
newItem.put("buttonName", "newItem");
|
||||
newItem.put("shortcutKeyValue", "ctrl+n");
|
||||
newItem.put(BUTTON_NAME, "newItem");
|
||||
newItem.put(SHORTCUT_KEY_VALUE, "ctrl+n");
|
||||
shortcutKeyArray.put(newItem);
|
||||
|
||||
JSONObject openItem = new JSONObject();
|
||||
openItem.put("buttonName", "openItem");
|
||||
openItem.put("shortcutKeyValue", "ctrl+o");
|
||||
openItem.put(BUTTON_NAME, "openItem");
|
||||
openItem.put(SHORTCUT_KEY_VALUE, "ctrl+o");
|
||||
shortcutKeyArray.put(openItem);
|
||||
|
||||
JSONObject saveItem = new JSONObject();
|
||||
saveItem.put("buttonName", "saveItem");
|
||||
saveItem.put("shortcutKeyValue", "ctrl+s");
|
||||
saveItem.put(BUTTON_NAME, "saveItem");
|
||||
saveItem.put(SHORTCUT_KEY_VALUE, "ctrl+s");
|
||||
shortcutKeyArray.put(saveItem);
|
||||
|
||||
JSONObject saveAsItem = new JSONObject();
|
||||
saveAsItem.put("buttonName", "saveAsItem");
|
||||
saveAsItem.put("shortcutKeyValue", "ctrl+alt+s");
|
||||
saveAsItem.put(BUTTON_NAME, "saveAsItem");
|
||||
saveAsItem.put(SHORTCUT_KEY_VALUE, "ctrl+alt+s");
|
||||
shortcutKeyArray.put(saveAsItem);
|
||||
|
||||
JSONObject lineFeedItem = new JSONObject();
|
||||
lineFeedItem.put("buttonName", "lineFeedItem");
|
||||
lineFeedItem.put("shortcutKeyValue", "");
|
||||
lineFeedItem.put(BUTTON_NAME, "lineFeedItem");
|
||||
lineFeedItem.put(SHORTCUT_KEY_VALUE, "");
|
||||
shortcutKeyArray.put(lineFeedItem);
|
||||
|
||||
JSONObject openConfigItem = new JSONObject();
|
||||
openConfigItem.put("buttonName", "openConfigItem");
|
||||
openConfigItem.put("shortcutKeyValue", "alt+s");
|
||||
openConfigItem.put(BUTTON_NAME, "openConfigItem");
|
||||
openConfigItem.put(SHORTCUT_KEY_VALUE, "alt+s");
|
||||
shortcutKeyArray.put(openConfigItem);
|
||||
|
||||
JSONObject addItem = new JSONObject();
|
||||
addItem.put("buttonName", "addItem");
|
||||
addItem.put("shortcutKeyValue", "");
|
||||
addItem.put(BUTTON_NAME, "addItem");
|
||||
addItem.put(SHORTCUT_KEY_VALUE, "");
|
||||
shortcutKeyArray.put(addItem);
|
||||
|
||||
JSONObject countItem = new JSONObject();
|
||||
countItem.put("buttonName", "countItem");
|
||||
countItem.put("shortcutKeyValue", "");
|
||||
countItem.put(BUTTON_NAME, "countItem");
|
||||
countItem.put(SHORTCUT_KEY_VALUE, "");
|
||||
shortcutKeyArray.put(countItem);
|
||||
|
||||
json.put("shortcutKey", shortcutKeyArray);
|
||||
|
||||
@ -2,8 +2,7 @@ package org.jcnc.jnotepad.controller.event.handler;
|
||||
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.event.EventHandler;
|
||||
import org.jcnc.jnotepad.app.init.LoadLanguageConfig;
|
||||
import org.jcnc.jnotepad.app.init.LoadShortcutKeyConfig;
|
||||
import org.jcnc.jnotepad.LunchApp;
|
||||
import org.jcnc.jnotepad.tool.LogUtil;
|
||||
import org.jcnc.jnotepad.ui.LineNumberTextArea;
|
||||
import org.jcnc.jnotepad.ui.tab.JNotepadTab;
|
||||
@ -47,12 +46,9 @@ public class SaveFile implements EventHandler<ActionEvent> {
|
||||
selectedTab.save();
|
||||
// 如果该文件是配置文件则刷新快捷键
|
||||
if (CONFIG_NAME.equals(selectedTab.getText())) {
|
||||
|
||||
// 刷新语言包
|
||||
View.getInstance().initJnotepadConfig(new LoadLanguageConfig());
|
||||
// 重新加载语言包和快捷键
|
||||
View.getInstance().initJnotepadConfigs(LunchApp.getLocalizationConfigs());
|
||||
logger.info("已刷新语言包!");
|
||||
// 初始化快捷键
|
||||
View.getInstance().initJnotepadConfig(new LoadShortcutKeyConfig());
|
||||
logger.info("已刷新快捷键!");
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,21 +1,32 @@
|
||||
package org.jcnc.jnotepad.ui.menu;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.stage.Stage;
|
||||
import org.jcnc.jnotepad.LunchApp;
|
||||
import org.jcnc.jnotepad.app.config.GlobalConfig;
|
||||
import org.jcnc.jnotepad.app.config.LocalizationConfig;
|
||||
import org.jcnc.jnotepad.controller.event.handler.*;
|
||||
import org.jcnc.jnotepad.exception.AppException;
|
||||
import org.jcnc.jnotepad.tool.LogUtil;
|
||||
import org.jcnc.jnotepad.ui.status.JNotepadStatusBox;
|
||||
import org.jcnc.jnotepad.ui.tab.JNotepadTab;
|
||||
import org.jcnc.jnotepad.ui.tab.JNotepadTabPane;
|
||||
import org.jcnc.jnotepad.view.init.View;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.fasterxml.jackson.core.JsonEncoding.UTF8;
|
||||
import static org.jcnc.jnotepad.constants.AppConstants.CONFIG_NAME;
|
||||
import static org.jcnc.jnotepad.constants.TextConstants.*;
|
||||
|
||||
/**
|
||||
@ -25,7 +36,6 @@ import static org.jcnc.jnotepad.constants.TextConstants.*;
|
||||
*/
|
||||
public class JNotepadMenuBar extends MenuBar {
|
||||
|
||||
|
||||
/**
|
||||
* 标签页布局组件封装。
|
||||
*/
|
||||
@ -246,31 +256,82 @@ public class JNotepadMenuBar extends MenuBar {
|
||||
// 设置窗口为置顶
|
||||
primaryStage.setAlwaysOnTop(after);
|
||||
});
|
||||
// todo 切换语言并将语言修改设置回本地,
|
||||
// 1.只更新json的language为english,没有保存
|
||||
englishItem.setOnAction(new OpenHandler() {
|
||||
@Override
|
||||
public void handle(ActionEvent actionEvent) {
|
||||
|
||||
// 获取当前的语言值
|
||||
JSONObject json = createShortcutKeyJson();
|
||||
|
||||
// 更新语言值为 "english"
|
||||
json.put("language", "english");
|
||||
|
||||
// 打印更新后的配置
|
||||
System.out.println(json.toString(4)); // 使用四个空格缩进
|
||||
|
||||
setCurrentLanguage(ENGLISH);
|
||||
}
|
||||
});
|
||||
chineseItem.setOnAction(new OpenHandler() {
|
||||
@Override
|
||||
public void handle(ActionEvent actionEvent) {
|
||||
|
||||
setCurrentLanguage(CHINESE);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置当前语言<br>
|
||||
*
|
||||
* @param language 要设置的语言
|
||||
* @since 2023/8/26 16:16
|
||||
*/
|
||||
private void setCurrentLanguage(String language) {
|
||||
boolean flag = false;
|
||||
JSONObject json = new JSONObject();
|
||||
// 获取本地配置文件
|
||||
logger.info("尝试读取本地配置文件!");
|
||||
StringBuffer jsonData = new StringBuffer();
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(CONFIG_NAME)))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
jsonData.append(line);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.error("读取失败,配置文件错误或不存在配置文件!");
|
||||
flag = true;
|
||||
}
|
||||
if (!flag) {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
JsonNode jsonNode;
|
||||
try {
|
||||
jsonNode = objectMapper.readTree(jsonData.toString());
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new AppException(e.getMessage());
|
||||
}
|
||||
JSONObject finalJson = json;
|
||||
jsonNode.fields().forEachRemaining(entry -> {
|
||||
String key = entry.getKey();
|
||||
JsonNode childNode = entry.getValue();
|
||||
if (!LOWER_LANGUAGE.equals(key)) {
|
||||
if (childNode.isArray()) {
|
||||
finalJson.put(key, new JSONArray(childNode.toString()));
|
||||
} else {
|
||||
finalJson.put(key, childNode.toString());
|
||||
}
|
||||
}
|
||||
});
|
||||
logger.info("读取本地配置文件成功!");
|
||||
}
|
||||
if (flag) {
|
||||
logger.info("获取默认内置配置文件!");
|
||||
// 如果读取本地失败则获取默认配置文件
|
||||
json = createShortcutKeyJson();
|
||||
}
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(CONFIG_NAME, Charset.forName(UTF8.name())))) {
|
||||
// 更新语言值为 language 并写回本地
|
||||
writer.write(json.put(LOWER_LANGUAGE, language).toString(4));
|
||||
// 刷新文件
|
||||
writer.flush();
|
||||
// 重新加载语言包和快捷键
|
||||
View.getInstance().initJnotepadConfigs(LunchApp.getLocalizationConfigs());
|
||||
logger.info("已刷新语言包!");
|
||||
logger.info("已刷新快捷键!");
|
||||
} catch (IOException e) {
|
||||
logger.error("配置文件写入失败,请检查配置文件");
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, MenuItem> getItemMap() {
|
||||
return itemMap;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user