初步完成1.0版本

This commit is contained in:
javas 2018-01-11 18:30:51 +08:00
parent a20dc7d071
commit d1dce8a339
12 changed files with 676 additions and 3 deletions

View File

@ -36,6 +36,13 @@
<actions> <actions>
<!-- Add your actions here --> <!-- Add your actions here -->
<group id="con.sjhy.easy.code.action" class="com.sjhy.plugin.action.MainActionGroup" text="EasyCode" popup="true">
<!--suppress PluginXmlValidity -->
<add-to-group group-id="DatabaseViewPopupMenu" anchor="first"/>
<!--后期功能通过sql查询结果生成代码。-->
<!--suppress PluginXmlValidity -->
<!--<add-to-group group-id="Console.TableResult.PopupGroup" anchor="first"/>-->
</group>
</actions> </actions>
</idea-plugin> </idea-plugin>

View File

@ -1,6 +1,15 @@
button.type.mapper.copy.group=Copy Group button.type.mapper.copy.group=Copy Group
label.all=A&ll
label.author.name=&\u4F5C\u8005\u540D\u79F0\uFF1A label.author.name=&\u4F5C\u8005\u540D\u79F0\uFF1A
label.cancel=Ca&ncel
label.choose=&Choose
label.choose2=C&hoose
label.default.encode=&\u9ED8\u8BA4\u7F16\u7801\uFF1A label.default.encode=&\u9ED8\u8BA4\u7F16\u7801\uFF1A
label.module=&Module:
label.ok=&OK
label.operate=\u64CD\u4F5C label.operate=\u64CD\u4F5C
label.package=&Package:
label.path=P&ath:
label.template=Template:
label.template.group=&\u6A21\u677F\u7EC4\uFF1A 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

View File

@ -1,5 +1,22 @@
#* @vtlvariable name="author" type="java.lang.String" *#
#* @vtlvariable name="importList" type="java.util.List<java.lang.String>" *#
#* @vtlvariable name="callback" type="com.sjhy.plugin.entity.Callback" *#
#* @vtlvariable name="packageName" type="java.lang.String" *#
#* @vtlvariable name="tool" type="com.sjhy.plugin.tool.NameUtils" *# #* @vtlvariable name="tool" type="com.sjhy.plugin.tool.NameUtils" *#
#* @vtlvariable name="tableInfo" type="com.sjhy.plugin.entity.TableInfo" *# #* @vtlvariable name="tableInfo" type="com.sjhy.plugin.entity.TableInfo" *#
#set($fileName = ${tableInfo.name}+".java")
$callback.setFileName(${fileName})
package $packageName;
#foreach($import in $importList)
import $import;
#end
/**
* $!{tableInfo.comment}(${tableInfo.name})表实体类
* @author $author
*/
public class ${tableInfo.name} { public class ${tableInfo.name} {
#foreach($column in $tableInfo.fullColumn) #foreach($column in $tableInfo.fullColumn)
#if(${column.comment})//${column.comment}#end #if(${column.comment})//${column.comment}#end

View File

@ -0,0 +1,17 @@
package com.sjhy.plugin.action;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import org.jetbrains.annotations.Nullable;
public class ConfigAction extends AnAction {
ConfigAction(@Nullable String text) {
super(text);
}
@Override
public void actionPerformed(AnActionEvent anActionEvent) {
}
}

View File

@ -0,0 +1,21 @@
package com.sjhy.plugin.action;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.sjhy.plugin.tool.CacheDataUtils;
import com.sjhy.plugin.ui.SelectSavePath;
import org.jetbrains.annotations.Nullable;
public class MainAction extends AnAction {
MainAction(@Nullable String text) {
super(text);
}
private CacheDataUtils cacheDataUtils = CacheDataUtils.getInstance();
@Override
public void actionPerformed(AnActionEvent e) {
//开始处理
new SelectSavePath().open();
}
}

View File

@ -0,0 +1,79 @@
package com.sjhy.plugin.action;
import com.intellij.database.psi.DbTable;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.sjhy.plugin.tool.CacheDataUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
public class MainActionGroup extends ActionGroup {
private CacheDataUtils cacheDataUtils = CacheDataUtils.getInstance();
@NotNull
@Override
public AnAction[] getChildren(@Nullable AnActionEvent anActionEvent) {
if (anActionEvent==null) {
return AnAction.EMPTY_ARRAY;
}
Project project = anActionEvent.getProject();
if (project==null) {
return AnAction.EMPTY_ARRAY;
}
//获取模型
Module[] modules = ModuleManager.getInstance(project).getModules();
//获取选中的单个表
PsiElement psiElement = anActionEvent.getData(LangDataKeys.PSI_ELEMENT);
DbTable selectDbTable = null;
if (psiElement instanceof DbTable) {
selectDbTable = (DbTable) psiElement;
}
if (selectDbTable==null) {
return AnAction.EMPTY_ARRAY;
}
//获取选中的所有表
PsiElement[] psiElements = anActionEvent.getData(LangDataKeys.PSI_ELEMENT_ARRAY);
if (psiElements == null || psiElements.length == 0) {
return AnAction.EMPTY_ARRAY;
}
List<DbTable> dbTableList = new ArrayList<>();
for (PsiElement element : psiElements) {
if (!(element instanceof DbTable)) {
continue;
}
DbTable dbTable = (DbTable) element;
dbTableList.add(dbTable);
}
if (dbTableList.isEmpty()) {
return AnAction.EMPTY_ARRAY;
}
//保存数据
cacheDataUtils.setProject(project);
cacheDataUtils.setDbTableList(dbTableList);
cacheDataUtils.setModules(modules);
cacheDataUtils.setSelectDbTable(selectDbTable);
return getMenuList();
}
private AnAction[] getMenuList() {
String mainActionId = "com.sjhy.easy.code.action.generate";
String configActionId = "com.sjhy.easy.code.action.config";
ActionManager actionManager = ActionManager.getInstance();
AnAction mainAction = actionManager.getAction(mainActionId);
if (mainAction==null) {
mainAction = new MainAction("Generate Code");
actionManager.registerAction(mainActionId, mainAction);
}
AnAction configAction = actionManager.getAction(configActionId);
if (configAction==null) {
configAction = new ConfigAction("Config Table");
actionManager.registerAction(configActionId, configAction);
}
return new AnAction[]{mainAction, configAction};
}
}

View File

@ -0,0 +1,13 @@
package com.sjhy.plugin.entity;
public class Callback {
private String fileName;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}

View File

@ -0,0 +1,83 @@
package com.sjhy.plugin.tool;
import com.intellij.database.psi.DbTable;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.sjhy.plugin.entity.Template;
import java.util.List;
public class CacheDataUtils {
//单例模式
private static class Instance {
private static final CacheDataUtils ME = new CacheDataUtils();
}
public static CacheDataUtils getInstance() {
return Instance.ME;
}
private CacheDataUtils(){}
private List<DbTable> dbTableList;
private Module[] modules;
private Project project;
private DbTable selectDbTable;
private String savePath;
private List<Template> selectTemplate;
private String packageName;
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public List<Template> getSelectTemplate() {
return selectTemplate;
}
public void setSelectTemplate(List<Template> selectTemplate) {
this.selectTemplate = selectTemplate;
}
public List<DbTable> getDbTableList() {
return dbTableList;
}
public void setDbTableList(List<DbTable> dbTableList) {
this.dbTableList = dbTableList;
}
public Module[] getModules() {
return modules;
}
public void setModules(Module[] modules) {
this.modules = modules;
}
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
public DbTable getSelectDbTable() {
return selectDbTable;
}
public void setSelectDbTable(DbTable selectDbTable) {
this.selectDbTable = selectDbTable;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}

View File

@ -1,11 +1,18 @@
package com.sjhy.plugin.tool; package com.sjhy.plugin.tool;
import com.intellij.openapi.util.io.FileUtil;
import com.sjhy.plugin.entity.Callback;
import com.sjhy.plugin.entity.TableInfo;
import com.sjhy.plugin.entity.Template;
import com.sjhy.plugin.service.ConfigService;
import org.apache.velocity.VelocityContext; import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.app.VelocityEngine;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.HashMap; import java.util.*;
import java.util.Map;
public class VelocityUtils { public class VelocityUtils {
//单例模式 //单例模式
@ -20,15 +27,92 @@ public class VelocityUtils {
} }
private final VelocityEngine velocityEngine; private final VelocityEngine velocityEngine;
private CacheDataUtils cacheDataUtils = CacheDataUtils.getInstance();
private TableInfoUtils tableInfoUtils = TableInfoUtils.getInstance();
private NameUtils nameUtils = NameUtils.getInstance();
public String generate(String template, Map<String,Object> map) { private String generate(String template, Map<String,Object> map, String encode) {
VelocityContext velocityContext = new VelocityContext(); VelocityContext velocityContext = new VelocityContext();
if (map==null) { if (map==null) {
map = new HashMap<>(); map = new HashMap<>();
} }
map.forEach(velocityContext::put); map.forEach(velocityContext::put);
StringWriter stringWriter = new StringWriter(); StringWriter stringWriter = new StringWriter();
velocityEngine.setProperty("input.encode", encode);
velocityEngine.setProperty("output.encode", encode);
velocityEngine.evaluate(velocityContext, stringWriter, "", template); velocityEngine.evaluate(velocityContext, stringWriter, "", template);
return stringWriter.toString(); return stringWriter.toString();
} }
public void handler() {
String savePath = cacheDataUtils.getSavePath();
File path = new File(savePath);
if (!new File(savePath).exists()) {
int result = JOptionPane.showConfirmDialog(null, "Save Path Is Not Exists, Confirm Create?", "Title Info", JOptionPane.OK_CANCEL_OPTION);
if(result==0){
if(!path.mkdirs()){
return;
}
}else{
return;
}
}
if (path.isFile()){
JOptionPane.showMessageDialog(null, "Error,Save Path Is File!");
return;
}
List<TableInfo> tableInfoList = tableInfoUtils.handler(cacheDataUtils.getDbTableList());
List<Template> templateList = cacheDataUtils.getSelectTemplate();
ConfigService configService = ConfigService.getInstance();
String encode = configService.getEncode();
String author = configService.getAuthor();
Map<String, Object> map = new HashMap<>();
map.put("tableInfoList", tableInfoList);
map.put("author", author);
map.put("tool", nameUtils);
map.put("packageName", cacheDataUtils.getPackageName());
tableInfoList.forEach(tableInfo -> {
Callback callback = new Callback();
map.put("tableInfo", tableInfo);
map.put("importList", getImportList(tableInfo));
map.put("callback", callback);
templateList.forEach(template -> {
String content = generate(template.getCode(), map, encode).trim();
String fileName = callback.getFileName();
if (fileName==null) {
fileName = tableInfo.getName()+"Default.java";
}
File file = new File(path, fileName);
try {
if (file.exists()) {
int result = JOptionPane.showConfirmDialog(null, "File "+fileName+" Exists, Confirm Continue?", "Title Info", JOptionPane.OK_CANCEL_OPTION);
if (result!=0){
return;
}
}else{
if(!file.createNewFile()){
return;
}
}
FileUtil.writeToFile(file, content, false);
} catch (IOException e) {
e.printStackTrace();
}
JOptionPane.showMessageDialog(null, "Code Generate Success!");
});
});
}
private Set<String> getImportList(TableInfo tableInfo) {
Set<String> result = new TreeSet<>();
tableInfo.getFullColumn().forEach(columnInfo -> {
if (!columnInfo.getType().startsWith("java.lang")){
result.add(columnInfo.getType());
}
});
return result;
}
} }

View File

@ -33,6 +33,7 @@ public class EditTemplatePanel {
//初始化系统编辑器 //初始化系统编辑器
EditorFactory factory = EditorFactory.getInstance(); EditorFactory factory = EditorFactory.getInstance();
Document velocityTemplate = factory.createDocument(template.getCode()); Document velocityTemplate = factory.createDocument(template.getCode());
//TODO 退出时会导致无法创建编辑器
editor = factory.createEditor(velocityTemplate, null, FILE_TYPE, false); editor = factory.createEditor(velocityTemplate, null, FILE_TYPE, false);
editPanel.add(editor.getComponent(), GRID_CONSTRAINTS); editPanel.add(editor.getComponent(), GRID_CONSTRAINTS);
} }

View File

@ -0,0 +1,159 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.sjhy.plugin.ui.SelectSavePath">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="48" y="54" width="436" height="297"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" 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>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<hspacer id="98af6">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<grid id="9538f" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" 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="e7465" class="javax.swing.JButton" binding="buttonOK">
<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>
<text resource-bundle="string" key="label.ok"/>
</properties>
</component>
<component id="5723f" class="javax.swing.JButton" binding="buttonCancel">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="string" key="label.cancel"/>
</properties>
</component>
</children>
</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">
<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"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="f79bc" 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="76436"/>
<text resource-bundle="string" key="label.module"/>
</properties>
</component>
<component id="76436" class="javax.swing.JComboBox" binding="moduleComboBox">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="2dff5" 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="158fc"/>
<text resource-bundle="string" key="label.package"/>
</properties>
</component>
<component id="158fc" class="javax.swing.JTextField" binding="packageField">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="d7b53" class="javax.swing.JLabel">
<constraints>
<grid row="2" 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="ab063"/>
<text resource-bundle="string" key="label.path"/>
</properties>
</component>
<component id="ab063" class="javax.swing.JTextField" binding="pathField">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="7467f" class="javax.swing.JButton" binding="packageChooseButton">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="string" key="label.choose"/>
</properties>
</component>
<component id="fba1b" class="javax.swing.JButton" binding="pathChooseButton">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="string" key="label.choose2"/>
</properties>
</component>
<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"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</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"/>
</constraints>
<properties>
<text resource-bundle="string" key="label.template"/>
</properties>
</component>
<component id="9a8d1" class="javax.swing.JCheckBox" binding="allCheckBox" default-binding="true">
<constraints>
<grid row="3" 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.all"/>
</properties>
</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"/>
</constraints>
</vspacer>
</children>
</grid>
</children>
</grid>
</form>

View File

@ -0,0 +1,183 @@
package com.sjhy.plugin.ui;
import com.intellij.ide.util.PackageChooserDialog;
import com.intellij.openapi.fileChooser.FileChooser;
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.Template;
import com.sjhy.plugin.entity.TemplateGroup;
import com.sjhy.plugin.service.ConfigService;
import com.sjhy.plugin.tool.CacheDataUtils;
import com.sjhy.plugin.tool.VelocityUtils;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
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 List<JCheckBox> checkBoxList = new ArrayList<>();
private TemplateGroup templateGroup;
public SelectSavePath() {
ConfigService configService = ConfigService.getInstance();
this.templateGroup = configService.getTemplateGroupMap().get(configService.getCurrTemplateGroupName());
init();
setContentPane(contentPane);
setModal(true);
getRootPane().setDefaultButton(buttonOK);
buttonOK.addActionListener(e -> onOK());
buttonCancel.addActionListener(e -> onCancel());
// call onCancel() when cross is clicked
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
onCancel();
}
});
// call onCancel() on ESCAPE
contentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
}
private List<Template> getSelectTemplate() {
List<String> selectTemplateNameList = new ArrayList<>();
checkBoxList.forEach(jCheckBox -> {
if(jCheckBox.isSelected()){
selectTemplateNameList.add(jCheckBox.getText());
}
});
List<Template> selectTemplateList = new ArrayList<>();
if (selectTemplateNameList.isEmpty()){
return selectTemplateList;
}
templateGroup.getTemplateList().forEach(template -> {
if (selectTemplateNameList.contains(template.getName())) {
selectTemplateList.add(template);
}
});
return selectTemplateList;
}
private void onOK() {
List<Template> selectTemplateList = getSelectTemplate();
if (selectTemplateList.isEmpty()) {
JOptionPane.showMessageDialog(null, "Can't Select Template!");
return;
}
String savePath = pathField.getText();
if (savePath.isEmpty()){
JOptionPane.showMessageDialog(null, "Can't Select Save Path!");
return;
}
cacheDataUtils.setSavePath(savePath);
cacheDataUtils.setSelectTemplate(selectTemplateList);
cacheDataUtils.setPackageName(packageField.getText());
VelocityUtils.getInstance().handler();
dispose();
}
private void onCancel() {
dispose();
}
@SuppressWarnings("unchecked")
private void init() {
//添加模板组
checkBoxList.clear();
templatePanel.setLayout(new GridLayout(6, 2));
templateGroup.getTemplateList().forEach(template -> {
JCheckBox checkBox = new JCheckBox(template.getName());
checkBoxList.add(checkBox);
templatePanel.add(checkBox);
});
//设置全选事件
allCheckBox.addActionListener(e -> checkBoxList.forEach(jCheckBox -> jCheckBox.setSelected(allCheckBox.isSelected())));
//初始化Module选择
for (Module module : cacheDataUtils.getModules()) {
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();
//选择路径
pathChooseButton.addActionListener(e -> {
@SuppressWarnings("ConstantConditions") VirtualFile virtualFile = FileChooser.chooseFile(FileChooserDescriptorFactory.createSingleFolderDescriptor(), cacheDataUtils.getProject(), getSelectModule().getModuleFile().getParent());
if (virtualFile!=null) {
pathField.setText(virtualFile.getPath());
}
});
}
private Module getSelectModule() {
String name = (String) moduleComboBox.getSelectedItem();
for (Module module : cacheDataUtils.getModules()) {
if(module.getName().equals(name)){
return module;
}
}
return cacheDataUtils.getModules()[0];
}
@SuppressWarnings("ConstantConditions")
private String getBasePath() {
Module module = getSelectModule();
String baseDir = module.getModuleFile().getParent().getPath();
File file = new File(baseDir+"/src/main/java");
if (file.exists()){
return file.getAbsolutePath();
}
file = new File(baseDir+"/src");
if (file.exists()){
return file.getAbsolutePath();
}
return baseDir;
}
private void refreshPath() {
String packageName = packageField.getText();
String path = getBasePath();
if (!packageName.isEmpty()){
path += "\\" + packageName.replaceAll("\\.", "\\\\");
}
pathField.setText(path);
}
public void open() {
this.pack();
setLocationRelativeTo(null);
this.setVisible(true);
}
}