提取分组操作的公共代码

This commit is contained in:
makejava 2021-08-10 15:59:29 +08:00
parent 4078fee1c5
commit 776f8386d8
10 changed files with 324 additions and 175 deletions

View File

@ -37,4 +37,8 @@ public class ColumnConfig {
this.type = type; this.type = type;
this.selectValue = selectValue; this.selectValue = selectValue;
} }
public static ColumnConfig defaultVal() {
return new ColumnConfig("demo", ColumnConfigType.TEXT);
}
} }

View File

@ -32,4 +32,8 @@ public class TypeMapper {
this.columnType = columnType; this.columnType = columnType;
this.javaType = javaType; this.javaType = javaType;
} }
public static TypeMapper defaultVal() {
return new TypeMapper("demo", "java.lang.String");
}
} }

View File

@ -46,6 +46,9 @@ public class CellEditorFactory {
comboBox.getPopup().getList().setBackground(JBColor.WHITE); comboBox.getPopup().getList().setBackground(JBColor.WHITE);
comboBox.getPopup().getList().setForeground(JBColor.GREEN); comboBox.getPopup().getList().setForeground(JBColor.GREEN);
} }
if (!editable) {
transmitFocusEvent(comboBox);
}
return new DefaultCellEditor(comboBox); return new DefaultCellEditor(comboBox);
} }
@ -56,16 +59,26 @@ public class CellEditorFactory {
*/ */
public static TableCellEditor createTextFieldEditor() { public static TableCellEditor createTextFieldEditor() {
JBTextField textField = new JBTextField(); JBTextField textField = new JBTextField();
// 添加失去焦点事件 transmitFocusEvent(textField);
textField.addFocusListener(new FocusListener() { return new DefaultCellEditor(textField);
}
/**
* 传递失去焦点事件
*
* @param component 组件
*/
private static void transmitFocusEvent(JComponent component) {
component.addFocusListener(new FocusListener() {
@Override @Override
public void focusGained(FocusEvent e) { public void focusGained(FocusEvent e) {
} }
@Override @Override
public void focusLost(FocusEvent e) { public void focusLost(FocusEvent e) {
// 失去焦点时向上层发起事件通知使table的值能够正常回写 // 失去焦点时向上层发起事件通知使table的值能够正常回写
ActionListener[] actionListeners = textField.getActionListeners(); ActionListener[] actionListeners = component.getListeners(ActionListener.class);
if (actionListeners == null) { if (actionListeners == null) {
return; return;
} }
@ -74,7 +87,6 @@ public class CellEditorFactory {
} }
} }
}); });
return new DefaultCellEditor(textField);
} }
} }

View File

@ -7,7 +7,7 @@
<properties/> <properties/>
<border type="none"/> <border type="none"/>
<children> <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"> <grid id="22699" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints border-constraint="North"/> <constraints border-constraint="North"/>
<properties/> <properties/>
@ -21,15 +21,9 @@
<text value="Group Name:"/> <text value="Group Name:"/>
</properties> </properties>
</component> </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"> <grid id="530db" binding="groupOperatorPanel" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1">
<constraints> <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"/> <grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
<properties/> <properties/>
<border type="none"/> <border type="none"/>
@ -37,7 +31,7 @@
</grid> </grid>
<hspacer id="40f2c"> <hspacer id="40f2c">
<constraints> <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"/> <grid row="0" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
</hspacer> </hspacer>
</children> </children>

View File

@ -1,12 +1,8 @@
package com.sjhy.plugin.ui; package com.sjhy.plugin.ui;
import com.fasterxml.jackson.core.type.TypeReference; 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.Configurable;
import com.intellij.openapi.options.ConfigurationException; 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.intellij.ui.ToolbarDecorator;
import com.sjhy.plugin.dict.GlobalDict; import com.sjhy.plugin.dict.GlobalDict;
import com.sjhy.plugin.dto.SettingsStorageDTO; import com.sjhy.plugin.dto.SettingsStorageDTO;
@ -15,19 +11,17 @@ import com.sjhy.plugin.entity.ColumnConfigGroup;
import com.sjhy.plugin.enums.ColumnConfigType; import com.sjhy.plugin.enums.ColumnConfigType;
import com.sjhy.plugin.factory.CellEditorFactory; import com.sjhy.plugin.factory.CellEditorFactory;
import com.sjhy.plugin.tool.CloneUtils; import com.sjhy.plugin.tool.CloneUtils;
import com.sjhy.plugin.tool.StringUtils; import com.sjhy.plugin.ui.component.GroupNameComponent;
import org.jetbrains.annotations.NotNull; import com.sjhy.plugin.ui.component.TableComponent;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import javax.swing.*; import javax.swing.*;
import javax.swing.table.TableCellEditor; import javax.swing.table.TableCellEditor;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.*;
import java.util.stream.Stream; import java.util.function.BiConsumer;
import java.util.function.Consumer;
/** /**
* @author makejava * @author makejava
@ -36,7 +30,6 @@ import java.util.stream.Stream;
*/ */
public class ColumnConfigSettingForm implements Configurable, BaseSettings { public class ColumnConfigSettingForm implements Configurable, BaseSettings {
private JPanel mainPanel; private JPanel mainPanel;
private JComboBox<String> groupComboBox;
private JPanel groupOperatorPanel; private JPanel groupOperatorPanel;
/** /**
* 列配置 * 列配置
@ -50,8 +43,10 @@ public class ColumnConfigSettingForm implements Configurable, BaseSettings {
* 表格组件 * 表格组件
*/ */
private TableComponent<ColumnConfig> tableComponent; private TableComponent<ColumnConfig> tableComponent;
/**
private boolean refresh; * 分组操作组件
*/
private GroupNameComponent groupNameComponent;
private void initTable() { private void initTable() {
// 第一列类型 // 第一列类型
@ -64,66 +59,51 @@ public class ColumnConfigSettingForm implements Configurable, BaseSettings {
TableCellEditor selectValueEditor = CellEditorFactory.createTextFieldEditor(); TableCellEditor selectValueEditor = CellEditorFactory.createTextFieldEditor();
TableComponent.Column<ColumnConfig> selectValueColumn = new TableComponent.Column<>("selectValue", ColumnConfig::getSelectValue, ColumnConfig::setSelectValue, selectValueEditor); TableComponent.Column<ColumnConfig> selectValueColumn = new TableComponent.Column<>("selectValue", ColumnConfig::getSelectValue, ColumnConfig::setSelectValue, selectValueEditor);
List<TableComponent.Column<ColumnConfig>> columns = Arrays.asList(typeColumn, titleColumn, selectValueColumn); List<TableComponent.Column<ColumnConfig>> columns = Arrays.asList(typeColumn, titleColumn, selectValueColumn);
this.tableComponent = new TableComponent<>(columns, Collections.emptyList(), () -> new ColumnConfig("demo", ColumnConfigType.TEXT)); this.tableComponent = new TableComponent<>(columns, ColumnConfig::defaultVal);
final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(this.tableComponent.getTable()); final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(this.tableComponent.getTable());
// 表格初始化 // 表格初始化
this.mainPanel.add(decorator.createPanel(), BorderLayout.CENTER); this.mainPanel.add(decorator.createPanel(), BorderLayout.CENTER);
} }
private void initPanel(SettingsStorageDTO settingsStorage) { private void initGroupName() {
BiConsumer<String, String> copyOperator = (groupName, oldGroupName) -> {
// 克隆对象
ColumnConfigGroup columnConfigGroup = CloneUtils.cloneByJson(columnConfigGroupMap.get(oldGroupName));
columnConfigGroup.setName(groupName);
columnConfigGroupMap.put(groupName, columnConfigGroup);
currColumnConfigGroup = columnConfigGroup;
refreshUiVal();
};
Consumer<String> addOperator = groupName -> {
ColumnConfigGroup columnConfigGroup = new ColumnConfigGroup();
columnConfigGroup.setName(groupName);
columnConfigGroup.setElementList(new ArrayList<>());
columnConfigGroup.getElementList().add(ColumnConfig.defaultVal());
columnConfigGroupMap.put(groupName, columnConfigGroup);
currColumnConfigGroup = columnConfigGroup;
refreshUiVal();
};
Consumer<String> switchGroupOperator = groupName -> {
currColumnConfigGroup = columnConfigGroupMap.get(groupName);
refreshUiVal();
};
Consumer<String> deleteOperator = groupName -> {
columnConfigGroupMap.remove(currColumnConfigGroup.getName());
currColumnConfigGroup = columnConfigGroupMap.get(GlobalDict.DEFAULT_GROUP_NAME);
refreshUiVal();
};
this.groupNameComponent = new GroupNameComponent(copyOperator, addOperator, deleteOperator, switchGroupOperator);
this.groupOperatorPanel.add(groupNameComponent.getPanel());
}
private void initPanel() {
// 初始化表格 // 初始化表格
this.initTable(); this.initTable();
// 分组操作 this.initGroupName();
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 @Override
@ -139,7 +119,8 @@ public class ColumnConfigSettingForm implements Configurable, BaseSettings {
@Override @Override
public @Nullable JComponent createComponent() { public @Nullable JComponent createComponent() {
this.initPanel(getSettingsStorage()); this.initPanel();
this.loadSettingsStore(getSettingsStorage());
return mainPanel; return mainPanel;
} }
@ -168,19 +149,19 @@ public class ColumnConfigSettingForm implements Configurable, BaseSettings {
this.columnConfigGroupMap = CloneUtils.cloneByJson(settingsStorage.getColumnConfigGroupMap(), new TypeReference<Map<String, ColumnConfigGroup>>() { this.columnConfigGroupMap = CloneUtils.cloneByJson(settingsStorage.getColumnConfigGroupMap(), new TypeReference<Map<String, ColumnConfigGroup>>() {
}); });
this.currColumnConfigGroup = this.columnConfigGroupMap.get(settingsStorage.getCurrColumnConfigGroupName()); this.currColumnConfigGroup = this.columnConfigGroupMap.get(settingsStorage.getCurrColumnConfigGroupName());
if (this.currColumnConfigGroup == null) {
this.currColumnConfigGroup = this.columnConfigGroupMap.get(GlobalDict.DEFAULT_GROUP_NAME);
}
this.refreshUiVal(); this.refreshUiVal();
} }
private void refreshUiVal() { private void refreshUiVal() {
this.refresh = true;
if (this.tableComponent != null) { if (this.tableComponent != null) {
this.tableComponent.setDataList(this.currColumnConfigGroup.getElementList()); this.tableComponent.setDataList(this.currColumnConfigGroup.getElementList());
} }
this.groupComboBox.removeAllItems(); if (this.groupNameComponent != null) {
for (String key : this.columnConfigGroupMap.keySet()) { this.groupNameComponent.setAllGroupNames(this.columnConfigGroupMap.keySet());
this.groupComboBox.addItem(key); this.groupNameComponent.setCurrGroupName(this.currColumnConfigGroup.getName());
} }
this.groupComboBox.setSelectedItem(this.currColumnConfigGroup.getName());
this.refresh = false;
} }
} }

View File

@ -7,7 +7,7 @@
<properties/> <properties/>
<border type="line"/> <border type="line"/>
<children> <children>
<grid id="4186a" layout-manager="GridLayoutManager" row-count="1" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <grid id="4186a" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints border-constraint="North"/> <constraints border-constraint="North"/>
<properties/> <properties/>
@ -21,25 +21,19 @@
<text value="Group Name:"/> <text value="Group Name:"/>
</properties> </properties>
</component> </component>
<hspacer id="30d98">
<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>
<grid id="b788" binding="groupOperatorPanel" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1"> <grid id="b788" binding="groupOperatorPanel" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1">
<constraints> <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"/> <grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
<properties/> <properties/>
<border type="none"/> <border type="none"/>
<children/> <children/>
</grid> </grid>
<component id="b7ffd" class="javax.swing.JComboBox" binding="groupComboBox"> <hspacer id="b9278">
<constraints> <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"/> <grid row="0" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
<properties/> </hspacer>
</component>
</children> </children>
</grid> </grid>
</children> </children>

View File

@ -1,12 +1,8 @@
package com.sjhy.plugin.ui; package com.sjhy.plugin.ui;
import com.fasterxml.jackson.core.type.TypeReference; 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.Configurable;
import com.intellij.openapi.options.ConfigurationException; 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.intellij.ui.ToolbarDecorator;
import com.sjhy.plugin.dict.GlobalDict; import com.sjhy.plugin.dict.GlobalDict;
import com.sjhy.plugin.dto.SettingsStorageDTO; import com.sjhy.plugin.dto.SettingsStorageDTO;
@ -15,18 +11,17 @@ import com.sjhy.plugin.entity.TypeMapperGroup;
import com.sjhy.plugin.enums.MatchType; import com.sjhy.plugin.enums.MatchType;
import com.sjhy.plugin.factory.CellEditorFactory; import com.sjhy.plugin.factory.CellEditorFactory;
import com.sjhy.plugin.tool.CloneUtils; import com.sjhy.plugin.tool.CloneUtils;
import com.sjhy.plugin.tool.StringUtils; import com.sjhy.plugin.ui.component.GroupNameComponent;
import org.jetbrains.annotations.NotNull; import com.sjhy.plugin.ui.component.TableComponent;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import javax.swing.*; import javax.swing.*;
import javax.swing.table.TableCellEditor; import javax.swing.table.TableCellEditor;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
/** /**
* @author makejava * @author makejava
@ -35,7 +30,6 @@ import java.util.Map;
*/ */
public class TypeMapperSettingForm implements Configurable, BaseSettings { public class TypeMapperSettingForm implements Configurable, BaseSettings {
private JPanel mainPanel; private JPanel mainPanel;
private JComboBox<String> groupComboBox;
private JPanel groupOperatorPanel; private JPanel groupOperatorPanel;
/** /**
* 类型映射配置 * 类型映射配置
@ -49,8 +43,10 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
* 表格组件 * 表格组件
*/ */
private TableComponent<TypeMapper> tableComponent; private TableComponent<TypeMapper> tableComponent;
/**
private boolean refresh; * 分组操作组件
*/
private GroupNameComponent groupNameComponent;
private void initTable() { private void initTable() {
// 第一列仅适用下拉框 // 第一列仅适用下拉框
@ -67,66 +63,51 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
TableCellEditor javaTypeEditor = CellEditorFactory.createComboBoxEditor(true, GlobalDict.DEFAULT_JAVA_TYPE_LIST); TableCellEditor javaTypeEditor = CellEditorFactory.createComboBoxEditor(true, GlobalDict.DEFAULT_JAVA_TYPE_LIST);
TableComponent.Column<TypeMapper> javaTypeColumn = new TableComponent.Column<>("javaType", TypeMapper::getJavaType, TypeMapper::setJavaType, javaTypeEditor); TableComponent.Column<TypeMapper> javaTypeColumn = new TableComponent.Column<>("javaType", TypeMapper::getJavaType, TypeMapper::setJavaType, javaTypeEditor);
List<TableComponent.Column<TypeMapper>> columns = Arrays.asList(matchTypeColumn, columnTypeColumn, javaTypeColumn); List<TableComponent.Column<TypeMapper>> columns = Arrays.asList(matchTypeColumn, columnTypeColumn, javaTypeColumn);
this.tableComponent = new TableComponent<>(columns, Collections.emptyList(), () -> new TypeMapper("demo", "java.lang.String")); this.tableComponent = new TableComponent<>(columns, Collections.emptyList(), TypeMapper::defaultVal);
final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(this.tableComponent.getTable()); final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(this.tableComponent.getTable());
// 表格初始化 // 表格初始化
this.mainPanel.add(decorator.createPanel(), BorderLayout.CENTER); this.mainPanel.add(decorator.createPanel(), BorderLayout.CENTER);
} }
private void initPanel(SettingsStorageDTO settingsStorage) { private void initGroupName() {
BiConsumer<String, String> copyOperator = (groupName, oldGroupName) -> {
// 克隆对象
TypeMapperGroup typeMapperGroup = CloneUtils.cloneByJson(typeMapperGroupMap.get(oldGroupName));
typeMapperGroup.setName(groupName);
typeMapperGroupMap.put(groupName, typeMapperGroup);
currTypeMapperGroup = typeMapperGroup;
refreshUiVal();
};
Consumer<String> addOperator = groupName -> {
TypeMapperGroup typeMapperGroup = new TypeMapperGroup();
typeMapperGroup.setName(groupName);
typeMapperGroup.setElementList(new ArrayList<>());
typeMapperGroup.getElementList().add(TypeMapper.defaultVal());
typeMapperGroupMap.put(groupName, typeMapperGroup);
currTypeMapperGroup = typeMapperGroup;
refreshUiVal();
};
Consumer<String> deleteOperator = groupName -> {
typeMapperGroupMap.remove(currTypeMapperGroup.getName());
currTypeMapperGroup = typeMapperGroupMap.get(GlobalDict.DEFAULT_GROUP_NAME);
refreshUiVal();
};
Consumer<String> switchGroupOperator = groupName -> {
currTypeMapperGroup = typeMapperGroupMap.get(groupName);
refreshUiVal();
};
this.groupNameComponent = new GroupNameComponent(copyOperator, addOperator, deleteOperator, switchGroupOperator);
this.groupOperatorPanel.add(groupNameComponent.getPanel());
}
private void initPanel() {
// 初始化表格 // 初始化表格
this.initTable(); this.initTable();
// 分组操作 this.initGroupName();
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(), currTypeMapperGroup.getName() + " Copy", new InputValidator() {
@Override
public boolean checkInput(String inputString) {
return !StringUtils.isEmpty(inputString) && !typeMapperGroupMap.containsKey(inputString);
}
@Override
public boolean canClose(String inputString) {
return this.checkInput(inputString);
}
});
if (value == null) {
return;
}
// 克隆对象
TypeMapperGroup typeMapperGroup = CloneUtils.cloneByJson(typeMapperGroupMap.get(currTypeMapperGroup.getName()));
typeMapperGroup.setName(value);
settingsStorage.getTypeMapperGroupMap().put(value, typeMapperGroup);
typeMapperGroupMap.put(value, typeMapperGroup);
currTypeMapperGroup = typeMapperGroup;
refreshUiVal();
}
}, new AnAction(AllIcons.General.Remove) {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
typeMapperGroupMap.remove(currTypeMapperGroup.getName());
currTypeMapperGroup = typeMapperGroupMap.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;
}
currTypeMapperGroup = typeMapperGroupMap.get(groupName);
refreshUiVal();
}
});
this.loadSettingsStore(settingsStorage);
} }
@Override @Override
@ -142,7 +123,8 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
@Override @Override
public @Nullable JComponent createComponent() { public @Nullable JComponent createComponent() {
this.initPanel(getSettingsStorage()); this.initPanel();
this.loadSettingsStore(getSettingsStorage());
return mainPanel; return mainPanel;
} }
@ -171,19 +153,19 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
this.typeMapperGroupMap = CloneUtils.cloneByJson(settingsStorage.getTypeMapperGroupMap(), new TypeReference<Map<String, TypeMapperGroup>>() { this.typeMapperGroupMap = CloneUtils.cloneByJson(settingsStorage.getTypeMapperGroupMap(), new TypeReference<Map<String, TypeMapperGroup>>() {
}); });
this.currTypeMapperGroup = this.typeMapperGroupMap.get(settingsStorage.getCurrTypeMapperGroupName()); this.currTypeMapperGroup = this.typeMapperGroupMap.get(settingsStorage.getCurrTypeMapperGroupName());
if (this.currTypeMapperGroup == null) {
this.currTypeMapperGroup = this.typeMapperGroupMap.get(GlobalDict.DEFAULT_GROUP_NAME);
}
this.refreshUiVal(); this.refreshUiVal();
} }
private void refreshUiVal() { private void refreshUiVal() {
this.refresh = true;
if (this.tableComponent != null) { if (this.tableComponent != null) {
this.tableComponent.setDataList(this.currTypeMapperGroup.getElementList()); this.tableComponent.setDataList(this.currTypeMapperGroup.getElementList());
} }
this.groupComboBox.removeAllItems(); if (this.groupNameComponent != null) {
for (String key : this.typeMapperGroupMap.keySet()) { this.groupNameComponent.setAllGroupNames(this.typeMapperGroupMap.keySet());
this.groupComboBox.addItem(key); this.groupNameComponent.setCurrGroupName(this.currTypeMapperGroup.getName());
} }
this.groupComboBox.setSelectedItem(this.currTypeMapperGroup.getName());
this.refresh = false;
} }
} }

View File

@ -0,0 +1,163 @@
package com.sjhy.plugin.ui.component;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.InputValidator;
import com.intellij.openapi.ui.Messages;
import com.sjhy.plugin.dict.GlobalDict;
import com.sjhy.plugin.tool.StringUtils;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
/**
* 分组编辑组件
*
* @author makejava
* @version 1.0.0
* @date 2021/08/10 14:13
*/
public class GroupNameComponent {
/**
* 复制分组param1=新分组名 param2=旧分组名
*/
private BiConsumer<String, String> copyGroupFun;
private Consumer<String> createGroupFun;
private Consumer<String> deleteGroupFun;
private Consumer<String> switchGroupFun;
/**
* 所有分组
*/
private Set<String> allGroupNames;
/**
* 当前分组
*/
private String currGroupName;
@Getter
private JPanel panel;
private ComboBox<String> groupComboBox;
private boolean refresh;
public GroupNameComponent(BiConsumer<String, String> copyGroupFun, Consumer<String> createGroupFun, Consumer<String> deleteGroupFun, Consumer<String> switchGroupFun) {
this.copyGroupFun = copyGroupFun;
this.createGroupFun = createGroupFun;
this.deleteGroupFun = deleteGroupFun;
this.switchGroupFun = switchGroupFun;
this.allGroupNames = Collections.emptySet();
this.init();
}
private void inputGroupName(String initValue, Consumer<String> consumer) {
String value = Messages.showInputDialog("Group Name:", "Input Group Name:", Messages.getQuestionIcon(), initValue, new InputValidator() {
@Override
public boolean checkInput(String inputString) {
return !StringUtils.isEmpty(inputString) && !allGroupNames.contains(inputString);
}
@Override
public boolean canClose(String inputString) {
return this.checkInput(inputString);
}
});
if (StringUtils.isEmpty(value)) {
return;
}
consumer.accept(value);
}
private AnAction copyAction() {
return new AnAction(AllIcons.Actions.Copy) {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
inputGroupName(currGroupName + "Copy", groupName -> copyGroupFun.accept(groupName, currGroupName));
}
};
}
private AnAction addAction() {
return new AnAction(AllIcons.General.Add) {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
inputGroupName("GroupName", createGroupFun);
}
};
}
private AnAction removeAction() {
return new AnAction(AllIcons.General.Remove) {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
deleteGroupFun.accept(currGroupName);
}
@Override
public void update(@NotNull AnActionEvent e) {
e.getPresentation().setEnabled(!GlobalDict.DEFAULT_GROUP_NAME.equals(currGroupName));
}
};
}
private void init() {
panel = new JPanel(new BorderLayout());
// 分组操作
DefaultActionGroup groupAction = new DefaultActionGroup(Arrays.asList(this.copyAction(), this.addAction(), this.removeAction()));
ActionToolbar groupActionToolbar = ActionManager.getInstance().createActionToolbar("Group Toolbar", groupAction, true);
this.panel.add(groupActionToolbar.getComponent(), BorderLayout.EAST);
this.groupComboBox = new ComboBox<>(this.allGroupNames.toArray(new String[0]));
this.panel.add(this.groupComboBox, BorderLayout.CENTER);
this.groupComboBox.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (refresh) {
return;
}
String selectedItem = (String) groupComboBox.getSelectedItem();
if (StringUtils.isEmpty(selectedItem)) {
return;
}
switchGroupFun.accept(selectedItem);
}
});
}
public void setAllGroupNames(Set<String> allGroupNames) {
this.allGroupNames = allGroupNames;
try {
this.refresh = true;
this.groupComboBox.removeAllItems();
for (String item : this.allGroupNames) {
this.groupComboBox.addItem(item);
}
} finally {
this.refresh = false;
}
}
public void setCurrGroupName(String currGroupName) {
this.currGroupName = currGroupName;
try {
this.refresh = true;
this.groupComboBox.setSelectedItem(this.currGroupName);
} finally {
this.refresh = false;
}
}
}

View File

@ -1,4 +1,4 @@
package com.sjhy.plugin.ui; package com.sjhy.plugin.ui.component;
import com.intellij.ui.table.JBTable; import com.intellij.ui.table.JBTable;
import com.intellij.util.ui.EditableModel; import com.intellij.util.ui.EditableModel;
@ -10,6 +10,7 @@ import lombok.NonNull;
import javax.swing.*; import javax.swing.*;
import javax.swing.table.DefaultTableModel; import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor; import javax.swing.table.TableCellEditor;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Vector; import java.util.Vector;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
@ -42,6 +43,10 @@ public class TableComponent<T> extends DefaultTableModel implements EditableMode
@Getter @Getter
private JBTable table; private JBTable table;
public TableComponent(@NonNull List<Column<T>> columns, @NonNull Supplier<T> defaultValFun) {
this(columns, Collections.emptyList(), defaultValFun);
}
public TableComponent(@NonNull List<Column<T>> columns, @NonNull List<T> dataList, @NonNull Supplier<T> defaultValFun) { public TableComponent(@NonNull List<Column<T>> columns, @NonNull List<T> dataList, @NonNull Supplier<T> defaultValFun) {
this.columns = columns; this.columns = columns;
this.dataList = dataList; this.dataList = dataList;
@ -85,9 +90,11 @@ public class TableComponent<T> extends DefaultTableModel implements EditableMode
@Override @Override
public void setValueAt(Object value, int row, int column) { public void setValueAt(Object value, int row, int column) {
super.setValueAt(value, row, column); if (row < this.dataList.size()) {
T obj = this.dataList.get(row); super.setValueAt(value, row, column);
this.columns.get(column).getSetFun().accept(obj, (String) value); T obj = this.dataList.get(row);
this.columns.get(column).getSetFun().accept(obj, (String) value);
}
} }
/** /**

View File

@ -0,0 +1,8 @@
/**
* UI组件
*
* @author makejava
* @version 1.0.0
* @date 2021/08/10 14:14
*/
package com.sjhy.plugin.ui.component;