mirror of https://gitee.com/makejava/EasyCode.git
提取分组操作的公共代码
This commit is contained in:
parent
4078fee1c5
commit
776f8386d8
|
@ -37,4 +37,8 @@ public class ColumnConfig {
|
|||
this.type = type;
|
||||
this.selectValue = selectValue;
|
||||
}
|
||||
|
||||
public static ColumnConfig defaultVal() {
|
||||
return new ColumnConfig("demo", ColumnConfigType.TEXT);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,4 +32,8 @@ public class TypeMapper {
|
|||
this.columnType = columnType;
|
||||
this.javaType = javaType;
|
||||
}
|
||||
|
||||
public static TypeMapper defaultVal() {
|
||||
return new TypeMapper("demo", "java.lang.String");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,6 +46,9 @@ public class CellEditorFactory {
|
|||
comboBox.getPopup().getList().setBackground(JBColor.WHITE);
|
||||
comboBox.getPopup().getList().setForeground(JBColor.GREEN);
|
||||
}
|
||||
if (!editable) {
|
||||
transmitFocusEvent(comboBox);
|
||||
}
|
||||
return new DefaultCellEditor(comboBox);
|
||||
}
|
||||
|
||||
|
@ -56,16 +59,26 @@ public class CellEditorFactory {
|
|||
*/
|
||||
public static TableCellEditor createTextFieldEditor() {
|
||||
JBTextField textField = new JBTextField();
|
||||
// 添加失去焦点事件
|
||||
textField.addFocusListener(new FocusListener() {
|
||||
transmitFocusEvent(textField);
|
||||
return new DefaultCellEditor(textField);
|
||||
}
|
||||
|
||||
/**
|
||||
* 传递失去焦点事件
|
||||
*
|
||||
* @param component 组件
|
||||
*/
|
||||
private static void transmitFocusEvent(JComponent component) {
|
||||
component.addFocusListener(new FocusListener() {
|
||||
@Override
|
||||
public void focusGained(FocusEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void focusLost(FocusEvent e) {
|
||||
// 失去焦点时向上层发起事件通知,使table的值能够正常回写
|
||||
ActionListener[] actionListeners = textField.getActionListeners();
|
||||
ActionListener[] actionListeners = component.getListeners(ActionListener.class);
|
||||
if (actionListeners == null) {
|
||||
return;
|
||||
}
|
||||
|
@ -74,7 +87,6 @@ public class CellEditorFactory {
|
|||
}
|
||||
}
|
||||
});
|
||||
return new DefaultCellEditor(textField);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<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">
|
||||
<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"/>
|
||||
<constraints border-constraint="North"/>
|
||||
<properties/>
|
||||
|
@ -21,15 +21,9 @@
|
|||
<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"/>
|
||||
<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>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
|
@ -37,7 +31,7 @@
|
|||
</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"/>
|
||||
<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>
|
||||
</hspacer>
|
||||
</children>
|
||||
|
|
|
@ -1,12 +1,8 @@
|
|||
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;
|
||||
|
@ -15,19 +11,17 @@ 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 com.sjhy.plugin.ui.component.GroupNameComponent;
|
||||
import com.sjhy.plugin.ui.component.TableComponent;
|
||||
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;
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* @author makejava
|
||||
|
@ -36,7 +30,6 @@ import java.util.stream.Stream;
|
|||
*/
|
||||
public class ColumnConfigSettingForm implements Configurable, BaseSettings {
|
||||
private JPanel mainPanel;
|
||||
private JComboBox<String> groupComboBox;
|
||||
private JPanel groupOperatorPanel;
|
||||
/**
|
||||
* 列配置
|
||||
|
@ -50,8 +43,10 @@ public class ColumnConfigSettingForm implements Configurable, BaseSettings {
|
|||
* 表格组件
|
||||
*/
|
||||
private TableComponent<ColumnConfig> tableComponent;
|
||||
|
||||
private boolean refresh;
|
||||
/**
|
||||
* 分组操作组件
|
||||
*/
|
||||
private GroupNameComponent groupNameComponent;
|
||||
|
||||
private void initTable() {
|
||||
// 第一列,类型
|
||||
|
@ -64,66 +59,51 @@ public class ColumnConfigSettingForm implements Configurable, BaseSettings {
|
|||
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));
|
||||
this.tableComponent = new TableComponent<>(columns, ColumnConfig::defaultVal);
|
||||
final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(this.tableComponent.getTable());
|
||||
// 表格初始化
|
||||
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();
|
||||
// 分组操作
|
||||
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);
|
||||
this.initGroupName();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -139,7 +119,8 @@ public class ColumnConfigSettingForm implements Configurable, BaseSettings {
|
|||
|
||||
@Override
|
||||
public @Nullable JComponent createComponent() {
|
||||
this.initPanel(getSettingsStorage());
|
||||
this.initPanel();
|
||||
this.loadSettingsStore(getSettingsStorage());
|
||||
return mainPanel;
|
||||
}
|
||||
|
||||
|
@ -168,19 +149,19 @@ public class ColumnConfigSettingForm implements Configurable, BaseSettings {
|
|||
this.columnConfigGroupMap = CloneUtils.cloneByJson(settingsStorage.getColumnConfigGroupMap(), new TypeReference<Map<String, ColumnConfigGroup>>() {
|
||||
});
|
||||
this.currColumnConfigGroup = this.columnConfigGroupMap.get(settingsStorage.getCurrColumnConfigGroupName());
|
||||
if (this.currColumnConfigGroup == null) {
|
||||
this.currColumnConfigGroup = this.columnConfigGroupMap.get(GlobalDict.DEFAULT_GROUP_NAME);
|
||||
}
|
||||
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);
|
||||
if (this.groupNameComponent != null) {
|
||||
this.groupNameComponent.setAllGroupNames(this.columnConfigGroupMap.keySet());
|
||||
this.groupNameComponent.setCurrGroupName(this.currColumnConfigGroup.getName());
|
||||
}
|
||||
this.groupComboBox.setSelectedItem(this.currColumnConfigGroup.getName());
|
||||
this.refresh = false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<properties/>
|
||||
<border type="line"/>
|
||||
<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"/>
|
||||
<constraints border-constraint="North"/>
|
||||
<properties/>
|
||||
|
@ -21,25 +21,19 @@
|
|||
<text value="Group Name:"/>
|
||||
</properties>
|
||||
</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">
|
||||
<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>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
</grid>
|
||||
<component id="b7ffd" class="javax.swing.JComboBox" binding="groupComboBox">
|
||||
<hspacer id="b9278">
|
||||
<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>
|
||||
<properties/>
|
||||
</component>
|
||||
</hspacer>
|
||||
</children>
|
||||
</grid>
|
||||
</children>
|
||||
|
|
|
@ -1,12 +1,8 @@
|
|||
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;
|
||||
|
@ -15,18 +11,17 @@ 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 com.sjhy.plugin.ui.component.GroupNameComponent;
|
||||
import com.sjhy.plugin.ui.component.TableComponent;
|
||||
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.*;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* @author makejava
|
||||
|
@ -35,7 +30,6 @@ import java.util.Map;
|
|||
*/
|
||||
public class TypeMapperSettingForm implements Configurable, BaseSettings {
|
||||
private JPanel mainPanel;
|
||||
private JComboBox<String> groupComboBox;
|
||||
private JPanel groupOperatorPanel;
|
||||
/**
|
||||
* 类型映射配置
|
||||
|
@ -49,8 +43,10 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
|
|||
* 表格组件
|
||||
*/
|
||||
private TableComponent<TypeMapper> tableComponent;
|
||||
|
||||
private boolean refresh;
|
||||
/**
|
||||
* 分组操作组件
|
||||
*/
|
||||
private GroupNameComponent groupNameComponent;
|
||||
|
||||
private void initTable() {
|
||||
// 第一列仅适用下拉框
|
||||
|
@ -67,66 +63,51 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
|
|||
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"));
|
||||
this.tableComponent = new TableComponent<>(columns, Collections.emptyList(), TypeMapper::defaultVal);
|
||||
final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(this.tableComponent.getTable());
|
||||
// 表格初始化
|
||||
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();
|
||||
// 分组操作
|
||||
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);
|
||||
this.initGroupName();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -142,7 +123,8 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
|
|||
|
||||
@Override
|
||||
public @Nullable JComponent createComponent() {
|
||||
this.initPanel(getSettingsStorage());
|
||||
this.initPanel();
|
||||
this.loadSettingsStore(getSettingsStorage());
|
||||
return mainPanel;
|
||||
}
|
||||
|
||||
|
@ -171,19 +153,19 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
|
|||
this.typeMapperGroupMap = CloneUtils.cloneByJson(settingsStorage.getTypeMapperGroupMap(), new TypeReference<Map<String, TypeMapperGroup>>() {
|
||||
});
|
||||
this.currTypeMapperGroup = this.typeMapperGroupMap.get(settingsStorage.getCurrTypeMapperGroupName());
|
||||
if (this.currTypeMapperGroup == null) {
|
||||
this.currTypeMapperGroup = this.typeMapperGroupMap.get(GlobalDict.DEFAULT_GROUP_NAME);
|
||||
}
|
||||
this.refreshUiVal();
|
||||
}
|
||||
|
||||
private void refreshUiVal() {
|
||||
this.refresh = true;
|
||||
if (this.tableComponent != null) {
|
||||
this.tableComponent.setDataList(this.currTypeMapperGroup.getElementList());
|
||||
}
|
||||
this.groupComboBox.removeAllItems();
|
||||
for (String key : this.typeMapperGroupMap.keySet()) {
|
||||
this.groupComboBox.addItem(key);
|
||||
if (this.groupNameComponent != null) {
|
||||
this.groupNameComponent.setAllGroupNames(this.typeMapperGroupMap.keySet());
|
||||
this.groupNameComponent.setCurrGroupName(this.currTypeMapperGroup.getName());
|
||||
}
|
||||
this.groupComboBox.setSelectedItem(this.currTypeMapperGroup.getName());
|
||||
this.refresh = false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.sjhy.plugin.ui;
|
||||
package com.sjhy.plugin.ui.component;
|
||||
|
||||
import com.intellij.ui.table.JBTable;
|
||||
import com.intellij.util.ui.EditableModel;
|
||||
|
@ -10,6 +10,7 @@ import lombok.NonNull;
|
|||
import javax.swing.*;
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
import javax.swing.table.TableCellEditor;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
import java.util.function.BiConsumer;
|
||||
|
@ -42,6 +43,10 @@ public class TableComponent<T> extends DefaultTableModel implements EditableMode
|
|||
@Getter
|
||||
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) {
|
||||
this.columns = columns;
|
||||
this.dataList = dataList;
|
||||
|
@ -85,9 +90,11 @@ public class TableComponent<T> extends DefaultTableModel implements EditableMode
|
|||
|
||||
@Override
|
||||
public void setValueAt(Object value, int row, int column) {
|
||||
super.setValueAt(value, row, column);
|
||||
T obj = this.dataList.get(row);
|
||||
this.columns.get(column).getSetFun().accept(obj, (String) value);
|
||||
if (row < this.dataList.size()) {
|
||||
super.setValueAt(value, row, column);
|
||||
T obj = this.dataList.get(row);
|
||||
this.columns.get(column).getSetFun().accept(obj, (String) value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
|
@ -0,0 +1,8 @@
|
|||
/**
|
||||
* UI组件
|
||||
*
|
||||
* @author makejava
|
||||
* @version 1.0.0
|
||||
* @date 2021/08/10 14:14
|
||||
*/
|
||||
package com.sjhy.plugin.ui.component;
|
Loading…
Reference in New Issue