完成全局变量的编辑功能

This commit is contained in:
makejava 2021-08-11 17:16:48 +08:00
parent 70909e95c1
commit e24ea01047
7 changed files with 222 additions and 18 deletions

View File

@ -52,8 +52,11 @@ public class SettingsStorageDTO {
storage.templateGroupMap = new HashMap<>(16); storage.templateGroupMap = new HashMap<>(16);
storage.templateGroupMap.put(GlobalDict.DEFAULT_GROUP_NAME, templateGroup); 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 = new HashMap<>(16);
storage.globalConfigGroupMap.put(GlobalDict.DEFAULT_GROUP_NAME, new GlobalConfigGroup()); storage.globalConfigGroupMap.put(GlobalDict.DEFAULT_GROUP_NAME, globalConfigGroup);
return storage; return storage;
} }

View File

@ -15,7 +15,7 @@ import lombok.NoArgsConstructor;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public class GlobalConfig implements AbstractItem<GlobalConfig>, Item { public class GlobalConfig implements AbstractEditorItem<GlobalConfig>, Item {
/** /**
* 名称 * 名称
*/ */
@ -29,4 +29,24 @@ public class GlobalConfig implements AbstractItem<GlobalConfig>, Item {
public GlobalConfig defaultVal() { public GlobalConfig defaultVal() {
return new GlobalConfig("demo", "value"); 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;
}
} }

View File

@ -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<String, GlobalConfigGroup> globalConfigGroupMap;
/**
* 当前分组名
*/
private GlobalConfigGroup currGlobalConfigGroup;
/**
* 编辑框组件
*/
private EditorComponent<GlobalConfig> editorComponent;
/**
* 分组操作组件
*/
private GroupNameComponent<GlobalConfig, GlobalConfigGroup> groupNameComponent;
/**
* 编辑列表框
*/
private EditListComponent<GlobalConfig> editListComponent;
public GlobalConfigSettingForm() {
this.mainPanel = new JPanel(new BorderLayout());
}
private void initGroupName() {
Consumer<GlobalConfigGroup> 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<GlobalConfig> 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<Map<String, GlobalConfigGroup>>() {
});
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());
}
}
}

View File

@ -79,7 +79,7 @@ public class MainSettingForm implements Configurable, Configurable.Composite, Ba
new TypeMapperSettingForm(), new TypeMapperSettingForm(),
new TemplateSettingForm(), new TemplateSettingForm(),
new ColumnConfigSettingForm(), new ColumnConfigSettingForm(),
// new GlobalConfigSettingPanel() new GlobalConfigSettingForm(),
}; };
this.loadChildSettingsStore(); this.loadChildSettingsStore();
return this.childConfigurableArray; return this.childConfigurableArray;

View File

@ -35,7 +35,7 @@ public class TemplateSettingForm implements Configurable, BaseSettings {
static { static {
String descriptionInfo = ""; String descriptionInfo = "";
try { try {
descriptionInfo = UrlUtil.loadText(TemplateSettingPanel.class.getResource("/description/templateDescription.html")); descriptionInfo = UrlUtil.loadText(TemplateSettingForm.class.getResource("/description/templateDescription.html"));
} catch (IOException e) { } catch (IOException e) {
ExceptionUtil.rethrow(e); ExceptionUtil.rethrow(e);
} finally { } finally {
@ -75,6 +75,8 @@ public class TemplateSettingForm implements Configurable, BaseSettings {
Consumer<TemplateGroup> switchGroupOperator = templateGroup -> { Consumer<TemplateGroup> switchGroupOperator = templateGroup -> {
this.currTemplateGroup = templateGroup; this.currTemplateGroup = templateGroup;
refreshUiVal(); refreshUiVal();
// 切换分组情况编辑框
this.editorComponent.setFile(null);
}; };
this.groupNameComponent = new GroupNameComponent<>(switchGroupOperator, this.templateGroupMap); this.groupNameComponent = new GroupNameComponent<>(switchGroupOperator, this.templateGroupMap);
@ -84,7 +86,9 @@ public class TemplateSettingForm implements Configurable, BaseSettings {
private void initEditList() { private void initEditList() {
Consumer<Template> switchItemFun = template -> { Consumer<Template> switchItemFun = template -> {
refreshUiVal(); refreshUiVal();
this.editListComponent.setCurrentItem(template.getName()); if (template != null) {
this.editListComponent.setCurrentItem(template.getName());
}
editorComponent.setFile(template); editorComponent.setFile(template);
}; };
this.editListComponent = new EditListComponent<>(switchItemFun, "Template Name:", Template.class, this.currTemplateGroup.getElementList()); this.editListComponent = new EditListComponent<>(switchItemFun, "Template Name:", Template.class, this.currTemplateGroup.getElementList());
@ -128,6 +132,10 @@ public class TemplateSettingForm implements Configurable, BaseSettings {
this.currTemplateGroup = this.templateGroupMap.get(GlobalDict.DEFAULT_GROUP_NAME); this.currTemplateGroup = this.templateGroupMap.get(GlobalDict.DEFAULT_GROUP_NAME);
} }
this.refreshUiVal(); this.refreshUiVal();
// 解决reset后编辑框未清空BUG
if (this.editorComponent != null) {
this.editorComponent.setFile(null);
}
} }
@Override @Override
@ -158,8 +166,5 @@ public class TemplateSettingForm implements Configurable, BaseSettings {
if (this.editListComponent != null) { if (this.editListComponent != null) {
this.editListComponent.setElementList(this.currTemplateGroup.getElementList()); this.editListComponent.setElementList(this.currTemplateGroup.getElementList());
} }
if (this.editorComponent != null) {
this.editorComponent.setFile(null);
}
} }
} }

View File

@ -16,6 +16,7 @@ import org.jetbrains.annotations.NotNull;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -210,6 +211,9 @@ public class EditListComponent<E extends AbstractEditorItem<E>> {
} }
private List<String> getAllItemName() { private List<String> getAllItemName() {
if (CollectionUtil.isEmpty(elementList)) {
return Collections.emptyList();
}
return elementList.stream().map(AbstractEditorItem::fileName).collect(Collectors.toList()); return elementList.stream().map(AbstractEditorItem::fileName).collect(Collectors.toList());
} }
@ -223,6 +227,7 @@ public class EditListComponent<E extends AbstractEditorItem<E>> {
} }
if (StringUtils.isEmpty(this.currentItem) && elementList != null && elementList.size() > 0) { if (StringUtils.isEmpty(this.currentItem) && elementList != null && elementList.size() > 0) {
setCurrentItem(elementList.get(0).fileName()); setCurrentItem(elementList.get(0).fileName());
switchItemFun.accept(findByName(this.currentItem));
} }
} }

View File

@ -1,23 +1,24 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="zh"> <html lang="zh">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>全局变量说明</title> <title>全局变量说明</title>
</head> </head>
<body> <body>
<h4>说明文档:</h4> <h3>说明文档:</h3>
<h4>使用案例一(简单)</h4>
<p> <p>
不仅仅是简单的全局变量,完全的自由发挥。<br> 名称为变量名,内容为变量值<br>
<span style="color: #ffb224; background: gray">注意:</span>全局变量具有优先处理权限,会影响到模板中的任意同名变量,<br> 使用示例:在模板中直接使用${变量名}引用变量
<span style="color: #ff4c41">所以在模板中一定不要定义与全局变量同名的变量。</span>
</p> </p>
<h4>使用方式:</h4> <h4>使用案例二(复杂)</h4>
<p> <p>
使用全局变量时,直接在模板中当普通变量引入即可。<br> 名称为变量名,内容为模板内容<br>
使用示例:在模板任意位置直接使用${变量名}引入模板
</p> </p>
<h4>作用:</h4>
<p> <p>
通常用于定义宏,初始化操作,定义字符串变量,等等。 <span style="color: #ffb224; background: gray">注意:</span>全局变量具有优先处理权限,会影响到模板中的任意同名变量,<br>
<span style="color: #ff4c41">所以在模板中一定不要定义与全局变量同名的变量。</span>
</p> </p>
</body> </body>
</html> </html>