对分组操作进行全面抽取公共代码,去除重复代码。

This commit is contained in:
makejava 2021-08-11 11:56:43 +08:00
parent ad7518b76b
commit 525f19a374
23 changed files with 544 additions and 162 deletions

View File

@ -46,10 +46,14 @@ public class SettingsStorageDTO {
storage.columnConfigGroupMap = new HashMap<>(16);
storage.columnConfigGroupMap.put(GlobalDict.DEFAULT_GROUP_NAME, columnConfigGroup);
TemplateGroup templateGroup = new TemplateGroup();
templateGroup.setName(GlobalDict.DEFAULT_GROUP_NAME);
templateGroup.setElementList(Arrays.asList(new Template("demo", "template"), new Template("entity.java", "public")));
storage.templateGroupMap = new HashMap<>(16);
storage.templateGroupMap.put(GlobalDict.DEFAULT_GROUP_NAME, templateGroup);
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());
return storage;
}

View File

@ -1,5 +1,9 @@
package com.sjhy.plugin.entity;
import com.sjhy.plugin.factory.AbstractItemFactory;
import com.sjhy.plugin.tool.CloneUtils;
import com.sjhy.plugin.tool.ReflectionUtils;
import java.util.List;
/**
@ -9,7 +13,7 @@ import java.util.List;
* @version 1.0.0
* @since 2018/07/17 13:10
*/
public interface AbstractGroup<E> {
public interface AbstractGroup<T, E extends AbstractItem<E>> {
/**
* 获取分组名称
*
@ -37,4 +41,25 @@ public interface AbstractGroup<E> {
* @param elementList 元素集合
*/
void setElementList(List<E> elementList);
/**
* 默认子元素
*
* @return {@link E}
*/
@SuppressWarnings("unchecked")
default E defaultChild() {
Class<E> cls = (Class<E>) ReflectionUtils.getGenericClass(this, 1);
return AbstractItemFactory.createDefaultVal(cls);
}
/**
* 克隆对象
*
* @return {@link T}
*/
default T cloneObj() {
//noinspection unchecked
return (T) CloneUtils.cloneByJson(this);
}
}

View File

@ -0,0 +1,29 @@
package com.sjhy.plugin.entity;
import com.sjhy.plugin.tool.CloneUtils;
/**
* 抽象的项
*
* @author makejava
* @version 1.0.0
* @date 2021/08/11 09:47
*/
public interface AbstractItem<T extends AbstractItem> {
/**
* 默认值
*
* @return {@link T}
*/
T defaultVal();
/**
* 克隆对象
*
* @return 克隆结果
*/
@SuppressWarnings("unchecked")
default T cloneObj() {
return (T) CloneUtils.cloneByJson(this);
}
}

View File

@ -13,7 +13,7 @@ import lombok.NoArgsConstructor;
*/
@Data
@NoArgsConstructor
public class ColumnConfig {
public class ColumnConfig implements AbstractItem<ColumnConfig> {
/**
* 标题
*/
@ -38,7 +38,8 @@ public class ColumnConfig {
this.selectValue = selectValue;
}
public static ColumnConfig defaultVal() {
@Override
public ColumnConfig defaultVal() {
return new ColumnConfig("demo", ColumnConfigType.TEXT);
}
}

View File

@ -12,7 +12,7 @@ import java.util.List;
* @since 2018/07/18 09:33
*/
@Data
public class ColumnConfigGroup implements AbstractGroup<ColumnConfig> {
public class ColumnConfigGroup implements AbstractGroup<ColumnConfigGroup, ColumnConfig> {
/**
* 分组名称
*/

View File

@ -15,7 +15,7 @@ import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class GlobalConfig implements Item {
public class GlobalConfig implements AbstractItem<GlobalConfig>, Item {
/**
* 名称
*/
@ -24,4 +24,9 @@ public class GlobalConfig implements Item {
*
*/
private String value;
@Override
public GlobalConfig defaultVal() {
return new GlobalConfig("demo", "value");
}
}

View File

@ -12,7 +12,7 @@ import java.util.List;
* @since 2018/07/27 13:10
*/
@Data
public class GlobalConfigGroup implements AbstractGroup<GlobalConfig> {
public class GlobalConfigGroup implements AbstractGroup<GlobalConfigGroup, GlobalConfig> {
/**
* 分组名称
*/

View File

@ -15,7 +15,7 @@ import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Template implements Item {
public class Template implements AbstractItem<Template>, Item {
/**
* 模板名称
*/
@ -24,4 +24,9 @@ public class Template implements Item {
* 模板代码
*/
private String code;
@Override
public Template defaultVal() {
return new Template("demo", "template");
}
}

View File

@ -12,7 +12,7 @@ import java.util.List;
* @since 2018/07/18 09:33
*/
@Data
public class TemplateGroup implements AbstractGroup<Template> {
public class TemplateGroup implements AbstractGroup<TemplateGroup, Template> {
/**
* 分组名称
*/

View File

@ -13,7 +13,7 @@ import lombok.NoArgsConstructor;
*/
@Data
@NoArgsConstructor
public class TypeMapper {
public class TypeMapper implements AbstractItem<TypeMapper> {
/**
* 匹配类型
*/
@ -33,7 +33,8 @@ public class TypeMapper {
this.javaType = javaType;
}
public static TypeMapper defaultVal() {
@Override
public TypeMapper defaultVal() {
return new TypeMapper("demo", "java.lang.String");
}
}

View File

@ -12,7 +12,7 @@ import java.util.List;
* @since 2018/07/17 13:10
*/
@Data
public class TypeMapperGroup implements AbstractGroup<TypeMapper> {
public class TypeMapperGroup implements AbstractGroup<TypeMapperGroup, TypeMapper> {
/**
* 分组名称
*/

View File

@ -0,0 +1,23 @@
package com.sjhy.plugin.factory;
import com.sjhy.plugin.entity.AbstractItem;
/**
* 抽象的项目工厂
*
* @author makejava
* @version 1.0.0
* @date 2021/08/11 10:44
*/
public class AbstractItemFactory {
public static <T extends AbstractItem<T>> T createDefaultVal(Class<T> cls) {
try {
T instance = cls.newInstance();
return instance.defaultVal();
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException("构建示例失败", e);
}
}
}

View File

@ -0,0 +1,28 @@
package com.sjhy.plugin.tool;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
/**
* 反射工具
*
* @author makejava
* @version 1.0.0
* @date 2021/08/11 10:09
*/
public class ReflectionUtils {
public static Class<?> getGenericClass(Object obj, int index) {
// 获取泛型接口
Type type = obj.getClass().getGenericInterfaces()[0];
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
Type targetType = parameterizedType.getActualTypeArguments()[index];
return (Class<?>) targetType;
} else {
// 不存在泛型
throw new IllegalArgumentException(obj.getClass() + " not found generic");
}
}
}

View File

@ -24,7 +24,7 @@ import java.util.*;
* @version 1.0.0
* @since 2018/07/17 13:10
*/
public abstract class AbstractTableGroupPanel<T extends AbstractGroup<E>, E> {
public abstract class AbstractTableGroupPanel<T extends AbstractGroup, E> {
/**
* 主面板
*/
@ -141,7 +141,7 @@ public abstract class AbstractTableGroupPanel<T extends AbstractGroup<E>, E> {
}
//初始化数据
getCurrGroup().getElementList().forEach(e -> {
tableModel.addRow(toRow(e));
tableModel.addRow(toRow((E) e));
});
table.setModel(tableModel);
refreshEditorType();

View File

@ -1,15 +1,26 @@
package com.sjhy.plugin.ui;
import com.intellij.openapi.options.UnnamedConfigurable;
import com.intellij.openapi.options.Configurable;
import com.sjhy.plugin.dto.SettingsStorageDTO;
import com.sjhy.plugin.service.SettingsStorageService;
import org.jetbrains.annotations.Nullable;
/**
* @author makejava
* @version 1.0.0
* @since 2021/08/07 19:42
*/
public interface BaseSettings extends UnnamedConfigurable {
public interface BaseSettings extends Configurable {
/**
* 帮助提示信息
*
* @return 提示信息
*/
@Nullable
@Override
default String getHelpTopic() {
return getDisplayName();
}
/**
* 重置设置

View File

@ -2,8 +2,6 @@ package com.sjhy.plugin.ui;
import com.fasterxml.jackson.core.type.TypeReference;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.ui.ToolbarDecorator;
import com.sjhy.plugin.dict.GlobalDict;
import com.sjhy.plugin.dto.SettingsStorageDTO;
import com.sjhy.plugin.entity.ColumnConfig;
@ -18,9 +16,9 @@ import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import javax.swing.table.TableCellEditor;
import java.awt.*;
import java.util.Arrays;
import java.util.List;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.Map;
import java.util.function.Consumer;
/**
@ -45,7 +43,7 @@ public class ColumnConfigSettingForm implements Configurable, BaseSettings {
/**
* 分组操作组件
*/
private GroupNameComponent groupNameComponent;
private GroupNameComponent<ColumnConfig, ColumnConfigGroup> groupNameComponent;
public ColumnConfigSettingForm() {
this.mainPanel = new JPanel(new BorderLayout());
@ -62,48 +60,26 @@ 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, ColumnConfig::defaultVal);
final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(this.tableComponent.getTable());
// 表格初始化
this.mainPanel.add(decorator.createPanel(), BorderLayout.CENTER);
this.tableComponent = new TableComponent<>(columns, this.currColumnConfigGroup.getElementList(), ColumnConfig.class);
this.mainPanel.add(this.tableComponent.createPanel(), BorderLayout.CENTER);
}
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;
// 切换分组操作
Consumer<ColumnConfigGroup> switchGroupOperator = typeColumnConfigGroupMap -> {
this.currColumnConfigGroup = typeColumnConfigGroupMap;
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.groupNameComponent = new GroupNameComponent<>(switchGroupOperator, this.columnConfigGroupMap);
this.mainPanel.add(groupNameComponent.getPanel(), BorderLayout.NORTH);
}
private void initPanel() {
this.loadSettingsStore(getSettingsStorage());
// 初始化表格
this.initTable();
this.initGroupName();
@ -123,7 +99,6 @@ public class ColumnConfigSettingForm implements Configurable, BaseSettings {
@Override
public @Nullable JComponent createComponent() {
this.initPanel();
this.loadSettingsStore(getSettingsStorage());
return mainPanel;
}
@ -134,7 +109,7 @@ public class ColumnConfigSettingForm implements Configurable, BaseSettings {
}
@Override
public void apply() throws ConfigurationException {
public void apply() {
getSettingsStorage().setColumnConfigGroupMap(this.columnConfigGroupMap);
getSettingsStorage().setCurrColumnConfigGroupName(this.currColumnConfigGroup.getName());
// 保存包后重新加载配置
@ -163,7 +138,7 @@ public class ColumnConfigSettingForm implements Configurable, BaseSettings {
this.tableComponent.setDataList(this.currColumnConfigGroup.getElementList());
}
if (this.groupNameComponent != null) {
this.groupNameComponent.setAllGroupNames(this.columnConfigGroupMap.keySet());
this.groupNameComponent.setGroupMap(this.columnConfigGroupMap);
this.groupNameComponent.setCurrGroupName(this.currColumnConfigGroup.getName());
}
}

View File

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

View File

@ -1,9 +1,6 @@
package com.sjhy.plugin.ui;
import com.fasterxml.jackson.core.type.TypeReference;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.ui.ToolbarDecorator;
import com.sjhy.plugin.dict.GlobalDict;
import com.sjhy.plugin.dto.SettingsStorageDTO;
import com.sjhy.plugin.entity.TypeMapper;
@ -18,9 +15,9 @@ import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import javax.swing.table.TableCellEditor;
import java.awt.*;
import java.util.Arrays;
import java.util.List;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.Map;
import java.util.function.Consumer;
/**
@ -28,7 +25,7 @@ import java.util.function.Consumer;
* @version 1.0.0
* @date 2021/08/07 15:33
*/
public class TypeMapperSettingForm implements Configurable, BaseSettings {
public class TypeMapperSettingForm implements BaseSettings {
private JPanel mainPanel;
/**
* 类型映射配置
@ -45,7 +42,7 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
/**
* 分组操作组件
*/
private GroupNameComponent groupNameComponent;
private GroupNameComponent<TypeMapper, TypeMapperGroup> groupNameComponent;
public TypeMapperSettingForm() {
this.mainPanel = new JPanel(new BorderLayout());
@ -66,48 +63,23 @@ 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(), TypeMapper::defaultVal);
final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(this.tableComponent.getTable());
// 表格初始化
this.mainPanel.add(decorator.createPanel(), BorderLayout.CENTER);
this.tableComponent = new TableComponent<>(columns, this.currTypeMapperGroup.getElementList(), TypeMapper.class);
this.mainPanel.add(this.tableComponent.createPanel(), BorderLayout.CENTER);
}
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;
// 切换分组操作
Consumer<TypeMapperGroup> switchGroupOperator = typeMapperGroupMap -> {
this.currTypeMapperGroup = typeMapperGroupMap;
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.groupNameComponent = new GroupNameComponent<>(switchGroupOperator, this.typeMapperGroupMap);
this.mainPanel.add(groupNameComponent.getPanel(), BorderLayout.NORTH);
}
private void initPanel() {
this.loadSettingsStore(getSettingsStorage());
// 初始化表格
this.initTable();
this.initGroupName();
@ -118,16 +90,9 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
return "Type Mapper";
}
@Nullable
@Override
public String getHelpTopic() {
return getDisplayName();
}
@Override
public @Nullable JComponent createComponent() {
this.initPanel();
this.loadSettingsStore(getSettingsStorage());
return mainPanel;
}
@ -138,7 +103,7 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
}
@Override
public void apply() throws ConfigurationException {
public void apply() {
getSettingsStorage().setTypeMapperGroupMap(this.typeMapperGroupMap);
getSettingsStorage().setCurrTypeMapperGroupName(this.currTypeMapperGroup.getName());
// 保存包后重新加载配置
@ -167,7 +132,7 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
this.tableComponent.setDataList(this.currTypeMapperGroup.getElementList());
}
if (this.groupNameComponent != null) {
this.groupNameComponent.setAllGroupNames(this.typeMapperGroupMap.keySet());
this.groupNameComponent.setGroupMap(this.typeMapperGroupMap);
this.groupNameComponent.setCurrGroupName(this.currTypeMapperGroup.getName());
}
}

View File

@ -0,0 +1,32 @@
package com.sjhy.plugin.ui.base;
import com.intellij.openapi.ui.InputValidator;
import com.sjhy.plugin.tool.StringUtils;
import java.util.Collection;
/**
* 输入存在验证器
*
* @author makejava
* @version 1.0.0
* @date 2021/08/10 17:16
*/
public class InputExistsValidator implements InputValidator {
private Collection<String> itemList;
public InputExistsValidator(Collection<String> itemList) {
this.itemList = itemList;
}
@Override
public boolean checkInput(String inputString) {
return !StringUtils.isEmpty(inputString) && !itemList.contains(inputString);
}
@Override
public boolean canClose(String inputString) {
return this.checkInput(inputString);
}
}

View File

@ -0,0 +1,198 @@
package com.sjhy.plugin.ui.component;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.ui.Messages;
import com.intellij.ui.CollectionListModel;
import com.intellij.ui.border.CustomLineBorder;
import com.intellij.ui.components.JBList;
import com.sjhy.plugin.entity.AbstractGroup;
import com.sjhy.plugin.tool.StringUtils;
import com.sjhy.plugin.ui.base.InputExistsValidator;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
/**
* 编辑列表组件
*
* @author makejava
* @version 1.0.0
* @date 2021/08/10 16:57
*/
public class EditListComponent<T extends AbstractGroup> {
@Getter
private JPanel mainPanel;
/**
* 复制分组param1=新分组名 param2=旧分组名
*/
private BiConsumer<String, String> copyItemFun;
private Consumer<String> createItemFun;
private Consumer<String> deleteItemFun;
private Consumer<String> switchItemFun;
/**
* 所有列表项
*/
@Getter
private List<String> itemList;
/**
* 当前选中项
*/
private String currentItem;
private String label;
private JBList<String> jbList;
public EditListComponent(BiConsumer<String, String> copyItemFun, Consumer<String> createItemFun, Consumer<String> deleteItemFun, Consumer<String> switchItemFun, String label) {
this.copyItemFun = copyItemFun;
this.createItemFun = createItemFun;
this.deleteItemFun = deleteItemFun;
this.switchItemFun = switchItemFun;
this.label = label;
this.itemList = new ArrayList<>();
this.init();
}
private void init() {
this.mainPanel = new JPanel(new BorderLayout());
this.mainPanel.setBorder(new CustomLineBorder(1, 1, 1, 1));
// 上边是操作项
this.initAction();
// 下边是列表
this.initList();
}
private void inputItemName(String initValue, Consumer<String> consumer) {
String value = Messages.showInputDialog(label, "Input " + label, Messages.getQuestionIcon(), initValue, new InputExistsValidator(itemList));
if (StringUtils.isEmpty(value)) {
return;
}
consumer.accept(value);
}
private AnAction createCopyAction() {
return new AnAction(AllIcons.Actions.Copy) {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
inputItemName(currentItem + "Copy", itemName -> copyItemFun.accept(itemName, currentItem));
}
@Override
public void update(@NotNull AnActionEvent e) {
e.getPresentation().setEnabled(!StringUtils.isEmpty(jbList.getSelectedValue()));
}
};
}
private AnAction createAddAction() {
return new AnAction(AllIcons.General.Add) {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
inputItemName("demo", createItemFun);
}
};
}
private AnAction createRemoveAction() {
return new AnAction(AllIcons.General.Remove) {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
deleteItemFun.accept(currentItem);
}
};
}
private AnAction createMoveUpAction() {
return new AnAction(AllIcons.Actions.MoveUp) {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
int index = itemList.indexOf(currentItem);
if (index <= 0) {
return;
}
String target = itemList.remove(index);
itemList.add(index - 1, target);
setItemList(itemList);
setCurrentItem(currentItem);
}
@Override
public void update(@NotNull AnActionEvent e) {
e.getPresentation().setEnabled(itemList.indexOf(currentItem) > 0);
}
};
}
private AnAction createMoveDownAction() {
return new AnAction(AllIcons.Actions.MoveDown) {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
int index = itemList.indexOf(currentItem);
if (index < 0 || index >= itemList.size() - 1) {
return;
}
String target = itemList.remove(index);
itemList.add(index + 1, target);
setItemList(itemList);
setCurrentItem(currentItem);
}
@Override
public void update(@NotNull AnActionEvent e) {
int index = itemList.indexOf(currentItem);
e.getPresentation().setEnabled(index >= 0 && index < itemList.size() - 1);
}
};
}
private void initAction() {
DefaultActionGroup actionGroup = new DefaultActionGroup();
// 复制操作
actionGroup.add(createCopyAction());
// 新增操作
actionGroup.add(createAddAction());
// 删除动作
actionGroup.add(createRemoveAction());
// 向上移动
actionGroup.add(createMoveUpAction());
// 向下移动
actionGroup.add(createMoveDownAction());
ActionToolbar actionToolbar = ActionManager.getInstance().createActionToolbar("Item Toolbar", actionGroup, true);
this.mainPanel.add(actionToolbar.getComponent(), BorderLayout.NORTH);
}
private void initList() {
this.jbList = new JBList<>(itemList);
this.mainPanel.add(this.jbList, BorderLayout.CENTER);
this.jbList.addListSelectionListener(e -> {
String selectedValue = jbList.getSelectedValue();
if (StringUtils.isEmpty(selectedValue)) {
return;
}
switchItemFun.accept(currentItem = selectedValue);
});
}
public void setItemList(List<String> itemList) {
this.itemList = itemList;
if (StringUtils.isEmpty(this.currentItem) && itemList != null && itemList.size() > 0) {
setCurrentItem(itemList.get(0));
}
this.jbList.setModel(new CollectionListModel<>(this.itemList));
}
public void setCurrentItem(String currentItem) {
this.currentItem = currentItem;
this.jbList.setSelectedIndex(this.itemList.indexOf(this.currentItem));
}
}

View File

@ -3,21 +3,22 @@ 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.intellij.ui.components.JBLabel;
import com.sjhy.plugin.dict.GlobalDict;
import com.sjhy.plugin.entity.AbstractGroup;
import com.sjhy.plugin.entity.AbstractItem;
import com.sjhy.plugin.tool.StringUtils;
import com.sjhy.plugin.ui.base.InputExistsValidator;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.Map;
import java.util.function.Consumer;
/**
@ -27,23 +28,9 @@ import java.util.function.Consumer;
* @version 1.0.0
* @date 2021/08/10 14:13
*/
public class GroupNameComponent {
public class GroupNameComponent<E extends AbstractItem<E>, T extends AbstractGroup<T, E>> {
/**
* 复制分组param1=新分组名 param2=旧分组名
*/
private BiConsumer<String, String> copyGroupFun;
private Consumer<String> createGroupFun;
private Consumer<String> deleteGroupFun;
private Consumer<String> switchGroupFun;
/**
* 所有分组
*/
private Set<String> allGroupNames;
private Consumer<T> switchGroupConsumer;
/**
* 当前分组
@ -57,27 +44,23 @@ public class GroupNameComponent {
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();
private Map<String, T> groupMap;
public GroupNameComponent(Consumer<T> switchGroupConsumer, Map<String, T> groupMap) {
this.switchGroupConsumer = switchGroupConsumer;
this.groupMap = groupMap;
this.currGroupName = groupMap.keySet().stream().findFirst().orElse(null);
this.init();
}
/**
* 输入分组名
*
* @param initValue 初始值
* @param consumer 消费分组名
*/
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);
}
});
String value = Messages.showInputDialog("Group Name:", "Input Group Name:", Messages.getQuestionIcon(), initValue, new InputExistsValidator(groupMap.keySet()));
if (StringUtils.isEmpty(value)) {
return;
}
@ -88,7 +71,15 @@ public class GroupNameComponent {
return new AnAction(AllIcons.Actions.Copy) {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
inputGroupName(currGroupName + "Copy", groupName -> copyGroupFun.accept(groupName, currGroupName));
inputGroupName(currGroupName + "Copy", groupName -> {
// 复制一份重名命
T cloneObj = groupMap.get(currGroupName).cloneObj();
cloneObj.setName(groupName);
// 添加分组
groupMap.put(groupName, cloneObj);
// 切换分组
switchGroupConsumer.accept(cloneObj);
});
}
};
}
@ -97,7 +88,16 @@ public class GroupNameComponent {
return new AnAction(AllIcons.General.Add) {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
inputGroupName("GroupName", createGroupFun);
inputGroupName("GroupName", groupName -> {
T obj = groupMap.get(currGroupName).cloneObj();
E item = obj.defaultChild();
obj.setName(groupName);
obj.setElementList(new ArrayList<>());
obj.getElementList().add(item);
groupMap.put(groupName, obj);
// 切换分组
switchGroupConsumer.accept(obj);
});
}
};
}
@ -106,7 +106,9 @@ public class GroupNameComponent {
return new AnAction(AllIcons.General.Remove) {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
deleteGroupFun.accept(currGroupName);
groupMap.remove(currGroupName);
// 切换分组
switchGroupConsumer.accept(groupMap.get(GlobalDict.DEFAULT_GROUP_NAME));
}
@Override
@ -119,7 +121,7 @@ public class GroupNameComponent {
private void init() {
panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
this.panel.add(new JBLabel("Group Name:"));
this.groupComboBox = new ComboBox<>(this.allGroupNames.toArray(new String[0]));
this.groupComboBox = new ComboBox<>(this.groupMap.keySet().toArray(new String[0]));
this.panel.add(this.groupComboBox);
// 分组操作
DefaultActionGroup groupAction = new DefaultActionGroup(Arrays.asList(this.copyAction(), this.addAction(), this.removeAction()));
@ -135,17 +137,29 @@ public class GroupNameComponent {
if (StringUtils.isEmpty(selectedItem)) {
return;
}
switchGroupFun.accept(selectedItem);
switchGroupConsumer.accept(groupMap.get(selectedItem));
}
});
}
public void setAllGroupNames(Set<String> allGroupNames) {
this.allGroupNames = allGroupNames;
/**
* 更新分组信息
*
* @param groupMap 所有分组信息
*/
public void setGroupMap(Map<String, T> groupMap) {
this.groupMap = groupMap;
this.refreshGroupComboBox();
}
/**
* 刷新下列框
*/
private void refreshGroupComboBox() {
try {
this.refresh = true;
this.groupComboBox.removeAllItems();
for (String item : this.allGroupNames) {
for (String item : this.groupMap.keySet()) {
this.groupComboBox.addItem(item);
}
} finally {
@ -153,6 +167,11 @@ public class GroupNameComponent {
}
}
/**
* 切换选中分组
*
* @param currGroupName 分组名
*/
public void setCurrGroupName(String currGroupName) {
this.currGroupName = currGroupName;
try {

View File

@ -0,0 +1,60 @@
package com.sjhy.plugin.ui.component;
import com.intellij.openapi.ui.Splitter;
import com.intellij.util.ui.JBUI;
import lombok.Getter;
import javax.swing.*;
import java.awt.*;
/**
* 左右组件
*
* @author makejava
* @version 1.0.0
* @date 2021/08/10 16:49
*/
public class LeftRightComponent {
/**
* 主面板
*/
@Getter
private JPanel mainPanel;
/**
* 左边面板
*/
private JPanel leftPanel;
/**
* 右边面板
*/
private JPanel rightPanel;
/**
* 分割比例
*/
private float proportion;
/**
* 预设值窗口大小
*/
private Dimension preferredSize;
public LeftRightComponent(JPanel leftPanel, JPanel rightPanel) {
this(leftPanel, rightPanel, 0.2F, JBUI.size(400, 300));
}
public LeftRightComponent(JPanel leftPanel, JPanel rightPanel, float proportion, Dimension preferredSize) {
this.leftPanel = leftPanel;
this.rightPanel = rightPanel;
this.proportion = proportion;
this.preferredSize = preferredSize;
this.init();
}
private void init() {
this.mainPanel = new JPanel(new BorderLayout());
Splitter splitter = new Splitter(false, proportion);
splitter.setFirstComponent(this.leftPanel);
splitter.setSecondComponent(this.rightPanel);
this.mainPanel.add(splitter, BorderLayout.CENTER);
mainPanel.setPreferredSize(this.preferredSize);
}
}

View File

@ -1,7 +1,10 @@
package com.sjhy.plugin.ui.component;
import com.intellij.ui.ToolbarDecorator;
import com.intellij.ui.table.JBTable;
import com.intellij.util.ui.EditableModel;
import com.sjhy.plugin.entity.AbstractItem;
import com.sjhy.plugin.factory.AbstractItemFactory;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
@ -15,7 +18,6 @@ import java.util.List;
import java.util.Vector;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* 表格组件
@ -24,7 +26,7 @@ import java.util.function.Supplier;
* @version 1.0.0
* @date 2021/08/10 09:52
*/
public class TableComponent<T> extends DefaultTableModel implements EditableModel {
public class TableComponent<T extends AbstractItem<T>> extends DefaultTableModel implements EditableModel {
/**
* 列信息
*/
@ -33,29 +35,28 @@ public class TableComponent<T> extends DefaultTableModel implements EditableMode
* 表数据
*/
private List<T> dataList;
/**
* 默认值方法
*/
private Supplier<T> defaultValFun;
/**
* 表格
*/
@Getter
private JBTable table;
public TableComponent(@NonNull List<Column<T>> columns, @NonNull Supplier<T> defaultValFun) {
this(columns, Collections.emptyList(), defaultValFun);
}
private Class<T> cls;
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, Class<T> cls) {
this.columns = columns;
this.dataList = dataList;
this.defaultValFun = defaultValFun;
this.cls = cls;
this.initColumnName();
this.initTable();
this.setDataList(dataList);
}
public JComponent createPanel() {
final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(this.table);
return decorator.createPanel();
}
private void initColumnName() {
for (Column<T> column : this.columns) {
addColumn(column.name);
@ -119,7 +120,7 @@ public class TableComponent<T> extends DefaultTableModel implements EditableMode
@Override
public void addRow() {
T entity = defaultValFun.get();
T entity = AbstractItemFactory.createDefaultVal(cls);
this.dataList.add(entity);
addRow(entity);
}