mirror of https://gitee.com/makejava/EasyCode.git
完成表配置信息的储存功能
This commit is contained in:
parent
fa94f9f200
commit
becb877e1a
|
@ -68,4 +68,8 @@ public class CacheDataUtils {
|
|||
* 选中的model
|
||||
*/
|
||||
private Module selectModule;
|
||||
/**
|
||||
* 是否统一配置
|
||||
*/
|
||||
private boolean unifiedConfig;
|
||||
}
|
||||
|
|
|
@ -194,10 +194,10 @@ public class TableInfoUtils extends AbstractService {
|
|||
//排除部分字段,这些字段不进行保存
|
||||
tableInfo.setOtherColumn(null);
|
||||
tableInfo.setPkColumn(null);
|
||||
//获取原数据
|
||||
TableInfo oldTableInfo = handler(Collections.singletonList(tableInfo.getObj()), false).get(0);
|
||||
//将原始对象置空
|
||||
tableInfo.setObj(null);
|
||||
//获取原数据
|
||||
TableInfo oldTableInfo = handler(Collections.singletonList(cacheDataUtils.getSelectDbTable()), false).get(0);
|
||||
//将一致的原数据置空,保证数据的动态修改
|
||||
for (int i = 0; i < oldTableInfo.getFullColumn().size(); i++) {
|
||||
ColumnInfo columnInfo = oldTableInfo.getFullColumn().get(i);
|
||||
|
|
|
@ -5,6 +5,7 @@ import com.intellij.openapi.vfs.VirtualFileManager;
|
|||
import com.sjhy.plugin.entity.Callback;
|
||||
import com.sjhy.plugin.entity.TableInfo;
|
||||
import com.sjhy.plugin.entity.Template;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
import org.apache.velocity.app.VelocityEngine;
|
||||
|
||||
|
@ -13,48 +14,97 @@ import java.io.File;
|
|||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* Velocity工具类,主要用于代码生成
|
||||
*
|
||||
* @author makejava
|
||||
* @version 1.0.0
|
||||
* @since 2018/07/17 13:10
|
||||
*/
|
||||
public class VelocityUtils {
|
||||
//单例模式
|
||||
private static class Instance {
|
||||
private static final VelocityUtils ME = new VelocityUtils();
|
||||
}
|
||||
private volatile static VelocityUtils velocityUtils;
|
||||
|
||||
/**
|
||||
* 单例模式
|
||||
*/
|
||||
public static VelocityUtils getInstance() {
|
||||
return Instance.ME;
|
||||
if (velocityUtils == null) {
|
||||
synchronized (VelocityUtils.class) {
|
||||
if (velocityUtils == null) {
|
||||
velocityUtils = new VelocityUtils();
|
||||
}
|
||||
}
|
||||
}
|
||||
return velocityUtils;
|
||||
}
|
||||
|
||||
/**
|
||||
* 私有构造方法
|
||||
*/
|
||||
private VelocityUtils() {
|
||||
velocityEngine = new VelocityEngine();
|
||||
}
|
||||
|
||||
/**
|
||||
* Velocity引擎
|
||||
*/
|
||||
private final VelocityEngine velocityEngine;
|
||||
/**
|
||||
* 缓存工具类
|
||||
*/
|
||||
private CacheDataUtils cacheDataUtils = CacheDataUtils.getInstance();
|
||||
/**
|
||||
* 表信息工具
|
||||
*/
|
||||
private TableInfoUtils tableInfoUtils = TableInfoUtils.getInstance();
|
||||
/**
|
||||
* 命名工具类
|
||||
*/
|
||||
private NameUtils nameUtils = NameUtils.getInstance();
|
||||
/**
|
||||
* 文件工具类
|
||||
*/
|
||||
private FileUtils fileUtils = FileUtils.getInstance();
|
||||
/**
|
||||
* 导入包时过滤的包前缀
|
||||
*/
|
||||
private static final String FILTER_PACKAGENAME = "java.lang";
|
||||
|
||||
/**
|
||||
* 生成代码
|
||||
* @param template 模板对象
|
||||
* @param map 参数集合
|
||||
* @param encode 编码类型
|
||||
* @return 生成结果
|
||||
*/
|
||||
private String generate(String template, Map<String, Object> map, String encode) {
|
||||
VelocityContext velocityContext = new VelocityContext();
|
||||
if (map == null) {
|
||||
map = new HashMap<>();
|
||||
if (map != null) {
|
||||
map.forEach(velocityContext::put);
|
||||
}
|
||||
map.forEach(velocityContext::put);
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
velocityEngine.setProperty("input.encode", encode);
|
||||
velocityEngine.setProperty("output.encode", encode);
|
||||
velocityEngine.evaluate(velocityContext, stringWriter, "", template);
|
||||
velocityEngine.evaluate(velocityContext, stringWriter, "Velocity Code Generate", template);
|
||||
return stringWriter.toString();
|
||||
}
|
||||
|
||||
//设置全局参数
|
||||
/**
|
||||
* 设置全局参数
|
||||
* @return 全局参数
|
||||
*/
|
||||
private Map<String, Object> handlerMap() {
|
||||
ConfigInfo configInfo = ConfigInfo.getInstance();
|
||||
Map<String, Object> map = new HashMap<>(10);
|
||||
Map<String, Object> map = new HashMap<>(16);
|
||||
// 编码类型
|
||||
String encode = configInfo.getEncode();
|
||||
// 坐着名称
|
||||
String author = configInfo.getAuthor();
|
||||
// 表信息集合
|
||||
List<TableInfo> tableInfoList = tableInfoUtils.handler(cacheDataUtils.getDbTableList());
|
||||
// 选中的module
|
||||
Module selectModule = cacheDataUtils.getSelectModule();
|
||||
|
||||
map.put("encode", encode);
|
||||
|
@ -67,14 +117,20 @@ public class VelocityUtils {
|
|||
//设置的包名
|
||||
map.put("packageName", cacheDataUtils.getPackageName());
|
||||
if (selectModule != null) {
|
||||
//module路径
|
||||
//module绝对路径
|
||||
//noinspection ConstantConditions
|
||||
map.put("modulePath", selectModule.getModuleFile().getParent().getPath());
|
||||
map.put("moduleName", selectModule.getName());
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
//创建目录
|
||||
/**
|
||||
* 创建目录
|
||||
* @param savePath 路径
|
||||
* @return 保存结果
|
||||
*/
|
||||
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
|
||||
private boolean createPath(String savePath) {
|
||||
File path = new File(savePath);
|
||||
if (!new File(savePath).exists()) {
|
||||
|
@ -94,13 +150,18 @@ public class VelocityUtils {
|
|||
return true;
|
||||
}
|
||||
|
||||
//创建或覆盖文件
|
||||
/**
|
||||
* 创建或覆盖文件
|
||||
* @param file 文件
|
||||
* @return 是否成功
|
||||
*/
|
||||
private boolean coverFile(File file) {
|
||||
if (file.exists()) {
|
||||
if (file.isDirectory()) {
|
||||
JOptionPane.showMessageDialog(null, "Error,Save File Is Path!");
|
||||
return false;
|
||||
}
|
||||
// 是否覆盖
|
||||
int result = JOptionPane.showConfirmDialog(null, "File " + file.getName() + " Exists, Confirm Continue?", "Title Info", JOptionPane.OK_CANCEL_OPTION);
|
||||
return result == 0;
|
||||
} else {
|
||||
|
@ -110,48 +171,62 @@ public class VelocityUtils {
|
|||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成代码
|
||||
*/
|
||||
public void handler() {
|
||||
AtomicReference<String> savePath = new AtomicReference<>(cacheDataUtils.getSavePath());
|
||||
if (!createPath(savePath.get())) {
|
||||
if (!createPath(cacheDataUtils.getSavePath())) {
|
||||
return;
|
||||
}
|
||||
List<TableInfo> tableInfoList = tableInfoUtils.handler(cacheDataUtils.getDbTableList());
|
||||
// 获取覆盖的表配置信息
|
||||
List<TableInfo> tableInfoList = coverConfigInfo();
|
||||
List<Template> templateList = cacheDataUtils.getSelectTemplate();
|
||||
ConfigInfo configInfo = ConfigInfo.getInstance();
|
||||
// 获取编码信息
|
||||
String encode = configInfo.getEncode();
|
||||
// 获取默认的配置信息
|
||||
Map<String, Object> map = handlerMap();
|
||||
|
||||
tableInfoList.forEach(tableInfo -> {
|
||||
Callback callback = new Callback();
|
||||
// 表信息对象
|
||||
map.put("tableInfo", tableInfo);
|
||||
// 导入的包集合
|
||||
map.put("importList", getImportList(tableInfo));
|
||||
// 回调函数
|
||||
map.put("callback", callback);
|
||||
templateList.forEach(template -> {
|
||||
// 获取保存路径
|
||||
String savePath = cacheDataUtils.getSavePath();
|
||||
// 生成代码并去除两端空格
|
||||
String content = generate(template.getCode(), map, encode).trim();
|
||||
//保存的文件名
|
||||
String fileName = callback.getFileName();
|
||||
//保存路径
|
||||
String callbackSavePath = callback.getSavePath();
|
||||
//是否使用保存路径
|
||||
if (callbackSavePath != null && callbackSavePath.trim().length() > 0) {
|
||||
//是否使用回调中的保存路径
|
||||
if (!StringUtils.isEmpty(callbackSavePath)) {
|
||||
if (!createPath(callbackSavePath)) {
|
||||
return;
|
||||
}
|
||||
savePath.set(callbackSavePath);
|
||||
savePath = callbackSavePath;
|
||||
}
|
||||
// 当回调中没有保存文件名时
|
||||
if (fileName == null) {
|
||||
fileName = tableInfo.getName() + "Default.java";
|
||||
}
|
||||
File file = new File(new File(savePath.get()), fileName);
|
||||
File file = new File(new File(savePath), fileName);
|
||||
//覆盖或创建文件
|
||||
if (!coverFile(file)) {
|
||||
return;
|
||||
}
|
||||
// 写入文件
|
||||
fileUtils.write(file, content);
|
||||
});
|
||||
});
|
||||
|
@ -160,10 +235,55 @@ public class VelocityUtils {
|
|||
VirtualFileManager.getInstance().syncRefresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* 覆盖保存配置信息
|
||||
* @return 覆盖后的表配置信息
|
||||
*/
|
||||
private List<TableInfo> coverConfigInfo() {
|
||||
AtomicBoolean isSave = new AtomicBoolean(false);
|
||||
List<TableInfo> tableInfoList = tableInfoUtils.handler(cacheDataUtils.getDbTableList());
|
||||
// 将选中表中的没有保存配置信息的表进行保存
|
||||
tableInfoList.forEach(tableInfo -> {
|
||||
// 只有所有项目都是空的才会进行覆盖保存
|
||||
if (StringUtils.isEmpty(tableInfo.getSaveModelName()) && StringUtils.isEmpty(tableInfo.getSavePath()) && StringUtils.isEmpty(tableInfo.getSavePackageName())) {
|
||||
tableInfo.setSavePath(cacheDataUtils.getSavePath());
|
||||
tableInfo.setSavePackageName(cacheDataUtils.getPackageName());
|
||||
if (cacheDataUtils.getSelectModule()!=null) {
|
||||
tableInfo.setSaveModelName(cacheDataUtils.getSelectModule().getName());
|
||||
}
|
||||
// 保存信息
|
||||
tableInfoUtils.save(tableInfo);
|
||||
isSave.set(true);
|
||||
}
|
||||
});
|
||||
// 保存完毕后需要重新获取数据
|
||||
if (isSave.get()) {
|
||||
tableInfoList = tableInfoUtils.handler(cacheDataUtils.getDbTableList());
|
||||
}
|
||||
// 判断是否进行覆盖,(临时覆盖,不保存)
|
||||
if (cacheDataUtils.isUnifiedConfig()) {
|
||||
tableInfoList.forEach(tableInfo -> {
|
||||
tableInfo.setSavePath(cacheDataUtils.getSavePath());
|
||||
tableInfo.setSavePackageName(cacheDataUtils.getPackageName());
|
||||
if (cacheDataUtils.getSelectModule()!=null) {
|
||||
tableInfo.setSaveModelName(cacheDataUtils.getSelectModule().getName());
|
||||
}
|
||||
});
|
||||
}
|
||||
return tableInfoList;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取导入列表
|
||||
* @param tableInfo 表信息对象
|
||||
* @return 导入列表
|
||||
*/
|
||||
private Set<String> getImportList(TableInfo tableInfo) {
|
||||
// 创建一个自带排序的集合
|
||||
Set<String> result = new TreeSet<>();
|
||||
tableInfo.getFullColumn().forEach(columnInfo -> {
|
||||
if (!columnInfo.getType().startsWith("java.lang")) {
|
||||
if (!columnInfo.getType().startsWith(FILTER_PACKAGENAME)) {
|
||||
result.add(columnInfo.getType());
|
||||
}
|
||||
});
|
||||
|
|
|
@ -6,6 +6,7 @@ import com.sjhy.plugin.entity.*;
|
|||
import com.sjhy.plugin.tool.CacheDataUtils;
|
||||
import com.sjhy.plugin.tool.ConfigInfo;
|
||||
import com.sjhy.plugin.tool.TableInfoUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
|
@ -15,25 +16,66 @@ import java.awt.event.WindowAdapter;
|
|||
import java.awt.event.WindowEvent;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 表配置窗口
|
||||
*
|
||||
* @author makejava
|
||||
* @version 1.0.0
|
||||
* @since 2018/07/17 13:10
|
||||
*/
|
||||
public class ConfigTableDialog extends JDialog {
|
||||
/**
|
||||
* 主面板
|
||||
*/
|
||||
private JPanel contentPane;
|
||||
/**
|
||||
* 确认按钮
|
||||
*/
|
||||
private JButton buttonOK;
|
||||
/**
|
||||
* 取消按钮
|
||||
*/
|
||||
private JButton buttonCancel;
|
||||
/**
|
||||
* 表展示
|
||||
*/
|
||||
private JTable table;
|
||||
/**
|
||||
* 添加按钮
|
||||
*/
|
||||
private JButton addButton;
|
||||
|
||||
/**
|
||||
* 缓存数据工具类
|
||||
*/
|
||||
private CacheDataUtils cacheDataUtils = CacheDataUtils.getInstance();
|
||||
|
||||
/**
|
||||
* 表信息工具类
|
||||
*/
|
||||
private TableInfoUtils tableInfoUtils = TableInfoUtils.getInstance();
|
||||
|
||||
/**
|
||||
* 默认的表模型
|
||||
*/
|
||||
private DefaultTableModel tableModel;
|
||||
|
||||
/**
|
||||
* 列配置
|
||||
*/
|
||||
private List<ColumnConfig> columnConfigList;
|
||||
|
||||
/**
|
||||
* 表信息对象
|
||||
*/
|
||||
private TableInfo tableInfo;
|
||||
|
||||
/**
|
||||
* 初始化标记
|
||||
*/
|
||||
private boolean initFlag;
|
||||
/**
|
||||
* 配置信息对象
|
||||
*/
|
||||
private ConfigInfo configInfo = ConfigInfo.getInstance();
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*/
|
||||
public ConfigTableDialog() {
|
||||
setContentPane(contentPane);
|
||||
setModal(true);
|
||||
|
@ -59,24 +101,33 @@ public class ConfigTableDialog extends JDialog {
|
|||
initEvent();
|
||||
}
|
||||
|
||||
/**
|
||||
* 取人按钮回调
|
||||
*/
|
||||
private void onOK() {
|
||||
tableInfoUtils.save(tableInfo);
|
||||
// add your code here
|
||||
dispose();
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消按钮回调
|
||||
*/
|
||||
private void onCancel() {
|
||||
// add your code here if necessary
|
||||
dispose();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 初始化方法
|
||||
*/
|
||||
private void init() {
|
||||
initFlag = false;
|
||||
ConfigInfo configInfo = ConfigInfo.getInstance();
|
||||
ColumnConfigGroup columnConfigGroup = configInfo.getColumnConfigGroupMap().get(configInfo.getCurrColumnConfigGroupName());
|
||||
// 拿到列配置信息
|
||||
columnConfigList = getInitColumn(columnConfigGroup.getElementList());
|
||||
//绑定数据
|
||||
//读取表配置信息(一次只能配置一张表)
|
||||
tableInfo = tableInfoUtils.handler(Collections.singletonList(cacheDataUtils.getSelectDbTable())).get(0);
|
||||
|
||||
refresh();
|
||||
|
@ -84,6 +135,9 @@ public class ConfigTableDialog extends JDialog {
|
|||
initFlag = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新界面
|
||||
*/
|
||||
private void refresh() {
|
||||
tableModel = new DefaultTableModel();
|
||||
columnConfigList.forEach(columnConfig -> tableModel.addColumn(columnConfig.getTitle()));
|
||||
|
@ -95,6 +149,7 @@ public class ConfigTableDialog extends JDialog {
|
|||
dataList.add(columnInfo.getComment());
|
||||
//渲染附加数据
|
||||
if (columnInfo.getExt() != null && !columnInfo.getExt().isEmpty()) {
|
||||
// 跳过默认的3条数据
|
||||
for (int i = 3; i < tableModel.getColumnCount(); i++) {
|
||||
dataList.add(columnInfo.getExt().get(tableModel.getColumnName(i)));
|
||||
}
|
||||
|
@ -107,6 +162,7 @@ public class ConfigTableDialog extends JDialog {
|
|||
|
||||
//添加数据修改事件
|
||||
tableModel.addTableModelListener(e -> {
|
||||
// 一键编辑多行时不处理。
|
||||
if (e.getFirstRow() != e.getLastRow()) {
|
||||
return;
|
||||
}
|
||||
|
@ -134,18 +190,19 @@ public class ConfigTableDialog extends JDialog {
|
|||
columnInfo.setComment((String) val);
|
||||
break;
|
||||
default:
|
||||
if (columnInfo.getExt() == null) {
|
||||
columnInfo.setExt(new HashMap<>(10));
|
||||
}
|
||||
String title = tableModel.getColumnName(column);
|
||||
columnInfo.getExt().put(title, val);
|
||||
break;
|
||||
}
|
||||
if (column > 2) {
|
||||
if (columnInfo.getExt() == null) {
|
||||
columnInfo.setExt(new HashMap<>());
|
||||
}
|
||||
String title = tableModel.getColumnName(column);
|
||||
columnInfo.getExt().put(title, val);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化事件
|
||||
*/
|
||||
private void initEvent() {
|
||||
//添加元素事件
|
||||
addButton.addActionListener(e -> {
|
||||
|
@ -153,10 +210,7 @@ public class ConfigTableDialog extends JDialog {
|
|||
return;
|
||||
}
|
||||
String value = JOptionPane.showInputDialog(null, "Input Column Name:", "Demo");
|
||||
if (value == null) {
|
||||
return;
|
||||
}
|
||||
if (value.trim().length() == 0) {
|
||||
if (StringUtils.isEmpty(value)) {
|
||||
JOptionPane.showMessageDialog(null, "Column Name Can't Is Empty!");
|
||||
return;
|
||||
}
|
||||
|
@ -175,6 +229,10 @@ public class ConfigTableDialog extends JDialog {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新列编辑器
|
||||
* @param columnConfigList 列配置集合
|
||||
*/
|
||||
private void refreshColumnEditor(List<ColumnConfig> columnConfigList) {
|
||||
columnConfigList.forEach(columnConfig -> {
|
||||
TableColumn tableColumn = table.getColumn(columnConfig.getTitle());
|
||||
|
@ -186,6 +244,11 @@ public class ConfigTableDialog extends JDialog {
|
|||
tableColumn.setCellEditor(new ComboBoxCellEditor() {
|
||||
@Override
|
||||
protected List<String> getComboBoxItems() {
|
||||
String selectValue = columnConfig.getSelectValue();
|
||||
if (StringUtils.isEmpty(selectValue)) {
|
||||
//noinspection unchecked
|
||||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
return Arrays.asList(columnConfig.getSelectValue().split(","));
|
||||
}
|
||||
});
|
||||
|
@ -205,6 +268,11 @@ public class ConfigTableDialog extends JDialog {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取初始列
|
||||
* @param columnConfigList 列配置集合
|
||||
* @return 初始列信息
|
||||
*/
|
||||
private List<ColumnConfig> getInitColumn(List<ColumnConfig> columnConfigList) {
|
||||
List<ColumnConfig> result = new ArrayList<>();
|
||||
result.add(new ColumnConfig("name", ColumnConfigType.TEXT));
|
||||
|
@ -214,6 +282,9 @@ public class ConfigTableDialog extends JDialog {
|
|||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开窗口
|
||||
*/
|
||||
public void open() {
|
||||
setTitle("Config Table " + cacheDataUtils.getSelectDbTable().getName());
|
||||
pack();
|
||||
|
|
|
@ -49,7 +49,7 @@
|
|||
</grid>
|
||||
</children>
|
||||
</grid>
|
||||
<grid id="e3588" layout-manager="GridLayoutManager" row-count="5" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<grid id="e3588" layout-manager="GridLayoutManager" row-count="6" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
|
@ -125,7 +125,7 @@
|
|||
<grid id="7831c" binding="templatePanel" layout-manager="GridLayoutManager" row-count="1" 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="3" column="1" row-span="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
<grid row="3" column="1" row-span="3" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
|
@ -133,7 +133,7 @@
|
|||
</grid>
|
||||
<component id="91ae2" class="javax.swing.JLabel">
|
||||
<constraints>
|
||||
<grid row="3" column="0" row-span="2" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
<grid row="3" column="0" row-span="3" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text resource-bundle="string" key="label.template"/>
|
||||
|
@ -149,9 +149,17 @@
|
|||
</component>
|
||||
<vspacer id="98aa7">
|
||||
<constraints>
|
||||
<grid row="4" column="2" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
||||
<grid row="5" column="2" 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="5e5a1" class="javax.swing.JCheckBox" binding="unifiedConfig">
|
||||
<constraints>
|
||||
<grid row="4" 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>
|
||||
<text resource-bundle="string" key="label.unified.config"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
</children>
|
||||
|
|
|
@ -6,11 +6,14 @@ import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
|
|||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiPackage;
|
||||
import com.sjhy.plugin.entity.TableInfo;
|
||||
import com.sjhy.plugin.entity.Template;
|
||||
import com.sjhy.plugin.entity.TemplateGroup;
|
||||
import com.sjhy.plugin.tool.CacheDataUtils;
|
||||
import com.sjhy.plugin.tool.ConfigInfo;
|
||||
import com.sjhy.plugin.tool.TableInfoUtils;
|
||||
import com.sjhy.plugin.tool.VelocityUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
@ -19,6 +22,7 @@ import java.awt.event.WindowAdapter;
|
|||
import java.awt.event.WindowEvent;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -29,21 +33,70 @@ import java.util.List;
|
|||
* @since 2018/07/17 13:10
|
||||
*/
|
||||
public class SelectSavePath extends JDialog {
|
||||
/**
|
||||
* 主面板
|
||||
*/
|
||||
private JPanel contentPane;
|
||||
/**
|
||||
* 确认按钮
|
||||
*/
|
||||
private JButton buttonOK;
|
||||
/**
|
||||
* 取消按钮
|
||||
*/
|
||||
private JButton buttonCancel;
|
||||
/**
|
||||
* 模型下拉框
|
||||
*/
|
||||
private JComboBox moduleComboBox;
|
||||
/**
|
||||
* 包字段
|
||||
*/
|
||||
private JTextField packageField;
|
||||
/**
|
||||
* 路径字段
|
||||
*/
|
||||
private JTextField pathField;
|
||||
/**
|
||||
* 包选择按钮
|
||||
*/
|
||||
private JButton packageChooseButton;
|
||||
/**
|
||||
* 路径选择按钮
|
||||
*/
|
||||
private JButton pathChooseButton;
|
||||
/**
|
||||
* 模板全选框
|
||||
*/
|
||||
private JCheckBox allCheckBox;
|
||||
/**
|
||||
* 模板面板
|
||||
*/
|
||||
private JPanel templatePanel;
|
||||
|
||||
private CacheDataUtils cacheDataUtils = CacheDataUtils.getInstance();
|
||||
/**
|
||||
* 统一配置复选框
|
||||
*/
|
||||
private JCheckBox unifiedConfig;
|
||||
/**
|
||||
* 所有模板复选框
|
||||
*/
|
||||
private List<JCheckBox> checkBoxList = new ArrayList<>();
|
||||
/**
|
||||
* 数据缓存工具类
|
||||
*/
|
||||
private CacheDataUtils cacheDataUtils = CacheDataUtils.getInstance();
|
||||
/**
|
||||
* 模板组对象
|
||||
*/
|
||||
private TemplateGroup templateGroup;
|
||||
/**
|
||||
* 表信息工具
|
||||
*/
|
||||
private TableInfoUtils tableInfoUtils = TableInfoUtils.getInstance();
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*/
|
||||
public SelectSavePath() {
|
||||
ConfigInfo configInfo = ConfigInfo.getInstance();
|
||||
this.templateGroup = configInfo.getTemplateGroupMap().get(configInfo.getCurrTemplateGroupName());
|
||||
|
@ -69,6 +122,11 @@ public class SelectSavePath extends JDialog {
|
|||
contentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取已经选中的模板
|
||||
*
|
||||
* @return 模板对象集合
|
||||
*/
|
||||
private List<Template> getSelectTemplate() {
|
||||
List<String> selectTemplateNameList = new ArrayList<>();
|
||||
checkBoxList.forEach(jCheckBox -> {
|
||||
|
@ -88,6 +146,9 @@ public class SelectSavePath extends JDialog {
|
|||
return selectTemplateList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 确认按钮回调事件
|
||||
*/
|
||||
private void onOK() {
|
||||
List<Template> selectTemplateList = getSelectTemplate();
|
||||
if (selectTemplateList.isEmpty()) {
|
||||
|
@ -99,19 +160,27 @@ public class SelectSavePath extends JDialog {
|
|||
JOptionPane.showMessageDialog(null, "Can't Select Save Path!");
|
||||
return;
|
||||
}
|
||||
// 设置好配置信息
|
||||
cacheDataUtils.setSavePath(savePath);
|
||||
cacheDataUtils.setSelectTemplate(selectTemplateList);
|
||||
cacheDataUtils.setPackageName(packageField.getText());
|
||||
cacheDataUtils.setSelectModule(getSelectModule());
|
||||
cacheDataUtils.setUnifiedConfig(unifiedConfig.isSelected());
|
||||
// 生成代码
|
||||
VelocityUtils.getInstance().handler();
|
||||
dispose();
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消按钮回调事件
|
||||
*/
|
||||
private void onCancel() {
|
||||
dispose();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
/**
|
||||
* 初始化方法
|
||||
*/
|
||||
private void init() {
|
||||
//添加模板组
|
||||
checkBoxList.clear();
|
||||
|
@ -121,23 +190,25 @@ public class SelectSavePath extends JDialog {
|
|||
checkBoxList.add(checkBox);
|
||||
templatePanel.add(checkBox);
|
||||
});
|
||||
//设置全选事件
|
||||
//添加全选事件
|
||||
allCheckBox.addActionListener(e -> checkBoxList.forEach(jCheckBox -> jCheckBox.setSelected(allCheckBox.isSelected())));
|
||||
|
||||
//初始化Module选择
|
||||
for (Module module : cacheDataUtils.getModules()) {
|
||||
//noinspection unchecked
|
||||
moduleComboBox.addItem(module.getName());
|
||||
}
|
||||
|
||||
//选择包
|
||||
//添加包选择事件
|
||||
packageChooseButton.addActionListener(e -> {
|
||||
PackageChooserDialog dialog = new PackageChooserDialog("Package Chooser", cacheDataUtils.getProject());
|
||||
dialog.show();
|
||||
PsiPackage psiPackage = dialog.getSelectedPackage();
|
||||
if (psiPackage != null) {
|
||||
packageField.setText(psiPackage.getQualifiedName());
|
||||
// 刷新路径
|
||||
refreshPath();
|
||||
}
|
||||
refreshPath();
|
||||
});
|
||||
|
||||
//初始化路径
|
||||
|
@ -145,13 +216,33 @@ public class SelectSavePath extends JDialog {
|
|||
|
||||
//选择路径
|
||||
pathChooseButton.addActionListener(e -> {
|
||||
@SuppressWarnings("ConstantConditions") VirtualFile virtualFile = FileChooser.chooseFile(FileChooserDescriptorFactory.createSingleFolderDescriptor(), cacheDataUtils.getProject(), getSelectModule().getModuleFile().getParent());
|
||||
//将当前选中的model设置为基础路径
|
||||
//noinspection ConstantConditions
|
||||
VirtualFile virtualFile = FileChooser.chooseFile(FileChooserDescriptorFactory.createSingleFolderDescriptor(), cacheDataUtils.getProject(), getSelectModule().getModuleFile().getParent());
|
||||
if (virtualFile != null) {
|
||||
pathField.setText(virtualFile.getPath());
|
||||
}
|
||||
});
|
||||
|
||||
// 获取选中的表信息(鼠标右键的那张表)
|
||||
TableInfo tableInfo = tableInfoUtils.handler(Collections.singletonList(cacheDataUtils.getSelectDbTable())).get(0);
|
||||
// 设置默认配置信息
|
||||
if (!StringUtils.isEmpty(tableInfo.getSaveModelName())) {
|
||||
moduleComboBox.setSelectedItem(tableInfo.getSaveModelName());
|
||||
}
|
||||
if (!StringUtils.isEmpty(tableInfo.getSavePackageName())) {
|
||||
packageField.setText(tableInfo.getSavePackageName());
|
||||
}
|
||||
if (!StringUtils.isEmpty(tableInfo.getSavePath())) {
|
||||
pathField.setText(tableInfo.getSavePath());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取选中的Module
|
||||
*
|
||||
* @return 选中的Module
|
||||
*/
|
||||
private Module getSelectModule() {
|
||||
String name = (String) moduleComboBox.getSelectedItem();
|
||||
for (Module module : cacheDataUtils.getModules()) {
|
||||
|
@ -162,14 +253,20 @@ public class SelectSavePath extends JDialog {
|
|||
return cacheDataUtils.getModules()[0];
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
/**
|
||||
* 获取基本路径
|
||||
* @return 基本路径
|
||||
*/
|
||||
private String getBasePath() {
|
||||
Module module = getSelectModule();
|
||||
//noinspection ConstantConditions
|
||||
String baseDir = module.getModuleFile().getParent().getPath();
|
||||
// 针对Maven项目
|
||||
File file = new File(baseDir + "/src/main/java");
|
||||
if (file.exists()) {
|
||||
return file.getAbsolutePath();
|
||||
}
|
||||
// 针对普通Java项目
|
||||
file = new File(baseDir + "/src");
|
||||
if (file.exists()) {
|
||||
return file.getAbsolutePath();
|
||||
|
@ -177,15 +274,23 @@ public class SelectSavePath extends JDialog {
|
|||
return baseDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新目录
|
||||
*/
|
||||
private void refreshPath() {
|
||||
String packageName = packageField.getText();
|
||||
// 获取基本路径
|
||||
String path = getBasePath();
|
||||
if (!packageName.isEmpty()) {
|
||||
// 如果存在包路径,添加包路径
|
||||
if (!StringUtils.isEmpty(packageName)) {
|
||||
path += "\\" + packageName.replaceAll("\\.", "\\\\");
|
||||
}
|
||||
pathField.setText(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开窗口
|
||||
*/
|
||||
public void open() {
|
||||
this.pack();
|
||||
setLocationRelativeTo(null);
|
||||
|
|
|
@ -10,4 +10,5 @@ label.package=&Package:
|
|||
label.path=&Path:
|
||||
label.template=Template:
|
||||
label.template.group=&\u6A21\u677F\u7EC4\uFF1A
|
||||
label.type.mapper=&\u7C7B\u578B\u6620\u5C04\u7EC4\uFF1A
|
||||
label.type.mapper=&\u7C7B\u578B\u6620\u5C04\u7EC4\uFF1A
|
||||
label.unified.config=\u7EDF\u4E00\u914D\u7F6E
|
Loading…
Reference in New Issue