类型映射初步可以进行编辑操作

This commit is contained in:
makejava 2021-08-09 18:39:02 +08:00
parent 583b8ee469
commit 122ada78c7
6 changed files with 154 additions and 35 deletions

View File

@ -1,5 +1,6 @@
package com.sjhy.plugin.comm;
import com.intellij.util.ui.EditableModel;
import com.sjhy.plugin.tool.CollectionUtil;
import javax.swing.table.DefaultTableModel;
@ -12,7 +13,7 @@ import java.util.List;
* @version 1.0.0
* @since 2018/07/17 13:10
*/
public abstract class AbstractTableModel<T> extends DefaultTableModel {
public abstract class AbstractTableModel<T> extends DefaultTableModel implements EditableModel {
/**
* 数据
*/
@ -114,4 +115,31 @@ public abstract class AbstractTableModel<T> extends DefaultTableModel {
* @param val
*/
protected abstract void setVal(T obj, int columnIndex, Object val);
/**
* 默认值
*
* @return 默认值
*/
protected abstract T defaultVal();
/**
* 添加行
*/
@Override
public void addRow() {
addRow(defaultVal());
}
@Override
public void exchangeRows(int oldIndex, int newIndex) {
super.moveRow(oldIndex, oldIndex, newIndex);
T remove = this.data.remove(oldIndex);
this.data.add(newIndex, remove);
}
@Override
public boolean canExchangeRows(int oldIndex, int newIndex) {
return true;
}
}

View File

@ -20,4 +20,27 @@ public interface GlobalDict {
* 默认分组名称
*/
String DEFAULT_GROUP_NAME = "Default";
/**
* 默认的Java类型列表
*/
String[] DEFAULT_JAVA_TYPE_LIST = new String[]{
"java.lang.String",
"java.lang.Integer",
"java.lang.Long",
"java.util.Boolean",
"java.util.Date",
"java.time.LocalDateTime",
"java.time.LocalDate",
"java.time.LocalTime",
"java.lang.Short",
"java.lang.Byte",
"java.lang.Character",
"java.lang.Character",
"java.math.BigDecimal",
"java.math.BigInteger",
"java.lang.String[]",
"java.util.List",
"java.util.Set",
"java.util.Map",
};
}

View File

@ -102,8 +102,4 @@ public class SettingsStorageDTO {
* 全局配置组
*/
private Map<String, GlobalConfigGroup> globalConfigGroupMap;
public TypeMapperGroup currentTypeMapperGroup() {
return typeMapperGroupMap.get(currColumnConfigGroupName);
}
}

View File

@ -1,6 +1,5 @@
package com.sjhy.plugin.entity;
import com.intellij.util.ui.EditableModel;
import com.sjhy.plugin.comm.AbstractTableModel;
import com.sjhy.plugin.enums.MatchType;
@ -11,9 +10,9 @@ import com.sjhy.plugin.enums.MatchType;
* @version 1.0.0
* @since 2018/07/17 13:10
*/
public class TypeMapperModel extends AbstractTableModel<TypeMapper> implements EditableModel {
public class TypeMapperModel extends AbstractTableModel<TypeMapper> {
@Override
protected String[] initColumnName() {
public String[] initColumnName() {
return new String[]{"matchType", "columnType", "javaType"};
}
@ -34,17 +33,7 @@ public class TypeMapperModel extends AbstractTableModel<TypeMapper> implements E
}
@Override
public void addRow() {
addRow(new TypeMapper("demo", "java.lang.String"));
}
@Override
public void exchangeRows(int oldIndex, int newIndex) {
}
@Override
public boolean canExchangeRows(int oldIndex, int newIndex) {
return false;
protected TypeMapper defaultVal() {
return new TypeMapper("demo", "java.lang.String");
}
}

View File

@ -16,7 +16,7 @@ public interface BaseSettings extends UnnamedConfigurable {
*/
@Override
default void reset() {
loadSettingsStore();
}
/**

View File

@ -1,22 +1,33 @@
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.ComboBox;
import com.intellij.openapi.ui.InputValidator;
import com.intellij.openapi.ui.Messages;
import com.intellij.ui.JBColor;
import com.intellij.ui.ToolbarDecorator;
import com.intellij.ui.components.JBTextField;
import com.intellij.ui.table.JBTable;
import com.sjhy.plugin.dict.GlobalDict;
import com.sjhy.plugin.dto.SettingsStorageDTO;
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;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Stream;
/**
@ -28,22 +39,59 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
private JPanel mainPanel;
private JComboBox<String> groupComboBox;
private JPanel groupOperatorPanel;
/**
* 类型映射配置
*/
private Map<String, TypeMapperGroup> typeMapperGroupMap;
/**
* 当前分组名
*/
private TypeMapperGroup currTypeMapperGroup;
/**
* 表格
*/
private JBTable table;
private TypeMapperModel typeMapperModel;
private void initPanel(SettingsStorageDTO settingsStorage) {
TypeMapperModel typeMapperModel = new TypeMapperModel();
typeMapperModel.init(settingsStorage.currentTypeMapperGroup().getElementList());
this.typeMapperModel = new TypeMapperModel();
this.loadSettingsStore(settingsStorage);
table = new JBTable(typeMapperModel);
ComboBox<String> comboField = new ComboBox<>(Stream.of(MatchType.values()).map(Enum::name).toArray(value -> new String[2]), 20);
if (comboField.getPopup() != null) {
comboField.getPopup().getList().setBackground(JBColor.MAGENTA);
}
DefaultCellEditor cellEditor = new DefaultCellEditor(comboField);
table.getColumn("matchType").setCellEditor(cellEditor);
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);
// 第二列监听输入状态及时修改属性值
JBTextField textField = new JBTextField();
textField.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
}
@Override
public void focusLost(FocusEvent e) {
// 失去焦点时向上层发起事件通知使table的值能够正常回写
textField.getActionListeners()[0].actionPerformed(null);
}
});
DefaultCellEditor textFieldCellEditor = new DefaultCellEditor(textField);
table.getColumn(typeMapperModel.initColumnName()[1]).setCellEditor(textFieldCellEditor);
// 第三列支持下拉框
ComboBox<String> javaTypeField = new ComboBox<>(GlobalDict.DEFAULT_JAVA_TYPE_LIST);
if (javaTypeField.getPopup() != null) {
javaTypeField.getPopup().getList().setBackground(JBColor.WHITE);
javaTypeField.getPopup().getList().setForeground(JBColor.GREEN);
}
javaTypeField.setEditable(true);
DefaultCellEditor javaTypeCellEditor = new DefaultCellEditor(javaTypeField);
table.getColumn(typeMapperModel.initColumnName()[2]).setCellEditor(javaTypeCellEditor);
final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(table);
// 表格初始化
this.mainPanel.add(decorator.createPanel(), BorderLayout.CENTER);
@ -51,7 +99,26 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
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);
currTypeMapperGroup = typeMapperGroup;
refreshUiVal();
}
}, new AnAction(AllIcons.General.Remove) {
@Override
@ -61,9 +128,6 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
}));
ActionToolbar groupActionToolbar = ActionManager.getInstance().createActionToolbar("Group Toolbar", groupAction, true);
this.groupOperatorPanel.add(groupActionToolbar.getComponent());
for (String key : settingsStorage.getTypeMapperGroupMap().keySet()) {
this.groupComboBox.addItem(key);
}
}
@Override
@ -79,17 +143,21 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
@Override
public @Nullable JComponent createComponent() {
this.initPanel(getSettingsStorage());
return mainPanel;
}
@Override
public boolean isModified() {
return false;
return !this.typeMapperGroupMap.equals(getSettingsStorage().getTypeMapperGroupMap());
}
@Override
public void apply() throws ConfigurationException {
getSettingsStorage().setTypeMapperGroupMap(this.typeMapperGroupMap);
getSettingsStorage().setCurrTypeMapperGroupName(this.currTypeMapperGroup.getName());
// 保存包后重新加载配置
this.loadSettingsStore(getSettingsStorage());
}
/**
@ -99,6 +167,21 @@ public class TypeMapperSettingForm implements Configurable, BaseSettings {
*/
@Override
public void loadSettingsStore(SettingsStorageDTO settingsStorage) {
this.initPanel(settingsStorage);
// 复制配置防止篡改
this.typeMapperGroupMap = CloneUtils.cloneByJson(settingsStorage.getTypeMapperGroupMap(), new TypeReference<Map<String, TypeMapperGroup>>() {
});
this.currTypeMapperGroup = this.typeMapperGroupMap.get(settingsStorage.getCurrTypeMapperGroupName());
this.refreshUiVal();
}
private void refreshUiVal() {
if (this.typeMapperModel != null) {
this.typeMapperModel.init(this.currTypeMapperGroup.getElementList());
}
this.groupComboBox.removeAllItems();
for (String key : this.typeMapperGroupMap.keySet()) {
this.groupComboBox.addItem(key);
}
this.groupComboBox.setSelectedItem(this.currTypeMapperGroup.getName());
}
}