diff --git a/src/main/java/com/sjhy/plugin/ui/SelectSavePath.form b/src/main/java/com/sjhy/plugin/ui/SelectSavePath.form
index b8a87f2..cd3c4fd 100644
--- a/src/main/java/com/sjhy/plugin/ui/SelectSavePath.form
+++ b/src/main/java/com/sjhy/plugin/ui/SelectSavePath.form
@@ -8,7 +8,7 @@
-
+
@@ -18,13 +18,13 @@
-
+
-
+
@@ -33,7 +33,7 @@
-
+
@@ -41,7 +41,7 @@
-
+
@@ -50,7 +50,7 @@
-
+
@@ -58,7 +58,7 @@
-
+
@@ -66,16 +66,15 @@
-
+
-
-
+
-
+
@@ -83,44 +82,20 @@
-
+
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -129,35 +104,36 @@
-
+
-
-
-
-
-
-
-
-
-
-
+
-
+
-
+
-
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/main/java/com/sjhy/plugin/ui/SelectSavePath.java b/src/main/java/com/sjhy/plugin/ui/SelectSavePath.java
index 916f261..5679744 100644
--- a/src/main/java/com/sjhy/plugin/ui/SelectSavePath.java
+++ b/src/main/java/com/sjhy/plugin/ui/SelectSavePath.java
@@ -14,22 +14,23 @@ import com.sjhy.plugin.dict.GlobalDict;
import com.sjhy.plugin.dto.SettingsStorageDTO;
import com.sjhy.plugin.entity.TableInfo;
import com.sjhy.plugin.entity.Template;
-import com.sjhy.plugin.entity.TemplateGroup;
import com.sjhy.plugin.service.CodeGenerateService;
import com.sjhy.plugin.service.SettingsStorageService;
import com.sjhy.plugin.service.TableInfoService;
-import com.sjhy.plugin.tool.*;
+import com.sjhy.plugin.tool.CacheDataUtils;
+import com.sjhy.plugin.tool.ModuleUtils;
+import com.sjhy.plugin.tool.ProjectUtils;
+import com.sjhy.plugin.tool.StringUtils;
+import com.sjhy.plugin.ui.component.TemplateSelectComponent;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
-import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
-import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
@@ -69,10 +70,6 @@ public class SelectSavePath extends DialogWrapper {
* 路径选择按钮
*/
private JButton pathChooseButton;
- /**
- * 模板全选框
- */
- private JCheckBox allCheckBox;
/**
* 模板面板
*/
@@ -85,22 +82,10 @@ public class SelectSavePath extends DialogWrapper {
* 禁止提示复选框
*/
private JCheckBox titleConfig;
- /**
- * 分组选择框
- */
- private JComboBox groupComboBox;
- /**
- * 所有模板复选框
- */
- private List checkBoxList = new ArrayList<>();
/**
* 数据缓存工具类
*/
private CacheDataUtils cacheDataUtils = CacheDataUtils.getInstance();
- /**
- * 模板组对象
- */
- private TemplateGroup templateGroup;
/**
* 表信息服务
*/
@@ -123,6 +108,11 @@ public class SelectSavePath extends DialogWrapper {
*/
private boolean entityMode;
+ /**
+ * 模板选择组件
+ */
+ private TemplateSelectComponent templateSelectComponent;
+
/**
* 构造方法
*/
@@ -146,7 +136,6 @@ public class SelectSavePath extends DialogWrapper {
this.project = project;
this.tableInfoService = TableInfoService.getInstance(project);
this.codeGenerateService = CodeGenerateService.getInstance(project);
- this.templateGroup = CurrGroupUtils.getCurrTemplateGroup();
// 初始化module,存在资源路径的排前面
this.moduleList = new LinkedList<>();
for (Module module : ModuleManager.getInstance(project).getModules()) {
@@ -157,48 +146,32 @@ public class SelectSavePath extends DialogWrapper {
this.moduleList.add(module);
}
}
- initPanel();
+ this.initPanel();
+ this.refreshData();
+ this.initEvent();
init();
setTitle(GlobalDict.TITLE_INFO);
}
+ private void initEvent() {
+
+ }
+
+ private void refreshData() {
+
+ }
+
@Override
protected void doOKAction() {
onOK();
super.doOKAction();
}
- /**
- * 获取已经选中的模板
- *
- * @return 模板对象集合
- */
- private List getSelectTemplate() {
- // 获取到已选择的复选框
- List selectTemplateNameList = new ArrayList<>();
- checkBoxList.forEach(jCheckBox -> {
- if (jCheckBox.isSelected()) {
- selectTemplateNameList.add(jCheckBox.getText());
- }
- });
- List selectTemplateList = new ArrayList<>(selectTemplateNameList.size());
- if (selectTemplateNameList.isEmpty()) {
- return selectTemplateList;
- }
- // 将复选框转换成对应的模板对象
- templateGroup.getElementList().forEach(template -> {
- if (selectTemplateNameList.contains(template.getName())) {
- selectTemplateList.add(template);
- }
- });
- return selectTemplateList;
- }
-
/**
* 确认按钮回调事件
*/
private void onOK() {
- List selectTemplateList = getSelectTemplate();
+ List selectTemplateList = templateSelectComponent.getAllSelectedTemplate();
// 如果选择的模板是空的
if (selectTemplateList.isEmpty()) {
Messages.showWarningDialog("Can't Select Template!", GlobalDict.TITLE_INFO);
@@ -232,7 +205,7 @@ public class SelectSavePath extends DialogWrapper {
tableInfo.setSavePath(savePath);
tableInfo.setSavePackageName(packageField.getText());
tableInfo.setPreName(preField.getText());
- tableInfo.setTemplateGroupName((String) groupComboBox.getSelectedItem());
+ tableInfo.setTemplateGroupName(templateSelectComponent.getselectedGroupName());
Module module = getSelectModule();
if (module != null) {
tableInfo.setSaveModelName(module.getName());
@@ -240,34 +213,7 @@ public class SelectSavePath extends DialogWrapper {
tableInfoService.save(tableInfo);
// 生成代码
- codeGenerateService.generateByUnifiedConfig(getSelectTemplate(), unifiedConfig.isSelected(), !titleConfig.isSelected(), this.entityMode);
- }
-
-
- /**
- * 初始化模板组
- */
- private void initTemplateGroup() {
- //添加模板组
- checkBoxList.clear();
- templatePanel.removeAll();
- templatePanel.repaint();
- templatePanel.setLayout(new GridLayout(6, 2));
- templateGroup.getElementList().forEach(template -> {
- JCheckBox checkBox = new JCheckBox(template.getName());
- checkBoxList.add(checkBox);
- templatePanel.add(checkBox);
- });
- // 移除所有旧事件
- ActionListener[] actionListeners = allCheckBox.getActionListeners();
- if (actionListeners != null && actionListeners.length > 0) {
- for (ActionListener actionListener : actionListeners) {
- allCheckBox.removeActionListener(actionListener);
- }
- }
- //添加全选事件
- allCheckBox.addActionListener(e -> checkBoxList.forEach(jCheckBox -> jCheckBox.setSelected(allCheckBox.isSelected())));
- allCheckBox.setSelected(false);
+ codeGenerateService.generateByUnifiedConfig(selectTemplateList, unifiedConfig.isSelected(), !titleConfig.isSelected(), this.entityMode);
}
/**
@@ -275,7 +221,8 @@ public class SelectSavePath extends DialogWrapper {
*/
private void initPanel() {
// 初始化模板组
- initTemplateGroup();
+ this.templateSelectComponent = new TemplateSelectComponent();
+ templatePanel.add(this.templateSelectComponent.getMainPanel(), BorderLayout.CENTER);
//初始化Module选择
for (Module module : this.moduleList) {
@@ -345,7 +292,7 @@ public class SelectSavePath extends DialogWrapper {
});
// 获取选中的表信息(鼠标右键的那张表),并提示未知类型
- TableInfo tableInfo = null;
+ TableInfo tableInfo;
if(entityMode) {
tableInfo = tableInfoService.getTableInfoAndConfigByPsiClass(cacheDataUtils.getSelectPsiClass());
} else {
@@ -367,24 +314,9 @@ public class SelectSavePath extends DialogWrapper {
if (!StringUtils.isEmpty(tableInfo.getTemplateGroupName())) {
if (settings.getTemplateGroupMap().containsKey(tableInfo.getTemplateGroupName())) {
groupName = tableInfo.getTemplateGroupName();
- this.templateGroup = settings.getTemplateGroupMap().get(groupName);
- // 选中的模板组发生变化,尝试重新初始化
- initTemplateGroup();
}
}
- for (String key : settings.getTemplateGroupMap().keySet()) {
- groupComboBox.addItem(key);
- }
- groupComboBox.setSelectedItem(groupName);
- groupComboBox.addActionListener(e -> {
- String selectedItem = (String) groupComboBox.getSelectedItem();
- if (this.templateGroup.getName().equals(selectedItem)) {
- return;
- }
- this.templateGroup = settings.getTemplateGroupMap().get(selectedItem);
- // 选中的模板组发生变化,尝试重新初始化
- initTemplateGroup();
- });
+ templateSelectComponent.setSelectedGroupName(groupName);
String savePath = tableInfo.getSavePath();
if (!StringUtils.isEmpty(savePath)) {
// 判断是否需要拼接项目路径
diff --git a/src/main/java/com/sjhy/plugin/ui/component/TemplateSelectComponent.java b/src/main/java/com/sjhy/plugin/ui/component/TemplateSelectComponent.java
new file mode 100644
index 0000000..61b1dcf
--- /dev/null
+++ b/src/main/java/com/sjhy/plugin/ui/component/TemplateSelectComponent.java
@@ -0,0 +1,136 @@
+package com.sjhy.plugin.ui.component;
+
+import com.intellij.openapi.ui.ComboBox;
+import com.intellij.ui.components.JBCheckBox;
+import com.sjhy.plugin.entity.Template;
+import com.sjhy.plugin.entity.TemplateGroup;
+import com.sjhy.plugin.service.SettingsStorageService;
+import com.sjhy.plugin.tool.StringUtils;
+import lombok.Getter;
+
+import javax.swing.*;
+import java.awt.*;
+import java.awt.event.ActionEvent;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * 模板选择组件
+ *
+ * @author makejava
+ * @version 1.0.0
+ * @date 2021/08/16 16:18
+ */
+public class TemplateSelectComponent {
+ @Getter
+ private JPanel mainPanel;
+
+ /**
+ * 分组
+ */
+ private ComboBox groupComboBox;
+
+ /**
+ * 选中所有复选框
+ */
+ private JBCheckBox allCheckbox;
+
+ /**
+ * 所有复选框
+ */
+ private List checkBoxList;
+
+ /**
+ * 模板面板
+ */
+ private JPanel templatePanel;
+
+ public TemplateSelectComponent() {
+ this.init();
+ }
+
+ private void init() {
+ this.mainPanel = new JPanel(new BorderLayout());
+ JPanel topPanel = new JPanel(new BorderLayout());
+ this.groupComboBox = new ComboBox<>();
+ this.groupComboBox.setSwingPopup(false);
+ this.groupComboBox.addActionListener(new AbstractAction() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ String groupName = (String) groupComboBox.getSelectedItem();
+ if (StringUtils.isEmpty(groupName)) {
+ return;
+ }
+ refreshTemplatePanel(groupName);
+ }
+ });
+ this.allCheckbox = new JBCheckBox("All");
+ this.allCheckbox.addActionListener(new AbstractAction() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ if (checkBoxList == null) {
+ return;
+ }
+ for (JBCheckBox checkBox : checkBoxList) {
+ checkBox.setSelected(allCheckbox.isSelected());
+ }
+ }
+ });
+ topPanel.add(this.groupComboBox, BorderLayout.WEST);
+ topPanel.add(this.allCheckbox, BorderLayout.EAST);
+ this.mainPanel.add(topPanel, BorderLayout.NORTH);
+ this.templatePanel = new JPanel(new GridLayout(-1, 2));
+ this.mainPanel.add(templatePanel, BorderLayout.CENTER);
+ this.refreshData();
+ }
+
+ private void refreshData() {
+ this.groupComboBox.removeAllItems();
+ for (String groupName : SettingsStorageService.getSettingsStorage().getTemplateGroupMap().keySet()) {
+ this.groupComboBox.addItem(groupName);
+ }
+ }
+
+ private void refreshTemplatePanel(String groupName) {
+ this.allCheckbox.setSelected(false);
+ this.templatePanel.removeAll();
+ this.checkBoxList = new ArrayList<>();
+ TemplateGroup templateGroup = SettingsStorageService.getSettingsStorage().getTemplateGroupMap().get(groupName);
+ for (Template template : templateGroup.getElementList()) {
+ JBCheckBox checkBox = new JBCheckBox(template.getName());
+ this.checkBoxList.add(checkBox);
+ this.templatePanel.add(checkBox);
+ }
+ this.mainPanel.updateUI();
+ }
+
+ public String getselectedGroupName() {
+ return (String) this.groupComboBox.getSelectedItem();
+ }
+
+ public void setSelectedGroupName(String groupName) {
+ this.groupComboBox.setSelectedItem(groupName);
+ }
+
+ public List getAllSelectedTemplate() {
+ String groupName = (String) this.groupComboBox.getSelectedItem();
+ if (StringUtils.isEmpty(groupName)) {
+ return Collections.emptyList();
+ }
+ TemplateGroup templateGroup = SettingsStorageService.getSettingsStorage().getTemplateGroupMap().get(groupName);
+ Map map = templateGroup.getElementList().stream().collect(Collectors.toMap(Template::getName, val -> val));
+ List result = new ArrayList<>();
+ for (JBCheckBox checkBox : this.checkBoxList) {
+ if (checkBox.isSelected()) {
+ Template template = map.get(checkBox.getText());
+ if (template != null) {
+ result.add(template);
+ }
+ }
+ }
+ return result;
+ }
+}