From e24ea010474ca613372d1c3686fda9d18301234e Mon Sep 17 00:00:00 2001 From: makejava <1353036300@qq.com> Date: Wed, 11 Aug 2021 17:16:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=85=A8=E5=B1=80=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E7=9A=84=E7=BC=96=E8=BE=91=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sjhy/plugin/dto/SettingsStorageDTO.java | 5 +- .../com/sjhy/plugin/entity/GlobalConfig.java | 22 ++- .../plugin/ui/GlobalConfigSettingForm.java | 170 ++++++++++++++++++ .../com/sjhy/plugin/ui/MainSettingForm.java | 2 +- .../sjhy/plugin/ui/TemplateSettingForm.java | 15 +- .../ui/component/EditListComponent.java | 5 + .../description/globalConfigDescription.html | 21 +-- 7 files changed, 222 insertions(+), 18 deletions(-) create mode 100644 src/main/java/com/sjhy/plugin/ui/GlobalConfigSettingForm.java diff --git a/src/main/java/com/sjhy/plugin/dto/SettingsStorageDTO.java b/src/main/java/com/sjhy/plugin/dto/SettingsStorageDTO.java index 396ba81..a181b7d 100644 --- a/src/main/java/com/sjhy/plugin/dto/SettingsStorageDTO.java +++ b/src/main/java/com/sjhy/plugin/dto/SettingsStorageDTO.java @@ -52,8 +52,11 @@ public class SettingsStorageDTO { storage.templateGroupMap = new HashMap<>(16); storage.templateGroupMap.put(GlobalDict.DEFAULT_GROUP_NAME, templateGroup); + GlobalConfigGroup globalConfigGroup = new GlobalConfigGroup(); + globalConfigGroup.setName(GlobalDict.DEFAULT_GROUP_NAME); + globalConfigGroup.setElementList(Arrays.asList(new GlobalConfig("test", "abc"), new GlobalConfig("demo", "value"))); storage.globalConfigGroupMap = new HashMap<>(16); - storage.globalConfigGroupMap.put(GlobalDict.DEFAULT_GROUP_NAME, new GlobalConfigGroup()); + storage.globalConfigGroupMap.put(GlobalDict.DEFAULT_GROUP_NAME, globalConfigGroup); return storage; } diff --git a/src/main/java/com/sjhy/plugin/entity/GlobalConfig.java b/src/main/java/com/sjhy/plugin/entity/GlobalConfig.java index e5b7681..3bb23ae 100644 --- a/src/main/java/com/sjhy/plugin/entity/GlobalConfig.java +++ b/src/main/java/com/sjhy/plugin/entity/GlobalConfig.java @@ -15,7 +15,7 @@ import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor -public class GlobalConfig implements AbstractItem, Item { +public class GlobalConfig implements AbstractEditorItem, Item { /** * 名称 */ @@ -29,4 +29,24 @@ public class GlobalConfig implements AbstractItem, Item { public GlobalConfig defaultVal() { return new GlobalConfig("demo", "value"); } + + @Override + public void changeFileName(String name) { + this.name = name; + } + + @Override + public String fileName() { + return this.name; + } + + @Override + public void changeFileContent(String content) { + this.value = content; + } + + @Override + public String fileContent() { + return this.value; + } } diff --git a/src/main/java/com/sjhy/plugin/ui/GlobalConfigSettingForm.java b/src/main/java/com/sjhy/plugin/ui/GlobalConfigSettingForm.java new file mode 100644 index 0000000..c7b1635 --- /dev/null +++ b/src/main/java/com/sjhy/plugin/ui/GlobalConfigSettingForm.java @@ -0,0 +1,170 @@ +package com.sjhy.plugin.ui; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.intellij.ide.fileTemplates.impl.UrlUtil; +import com.intellij.openapi.options.Configurable; +import com.intellij.util.ExceptionUtil; +import com.sjhy.plugin.dict.GlobalDict; +import com.sjhy.plugin.dto.SettingsStorageDTO; +import com.sjhy.plugin.entity.GlobalConfig; +import com.sjhy.plugin.entity.GlobalConfigGroup; +import com.sjhy.plugin.tool.CloneUtils; +import com.sjhy.plugin.ui.component.EditListComponent; +import com.sjhy.plugin.ui.component.EditorComponent; +import com.sjhy.plugin.ui.component.GroupNameComponent; +import com.sjhy.plugin.ui.component.LeftRightComponent; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; +import java.awt.*; +import java.io.IOException; +import java.util.Map; +import java.util.function.Consumer; + +/** + * @author makejava + * @version 1.0.0 + * @date 2021/08/10 16:14 + */ +public class GlobalConfigSettingForm implements Configurable, BaseSettings { + /** + * 全局变量描述信息,说明文档 + */ + private static final String TEMPLATE_DESCRIPTION_INFO; + + static { + String descriptionInfo = ""; + try { + descriptionInfo = UrlUtil.loadText(GlobalConfigSettingForm.class.getResource("/description/globalConfigDescription.html")); + } catch (IOException e) { + ExceptionUtil.rethrow(e); + } finally { + TEMPLATE_DESCRIPTION_INFO = descriptionInfo; + } + } + + private JPanel mainPanel; + /** + * 类型映射配置 + */ + private Map globalConfigGroupMap; + /** + * 当前分组名 + */ + private GlobalConfigGroup currGlobalConfigGroup; + /** + * 编辑框组件 + */ + private EditorComponent editorComponent; + /** + * 分组操作组件 + */ + private GroupNameComponent groupNameComponent; + /** + * 编辑列表框 + */ + private EditListComponent editListComponent; + + + public GlobalConfigSettingForm() { + this.mainPanel = new JPanel(new BorderLayout()); + } + + + private void initGroupName() { + Consumer switchGroupOperator = globalConfigGroup -> { + this.currGlobalConfigGroup = globalConfigGroup; + refreshUiVal(); + // 切换分组情况编辑框 + this.editorComponent.setFile(null); + }; + + this.groupNameComponent = new GroupNameComponent<>(switchGroupOperator, this.globalConfigGroupMap); + this.mainPanel.add(groupNameComponent.getPanel(), BorderLayout.NORTH); + } + + private void initEditList() { + Consumer switchItemFun = globalConfig -> { + refreshUiVal(); + if (globalConfig != null) { + this.editListComponent.setCurrentItem(globalConfig.getName()); + } + editorComponent.setFile(globalConfig); + }; + this.editListComponent = new EditListComponent<>(switchItemFun, "GlobalConfig Name:", GlobalConfig.class, this.currGlobalConfigGroup.getElementList()); + } + + private void initEditor() { + this.editorComponent = new EditorComponent<>(null, TEMPLATE_DESCRIPTION_INFO); + } + + private void initPanel() { + this.loadSettingsStore(getSettingsStorage()); + // 初始化表格 + this.initGroupName(); + // 初始化编辑列表组件 + this.initEditList(); + // 初始化编辑框组件 + this.initEditor(); + // 左右组件 + LeftRightComponent leftRightComponent = new LeftRightComponent(editListComponent.getMainPanel(), this.editorComponent.getMainPanel()); + this.mainPanel.add(leftRightComponent.getMainPanel(), BorderLayout.CENTER); + } + + @Override + public String getDisplayName() { + return "Global Config"; + } + + @Nullable + @Override + public String getHelpTopic() { + return getDisplayName(); + } + + @Override + public void loadSettingsStore(SettingsStorageDTO settingsStorage) { + // 复制配置,防止篡改 + this.globalConfigGroupMap = CloneUtils.cloneByJson(settingsStorage.getGlobalConfigGroupMap(), new TypeReference>() { + }); + this.currGlobalConfigGroup = this.globalConfigGroupMap.get(settingsStorage.getCurrTypeMapperGroupName()); + if (this.currGlobalConfigGroup == null) { + this.currGlobalConfigGroup = this.globalConfigGroupMap.get(GlobalDict.DEFAULT_GROUP_NAME); + } + this.refreshUiVal(); + // 解决reset后编辑框未清空BUG + if (this.editorComponent != null) { + this.editorComponent.setFile(null); + } + } + + @Override + public @Nullable JComponent createComponent() { + this.initPanel(); + return mainPanel; + } + + @Override + public boolean isModified() { + return !this.globalConfigGroupMap.equals(getSettingsStorage().getGlobalConfigGroupMap()) + || !getSettingsStorage().getCurrGlobalConfigGroupName().equals(this.currGlobalConfigGroup.getName()); + } + + @Override + public void apply() { + getSettingsStorage().setGlobalConfigGroupMap(this.globalConfigGroupMap); + getSettingsStorage().setCurrTypeMapperGroupName(this.currGlobalConfigGroup.getName()); + // 保存包后重新加载配置 + this.loadSettingsStore(getSettingsStorage()); + } + + private void refreshUiVal() { + if (this.groupNameComponent != null) { + this.groupNameComponent.setGroupMap(this.globalConfigGroupMap); + this.groupNameComponent.setCurrGroupName(this.currGlobalConfigGroup.getName()); + } + if (this.editListComponent != null) { + this.editListComponent.setElementList(this.currGlobalConfigGroup.getElementList()); + } + } +} diff --git a/src/main/java/com/sjhy/plugin/ui/MainSettingForm.java b/src/main/java/com/sjhy/plugin/ui/MainSettingForm.java index d013990..2688695 100644 --- a/src/main/java/com/sjhy/plugin/ui/MainSettingForm.java +++ b/src/main/java/com/sjhy/plugin/ui/MainSettingForm.java @@ -79,7 +79,7 @@ public class MainSettingForm implements Configurable, Configurable.Composite, Ba new TypeMapperSettingForm(), new TemplateSettingForm(), new ColumnConfigSettingForm(), -// new GlobalConfigSettingPanel() + new GlobalConfigSettingForm(), }; this.loadChildSettingsStore(); return this.childConfigurableArray; diff --git a/src/main/java/com/sjhy/plugin/ui/TemplateSettingForm.java b/src/main/java/com/sjhy/plugin/ui/TemplateSettingForm.java index 53263de..f7b5a76 100644 --- a/src/main/java/com/sjhy/plugin/ui/TemplateSettingForm.java +++ b/src/main/java/com/sjhy/plugin/ui/TemplateSettingForm.java @@ -35,7 +35,7 @@ public class TemplateSettingForm implements Configurable, BaseSettings { static { String descriptionInfo = ""; try { - descriptionInfo = UrlUtil.loadText(TemplateSettingPanel.class.getResource("/description/templateDescription.html")); + descriptionInfo = UrlUtil.loadText(TemplateSettingForm.class.getResource("/description/templateDescription.html")); } catch (IOException e) { ExceptionUtil.rethrow(e); } finally { @@ -75,6 +75,8 @@ public class TemplateSettingForm implements Configurable, BaseSettings { Consumer switchGroupOperator = templateGroup -> { this.currTemplateGroup = templateGroup; refreshUiVal(); + // 切换分组情况编辑框 + this.editorComponent.setFile(null); }; this.groupNameComponent = new GroupNameComponent<>(switchGroupOperator, this.templateGroupMap); @@ -84,7 +86,9 @@ public class TemplateSettingForm implements Configurable, BaseSettings { private void initEditList() { Consumer