package club.bigtian.mf.plugin.windows;
import club.bigtian.mf.plugin.core.RenderMybatisFlexTemplate;
import club.bigtian.mf.plugin.core.Template;
import club.bigtian.mf.plugin.core.config.MybatisFlexConfig;
import club.bigtian.mf.plugin.core.listener.ComBoxDocumentListener;
import club.bigtian.mf.plugin.core.persistent.MybatisFlexPluginConfigData;
import club.bigtian.mf.plugin.core.render.TableListCellRenderer;
import club.bigtian.mf.plugin.core.search.InvertedIndexSearch;
import club.bigtian.mf.plugin.core.util.Package;
import club.bigtian.mf.plugin.core.util.*;
import club.bigtian.mf.plugin.core.validator.InputValidatorImpl;
import club.bigtian.mf.plugin.entity.ColumnInfo;
import club.bigtian.mf.plugin.entity.TableInfo;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.intellij.codeInspection.reference.RefUtil;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComponentValidator;
import com.intellij.openapi.ui.FixedSizeButton;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.ui.DocumentAdapter;
import com.intellij.ui.components.fields.ExtendableTextField;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
import java.util.stream.Collectors;
public class MybatisFlexCodeGenerateDialog extends JDialog {
public static final String SINCE_CONFIG = "---请选择配置---";
public static final String SINCE_CONFIG_ADD = "添加配置";
private JPanel contentPane;
private JButton generateBtn;
private JButton cancelBtn;
private JComboBox idTypeCombox;
private JComboBox cotrollerCombox;
private ExtendableTextField modelPackagePath;
private JPanel modelPanel;
private ExtendableTextField mapperPackagePath;
private com.intellij.openapi.ui.FixedSizeButton mapperBtn;
private com.intellij.openapi.ui.FixedSizeButton modelBtn;
private JTextField serviceIntefacePath;
private com.intellij.openapi.ui.FixedSizeButton serviceInterfaceBtn;
private JTextField serviceImpPath;
private com.intellij.openapi.ui.FixedSizeButton serviceImplBtn;
private JTextField controllerPath;
private com.intellij.openapi.ui.FixedSizeButton controllerBtn;
private JComboBox modelCombox;
private JComboBox serviceInteCombox;
private JComboBox serviceImplComBox;
private JComboBox mapperComBox;
private JTextField mapperXmlPath;
private FixedSizeButton mapperXmlBtn;
private JComboBox xmlComBox;
private JCheckBox syncCheckBox;
private FixedSizeButton settingLabel;
private JComboBox sinceComBox;
private JList tableList;
private JCheckBox selectAllChexBox;
private JTextField tableSearch;
private FixedSizeButton sortBtn;
private JCheckBox strictComBox;
private JButton button1;
private JCheckBox enableControllerBox;
private JCheckBox enableModelBox;
private JCheckBox enableInterBox;
private JCheckBox enableImplBox;
private JCheckBox enableMapperBox;
private JCheckBox enableXmlBox;
private AnActionEvent actionEvent;
List<JComboBox> list = Arrays.asList(cotrollerCombox, modelCombox, serviceInteCombox, serviceImplComBox, mapperComBox, xmlComBox);
List<JCheckBox> enableList = Arrays.asList(enableControllerBox, enableModelBox, enableInterBox, enableImplBox, enableMapperBox, enableXmlBox);
List<JTextField> packageList = Arrays.asList(controllerPath, modelPackagePath, serviceIntefacePath, serviceImpPath, mapperPackagePath, mapperXmlPath);
Project project;
List<String> tableNameList;
Map<String, TableInfo> tableInfoMap;
private boolean sinceFlag;
public MybatisFlexCodeGenerateDialog(AnActionEvent actionEvent) {
this.actionEvent = actionEvent;
setContentPane(contentPane);
setModal(true);
setTitle("Mybatis Flex Code Generate");
getRootPane().setDefaultButton(generateBtn);
setSize(1050, 460);
DialogUtil.centerShow(this);
project = actionEvent.getProject();
ProjectUtils.setCurrentProject(project);
generateBtn.addActionListener(e -> onGenerate());
cancelBtn.addActionListener(e -> onCancel());
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
onCancel();
}
});
contentPane.registerKeyboardAction(e ->
onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
init(project);
syncCheckBox.addActionListener(e -> {
if (syncCheckBox.isSelected()) {
Modules.syncModules(list, cotrollerCombox.getSelectedItem());
}
});
cotrollerCombox.addActionListener(e -> {
if (syncCheckBox.isSelected()) {
Modules.syncModules(list, cotrollerCombox.getSelectedItem());
}
});
JTextField textField = (JTextField) cotrollerCombox.getEditor().getEditorComponent();
textField.addActionListener(e -> {
if (syncCheckBox.isSelected()) {
Modules.syncModules(list, cotrollerCombox.getSelectedItem());
}
});
settingLabel.addActionListener(e -> {
Set<String> sinces = MybatisFlexPluginConfigData.getSinceMap().keySet();
MybatisFlexSettingDialog dialog = new MybatisFlexSettingDialog(project, () -> {
initConfigData(null);
});
dialog.show();
sinceFlag = true;
// 避免用户配置后,直接点击设置界面,再回来导致配置丢失
MybatisFlexConfig configData = getConfigData();
Set<String> sinceSet = MybatisFlexPluginConfigData.getSinceMap().keySet();
if (sinces.size() > sinceSet.size()) {
initSinceComBox(0);
} else {
initSinceComBox(CollUtil.isEmpty(list) ? null : sinceComBox.getSelectedIndex());
}
// 再次设置是因为initSinceComBox最终会把sinceFlag设置为false
sinceFlag = true;
initConfigData(configData);
});
sinceComBox.addActionListener(e -> {
Object selectedItem = sinceComBox.getSelectedItem();
if (ObjectUtil.isNull(selectedItem)) {
return;
}
if (selectedItem.toString().equals(SINCE_CONFIG_ADD)) {
sinceComBox.hidePopup();
Messages.InputDialog dialog = new Messages.InputDialog("请输入配置名称", "配置名称", Messages.getQuestionIcon(), "", new InputValidatorImpl());
dialog.show();
String configName = dialog.getInputString();
if (StrUtil.isEmpty(configName)) {
return;
}
MybatisFlexPluginConfigData.configSince(configName, getConfigData());
NotificationUtils.notifySuccess("保存成功", project);
initSinceComBox(null);
return;
}
String key = selectedItem.toString();
MybatisFlexConfig config = MybatisFlexPluginConfigData.getConfig(key);
sinceFlag = !SINCE_CONFIG.equals(selectedItem.toString());
initConfigData(config);
});
initSinceComBox(null);
initPackagePath();