mirror of https://gitee.com/makejava/EasyCode.git
添加部分注释,抽象优化部分代码。
This commit is contained in:
parent
d04596fe0b
commit
e24bfc065d
|
@ -0,0 +1,65 @@
|
|||
package com.sjhy.plugin.comm;
|
||||
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class AbstractTableModel<T> extends DefaultTableModel {
|
||||
//数据
|
||||
private List<T> data;
|
||||
|
||||
public AbstractTableModel() {
|
||||
for (String columnName : initColumnName()) {
|
||||
super.addColumn(columnName);
|
||||
}
|
||||
}
|
||||
|
||||
public void init(List<T> data) {
|
||||
if (data==null){
|
||||
return;
|
||||
}
|
||||
this.data = data;
|
||||
removeAllRow();
|
||||
data.forEach(this::addRow);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 移除所有行
|
||||
*/
|
||||
private void removeAllRow() {
|
||||
int rowCount = getRowCount();
|
||||
if (rowCount>0){
|
||||
for (int i = 0; i< rowCount; i++) {
|
||||
super.removeRow(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeRow(int row) {
|
||||
super.removeRow(row);
|
||||
this.data.remove(row);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValueAt(Object aValue, int row, int column) {
|
||||
super.setValueAt(aValue, row, column);
|
||||
T obj = data.get(row);
|
||||
setVal(obj, column, aValue);
|
||||
}
|
||||
|
||||
public void addRow(T entity) {
|
||||
super.addRow(toObj(entity));
|
||||
this.data.add(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 抽象初始化列名
|
||||
* @return 列名
|
||||
*/
|
||||
protected abstract String[] initColumnName();
|
||||
|
||||
protected abstract Object[] toObj(T entity);
|
||||
|
||||
protected abstract void setVal(T obj, int columnIndex, Object val);
|
||||
}
|
|
@ -1,49 +1,24 @@
|
|||
package com.sjhy.plugin.entity;
|
||||
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
import com.sjhy.plugin.comm.AbstractTableModel;
|
||||
|
||||
public class TypeMapperModel extends DefaultTableModel {
|
||||
private TypeMapperGroup typeMapperGroup;
|
||||
|
||||
public TypeMapperModel() {
|
||||
super.addColumn("columnType");
|
||||
super.addColumn("javaType");
|
||||
}
|
||||
|
||||
public void init(TypeMapperGroup typeMapperGroup) {
|
||||
this.typeMapperGroup =typeMapperGroup;
|
||||
removeAllRow();
|
||||
this.typeMapperGroup.getTypeMapperList().forEach(typeMapper -> super.addRow(new Object[]{typeMapper.getColumnType(), typeMapper.getJavaType()}));
|
||||
}
|
||||
|
||||
private void removeAllRow() {
|
||||
int rowCount = getRowCount();
|
||||
if (rowCount>0){
|
||||
for (int i = 0; i< rowCount; i++) {
|
||||
super.removeRow(0);
|
||||
}
|
||||
}
|
||||
public class TypeMapperModel extends AbstractTableModel<TypeMapper> {
|
||||
@Override
|
||||
protected String[] initColumnName() {
|
||||
return new String[]{"columnType", "javaType"};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeRow(int row) {
|
||||
super.removeRow(row);
|
||||
this.typeMapperGroup.getTypeMapperList().remove(row);
|
||||
}
|
||||
|
||||
public void addRow(TypeMapper typeMapper) {
|
||||
super.addRow(new Object[]{typeMapper.getColumnType(), typeMapper.getJavaType()});
|
||||
this.typeMapperGroup.getTypeMapperList().add(typeMapper);
|
||||
protected Object[] toObj(TypeMapper entity) {
|
||||
return new Object[]{entity.getColumnType(), entity.getJavaType()};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
|
||||
super.setValueAt(aValue, rowIndex, columnIndex);
|
||||
TypeMapper typeMapper = typeMapperGroup.getTypeMapperList().get(rowIndex);
|
||||
protected void setVal(TypeMapper obj, int columnIndex, Object val) {
|
||||
if (columnIndex==0){
|
||||
typeMapper.setColumnType((String) aValue);
|
||||
} else {
|
||||
typeMapper.setJavaType((String) aValue);
|
||||
obj.setColumnType((String) val);
|
||||
}else{
|
||||
obj.setJavaType((String) val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,15 +10,23 @@ import org.jetbrains.annotations.Nullable;
|
|||
import javax.swing.*;
|
||||
|
||||
public class MainSetting extends ServiceComm implements Configurable, Configurable.Composite {
|
||||
//主面板
|
||||
private JPanel mainPanel;
|
||||
//编码选择下拉框
|
||||
private JComboBox encodeComboBox;
|
||||
//作者编辑框
|
||||
private JTextField authorTextField;
|
||||
//全局配置服务
|
||||
private ConfigService configService;
|
||||
|
||||
public MainSetting(ConfigService configService) {
|
||||
this.configService = configService;
|
||||
init();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化方法
|
||||
*/
|
||||
private void init() {
|
||||
//初始化数据
|
||||
authorTextField.setText(configService.getAuthor());
|
||||
|
@ -31,6 +39,10 @@ public class MainSetting extends ServiceComm implements Configurable, Configurab
|
|||
return "Easy Code";
|
||||
}
|
||||
|
||||
/**
|
||||
* 更多配置
|
||||
* @return 配置选项
|
||||
*/
|
||||
@NotNull
|
||||
@Override
|
||||
public Configurable[] getConfigurables() {
|
||||
|
@ -42,20 +54,17 @@ public class MainSetting extends ServiceComm implements Configurable, Configurab
|
|||
@Nullable
|
||||
@Override
|
||||
public JComponent createComponent() {
|
||||
init();
|
||||
return mainPanel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isModified() {
|
||||
ConfigService configService = getConfigService();
|
||||
return !configService.getEncode().equals(encodeComboBox.getSelectedItem()) || !configService.getAuthor().equals(authorTextField.getText());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply() {
|
||||
//保存数据
|
||||
ConfigService configService = getConfigService();
|
||||
configService.setAuthor(authorTextField.getText());
|
||||
configService.setEncode((String) encodeComboBox.getSelectedItem());
|
||||
}
|
||||
|
|
|
@ -13,26 +13,35 @@ import java.util.LinkedHashMap;
|
|||
import java.util.Map;
|
||||
|
||||
public class TypeMapperSetting implements Configurable {
|
||||
//主面板
|
||||
private JPanel mainPanel;
|
||||
//类型映射分组切换下拉框
|
||||
private JComboBox typeMapperComboBox;
|
||||
//分组复制按钮
|
||||
private JButton typeMapperCopyButton;
|
||||
//类型映射表
|
||||
private JTable typeMapperTable;
|
||||
//添加映射按钮
|
||||
private JButton addButton;
|
||||
//移除映射按钮
|
||||
private JButton removeButton;
|
||||
//删除分组按钮
|
||||
private JButton deleteButton;
|
||||
|
||||
//是否初始化完成
|
||||
private boolean init;
|
||||
//类型映射表模型
|
||||
private TypeMapperModel typeMapperModel;
|
||||
|
||||
//当前选中分组
|
||||
private String currGroupName;
|
||||
|
||||
//类型映射分组集合
|
||||
private Map<String, TypeMapperGroup> typeMapperGroupMap;
|
||||
|
||||
//全局配置服务
|
||||
private ConfigService configService;
|
||||
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public TypeMapperSetting(ConfigService configService) {
|
||||
TypeMapperSetting(ConfigService configService) {
|
||||
this.configService = configService;
|
||||
//添加类型
|
||||
addButton.addActionListener(e -> typeMapperModel.addRow(new TypeMapper("demoColumn", "java.lang.Object")));
|
||||
|
@ -95,10 +104,14 @@ public class TypeMapperSetting implements Configurable {
|
|||
refresh();
|
||||
}
|
||||
});
|
||||
|
||||
init();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 初始化方法
|
||||
*/
|
||||
private void init() {
|
||||
//复制数据
|
||||
this.typeMapperGroupMap = new LinkedHashMap<>();
|
||||
|
@ -113,6 +126,9 @@ public class TypeMapperSetting implements Configurable {
|
|||
refresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新方法
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private void refresh() {
|
||||
init = false;
|
||||
|
@ -120,7 +136,7 @@ public class TypeMapperSetting implements Configurable {
|
|||
this.typeMapperComboBox.removeAllItems();
|
||||
typeMapperGroupMap.keySet().forEach(this.typeMapperComboBox::addItem);
|
||||
this.typeMapperComboBox.setSelectedItem(this.currGroupName);
|
||||
this.typeMapperModel.init(this.typeMapperGroupMap.get(currGroupName));
|
||||
this.typeMapperModel.init(this.typeMapperGroupMap.get(currGroupName).getTypeMapperList());
|
||||
init = true;
|
||||
}
|
||||
|
||||
|
@ -133,7 +149,6 @@ public class TypeMapperSetting implements Configurable {
|
|||
@Nullable
|
||||
@Override
|
||||
public JComponent createComponent() {
|
||||
init();
|
||||
return mainPanel;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue