优化代码

This commit is contained in:
makejava 2018-07-17 18:16:13 +08:00
parent eb56baf5af
commit 6f2e067292
14 changed files with 227 additions and 150 deletions

View File

@ -1,11 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile default="true" name="Default" enabled="true" />
</annotationProcessing>
<bytecodeTargetLevel>
<module name="EasyCode_main" target="1.8" />
<module name="EasyCode_test" target="1.8" />
<module name="plugin_main" target="1.8" />
<module name="plugin_test" target="1.8" />
</bytecodeTargetLevel>
</component>
</project>

View File

@ -1,4 +1,4 @@
#Tue Jul 17 10:19:47 CST 2018
#Tue Jul 17 15:13:01 CST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

View File

@ -5,13 +5,30 @@ import com.intellij.openapi.actionSystem.AnActionEvent;
import com.sjhy.plugin.ui.ConfigTableDialog;
import org.jetbrains.annotations.Nullable;
/**
* 表配置菜单
*
* @author makejava
* @version 1.0.0
* @since 2018/07/17 13:10
*/
public class ConfigAction extends AnAction {
/**
* 构造方法
*
* @param text 菜单名称
*/
ConfigAction(@Nullable String text) {
super(text);
}
/**
* 处理方法
*
* @param event 事件对象
*/
@Override
public void actionPerformed(AnActionEvent anActionEvent) {
public void actionPerformed(AnActionEvent event) {
new ConfigTableDialog().open();
}
}

View File

@ -5,13 +5,30 @@ import com.intellij.openapi.actionSystem.AnActionEvent;
import com.sjhy.plugin.ui.SelectSavePath;
import org.jetbrains.annotations.Nullable;
/**
* 代码生成菜单
*
* @author makejava
* @version 1.0.0
* @since 2018/07/17 13:10
*/
public class MainAction extends AnAction {
/**
* 构造方法
*
* @param text 菜单名称
*/
MainAction(@Nullable String text) {
super(text);
}
/**
* 处理方法
*
* @param event 事件对象
*/
@Override
public void actionPerformed(AnActionEvent e) {
public void actionPerformed(AnActionEvent event) {
//开始处理
new SelectSavePath().open();
}

View File

@ -13,33 +13,67 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
/**
* 操作按钮分组
*
* @author makejava
* @version 1.0.0
* @since 2018/07/17 13:10
*/
public class MainActionGroup extends ActionGroup {
/**
* 缓存数据工具类
*/
private CacheDataUtils cacheDataUtils = CacheDataUtils.getInstance();
/**
* 是否不存在子菜单
*/
private boolean notExistsChildren;
/**
* 是否分组按钮
*
* @return 是否隐藏
*/
@Override
public boolean hideIfNoVisibleChildren() {
return this.notExistsChildren;
}
/**
* 根据右键在不同的选项上展示不同的子菜单
*
* @param event 事件对象
* @return 动作组
*/
@NotNull
@Override
public AnAction[] getChildren(@Nullable AnActionEvent anActionEvent) {
if (anActionEvent == null) {
return AnAction.EMPTY_ARRAY;
}
Project project = anActionEvent.getProject();
public AnAction[] getChildren(@Nullable AnActionEvent event) {
// 获取当前项目
Project project = getEventProject(event);
if (project == null) {
this.notExistsChildren = true;
return AnAction.EMPTY_ARRAY;
}
//获取模型
Module[] modules = ModuleManager.getInstance(project).getModules();
//获取选中的单个表
PsiElement psiElement = anActionEvent.getData(LangDataKeys.PSI_ELEMENT);
PsiElement psiElement = event.getData(LangDataKeys.PSI_ELEMENT);
DbTable selectDbTable = null;
if (psiElement instanceof DbTable) {
selectDbTable = (DbTable) psiElement;
}
if (selectDbTable == null) {
this.notExistsChildren = true;
return AnAction.EMPTY_ARRAY;
}
//获取选中的所有表
PsiElement[] psiElements = anActionEvent.getData(LangDataKeys.PSI_ELEMENT_ARRAY);
PsiElement[] psiElements = event.getData(LangDataKeys.PSI_ELEMENT_ARRAY);
if (psiElements == null || psiElements.length == 0) {
this.notExistsChildren = true;
return AnAction.EMPTY_ARRAY;
}
List<DbTable> dbTableList = new ArrayList<>();
@ -51,30 +85,41 @@ public class MainActionGroup extends ActionGroup {
dbTableList.add(dbTable);
}
if (dbTableList.isEmpty()) {
this.notExistsChildren = true;
return AnAction.EMPTY_ARRAY;
}
//保存数据
//保存数据到缓存
cacheDataUtils.setProject(project);
cacheDataUtils.setDbTableList(dbTableList);
cacheDataUtils.setModules(modules);
cacheDataUtils.setSelectDbTable(selectDbTable);
this.notExistsChildren = false;
return getMenuList();
}
/**
* 初始化注册子菜单项目
*
* @return 子菜单数组
*/
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

@ -4,10 +4,21 @@ import com.sjhy.plugin.comm.CommClone;
import java.util.List;
/**
* 抽象分组类
*
* @author makejava
* @version 1.0.0
* @since 2018/07/17 13:10
*/
public abstract class AbstractGroup<T extends CommClone, E extends CommClone> extends CommClone<T> {
//组名
/**
* 组名
*/
private String name;
//组元素
/**
* 组元素
*/
private List<E> elementList;
public String getName() {
@ -37,8 +48,12 @@ public abstract class AbstractGroup<T extends CommClone, E extends CommClone> ex
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AbstractGroup that = (AbstractGroup) o;

View File

@ -1,22 +1,16 @@
package com.sjhy.plugin.entity;
import lombok.Data;
/**
* 回调实体类
*
* @author makejava
* @version 1.0.0
* @since 2018/07/17 13:10
*/
@Data
public class Callback {
private String fileName;
private String savePath;
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}

View File

@ -1,15 +1,21 @@
package com.sjhy.plugin.entity;
import com.sjhy.plugin.comm.CommClone;
import lombok.Data;
/**
* 列配置信息
*
* @author makejava
* @version 1.0.0
* @since 2018/07/17 13:10
*/
@Data
public class ColumnConfig extends CommClone<ColumnConfig> {
private String title;
private ColumnConfigType type;
private String selectValue;
public ColumnConfig() {
}
public ColumnConfig(String title, ColumnConfigType type) {
this.title = title;
this.type = type;
@ -20,46 +26,4 @@ public class ColumnConfig extends CommClone<ColumnConfig> {
this.type = type;
this.selectValue = selectValue;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public ColumnConfigType getType() {
return type;
}
public void setType(ColumnConfigType type) {
this.type = type;
}
public String getSelectValue() {
return selectValue;
}
public void setSelectValue(String selectValue) {
this.selectValue = selectValue;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ColumnConfig that = (ColumnConfig) o;
return title.equals(that.title) && type == that.type && (selectValue != null ? selectValue.equals(that.selectValue) : that.selectValue == null);
}
@Override
public int hashCode() {
int result = title.hashCode();
result = 31 * result + type.hashCode();
result = 31 * result + (selectValue != null ? selectValue.hashCode() : 0);
return result;
}
}

View File

@ -1,5 +1,17 @@
package com.sjhy.plugin.entity;
/**
* 列配置类型
*
* @author makejava
* @version 1.0.0
* @since 2018/07/17 13:10
*/
public enum ColumnConfigType {
TEXT,SELECT,BOOLEAN
// 文本类型
TEXT,
// 下拉框类型
SELECT,
// 选中框类型
BOOLEAN
}

View File

@ -8,10 +8,24 @@ import com.sjhy.plugin.entity.TypeMapperGroup;
import java.util.Map;
/**
* 配置服务接口
*
* @author makejava
* @version 1.0.0
* @since 2018/07/17 13:10
*/
public interface ConfigService extends PersistentStateComponent<ConfigService> {
//默认名称
/**
* 默认名称
*/
String DEFAULT_NAME = "Default";
/**
* 获取单例实例对象
*
* @return 实例对象
*/
static ConfigService getInstance() {
return ServiceManager.getService(ConfigService.class);
}

View File

@ -13,6 +13,13 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* 配置服务实现类
*
* @author makejava
* @version 1.0.0
* @since 2018/07/17 13:10
*/
@State(name = "EasyCodeSetting", storages = @Storage("$APP_CONFIG$/EasyCode-settings.xml"))
public class ConfigServiceImpl implements ConfigService {
//当前类型映射组名

View File

@ -4,89 +4,68 @@ 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 lombok.Data;
import java.util.List;
/**
* 缓存数据工具类
*
* @author makejava
* @version 1.0.0
* @since 2018/07/17 13:10
*/
@Data
public class CacheDataUtils {
//单例模式
private static class Instance {
private static final CacheDataUtils ME = new CacheDataUtils();
}
private volatile static CacheDataUtils cacheDataUtils;
/**
* 单例模式
*/
public static CacheDataUtils getInstance() {
return Instance.ME;
if (cacheDataUtils == null) {
synchronized (CacheDataUtils.class) {
if (cacheDataUtils == null) {
cacheDataUtils = new CacheDataUtils();
}
}
}
return cacheDataUtils;
}
private CacheDataUtils(){}
private CacheDataUtils() {
}
/**
* 所有选中的表
*/
private List<DbTable> dbTableList;
/**
* 项目中所有的modules
*/
private Module[] modules;
/**
* 当前项目
*/
private Project project;
/**
* 当前选中的表
*/
private DbTable selectDbTable;
/**
* 保存路径
*/
private String savePath;
/**
* 选中的所有模板
*/
private List<Template> selectTemplate;
/**
* 设置的包名称
*/
private String packageName;
/**
* 选中的model
*/
private Module selectModule;
public Module getSelectModule() {
return selectModule;
}
public void setSelectModule(Module selectModule) {
this.selectModule = selectModule;
}
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

@ -19,6 +19,13 @@ import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* 选择保存路径
*
* @author makejava
* @version 1.0.0
* @since 2018/07/17 13:10
*/
public class SelectSavePath extends JDialog {
private JPanel contentPane;
private JButton buttonOK;
@ -50,6 +57,7 @@ public class SelectSavePath extends JDialog {
// call onCancel() when cross is clicked
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
onCancel();
}

View File

@ -16,6 +16,8 @@
]]>
</change-notes>
<idea-version since-build="IU-181.4445.20"/>
<!-- 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
@ -24,6 +26,8 @@
<depends>com.intellij.modules.lang</depends>
<!--必须运行在企业版-->
<depends optional="true">com.intellij.modules.ultimate</depends>
<!--必须存在database插件-->
<!--suppress PluginXmlValidity -->
<depends optional="true">com.intellij.database</depends>
<extensions defaultExtensionNs="com.intellij">