diff --git a/src/main/java/com/sjhy/plugin/dto/SettingsStorageDTO.java b/src/main/java/com/sjhy/plugin/dto/SettingsStorageDTO.java index cfdff33..1ef6d72 100644 --- a/src/main/java/com/sjhy/plugin/dto/SettingsStorageDTO.java +++ b/src/main/java/com/sjhy/plugin/dto/SettingsStorageDTO.java @@ -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; } diff --git a/src/main/java/com/sjhy/plugin/factory/CellEditorFactory.java b/src/main/java/com/sjhy/plugin/factory/CellEditorFactory.java new file mode 100644 index 0000000..8fbe7d5 --- /dev/null +++ b/src/main/java/com/sjhy/plugin/factory/CellEditorFactory.java @@ -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 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 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); + } + +} diff --git a/src/main/java/com/sjhy/plugin/ui/ColumnConfigSettingForm.form b/src/main/java/com/sjhy/plugin/ui/ColumnConfigSettingForm.form new file mode 100644 index 0000000..a849c59 --- /dev/null +++ b/src/main/java/com/sjhy/plugin/ui/ColumnConfigSettingForm.form @@ -0,0 +1,47 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/main/java/com/sjhy/plugin/ui/ColumnConfigSettingForm.java b/src/main/java/com/sjhy/plugin/ui/ColumnConfigSettingForm.java new file mode 100644 index 0000000..b823dc6 --- /dev/null +++ b/src/main/java/com/sjhy/plugin/ui/ColumnConfigSettingForm.java @@ -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 groupComboBox; + private JPanel groupOperatorPanel; + /** + * 列配置 + */ + private Map columnConfigGroupMap; + /** + * 当前分组名 + */ + private ColumnConfigGroup currColumnConfigGroup; + /** + * 表格组件 + */ + private TableComponent tableComponent; + + private boolean refresh; + + private void initTable() { + // 第一列,类型 + TableCellEditor typeEditor = CellEditorFactory.createComboBoxEditor(false, ColumnConfigType.class); + TableComponent.Column typeColumn = new TableComponent.Column<>("type", item -> item.getType().name(), (entity, val) -> entity.setType(ColumnConfigType.valueOf(val)), typeEditor); + // 第二列标题 + TableCellEditor titleEditor = CellEditorFactory.createTextFieldEditor(); + TableComponent.Column titleColumn = new TableComponent.Column<>("title", ColumnConfig::getTitle, ColumnConfig::setTitle, titleEditor); + // 第三列选项 + TableCellEditor selectValueEditor = CellEditorFactory.createTextFieldEditor(); + TableComponent.Column selectValueColumn = new TableComponent.Column<>("selectValue", ColumnConfig::getSelectValue, ColumnConfig::setSelectValue, selectValueEditor); + List> 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>() { + }); + 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; + } +} diff --git a/src/main/java/com/sjhy/plugin/ui/MainSettingForm.java b/src/main/java/com/sjhy/plugin/ui/MainSettingForm.java index 1dd0609..dc9962f 100644 --- a/src/main/java/com/sjhy/plugin/ui/MainSettingForm.java +++ b/src/main/java/com/sjhy/plugin/ui/MainSettingForm.java @@ -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() }; diff --git a/src/main/java/com/sjhy/plugin/ui/TypeMapperSettingForm.java b/src/main/java/com/sjhy/plugin/ui/TypeMapperSettingForm.java index b774710..d44ce83 100644 --- a/src/main/java/com/sjhy/plugin/ui/TypeMapperSettingForm.java +++ b/src/main/java/com/sjhy/plugin/ui/TypeMapperSettingForm.java @@ -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 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 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 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 columnTypeColumn = new TableComponent.Column<>("columnType", TypeMapper::getColumnType, TypeMapper::setColumnType, new DefaultCellEditor(textField)); + TableCellEditor columnTypeEditor = CellEditorFactory.createTextFieldEditor(); + TableComponent.Column columnTypeColumn = new TableComponent.Column<>("columnType", TypeMapper::getColumnType, TypeMapper::setColumnType, columnTypeEditor); // 第三列支持下拉框 - ComboBox 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 javaTypeColumn = new TableComponent.Column<>("javaType", TypeMapper::getJavaType, TypeMapper::setJavaType, new DefaultCellEditor(javaTypeField)); + TableCellEditor javaTypeEditor = CellEditorFactory.createComboBoxEditor(true, GlobalDict.DEFAULT_JAVA_TYPE_LIST); + TableComponent.Column javaTypeColumn = new TableComponent.Column<>("javaType", TypeMapper::getJavaType, TypeMapper::setJavaType, javaTypeEditor); List> 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());