mirror of https://gitee.com/makejava/EasyCode.git
对表格操作进行抽取重构
This commit is contained in:
parent
122ada78c7
commit
c48063f7d8
|
@ -0,0 +1,153 @@
|
|||
package com.sjhy.plugin.ui;
|
||||
|
||||
import com.intellij.ui.table.JBTable;
|
||||
import com.intellij.util.ui.EditableModel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
import javax.swing.table.TableCellEditor;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 表格组件
|
||||
*
|
||||
* @author makejava
|
||||
* @version 1.0.0
|
||||
* @date 2021/08/10 09:52
|
||||
*/
|
||||
public class TableComponent<T> extends DefaultTableModel implements EditableModel {
|
||||
/**
|
||||
* 列信息
|
||||
*/
|
||||
private List<Column<T>> columns;
|
||||
/**
|
||||
* 表数据
|
||||
*/
|
||||
private List<T> dataList;
|
||||
/**
|
||||
* 默认值方法
|
||||
*/
|
||||
private Supplier<T> defaultValFun;
|
||||
/**
|
||||
* 表格
|
||||
*/
|
||||
@Getter
|
||||
private JBTable table;
|
||||
|
||||
public TableComponent(@NonNull List<Column<T>> columns, @NonNull List<T> dataList, @NonNull Supplier<T> defaultValFun) {
|
||||
this.columns = columns;
|
||||
this.dataList = dataList;
|
||||
this.defaultValFun = defaultValFun;
|
||||
this.initColumnName();
|
||||
this.initTable();
|
||||
this.setDataList(dataList);
|
||||
}
|
||||
|
||||
private void initColumnName() {
|
||||
for (Column<T> column : this.columns) {
|
||||
addColumn(column.name);
|
||||
}
|
||||
}
|
||||
|
||||
private void initTable() {
|
||||
this.table = new JBTable(this);
|
||||
this.table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||
// 指定编辑器
|
||||
for (Column<T> column : this.columns) {
|
||||
if (column.editor != null) {
|
||||
this.table.getColumn(column.name).setCellEditor(column.editor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Vector<String> toObj(T e) {
|
||||
Vector<String> vector = new Vector<>();
|
||||
this.columns.stream().map(item -> item.getFun.apply(e)).forEach(vector::add);
|
||||
return vector;
|
||||
}
|
||||
|
||||
public void setDataList(List<T> dataList) {
|
||||
this.dataList = dataList;
|
||||
// 清空数据
|
||||
removeAllRow();
|
||||
for (T entity : this.dataList) {
|
||||
addRow(entity);
|
||||
}
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除所有行
|
||||
*/
|
||||
public void removeAllRow() {
|
||||
int rowCount = getRowCount();
|
||||
for (int i = 0; i < rowCount; i++) {
|
||||
super.removeRow(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeRow(int row) {
|
||||
super.removeRow(row);
|
||||
this.dataList.remove(row);
|
||||
}
|
||||
|
||||
public void addRow(T entity) {
|
||||
addRow(toObj(entity));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addRow() {
|
||||
T entity = defaultValFun.get();
|
||||
this.dataList.add(entity);
|
||||
addRow(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exchangeRows(int oldIndex, int newIndex) {
|
||||
super.moveRow(oldIndex, oldIndex, newIndex);
|
||||
T remove = this.dataList.remove(oldIndex);
|
||||
this.dataList.add(newIndex, remove);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canExchangeRows(int oldIndex, int newIndex) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public static class Column<T> {
|
||||
/**
|
||||
* 列名
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* get方法
|
||||
*/
|
||||
private Function<T, String> getFun;
|
||||
/**
|
||||
* set方法
|
||||
*/
|
||||
private BiConsumer<T, String> setFun;
|
||||
/**
|
||||
* 列编辑器
|
||||
*/
|
||||
private TableCellEditor editor;
|
||||
}
|
||||
}
|
|
@ -11,11 +11,10 @@ import com.intellij.openapi.ui.Messages;
|
|||
import com.intellij.ui.JBColor;
|
||||
import com.intellij.ui.ToolbarDecorator;
|
||||
import com.intellij.ui.components.JBTextField;
|
||||
import com.intellij.ui.table.JBTable;
|
||||
import com.sjhy.plugin.dict.GlobalDict;
|
||||
import com.sjhy.plugin.dto.SettingsStorageDTO;
|
||||
import com.sjhy.plugin.entity.TypeMapper;
|
||||
import com.sjhy.plugin.entity.TypeMapperGroup;
|
||||
import com.sjhy.plugin.entity.TypeMapperModel;
|
||||
import com.sjhy.plugin.enums.MatchType;
|
||||
import com.sjhy.plugin.tool.CloneUtils;
|
||||
import com.sjhy.plugin.tool.StringUtils;
|
||||
|
@ -24,9 +23,12 @@ import org.jetbrains.annotations.Nullable;
|
|||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.FocusEvent;
|
||||
import java.awt.event.FocusListener;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
|
@ -48,25 +50,22 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
|
|||
*/
|
||||
private TypeMapperGroup currTypeMapperGroup;
|
||||
/**
|
||||
* 表格
|
||||
* 表格组件
|
||||
*/
|
||||
private JBTable table;
|
||||
private TableComponent<TypeMapper> tableComponent;
|
||||
|
||||
private TypeMapperModel typeMapperModel;
|
||||
private boolean refresh;
|
||||
|
||||
private void initPanel(SettingsStorageDTO settingsStorage) {
|
||||
this.typeMapperModel = new TypeMapperModel();
|
||||
this.loadSettingsStore(settingsStorage);
|
||||
table = new JBTable(typeMapperModel);
|
||||
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||
// 第一列仅适用下拉框
|
||||
ComboBox<String> matchTypeField = new ComboBox<>(Stream.of(MatchType.values()).map(Enum::name).toArray(value -> new String[2]));
|
||||
if (matchTypeField.getPopup() != null) {
|
||||
matchTypeField.getPopup().getList().setBackground(JBColor.WHITE);
|
||||
matchTypeField.getPopup().getList().setForeground(JBColor.GREEN);
|
||||
}
|
||||
DefaultCellEditor matchTypeCellEditor = new DefaultCellEditor(matchTypeField);
|
||||
table.getColumn(typeMapperModel.initColumnName()[0]).setCellEditor(matchTypeCellEditor);
|
||||
TableComponent.Column<TypeMapper> matchTypeColumn = new TableComponent.Column<>("matchType", item -> item.getMatchType().name(), (entity, val) -> {
|
||||
entity.setMatchType(MatchType.valueOf(val));
|
||||
}, new DefaultCellEditor(matchTypeField));
|
||||
// 第二列监听输入状态,及时修改属性值
|
||||
JBTextField textField = new JBTextField();
|
||||
textField.addFocusListener(new FocusListener() {
|
||||
|
@ -80,9 +79,7 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
|
|||
textField.getActionListeners()[0].actionPerformed(null);
|
||||
}
|
||||
});
|
||||
DefaultCellEditor textFieldCellEditor = new DefaultCellEditor(textField);
|
||||
table.getColumn(typeMapperModel.initColumnName()[1]).setCellEditor(textFieldCellEditor);
|
||||
|
||||
TableComponent.Column<TypeMapper> columnTypeColumn = new TableComponent.Column<>("columnType", TypeMapper::getColumnType, TypeMapper::setColumnType, new DefaultCellEditor(textField));
|
||||
// 第三列支持下拉框
|
||||
ComboBox<String> javaTypeField = new ComboBox<>(GlobalDict.DEFAULT_JAVA_TYPE_LIST);
|
||||
if (javaTypeField.getPopup() != null) {
|
||||
|
@ -90,9 +87,10 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
|
|||
javaTypeField.getPopup().getList().setForeground(JBColor.GREEN);
|
||||
}
|
||||
javaTypeField.setEditable(true);
|
||||
DefaultCellEditor javaTypeCellEditor = new DefaultCellEditor(javaTypeField);
|
||||
table.getColumn(typeMapperModel.initColumnName()[2]).setCellEditor(javaTypeCellEditor);
|
||||
final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(table);
|
||||
TableComponent.Column<TypeMapper> javaTypeColumn = new TableComponent.Column<>("javaType", TypeMapper::getJavaType, TypeMapper::setJavaType, new DefaultCellEditor(javaTypeField));
|
||||
List<TableComponent.Column<TypeMapper>> columns = Arrays.asList(matchTypeColumn, columnTypeColumn, javaTypeColumn);
|
||||
this.tableComponent = new TableComponent<>(columns, Collections.emptyList(), () -> new TypeMapper("demo", "java.lang.String"));
|
||||
final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(this.tableComponent.getTable());
|
||||
// 表格初始化
|
||||
this.mainPanel.add(decorator.createPanel(), BorderLayout.CENTER);
|
||||
// 分组操作
|
||||
|
@ -117,17 +115,35 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
|
|||
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
|
||||
|
@ -149,7 +165,8 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
|
|||
|
||||
@Override
|
||||
public boolean isModified() {
|
||||
return !this.typeMapperGroupMap.equals(getSettingsStorage().getTypeMapperGroupMap());
|
||||
return !this.typeMapperGroupMap.equals(getSettingsStorage().getTypeMapperGroupMap())
|
||||
|| !getSettingsStorage().getCurrTypeMapperGroupName().equals(this.currTypeMapperGroup.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -175,13 +192,15 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
|
|||
}
|
||||
|
||||
private void refreshUiVal() {
|
||||
if (this.typeMapperModel != null) {
|
||||
this.typeMapperModel.init(this.currTypeMapperGroup.getElementList());
|
||||
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);
|
||||
}
|
||||
this.groupComboBox.setSelectedItem(this.currTypeMapperGroup.getName());
|
||||
this.refresh = false;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue