完成列配置界面

This commit is contained in:
makejava 2021-08-10 14:10:40 +08:00
parent 87fbc236a4
commit 4078fee1c5
6 changed files with 333 additions and 37 deletions

View File

@ -2,6 +2,7 @@ package com.sjhy.plugin.dto;
import com.sjhy.plugin.dict.GlobalDict;
import com.sjhy.plugin.entity.*;
import com.sjhy.plugin.enums.ColumnConfigType;
import lombok.Data;
import java.util.Arrays;
@ -39,12 +40,16 @@ public class SettingsStorageDTO {
typeMapperGroup.setElementList(Arrays.asList(new TypeMapper("varchar", "java.lang.String"), new TypeMapper("varchar\\(\\)", "java.lang.String")));
storage.typeMapperGroupMap.put(GlobalDict.DEFAULT_GROUP_NAME, typeMapperGroup);
ColumnConfigGroup columnConfigGroup = new ColumnConfigGroup();
columnConfigGroup.setName(GlobalDict.DEFAULT_GROUP_NAME);
columnConfigGroup.setElementList(Arrays.asList(new ColumnConfig("disable", ColumnConfigType.BOOLEAN), new ColumnConfig("operator", ColumnConfigType.SELECT, "insert,update,delete,select")));
storage.columnConfigGroupMap = new HashMap<>(16);
storage.columnConfigGroupMap.put(GlobalDict.DEFAULT_GROUP_NAME, columnConfigGroup);
storage.globalConfigGroupMap = new HashMap<>(16);
storage.globalConfigGroupMap.put(GlobalDict.DEFAULT_GROUP_NAME, new GlobalConfigGroup());
storage.templateGroupMap = new HashMap<>(16);
storage.templateGroupMap.put(GlobalDict.DEFAULT_GROUP_NAME, new TemplateGroup());
storage.columnConfigGroupMap = new HashMap<>(16);
storage.columnConfigGroupMap.put(GlobalDict.DEFAULT_GROUP_NAME, new ColumnConfigGroup());
return storage;
}

View File

@ -0,0 +1,80 @@
package com.sjhy.plugin.factory;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.ui.JBColor;
import com.intellij.ui.components.JBTextField;
import javax.swing.*;
import javax.swing.table.TableCellEditor;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.stream.Stream;
/**
* 表格编辑器构建工厂类
*
* @author makejava
* @version 1.0.0
* @date 2021/08/10 13:38
*/
public class CellEditorFactory {
/**
* 创建下拉框编辑器
*
* @param editable 可编辑的
* @return {@link TableCellEditor}
*/
public static TableCellEditor createComboBoxEditor(boolean editable, Class<? extends Enum> enumCls) {
Enum[] enumConstants = enumCls.getEnumConstants();
return createComboBoxEditor(editable, Stream.of(enumConstants).map(Enum::name).toArray(value -> new String[enumConstants.length]));
}
/**
* 创建下拉框编辑器
*
* @param editable 可编辑的
* @param items 选项
* @return {@link TableCellEditor}
*/
public static TableCellEditor createComboBoxEditor(boolean editable, String... items) {
ComboBox<String> comboBox = new ComboBox<>(items);
comboBox.setEditable(editable);
// 配色
if (comboBox.getPopup() != null) {
comboBox.getPopup().getList().setBackground(JBColor.WHITE);
comboBox.getPopup().getList().setForeground(JBColor.GREEN);
}
return new DefaultCellEditor(comboBox);
}
/**
* 创建文本框编辑器
*
* @return {@link TableCellEditor}
*/
public static TableCellEditor createTextFieldEditor() {
JBTextField textField = new JBTextField();
// 添加失去焦点事件
textField.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
}
@Override
public void focusLost(FocusEvent e) {
// 失去焦点时向上层发起事件通知使table的值能够正常回写
ActionListener[] actionListeners = textField.getActionListeners();
if (actionListeners == null) {
return;
}
for (ActionListener actionListener : actionListeners) {
actionListener.actionPerformed(null);
}
}
});
return new DefaultCellEditor(textField);
}
}

View File

@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.sjhy.plugin.ui.ColumnConfigSettingForm">
<grid id="27dc6" binding="mainPanel" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="22699" layout-manager="GridLayoutManager" row-count="1" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints border-constraint="North"/>
<properties/>
<border type="none"/>
<children>
<component id="2eb33" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Group Name:"/>
</properties>
</component>
<component id="48b8d" class="javax.swing.JComboBox" binding="groupComboBox">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<grid id="530db" binding="groupOperatorPanel" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<hspacer id="40f2c">
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
</children>
</grid>
</children>
</grid>
</form>

View File

@ -0,0 +1,186 @@
package com.sjhy.plugin.ui;
import com.fasterxml.jackson.core.type.TypeReference;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.ui.InputValidator;
import com.intellij.openapi.ui.Messages;
import com.intellij.ui.ToolbarDecorator;
import com.sjhy.plugin.dict.GlobalDict;
import com.sjhy.plugin.dto.SettingsStorageDTO;
import com.sjhy.plugin.entity.ColumnConfig;
import com.sjhy.plugin.entity.ColumnConfigGroup;
import com.sjhy.plugin.enums.ColumnConfigType;
import com.sjhy.plugin.factory.CellEditorFactory;
import com.sjhy.plugin.tool.CloneUtils;
import com.sjhy.plugin.tool.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import javax.swing.table.TableCellEditor;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
/**
* @author makejava
* @version 1.0.0
* @date 2021/08/10 13:27
*/
public class ColumnConfigSettingForm implements Configurable, BaseSettings {
private JPanel mainPanel;
private JComboBox<String> groupComboBox;
private JPanel groupOperatorPanel;
/**
* 列配置
*/
private Map<String, ColumnConfigGroup> columnConfigGroupMap;
/**
* 当前分组名
*/
private ColumnConfigGroup currColumnConfigGroup;
/**
* 表格组件
*/
private TableComponent<ColumnConfig> tableComponent;
private boolean refresh;
private void initTable() {
// 第一列类型
TableCellEditor typeEditor = CellEditorFactory.createComboBoxEditor(false, ColumnConfigType.class);
TableComponent.Column<ColumnConfig> typeColumn = new TableComponent.Column<>("type", item -> item.getType().name(), (entity, val) -> entity.setType(ColumnConfigType.valueOf(val)), typeEditor);
// 第二列标题
TableCellEditor titleEditor = CellEditorFactory.createTextFieldEditor();
TableComponent.Column<ColumnConfig> titleColumn = new TableComponent.Column<>("title", ColumnConfig::getTitle, ColumnConfig::setTitle, titleEditor);
// 第三列选项
TableCellEditor selectValueEditor = CellEditorFactory.createTextFieldEditor();
TableComponent.Column<ColumnConfig> selectValueColumn = new TableComponent.Column<>("selectValue", ColumnConfig::getSelectValue, ColumnConfig::setSelectValue, selectValueEditor);
List<TableComponent.Column<ColumnConfig>> columns = Arrays.asList(typeColumn, titleColumn, selectValueColumn);
this.tableComponent = new TableComponent<>(columns, Collections.emptyList(), () -> new ColumnConfig("demo", ColumnConfigType.TEXT));
final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(this.tableComponent.getTable());
// 表格初始化
this.mainPanel.add(decorator.createPanel(), BorderLayout.CENTER);
}
private void initPanel(SettingsStorageDTO settingsStorage) {
// 初始化表格
this.initTable();
// 分组操作
DefaultActionGroup groupAction = new DefaultActionGroup(Arrays.asList(new AnAction(AllIcons.Actions.Copy) {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
String value = Messages.showInputDialog("Group Name:", "Input Group Name:", Messages.getQuestionIcon(), currColumnConfigGroup.getName() + " Copy", new InputValidator() {
@Override
public boolean checkInput(String inputString) {
return !StringUtils.isEmpty(inputString) && !columnConfigGroupMap.containsKey(inputString);
}
@Override
public boolean canClose(String inputString) {
return this.checkInput(inputString);
}
});
if (value == null) {
return;
}
// 克隆对象
ColumnConfigGroup columnConfigGroup = CloneUtils.cloneByJson(columnConfigGroupMap.get(currColumnConfigGroup.getName()));
columnConfigGroup.setName(value);
settingsStorage.getColumnConfigGroupMap().put(value, columnConfigGroup);
columnConfigGroupMap.put(value, columnConfigGroup);
currColumnConfigGroup = columnConfigGroup;
refreshUiVal();
}
}, new AnAction(AllIcons.General.Remove) {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
columnConfigGroupMap.remove(currColumnConfigGroup.getName());
currColumnConfigGroup = columnConfigGroupMap.get(GlobalDict.DEFAULT_GROUP_NAME);
refreshUiVal();
}
}));
ActionToolbar groupActionToolbar = ActionManager.getInstance().createActionToolbar("Group Toolbar", groupAction, true);
this.groupOperatorPanel.add(groupActionToolbar.getComponent());
this.groupComboBox.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (refresh) {
return;
}
String groupName = (String) groupComboBox.getSelectedItem();
if (StringUtils.isEmpty(groupName)) {
return;
}
currColumnConfigGroup = columnConfigGroupMap.get(groupName);
refreshUiVal();
}
});
this.loadSettingsStore(settingsStorage);
}
@Override
public String getDisplayName() {
return "Column Config";
}
@Nullable
@Override
public String getHelpTopic() {
return getDisplayName();
}
@Override
public @Nullable JComponent createComponent() {
this.initPanel(getSettingsStorage());
return mainPanel;
}
@Override
public boolean isModified() {
return !this.columnConfigGroupMap.equals(getSettingsStorage().getColumnConfigGroupMap())
|| !getSettingsStorage().getCurrColumnConfigGroupName().equals(this.currColumnConfigGroup.getName());
}
@Override
public void apply() throws ConfigurationException {
getSettingsStorage().setColumnConfigGroupMap(this.columnConfigGroupMap);
getSettingsStorage().setCurrColumnConfigGroupName(this.currColumnConfigGroup.getName());
// 保存包后重新加载配置
this.loadSettingsStore(getSettingsStorage());
}
/**
* 加载配置信息
*
* @param settingsStorage 配置信息
*/
@Override
public void loadSettingsStore(SettingsStorageDTO settingsStorage) {
// 复制配置防止篡改
this.columnConfigGroupMap = CloneUtils.cloneByJson(settingsStorage.getColumnConfigGroupMap(), new TypeReference<Map<String, ColumnConfigGroup>>() {
});
this.currColumnConfigGroup = this.columnConfigGroupMap.get(settingsStorage.getCurrColumnConfigGroupName());
this.refreshUiVal();
}
private void refreshUiVal() {
this.refresh = true;
if (this.tableComponent != null) {
this.tableComponent.setDataList(this.currColumnConfigGroup.getElementList());
}
this.groupComboBox.removeAllItems();
for (String key : this.columnConfigGroupMap.keySet()) {
this.groupComboBox.addItem(key);
}
this.groupComboBox.setSelectedItem(this.currColumnConfigGroup.getName());
this.refresh = false;
}
}

View File

@ -77,7 +77,7 @@ public class MainSettingForm implements Configurable, Configurable.Composite, Ba
public Configurable @NotNull [] getConfigurables() {
this.childConfigurableArray = new Configurable[]{
new TypeMapperSettingForm(),
// new TemplateSettingPanel(),
new ColumnConfigSettingForm(),
// new TableSettingPanel(),
// new GlobalConfigSettingPanel()
};

View File

@ -5,32 +5,28 @@ import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.InputValidator;
import com.intellij.openapi.ui.Messages;
import com.intellij.ui.JBColor;
import com.intellij.ui.ToolbarDecorator;
import com.intellij.ui.components.JBTextField;
import com.sjhy.plugin.dict.GlobalDict;
import com.sjhy.plugin.dto.SettingsStorageDTO;
import com.sjhy.plugin.entity.TypeMapper;
import com.sjhy.plugin.entity.TypeMapperGroup;
import com.sjhy.plugin.enums.MatchType;
import com.sjhy.plugin.factory.CellEditorFactory;
import com.sjhy.plugin.tool.CloneUtils;
import com.sjhy.plugin.tool.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import javax.swing.table.TableCellEditor;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
/**
* @author makejava
@ -58,36 +54,18 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
private void initTable() {
// 第一列仅适用下拉框
ComboBox<String> matchTypeField = new ComboBox<>(Stream.of(MatchType.values()).map(Enum::name).toArray(value -> new String[2]));
if (matchTypeField.getPopup() != null) {
matchTypeField.getPopup().getList().setBackground(JBColor.WHITE);
matchTypeField.getPopup().getList().setForeground(JBColor.GREEN);
}
TableComponent.Column<TypeMapper> matchTypeColumn = new TableComponent.Column<>("matchType", item -> item.getMatchType().name(), (entity, val) -> {
entity.setMatchType(MatchType.valueOf(val));
}, new DefaultCellEditor(matchTypeField));
TableCellEditor matchTypeEditor = CellEditorFactory.createComboBoxEditor(false, MatchType.class);
TableComponent.Column<TypeMapper> matchTypeColumn = new TableComponent.Column<>("matchType",
item -> item.getMatchType().name(),
(entity, val) -> entity.setMatchType(MatchType.valueOf(val)),
matchTypeEditor
);
// 第二列监听输入状态及时修改属性值
JBTextField textField = new JBTextField();
textField.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
}
@Override
public void focusLost(FocusEvent e) {
// 失去焦点时向上层发起事件通知使table的值能够正常回写
textField.getActionListeners()[0].actionPerformed(null);
}
});
TableComponent.Column<TypeMapper> columnTypeColumn = new TableComponent.Column<>("columnType", TypeMapper::getColumnType, TypeMapper::setColumnType, new DefaultCellEditor(textField));
TableCellEditor columnTypeEditor = CellEditorFactory.createTextFieldEditor();
TableComponent.Column<TypeMapper> columnTypeColumn = new TableComponent.Column<>("columnType", TypeMapper::getColumnType, TypeMapper::setColumnType, columnTypeEditor);
// 第三列支持下拉框
ComboBox<String> javaTypeField = new ComboBox<>(GlobalDict.DEFAULT_JAVA_TYPE_LIST);
if (javaTypeField.getPopup() != null) {
javaTypeField.getPopup().getList().setBackground(JBColor.WHITE);
javaTypeField.getPopup().getList().setForeground(JBColor.GREEN);
}
javaTypeField.setEditable(true);
TableComponent.Column<TypeMapper> javaTypeColumn = new TableComponent.Column<>("javaType", TypeMapper::getJavaType, TypeMapper::setJavaType, new DefaultCellEditor(javaTypeField));
TableCellEditor javaTypeEditor = CellEditorFactory.createComboBoxEditor(true, GlobalDict.DEFAULT_JAVA_TYPE_LIST);
TableComponent.Column<TypeMapper> javaTypeColumn = new TableComponent.Column<>("javaType", TypeMapper::getJavaType, TypeMapper::setJavaType, javaTypeEditor);
List<TableComponent.Column<TypeMapper>> columns = Arrays.asList(matchTypeColumn, columnTypeColumn, javaTypeColumn);
this.tableComponent = new TableComponent<>(columns, Collections.emptyList(), () -> new TypeMapper("demo", "java.lang.String"));
final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(this.tableComponent.getTable());