UiPrefs.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package com.mes.core;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import com.alibaba.fastjson2.JSONWriter;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import java.io.IOException;
  8. import java.nio.charset.StandardCharsets;
  9. import java.nio.file.Files;
  10. import java.nio.file.Path;
  11. import java.nio.file.Paths;
  12. import java.util.HashSet;
  13. import java.util.Set;
  14. /**
  15. * 本机 UI 偏好 - 管理“始终显示”的菜单白名单
  16. *
  17. * 存储位置:config/ui_prefs.json
  18. * 结构:
  19. * {
  20. * "alwaysShow": ["manual_submit"]
  21. * }
  22. */
  23. public class UiPrefs {
  24. private static final Logger log = LoggerFactory.getLogger(UiPrefs.class);
  25. private static final String CONFIG_FILE = "config/ui_prefs.json";
  26. /** 菜单 key:手动提交子菜单 */
  27. public static final String KEY_MANUAL_SUBMIT = "manual_submit";
  28. private static UiPrefs INSTANCE;
  29. private final Path configPath;
  30. private final Set<String> alwaysShow = new HashSet<>();
  31. private UiPrefs() {
  32. this.configPath = Paths.get(CONFIG_FILE);
  33. load();
  34. }
  35. public static synchronized UiPrefs getInstance() {
  36. if (INSTANCE == null) {
  37. INSTANCE = new UiPrefs();
  38. }
  39. return INSTANCE;
  40. }
  41. /** 是否勾选了“始终显示” */
  42. public boolean isAlwaysShow(String key) {
  43. return alwaysShow.contains(key);
  44. }
  45. /** 设置“始终显示”状态,立即落盘 */
  46. public void setAlwaysShow(String key, boolean always) {
  47. boolean changed;
  48. if (always) {
  49. changed = alwaysShow.add(key);
  50. } else {
  51. changed = alwaysShow.remove(key);
  52. }
  53. if (changed) {
  54. save();
  55. }
  56. }
  57. private void load() {
  58. try {
  59. if (!Files.exists(configPath)) {
  60. log.info("UI偏好文件不存在,使用默认值: {}", configPath);
  61. return;
  62. }
  63. String content = new String(Files.readAllBytes(configPath), StandardCharsets.UTF_8);
  64. JSONObject json = JSON.parseObject(content);
  65. if (json == null) return;
  66. alwaysShow.clear();
  67. java.util.List<Object> list = json.getJSONArray("alwaysShow");
  68. if (list != null) {
  69. for (Object o : list) {
  70. if (o != null) alwaysShow.add(o.toString());
  71. }
  72. }
  73. log.info("UI偏好加载成功: alwaysShow={}", alwaysShow);
  74. } catch (Exception e) {
  75. log.error("加载UI偏好失败: {}", e.getMessage(), e);
  76. }
  77. }
  78. private void save() {
  79. try {
  80. if (configPath.getParent() != null && !Files.exists(configPath.getParent())) {
  81. Files.createDirectories(configPath.getParent());
  82. }
  83. JSONObject root = new JSONObject();
  84. root.put("alwaysShow", new java.util.ArrayList<>(alwaysShow));
  85. String content = JSON.toJSONString(root, JSONWriter.Feature.PrettyFormat);
  86. Files.write(configPath, content.getBytes(StandardCharsets.UTF_8));
  87. log.info("UI偏好已保存: {}", alwaysShow);
  88. } catch (IOException e) {
  89. log.error("保存UI偏好失败: {}", e.getMessage(), e);
  90. }
  91. }
  92. }