完成一个初步的类型映射设置。

This commit is contained in:
makejava 2018-01-10 21:02:57 +08:00
parent 692b850dc2
commit d04596fe0b
20 changed files with 1176 additions and 0 deletions

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/

View File

@ -0,0 +1,41 @@
<idea-plugin>
<id>com.sjhy.plugin</id>
<name>Easy Code</name>
<version>1.0</version>
<vendor email="1353036300@qq.com" url="http://www.shujuhaiyang.com">数据海洋</vendor>
<description><![CDATA[
Easy Code.<br>
<em>make code so easy!</em>
<em>让代码变得简单。</em>
]]></description>
<change-notes><![CDATA[
Add change notes here.<br>
<em>most HTML tags may be used</em>
]]>
</change-notes>
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
<idea-version since-build="173.0"/>
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
on how to target different products -->
<!-- uncomment to enable plugin in all products
<depends>com.intellij.modules.lang</depends>
-->
<!--suppress PluginXmlValidity -->
<depends optional="true">com.intellij.database</depends>
<extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
<applicationService serviceInterface="com.sjhy.plugin.service.ConfigService" serviceImplementation="com.sjhy.plugin.service.impl.ConfigServiceImpl"/>
<applicationConfigurable dynamic="true" instance="com.sjhy.plugin.ui.MainSetting"/>
</extensions>
<actions>
<!-- Add your actions here -->
</actions>
</idea-plugin>

View File

@ -0,0 +1,4 @@
button.type.mapper.copy.group=Copy Group
label.author.name=&\u4F5C\u8005\u540D\u79F0\uFF1A
label.default.encode=&\u9ED8\u8BA4\u7F16\u7801\uFF1A
label.type.mapper=&\u7C7B\u578B\u6620\u5C04\u7EC4\uFF1A

View File

@ -0,0 +1,19 @@
#* @vtlvariable name="tool" type="com.sjhy.plugin.tool.NameUtils" *#
#* @vtlvariable name="tableInfo" type="com.sjhy.plugin.entity.TableInfo" *#
public class ${tableInfo.name} {
#foreach($column in $tableInfo.fullColumn)
#if(${column.comment})//${column.comment}#end
private ${tool.getClsNameByFullName($column.type)} ${column.name};
#end
#foreach($column in $tableInfo.fullColumn)
public ${tool.getClsNameByFullName($column.type)} get${tool.firstUpperCase($column.name)}() {
return ${column.name};
}
public void set${tool.firstUpperCase($column.name)}(${tool.getClsNameByFullName($column.type)} ${column.name}) {
this.${column.name} = ${column.name};
}
#end
}

View File

@ -0,0 +1,9 @@
package com.sjhy.plugin;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
System.out.println("varchar(20)".matches("varchar(\\(\\d+\\))?"));
}
}

View File

@ -0,0 +1,35 @@
package com.sjhy.plugin.comm;
import com.sjhy.plugin.entity.TypeMapperGroup;
import com.sjhy.plugin.service.ConfigService;
import com.sjhy.plugin.service.impl.ConfigServiceImpl;
public abstract class ServiceComm {
protected ConfigService getConfigService() {
try {
return ConfigService.getInstance();
} catch (Exception e) {
e.printStackTrace();
return new ConfigServiceImpl();
}
}
protected TypeMapperGroup getCurrMapper() {
ConfigService configService = getConfigService();
return configService.getTypeMapperGroupMap().get(configService.getCurrTemplateGroupName());
}
protected void setCurrMapper(TypeMapperGroup typeMapper) {
ConfigService configService = getConfigService();
configService.getTypeMapperGroupMap().put(configService.getCurrTypeMapperGroupName(), typeMapper);
}
// protected Map<String, String> getCurrTemplate() {
// ConfigService configService = getConfigService();
// return configService.getTemplateGroup().get(configService.getCurrTemplateGroupName());
// }
//
// protected void setCurrTemplate(Map<String, String> template) {
// ConfigService configService = getConfigService();
// configService.getTemplateGroup().put(configService.getCurrTemplateGroupName(), template);
// }
}

View File

@ -0,0 +1,46 @@
package com.sjhy.plugin.entity;
import com.intellij.database.model.DasColumn;
public class ColumnInfo {
//原对象
private DasColumn obj;
//名称
private String name;
//注释
private String comment;
//类型
private String type;
public DasColumn getObj() {
return obj;
}
public void setObj(DasColumn obj) {
this.obj = obj;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}

View File

@ -0,0 +1,68 @@
package com.sjhy.plugin.entity;
import com.intellij.database.psi.DbTable;
import java.util.List;
public class TableInfo {
//原对象
private DbTable obj;
//表名
private String name;
//注释
private String comment;
//所有列
private List<ColumnInfo> fullColumn;
//主键列
private List<ColumnInfo> pkColumn;
//其他列
private List<ColumnInfo> otherColumn;
public DbTable getObj() {
return obj;
}
public void setObj(DbTable obj) {
this.obj = obj;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public List<ColumnInfo> getFullColumn() {
return fullColumn;
}
public void setFullColumn(List<ColumnInfo> fullColumn) {
this.fullColumn = fullColumn;
}
public List<ColumnInfo> getPkColumn() {
return pkColumn;
}
public void setPkColumn(List<ColumnInfo> pkColumn) {
this.pkColumn = pkColumn;
}
public List<ColumnInfo> getOtherColumn() {
return otherColumn;
}
public void setOtherColumn(List<ColumnInfo> otherColumn) {
this.otherColumn = otherColumn;
}
}

View File

@ -0,0 +1,56 @@
package com.sjhy.plugin.entity;
import java.io.Serializable;
public class TypeMapper implements Cloneable, Serializable {
//列类型
private String columnType;
//java类型
private String javaType;
public TypeMapper() {
}
public TypeMapper(String columnType, String javaType) {
this.columnType = columnType;
this.javaType = javaType;
}
public String getColumnType() {
return columnType;
}
public void setColumnType(String columnType) {
this.columnType = columnType;
}
public String getJavaType() {
return javaType;
}
public void setJavaType(String javaType) {
this.javaType = javaType;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TypeMapper that = (TypeMapper) o;
return columnType.equals(that.columnType) && javaType.equals(that.javaType);
}
@Override
public int hashCode() {
int result = columnType.hashCode();
result = 31 * result + javaType.hashCode();
return result;
}
}

View File

@ -0,0 +1,66 @@
package com.sjhy.plugin.entity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class TypeMapperGroup implements Cloneable, Serializable {
private String name;
private List<TypeMapper> typeMapperList;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<TypeMapper> getTypeMapperList() {
return typeMapperList;
}
public void setTypeMapperList(List<TypeMapper> typeMapperList) {
this.typeMapperList = typeMapperList;
}
public TypeMapperGroup cloneTypeMapperGroup() {
try {
return (TypeMapperGroup) clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return null;
}
@Override
public Object clone() throws CloneNotSupportedException {
TypeMapperGroup typeMapperGroup = (TypeMapperGroup) super.clone();
typeMapperGroup.typeMapperList = new ArrayList<>();
this.typeMapperList.forEach(typeMapper -> {
try {
typeMapperGroup.typeMapperList.add((TypeMapper) typeMapper.clone());
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
});
return typeMapperGroup;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TypeMapperGroup that = (TypeMapperGroup) o;
return name.equals(that.name) && typeMapperList.equals(that.typeMapperList);
}
@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + typeMapperList.hashCode();
return result;
}
}

View File

@ -0,0 +1,49 @@
package com.sjhy.plugin.entity;
import javax.swing.table.DefaultTableModel;
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);
}
}
}
@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);
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
super.setValueAt(aValue, rowIndex, columnIndex);
TypeMapper typeMapper = typeMapperGroup.getTypeMapperList().get(rowIndex);
if (columnIndex==0){
typeMapper.setColumnType((String) aValue);
} else {
typeMapper.setJavaType((String) aValue);
}
}
}

View File

@ -0,0 +1,37 @@
package com.sjhy.plugin.service;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.sjhy.plugin.entity.TypeMapperGroup;
import java.util.Map;
public interface ConfigService extends PersistentStateComponent<ConfigService> {
//默认名称
String DEFAULT_NAME = "Default";
static ConfigService getInstance() {
return ServiceManager.getService(ConfigService.class);
}
String getCurrTypeMapperGroupName();
void setCurrTypeMapperGroupName(String currTypeMapperGroupName);
Map<String, TypeMapperGroup> getTypeMapperGroupMap();
String getCurrTemplateGroupName();
void setCurrTemplateGroupName(String currTemplateGroupName);
// Map<String, Map<String, String>> getTemplateGroup();
String getEncode();
void setEncode(String encode);
String getAuthor();
void setAuthor(String author);
void setTypeMapperGroupMap(Map<String, TypeMapperGroup> typeMapperGroupMap);
}

View File

@ -0,0 +1,178 @@
package com.sjhy.plugin.service.impl;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.util.xmlb.XmlSerializerUtil;
import com.sjhy.plugin.entity.TypeMapper;
import com.sjhy.plugin.entity.TypeMapperGroup;
import com.sjhy.plugin.service.ConfigService;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@State(name = "EasyCodeSetting", storages = @Storage("$APP_CONFIG$/EasyCode-settings.xml"))
public class ConfigServiceImpl implements ConfigService {
//当前类型映射组名
private String currTypeMapperGroupName;
//类型映射组
private Map<String, TypeMapperGroup> typeMapperGroupMap;
//当前模板组名
private String currTemplateGroupName;
// //模板组
// private Map<String, Map<String, String>> templateGroup;
//默认编码
private String encode;
//作者
private String author;
private Boolean init = false;
public ConfigServiceImpl() {
initDefault();
}
@Nullable
@Override
public ConfigService getState() {
if (this.encode==null && !init) {
initDefault();
}
return this;
}
@Override
public void loadState(ConfigService configService) {
//重点没有数据时不要序列化
if (configService.getTypeMapperGroupMap().isEmpty()) {
return;
}
//加载配置信息
XmlSerializerUtil.copyBean(configService, this);
}
/**
* 初始化默认设置
*/
private void initDefault() {
if (this.encode==null) {
this.encode = "UTF-8";
}
if (this.author==null) {
this.author = "Mr.Wang";
}
if (this.currTemplateGroupName==null) {
this.currTemplateGroupName = DEFAULT_NAME;
}
if (this.currTypeMapperGroupName==null) {
this.currTypeMapperGroupName = DEFAULT_NAME;
}
// //配置默认模板
// if (this.templateGroup==null) {
// this.templateGroup = new LinkedHashMap<>();
// }
// Map<String, String> template = new LinkedHashMap<>();
// template.put("entity", loadTemplate("entity"));
// this.templateGroup.put(DEFAULT_NAME, template);
//配置默认类型映射
if (this.typeMapperGroupMap==null) {
this.typeMapperGroupMap = new LinkedHashMap<>();
}
TypeMapperGroup typeMapperGroup = new TypeMapperGroup();
List<TypeMapper> typeMapperList = new ArrayList<>();
typeMapperList.add(new TypeMapper("varchar(\\(\\d+\\))?", "java.lang.String"));
typeMapperList.add(new TypeMapper("decimal(\\(\\d+\\))?", "java.lang.Double"));
typeMapperList.add(new TypeMapper("integer", "java.lang.Integer"));
typeMapperList.add(new TypeMapper("int4", "java.lang.Integer"));
typeMapperList.add(new TypeMapper("int8", "java.lang.Long"));
typeMapperList.add(new TypeMapper("bigint", "java.lang.Long"));
typeMapperList.add(new TypeMapper("datetime", "java.util.Date"));
typeMapperList.add(new TypeMapper("timestamp", "java.util.Date"));
typeMapperGroup.setName(DEFAULT_NAME);
typeMapperGroup.setTypeMapperList(typeMapperList);
typeMapperGroupMap.put(DEFAULT_NAME, typeMapperGroup);
}
private String loadTemplate(String name) {
try {
return FileUtil.loadTextAndClose(getClass().getResourceAsStream("/template/"+name+".vm"));
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
//GET SET
@Override
public String getCurrTypeMapperGroupName() {
return currTypeMapperGroupName;
}
@Override
public void setCurrTypeMapperGroupName(String currTypeMapperGroupName) {
this.currTypeMapperGroupName = currTypeMapperGroupName;
}
@Override
public Map<String, TypeMapperGroup> getTypeMapperGroupMap() {
return typeMapperGroupMap;
}
@Override
public String getCurrTemplateGroupName() {
return currTemplateGroupName;
}
@Override
public void setCurrTemplateGroupName(String currTemplateGroupName) {
this.currTemplateGroupName = currTemplateGroupName;
}
// @Override
// public Map<String, Map<String, String>> getTemplateGroup() {
// return templateGroup;
// }
@Override
public String getEncode() {
return encode;
}
@Override
public void setEncode(String encode) {
this.encode = encode;
}
@Override
public String getAuthor() {
return author;
}
@Override
public void setAuthor(String author) {
this.author = author;
}
@Override
public void setTypeMapperGroupMap(Map<String, TypeMapperGroup> typeMapperGroupMap) {
this.typeMapperGroupMap = typeMapperGroupMap;
}
// public void setTemplateGroup(Map<String, Map<String, String>> templateGroup) {
// this.templateGroup = templateGroup;
// }
public Boolean getInit() {
return init;
}
public void setInit(Boolean init) {
this.init = init;
}
}

View File

@ -0,0 +1,62 @@
package com.sjhy.plugin.tool;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class NameUtils {
//单例模式
private static class Instance {
private static final NameUtils ME = new NameUtils();
}
public static NameUtils getInstance() {
return Instance.ME;
}
private NameUtils(){}
/**
* 首字母大写方法
* @param name 名称
* @return 结果
*/
public String firstUpperCase(String name) {
StringBuilder builder = new StringBuilder(name);
builder.replace(0, 1, name.substring(0, 1).toUpperCase());
return builder.toString();
}
/**
* 首字母小写方法
* @param name 名称
* @return 结果
*/
public String firstLowerCase(String name) {
StringBuilder builder = new StringBuilder(name);
builder.replace(0, 1, name.substring(0, 1).toLowerCase());
return builder.toString();
}
/**
* 通过java全名获取类名
* @param fullName 全名
* @return 类名
*/
public String getClsNameByFullName(String fullName) {
return fullName.substring(fullName.lastIndexOf('.')+1, fullName.length());
}
/**
* 下划线中横线命名转驼峰命名
* @param name 名称
* @return 结果
*/
public String getJavaName(String name) {
Pattern pattern = Pattern.compile("[-_]([a-z])");
Matcher matcher = pattern.matcher(name.toLowerCase());
StringBuffer buffer = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(buffer, matcher.group(1).toUpperCase());
}
matcher.appendTail(buffer);
return buffer.toString();
}
}

View File

@ -0,0 +1,79 @@
package com.sjhy.plugin.tool;
import com.intellij.database.model.DasColumn;
import com.intellij.database.psi.DbTable;
import com.intellij.database.util.DasUtil;
import com.intellij.util.containers.JBIterable;
import com.sjhy.plugin.comm.ServiceComm;
import com.sjhy.plugin.entity.ColumnInfo;
import com.sjhy.plugin.entity.TableInfo;
import com.sjhy.plugin.entity.TypeMapper;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.Pattern;
public class TableInfoUtils extends ServiceComm {
//单例模式
private static class Instance {
private static final TableInfoUtils ME = new TableInfoUtils();
}
public static TableInfoUtils getInstance() {
return Instance.ME;
}
private TableInfoUtils(){}
//注入命名工具类
private NameUtils nameUtils = NameUtils.getInstance();
/**
* 数据库表处理器
* @param dbTables 数据库表
* @return 处理结果
*/
public List<TableInfo> handler(Collection<DbTable> dbTables) {
List<TableInfo> result = new ArrayList<>();
dbTables.forEach(dbTable -> {
TableInfo tableInfo = new TableInfo();
tableInfo.setObj(dbTable);
tableInfo.setName(nameUtils.firstUpperCase(nameUtils.getJavaName(dbTable.getName().toLowerCase())));
tableInfo.setComment(dbTable.getComment());
tableInfo.setFullColumn(new ArrayList<>());
tableInfo.setPkColumn(new ArrayList<>());
tableInfo.setOtherColumn(new ArrayList<>());
JBIterable<? extends DasColumn> columns = DasUtil.getColumns(dbTable);
for (DasColumn column : columns) {
ColumnInfo columnInfo = new ColumnInfo();
columnInfo.setObj(column);
columnInfo.setType(getColumnType(column.getDataType().getSpecification()));
columnInfo.setName(nameUtils.getJavaName(column.getName().toLowerCase()));
columnInfo.setComment(column.getComment());
tableInfo.getFullColumn().add(columnInfo);
if(DasUtil.isPrimary(column)){
tableInfo.getPkColumn().add(columnInfo);
}else{
tableInfo.getOtherColumn().add(columnInfo);
}
}
result.add(tableInfo);
});
return result;
}
/**
* 通过映射获取对应的java类型类型名称
* @param typeName 列类型
* @return java类型
*/
private String getColumnType(String typeName) {
for (TypeMapper typeMapper : getCurrMapper().getTypeMapperList()) {
if (Pattern.compile(typeMapper.getColumnType(), Pattern.CASE_INSENSITIVE).matcher(typeName).matches()) {
return typeMapper.getJavaType();
}
}
//弹出消息框
JOptionPane.showMessageDialog(null, "未知类型:"+typeName, "温馨提示", JOptionPane.PLAIN_MESSAGE);
return "java.lang.Object";
}
}

View File

@ -0,0 +1,34 @@
package com.sjhy.plugin.tool;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
public class VelocityUtils {
//单例模式
private static class Instance {
private static final VelocityUtils ME = new VelocityUtils();
}
public static VelocityUtils getInstance() {
return Instance.ME;
}
private VelocityUtils(){
velocityEngine = new VelocityEngine();
}
private final VelocityEngine velocityEngine;
public String generate(String template, Map<String,Object> map) {
VelocityContext velocityContext = new VelocityContext();
if (map==null) {
map = new HashMap<>();
}
map.forEach(velocityContext::put);
StringWriter stringWriter = new StringWriter();
velocityEngine.evaluate(velocityContext, stringWriter, "", template);
return stringWriter.toString();
}
}

View File

@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.sjhy.plugin.ui.MainSetting">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="0" y="1" width="606" height="499"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="c1393" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="3e397"/>
<text resource-bundle="string" key="label.default.encode"/>
</properties>
</component>
<component id="9b299" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="94649"/>
<text resource-bundle="string" key="label.author.name"/>
</properties>
</component>
<component id="3e397" class="javax.swing.JComboBox" binding="encodeComboBox">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="UTF-8"/>
<item value="GBK"/>
<item value="GB2312"/>
<item value="UNICODE"/>
<item value="ANSI"/>
<item value="ASCII"/>
<item value="ISO-8859-1"/>
</model>
</properties>
</component>
<vspacer id="fd955">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="94649" class="javax.swing.JTextField" binding="authorTextField">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="0" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
</children>
</grid>
</form>

View File

@ -0,0 +1,67 @@
package com.sjhy.plugin.ui;
import com.intellij.openapi.options.Configurable;
import com.sjhy.plugin.comm.ServiceComm;
import com.sjhy.plugin.service.ConfigService;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
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;
}
private void init() {
//初始化数据
authorTextField.setText(configService.getAuthor());
encodeComboBox.setSelectedItem(configService.getEncode());
}
@Nls
@Override
public String getDisplayName() {
return "Easy Code";
}
@NotNull
@Override
public Configurable[] getConfigurables() {
Configurable[] result = new Configurable[1];
result[0] = new TypeMapperSetting(configService);
return result;
}
@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());
}
@Override
public void reset() {
init();
}
}

View File

@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.sjhy.plugin.ui.TypeMapperSetting">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="2" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="633" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="71036" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="448"/>
<text resource-bundle="string" key="label.type.mapper"/>
</properties>
</component>
<scrollpane id="abcb7">
<constraints>
<grid row="1" column="0" row-span="1" col-span="5" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="5cf20" class="javax.swing.JTable" binding="typeMapperTable">
<constraints/>
<properties/>
</component>
</children>
</scrollpane>
<hspacer id="5a2c9">
<constraints>
<grid row="0" column="4" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="448" class="javax.swing.JComboBox" binding="typeMapperComboBox">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="0" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<enabled value="true"/>
</properties>
</component>
<component id="7b067" class="javax.swing.JButton" binding="typeMapperCopyButton">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="actions/copy.png"/>
<toolTipText resource-bundle="string" key="button.type.mapper.copy.group"/>
</properties>
</component>
<grid id="c0236" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="5" 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>
<component id="24466" class="javax.swing.JButton" binding="addButton" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="general/add.png"/>
<text value=""/>
</properties>
</component>
<component id="222d7" class="javax.swing.JButton" binding="removeButton" default-binding="true">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="actions/delete.png"/>
</properties>
</component>
<vspacer id="99a7d">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
</children>
</grid>
<component id="f5cf8" class="javax.swing.JButton" binding="deleteButton" default-binding="true">
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="actions/delete.png"/>
</properties>
</component>
</children>
</grid>
<inspectionSuppressions>
<suppress inspection="NoLabelFor" id="abcb7"/>
</inspectionSuppressions>
</form>

View File

@ -0,0 +1,155 @@
package com.sjhy.plugin.ui;
import com.intellij.openapi.options.Configurable;
import com.sjhy.plugin.entity.TypeMapper;
import com.sjhy.plugin.entity.TypeMapperGroup;
import com.sjhy.plugin.entity.TypeMapperModel;
import com.sjhy.plugin.service.ConfigService;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
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) {
this.configService = configService;
//添加类型
addButton.addActionListener(e -> typeMapperModel.addRow(new TypeMapper("demoColumn", "java.lang.Object")));
//移除类型
removeButton.addActionListener(e -> {
int[] selectRows = typeMapperTable.getSelectedRows();
for (int i = selectRows.length-1; i>=0; i--){
typeMapperModel.removeRow(selectRows[i]);
}
});
//切换分组
typeMapperComboBox.addActionListener(e -> {
if (!init){
return;
}
String value = (String) typeMapperComboBox.getSelectedItem();
if (value==null) {
return;
}
if (currGroupName.equals(value)) {
return;
}
currGroupName = value;
refresh();
});
//复制分组按钮
typeMapperCopyButton.addActionListener(e -> {
String value = JOptionPane.showInputDialog(null, "Input Group Name:", currGroupName+" Copy");
if (value==null) {
return;
}
if (value.trim().length()==0){
JOptionPane.showMessageDialog(null, "Group Name Can't Is Empty!");
return;
}
if (typeMapperGroupMap.containsKey(value)){
JOptionPane.showMessageDialog(null, "Group Name Already exist!");
return;
}
TypeMapperGroup typeMapperGroup = typeMapperGroupMap.get(currGroupName).cloneTypeMapperGroup();
typeMapperGroup.setName(value);
typeMapperGroupMap.put(value, typeMapperGroup);
currGroupName = value;
refresh();
});
//删除分组
deleteButton.addActionListener(e -> {
int result = JOptionPane.showConfirmDialog(null, "Confirm Delete Group "+typeMapperComboBox.getSelectedItem()+"?", "温馨提示", JOptionPane.OK_CANCEL_OPTION);
if (result==0){
if(ConfigService.DEFAULT_NAME.equals(currGroupName)){
JOptionPane.showMessageDialog(null, "Can't Delete Default Group!");
return;
}
typeMapperGroupMap.remove(currGroupName);
currGroupName = ConfigService.DEFAULT_NAME;
refresh();
}
});
}
private void init() {
//复制数据
this.typeMapperGroupMap = new LinkedHashMap<>();
for (Map.Entry<String, TypeMapperGroup> entry : configService.getTypeMapperGroupMap().entrySet()) {
this.typeMapperGroupMap.put(entry.getKey(), entry.getValue().cloneTypeMapperGroup());
}
this.currGroupName = configService.getCurrTypeMapperGroupName();
//初始化表格
this.typeMapperModel = new TypeMapperModel();
this.typeMapperTable.setModel(typeMapperModel);
refresh();
}
@SuppressWarnings("unchecked")
private void refresh() {
init = false;
//初始化下拉框
this.typeMapperComboBox.removeAllItems();
typeMapperGroupMap.keySet().forEach(this.typeMapperComboBox::addItem);
this.typeMapperComboBox.setSelectedItem(this.currGroupName);
this.typeMapperModel.init(this.typeMapperGroupMap.get(currGroupName));
init = true;
}
@Nls
@Override
public String getDisplayName() {
return "Type Mapper";
}
@Nullable
@Override
public JComponent createComponent() {
init();
return mainPanel;
}
@Override
public boolean isModified() {
return !typeMapperGroupMap.equals(configService.getTypeMapperGroupMap()) || !currGroupName.equals(configService.getCurrTypeMapperGroupName());
}
@Override
public void apply() {
configService.setCurrTypeMapperGroupName(currGroupName);
configService.setTypeMapperGroupMap(typeMapperGroupMap);
}
@Override
public void reset() {
init();
}
}