From fd765d4e98a305b6fb9c25f60e83d33d7f78f0bf Mon Sep 17 00:00:00 2001 From: leecho571 Date: Thu, 13 Dec 2018 14:37:36 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89Example=E7=B1=BB?= =?UTF-8?q?=E5=90=8E=E7=BC=80=EF=BC=8C=E4=BC=98=E5=8C=96UI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...s-generator-lombok-plugin-1.1-SNAPSHOT.jar | Bin 8833 -> 0 bytes mybatis-generator-plus.iml | 2 - resources/META-INF/plugin.xml | 2 +- .../generate/MyBatisGenerateCommand.java | 29 +- .../mybatis/generator/model/EntityConfig.java | 55 +++- .../mybatis/generator/model/GlobalConfig.java | 29 +- .../generator/plugin/LombokPlugin.java | 271 ++++++++++++++++++ .../plugin/RenameExampleClassPlugin.java | 43 +++ .../generator/ui/GenerateSettingUI.java | 235 +++++++++++---- .../generator/ui/GeneratorSettingUI.java | 89 ++++-- 10 files changed, 653 insertions(+), 102 deletions(-) delete mode 100644 lib/mybatis-generator-lombok-plugin-1.1-SNAPSHOT.jar create mode 100644 src/com/github/leecho/idea/plugin/mybatis/generator/plugin/LombokPlugin.java create mode 100644 src/com/github/leecho/idea/plugin/mybatis/generator/plugin/RenameExampleClassPlugin.java diff --git a/lib/mybatis-generator-lombok-plugin-1.1-SNAPSHOT.jar b/lib/mybatis-generator-lombok-plugin-1.1-SNAPSHOT.jar deleted file mode 100644 index df89e33680c4195228013f978c2fcf096b5f91f3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8833 zcmb_i1yo#Fwk1e{yA|$k!3nN`D%>p)+}&M*JB4cq?(PyCg1b8;5Q0OHpr1^4Iz7KL zJ@fyXTkA=!I%l7A?tAOq{mw3V5EL{##P?CB3Y7WB$?rS3$D@q6vM{5RtOT?C?`E(N zwU1`&vq^@Qk3YY8+%W%aCL=5>B_Xb)%p@amCNn%F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t|E(#gf3I?W(n(bT=*{9t;K^(P)oxvD9>MgUsKgNJC^bC#K2X2 z-sa7ESOgv8&U>$C$EihB*=QF_q<0uK`8qo)n&*=WNwwY=WrCKfyybbjtD_c1ZUz~* zL9ZJmX8oaVpFt?FQbub=1qr8sD4TG5a^*c-Qdd1||amh;8D_Lyb@uZhN)WQ$Vjom|Dz1bs<$ zMeZz(=j|J}Jp6+!J9iUJ6}CeD=iH*MvC_8%)REJXO@+K2*sh!Om_CdNE&vj3&E(bnF^*2vz`%*cUqP%`n9e%@EL2zV0>9z=^pHTWVxD+9*u0ZfIjgL@G;_U zFOuAzs^yW6eGIHY>fSUfHlz!5pRsYWVK?#0a`LWC$a(&%FkBbImL!#jjr4l7(RIC1 z3*(OL`T;16$gX)E;->E5Gc70&dIE_F^{-Xj$GRT}D#U^C_v0Tt=cn7RRo(B#-$$+= z_2d2zw?ApTKN0?_tA1Bre;eNe_dcHfro;YB^?z%%-<8m_O**BO&`))~}S?pMX*S8Q@PH_XqH=mE0e>=K9V3 z-+=#K*Zo<~uiJJ%!|Eaa%{>2{pda=8IqYvuyq}?dP0N2w-NVEF<6jN_xC?)f^dHmn zS7(wRNj~2B4-WvWzc~MbwEt@~p?^YC`w!6mJ&FGj?bkH^N5EXay8lA7-%0(?+5YwO o@n_Hpf`30@|E|REL4Q6i - - \ No newline at end of file diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index c5e412f..32f9fe5 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -1,7 +1,7 @@ com.github.leecho.idea.plugin.mybatis.generator MyBatis Generator Plus - 1.0 + 1.1 Leecho diff --git a/src/com/github/leecho/idea/plugin/mybatis/generator/generate/MyBatisGenerateCommand.java b/src/com/github/leecho/idea/plugin/mybatis/generator/generate/MyBatisGenerateCommand.java index efe16d1..2c5212a 100644 --- a/src/com/github/leecho/idea/plugin/mybatis/generator/generate/MyBatisGenerateCommand.java +++ b/src/com/github/leecho/idea/plugin/mybatis/generator/generate/MyBatisGenerateCommand.java @@ -22,7 +22,6 @@ import com.github.leecho.idea.plugin.mybatis.generator.model.DbType; import com.github.leecho.idea.plugin.mybatis.generator.setting.MyBatisGeneratorConfiguration; import com.github.leecho.idea.plugin.mybatis.generator.util.StringUtils; -import org.mybatis.generator.api.GeneratedXmlFile; import org.mybatis.generator.api.ShellCallback; import org.mybatis.generator.config.*; import org.mybatis.generator.internal.DefaultShellCallback; @@ -180,10 +179,10 @@ public void run(ProgressIndicator indicator) { } private String getRelativePath(Project project) { - if (entityConfig.getProjectRootPath().equals(project.getBasePath())) { + if (entityConfig.getModuleRootPath().equals(project.getBasePath())) { return ""; } else { - return entityConfig.getProjectRootPath().replace(project.getBasePath() + "/", "") + "/"; + return entityConfig.getModuleRootPath().replace(project.getBasePath() + "/", "") + "/"; } } @@ -196,8 +195,8 @@ private String getRelativePath(Project project) { private void createFolderForNeed(EntityConfig entityConfig) { - String sourcePath = entityConfig.getProjectRootPath() + "/" + entityConfig.getSourcePath() + "/"; - String resourcePath = entityConfig.getProjectRootPath() + "/" + entityConfig.getResourcePath() + "/"; + String sourcePath = entityConfig.getModuleRootPath() + "/" + entityConfig.getSourcePath() + "/"; + String resourcePath = entityConfig.getModuleRootPath() + "/" + entityConfig.getResourcePath() + "/"; File sourceFile = new File(sourcePath); if (!sourceFile.exists() && !sourceFile.isDirectory()) { @@ -351,7 +350,7 @@ private TableConfiguration buildTableConfig(Context context) { * @return */ private JavaModelGeneratorConfiguration buildModelConfig() { - String projectFolder = entityConfig.getProjectRootPath(); + String projectFolder = entityConfig.getModuleRootPath(); String entityPackage = entityConfig.getEntityPackage(); String sourcePath = entityConfig.getSourcePath(); @@ -373,7 +372,7 @@ private JavaModelGeneratorConfiguration buildModelConfig() { */ private SqlMapGeneratorConfiguration buildMapperXmlConfig() { - String projectFolder = entityConfig.getProjectRootPath(); + String projectFolder = entityConfig.getModuleRootPath(); String mappingXMLPackage = entityConfig.getXmlPackage(); String resourcePath = entityConfig.getResourcePath(); @@ -406,7 +405,7 @@ private SqlMapGeneratorConfiguration buildMapperXmlConfig() { */ private JavaClientGeneratorConfiguration buildMapperConfig() { - String projectFolder = entityConfig.getProjectRootPath(); + String projectFolder = entityConfig.getModuleRootPath(); String mapperPackage = entityConfig.getMapperPackage(); String mapperPath = entityConfig.getSourcePath(); @@ -472,8 +471,8 @@ private void addPluginConfiguration(Context context) { if (entityConfig.isLombokAnnotation()) { PluginConfiguration lombokPlugin = new PluginConfiguration(); - lombokPlugin.addProperty("type", "com.softwareloop.mybatis.generator.plugins.LombokPlugin"); - lombokPlugin.setConfigurationType("com.softwareloop.mybatis.generator.plugins.LombokPlugin"); + lombokPlugin.addProperty("type", "com.github.leecho.idea.plugin.mybatis.generator.plugin.LombokPlugin"); + lombokPlugin.setConfigurationType("com.github.leecho.idea.plugin.mybatis.generator.plugin.LombokPlugin"); if (entityConfig.isLombokBuilderAnnotation()) { lombokPlugin.addProperty("builder", "true"); lombokPlugin.addProperty("allArgsConstructor", "true"); @@ -482,6 +481,14 @@ private void addPluginConfiguration(Context context) { context.addPluginConfiguration(lombokPlugin); } + if (entityConfig.isUseExample()) { + PluginConfiguration renameExamplePlugin = new PluginConfiguration(); + renameExamplePlugin.addProperty("type", "com.github.leecho.idea.plugin.mybatis.generator.plugin.RenameExampleClassPlugin"); + renameExamplePlugin.setConfigurationType("com.github.leecho.idea.plugin.mybatis.generator.plugin.RenameExampleClassPlugin"); + renameExamplePlugin.addProperty("target", entityConfig.getExamplePackage() + "." + entityConfig.getExampleName()); + context.addPluginConfiguration(renameExamplePlugin); + } + // limit/offset插件 if (entityConfig.isOffsetLimit()) { @@ -546,7 +553,7 @@ private String getMappingXMLFilePath(EntityConfig entityConfig) { StringBuilder sb = new StringBuilder(); String mappingXMLPackage = entityConfig.getXmlPackage(); String xmlMvnPath = entityConfig.getResourcePath(); - sb.append(entityConfig.getProjectRootPath() + "/" + xmlMvnPath + "/"); + sb.append(entityConfig.getModuleRootPath() + "/" + xmlMvnPath + "/"); if (!StringUtils.isEmpty(mappingXMLPackage)) { sb.append(mappingXMLPackage.replace(".", "/")).append("/"); diff --git a/src/com/github/leecho/idea/plugin/mybatis/generator/model/EntityConfig.java b/src/com/github/leecho/idea/plugin/mybatis/generator/model/EntityConfig.java index 5299c2d..90a0f42 100644 --- a/src/com/github/leecho/idea/plugin/mybatis/generator/model/EntityConfig.java +++ b/src/com/github/leecho/idea/plugin/mybatis/generator/model/EntityConfig.java @@ -30,23 +30,36 @@ public class EntityConfig { */ private String mapperName; + /** + * dao名称 + */ + private String exampleName; + /** * dao后缀 */ private String mapperPostfix; + /** + * dao后缀 + */ + private String examplePostfix; + /** * 工程目录 */ - private String projectRootPath; + private String moduleRootPath; private String sourcePath; private String resourcePath; + private String basePackage; private String entityPackage; private String mapperPackage; + private String examplePackage; + private String xmlPackage; @@ -182,12 +195,12 @@ public void setConnectorJarPath(String connectorJarPath) { this.connectorJarPath = connectorJarPath; } - public String getProjectRootPath() { - return projectRootPath; + public String getModuleRootPath() { + return moduleRootPath; } - public void setProjectRootPath(String projectRootPath) { - this.projectRootPath = projectRootPath; + public void setModuleRootPath(String moduleRootPath) { + this.moduleRootPath = moduleRootPath; } public String getEntityPackage() { @@ -371,4 +384,36 @@ public boolean isLombokBuilderAnnotation() { public void setLombokBuilderAnnotation(boolean lombokBuilderAnnotation) { this.lombokBuilderAnnotation = lombokBuilderAnnotation; } + + public String getExamplePackage() { + return examplePackage; + } + + public void setExamplePackage(String examplePackage) { + this.examplePackage = examplePackage; + } + + public String getExamplePostfix() { + return examplePostfix; + } + + public void setExamplePostfix(String examplePostfix) { + this.examplePostfix = examplePostfix; + } + + public String getExampleName() { + return exampleName; + } + + public void setExampleName(String exampleName) { + this.exampleName = exampleName; + } + + public String getBasePackage() { + return basePackage; + } + + public void setBasePackage(String basePackage) { + this.basePackage = basePackage; + } } diff --git a/src/com/github/leecho/idea/plugin/mybatis/generator/model/GlobalConfig.java b/src/com/github/leecho/idea/plugin/mybatis/generator/model/GlobalConfig.java index c92ad4d..323eccd 100644 --- a/src/com/github/leecho/idea/plugin/mybatis/generator/model/GlobalConfig.java +++ b/src/com/github/leecho/idea/plugin/mybatis/generator/model/GlobalConfig.java @@ -2,13 +2,15 @@ public class GlobalConfig { - private String projectRootPath; + private String moduleRootPath; private String sourcePath; private String resourcePath; private String mapperPostfix; + private String examplePostfix; private String entityPackage; private String mapperPackage; + private String examplePackage; private String xmlPackage; @@ -262,12 +264,28 @@ public void setLombokBuilderAnnotation(boolean lombokBuilderAnnotation) { this.lombokBuilderAnnotation = lombokBuilderAnnotation; } - public String getProjectRootPath() { - return projectRootPath; + public String getModuleRootPath() { + return moduleRootPath; } - public void setProjectRootPath(String projectRootPath) { - this.projectRootPath = projectRootPath; + public void setModuleRootPath(String moduleRootPath) { + this.moduleRootPath = moduleRootPath; + } + + public String getExamplePostfix() { + return examplePostfix; + } + + public void setExamplePostfix(String examplePostfix) { + this.examplePostfix = examplePostfix; + } + + public String getExamplePackage() { + return examplePackage; + } + + public void setExamplePackage(String examplePackage) { + this.examplePackage = examplePackage; } public static GlobalConfig getDefault() { @@ -276,6 +294,7 @@ public static GlobalConfig getDefault() { globalConfig.setResourcePath("src/main/resources"); globalConfig.setMapperPostfix("Mapper"); globalConfig.setXmlPackage("mapper"); + globalConfig.setExamplePostfix("Example"); globalConfig.setUseExample(true); globalConfig.setComment(true); return globalConfig; diff --git a/src/com/github/leecho/idea/plugin/mybatis/generator/plugin/LombokPlugin.java b/src/com/github/leecho/idea/plugin/mybatis/generator/plugin/LombokPlugin.java new file mode 100644 index 0000000..334a7f7 --- /dev/null +++ b/src/com/github/leecho/idea/plugin/mybatis/generator/plugin/LombokPlugin.java @@ -0,0 +1,271 @@ +package com.github.leecho.idea.plugin.mybatis.generator.plugin; + +import org.mybatis.generator.api.IntrospectedColumn; +import org.mybatis.generator.api.IntrospectedTable; +import org.mybatis.generator.api.PluginAdapter; +import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; +import org.mybatis.generator.api.dom.java.Interface; +import org.mybatis.generator.api.dom.java.Method; +import org.mybatis.generator.api.dom.java.TopLevelClass; + +import java.util.*; + + +/** + * A MyBatis Generator plugin to use Lombok's annotations. + * For example, use @Data annotation instead of getter ands setter. + * + * @author Paolo Predonzani (http://softwareloop.com/) + */ +public class LombokPlugin extends PluginAdapter { + + private final Collection annotations; + + /** + * LombokPlugin constructor + */ + public LombokPlugin() { + annotations = new LinkedHashSet(Annotations.values().length); + } + + /** + * @param warnings list of warnings + * @return always true + */ + @Override + public boolean validate(List warnings) { + return true; + } + + /** + * Intercepts base record class generation + * + * @param topLevelClass the generated base record class + * @param introspectedTable The class containing information about the table as + * introspected from the database + * @return always true + */ + @Override + public boolean modelBaseRecordClassGenerated( + TopLevelClass topLevelClass, + IntrospectedTable introspectedTable + ) { + addAnnotations(topLevelClass); + return true; + } + + /** + * Intercepts primary key class generation + * + * @param topLevelClass the generated primary key class + * @param introspectedTable The class containing information about the table as + * introspected from the database + * @return always true + */ + @Override + public boolean modelPrimaryKeyClassGenerated( + TopLevelClass topLevelClass, + IntrospectedTable introspectedTable + ) { + addAnnotations(topLevelClass); + return true; + } + + /** + * Intercepts "record with blob" class generation + * + * @param topLevelClass the generated record with BLOBs class + * @param introspectedTable The class containing information about the table as + * introspected from the database + * @return always true + */ + @Override + public boolean modelRecordWithBLOBsClassGenerated( + TopLevelClass topLevelClass, + IntrospectedTable introspectedTable + ) { + addAnnotations(topLevelClass); + return true; + } + + /** + * Prevents all getters from being generated. + * See SimpleModelGenerator + * + * @param method the getter, or accessor, method generated for the specified + * column + * @param topLevelClass the partially implemented model class + * @param introspectedColumn The class containing information about the column related + * to this field as introspected from the database + * @param introspectedTable The class containing information about the table as + * introspected from the database + * @param modelClassType the type of class that the field is generated for + */ + @Override + public boolean modelGetterMethodGenerated( + Method method, + TopLevelClass topLevelClass, + IntrospectedColumn introspectedColumn, + IntrospectedTable introspectedTable, + ModelClassType modelClassType + ) { + return false; + } + + /** + * Prevents all setters from being generated + * See SimpleModelGenerator + * + * @param method the setter, or mutator, method generated for the specified + * column + * @param topLevelClass the partially implemented model class + * @param introspectedColumn The class containing information about the column related + * to this field as introspected from the database + * @param introspectedTable The class containing information about the table as + * introspected from the database + * @param modelClassType the type of class that the field is generated for + * @return always false + */ + @Override + public boolean modelSetterMethodGenerated( + Method method, + TopLevelClass topLevelClass, + IntrospectedColumn introspectedColumn, + IntrospectedTable introspectedTable, + ModelClassType modelClassType + ) { + return false; + } + + /** + * Adds the lombok annotations' imports and annotations to the class + * + * @param topLevelClass the partially implemented model class + */ + private void addAnnotations(TopLevelClass topLevelClass) { + for (Annotations annotation : annotations) { + topLevelClass.addImportedType(annotation.javaType); + topLevelClass.addAnnotation(annotation.asAnnotation()); + } + } + + @Override + public void setProperties(Properties properties) { + super.setProperties(properties); + + //@Data is default annotation + annotations.add(Annotations.DATA); + + for (String annotationName : properties.stringPropertyNames()) { + if (annotationName.contains(".")) { + // Not an annotation name + continue; + } + String value = properties.getProperty(annotationName); + if (!Boolean.parseBoolean(value)) { + // The annotation is disabled, skip it + continue; + } + Annotations annotation = Annotations.getValueOf(annotationName); + if (annotation == null) { + continue; + } + String optionsPrefix = annotationName + "."; + for (String propertyName : properties.stringPropertyNames()) { + if (!propertyName.startsWith(optionsPrefix)) { + // A property not related to this annotation + continue; + } + String propertyValue = properties.getProperty(propertyName); + annotation.appendOptions(propertyName, propertyValue); + } + annotations.add(annotation); + annotations.addAll(Annotations.getDependencies(annotation)); + } + } + + @Override + public boolean clientGenerated( + Interface interfaze, + TopLevelClass topLevelClass, + IntrospectedTable introspectedTable + ) { + interfaze.addImportedType(new FullyQualifiedJavaType( + "org.apache.ibatis.annotations.Mapper")); + interfaze.addAnnotation("@Mapper"); + return true; + } + + private enum Annotations { + DATA("data", "@Data", "lombok.Data"), + BUILDER("builder", "@Builder", "lombok.Builder"), + ALL_ARGS_CONSTRUCTOR("allArgsConstructor", "@AllArgsConstructor", "lombok.AllArgsConstructor"), + NO_ARGS_CONSTRUCTOR("noArgsConstructor", "@NoArgsConstructor", "lombok.NoArgsConstructor"), + ACCESSORS("accessors", "@Accessors", "lombok.experimental.Accessors"), + TO_STRING("toString", "@ToString", "lombok.ToString"); + + + private final String paramName; + private final String name; + private final FullyQualifiedJavaType javaType; + private final List options; + + + Annotations(String paramName, String name, String className) { + this.paramName = paramName; + this.name = name; + this.javaType = new FullyQualifiedJavaType(className); + this.options = new ArrayList(); + } + + private static Annotations getValueOf(String paramName) { + for (Annotations annotation : Annotations.values()) + if (String.CASE_INSENSITIVE_ORDER.compare(paramName, annotation.paramName) == 0) + return annotation; + + return null; + } + + private static Collection getDependencies(Annotations annotation) { + if (annotation == ALL_ARGS_CONSTRUCTOR) + return Collections.singleton(NO_ARGS_CONSTRUCTOR); + else + return Collections.emptyList(); + } + + // A trivial quoting. + // Because Lombok annotation options type is almost String or boolean. + private static String quote(String value) { + if (Boolean.TRUE.toString().equals(value) || Boolean.FALSE.toString().equals(value)) + // case of boolean, not passed as an array. + return value; + return value.replaceAll("[\\w]+", "\"$0\""); + } + + private void appendOptions(String key, String value) { + String keyPart = key.substring(key.indexOf(".") + 1); + String valuePart = value.contains(",") ? String.format("{%s}", value) : value; + this.options.add(String.format("%s=%s", keyPart, quote(valuePart))); + } + + private String asAnnotation() { + if (options.isEmpty()) { + return name; + } + StringBuilder sb = new StringBuilder(); + sb.append(name); + sb.append("("); + boolean first = true; + for (String option : options) { + if (first) { + first = false; + } else { + sb.append(", "); + } + sb.append(option); + } + sb.append(")"); + return sb.toString(); + } + } +} diff --git a/src/com/github/leecho/idea/plugin/mybatis/generator/plugin/RenameExampleClassPlugin.java b/src/com/github/leecho/idea/plugin/mybatis/generator/plugin/RenameExampleClassPlugin.java new file mode 100644 index 0000000..a10a16d --- /dev/null +++ b/src/com/github/leecho/idea/plugin/mybatis/generator/plugin/RenameExampleClassPlugin.java @@ -0,0 +1,43 @@ +package com.github.leecho.idea.plugin.mybatis.generator.plugin; + +import org.mybatis.generator.api.IntrospectedTable; +import org.mybatis.generator.api.PluginAdapter; + +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static org.mybatis.generator.internal.util.StringUtility.stringHasValue; +import static org.mybatis.generator.internal.util.messages.Messages.getString; + +public class RenameExampleClassPlugin extends PluginAdapter { + private String target; + + /** + * + */ + public RenameExampleClassPlugin() { + } + + @Override + public boolean validate(List warnings) { + + target = this.getProperties().getProperty("target"); + boolean valid = stringHasValue(target); + + if (!valid) { + if (!stringHasValue(target)) { + warnings.add(getString("ValidationError.18", + "RenameExampleClassPlugin", + "searchString")); + } + } + + return valid; + } + + @Override + public void initialized(IntrospectedTable introspectedTable) { + introspectedTable.setExampleType(target); + } +} diff --git a/src/com/github/leecho/idea/plugin/mybatis/generator/ui/GenerateSettingUI.java b/src/com/github/leecho/idea/plugin/mybatis/generator/ui/GenerateSettingUI.java index df682e6..fea3414 100644 --- a/src/com/github/leecho/idea/plugin/mybatis/generator/ui/GenerateSettingUI.java +++ b/src/com/github/leecho/idea/plugin/mybatis/generator/ui/GenerateSettingUI.java @@ -24,16 +24,20 @@ import com.intellij.openapi.ui.*; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiPackage; +import com.intellij.ui.SeparatorWithText; +import com.intellij.ui.TitledSeparator; import com.intellij.ui.components.JBLabel; import com.intellij.ui.components.JBPanel; import com.intellij.ui.components.JBTextField; import org.jetbrains.annotations.Nullable; import javax.swing.*; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.KeyAdapter; -import java.awt.event.KeyEvent; +import java.awt.event.*; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @@ -54,14 +58,20 @@ public class GenerateSettingUI extends DialogWrapper { private JPanel contentPane = new JBPanel<>(); private JTextField tableNameField = new JBTextField(20); - private TextFieldWithBrowseButton projectRootField = new TextFieldWithBrowseButton(); + private TextFieldWithBrowseButton moduleRootField = new TextFieldWithBrowseButton(); + private TextFieldWithBrowseButton basePackageField = new TextFieldWithBrowseButton(); private TextFieldWithBrowseButton entityPackageField = new TextFieldWithBrowseButton(); private TextFieldWithBrowseButton mapperPackageField = new TextFieldWithBrowseButton(); - private TextFieldWithBrowseButton xmlPackageField = new TextFieldWithBrowseButton(); + private TextFieldWithBrowseButton examplePackageField = new TextFieldWithBrowseButton(); + private JTextField xmlPackageField = new JTextField(); private JTextField mapperNameField = new JBTextField(20); private JTextField entityNameField = new JBTextField(20); + private JTextField exampleNameField = new JBTextField(20); private JTextField primaryKeyField = new JBTextField(20); + private JPanel examplePackagePanel = new JPanel(); + private JPanel exampleNamePanel = new JPanel(); + private JLabel connectStatusLabel = new JLabel(); private JCheckBox offsetLimitBox = new JCheckBox("Page(分页)"); @@ -116,12 +126,14 @@ public GenerateSettingUI(AnActionEvent anActionEvent) { if (entityConfig == null) { //初始化配置 entityConfig = new EntityConfig(); - entityConfig.setProjectRootPath(globalConfig.getProjectRootPath()); + entityConfig.setModuleRootPath(globalConfig.getModuleRootPath()); entityConfig.setSourcePath(globalConfig.getSourcePath()); entityConfig.setResourcePath(globalConfig.getResourcePath()); entityConfig.setEntityPackage(globalConfig.getEntityPackage()); entityConfig.setMapperPackage(globalConfig.getMapperPackage()); entityConfig.setMapperPostfix(globalConfig.getMapperPostfix()); + entityConfig.setExamplePostfix(globalConfig.getExamplePostfix()); + entityConfig.setExamplePackage(globalConfig.getExamplePackage()); entityConfig.setXmlPackage(globalConfig.getXmlPackage()); entityConfig.setOffsetLimit(globalConfig.isOffsetLimit()); @@ -159,8 +171,8 @@ public GenerateSettingUI(AnActionEvent anActionEvent) { private List validateSetting() { List errors = new ArrayList<>(); - if (StringUtils.isEmpty(projectRootField.getText())) { - errors.add("Project root must not be null"); + if (StringUtils.isEmpty(moduleRootField.getText())) { + errors.add("Module root must not be null"); } if (StringUtils.isEmpty(entityNameField.getText())) { @@ -183,6 +195,14 @@ private List validateSetting() { errors.add("Mapper xml package must not be null"); } + if(useExampleBox.getSelectedObjects() != null){ + if(StringUtils.isEmpty(exampleNameField.getText())){ + errors.add("Example name must not be null"); + } + if(StringUtils.isEmpty(examplePackageField.getText())){ + errors.add("Example package must not be null"); + } + } return errors; } @@ -219,6 +239,11 @@ protected void doOKAction() { if (confirm == 2) { return; } + }else{ + int confirm = Messages.showOkCancelDialog(project, "Confirm start generate?", "Mybatis Generator Plus", Messages.getQuestionIcon()); + if (confirm == 2) { + return; + } } super.doOKAction(); @@ -265,7 +290,10 @@ private boolean getDatabaseCredential(RawConnectionConfig connectionConfig) { private void initOptionsPanel() { JBPanel optionsPanel = new JBPanel(new GridLayout(8, 2, 10, 10)); - optionsPanel.setBorder(BorderFactory.createTitledBorder("Options")); + TitledSeparator separator = new TitledSeparator(); + separator.setText("Options"); + separator.setLabelFor(optionsPanel); + contentPane.add(separator); optionsPanel.add(offsetLimitBox); optionsPanel.add(commentBox); @@ -284,6 +312,11 @@ private void initOptionsPanel() { optionsPanel.add(lombokAnnotationBox); optionsPanel.add(lombokBuilderAnnotationBox); + useExampleBox.addChangeListener(e -> { + exampleNamePanel.setVisible(useExampleBox.getSelectedObjects() != null); + examplePackagePanel.setVisible(useExampleBox.getSelectedObjects() != null); + }); + offsetLimitBox.setSelected(entityConfig.isOffsetLimit()); commentBox.setSelected(entityConfig.isComment()); overrideBox.setSelected(entityConfig.isOverride()); @@ -304,31 +337,58 @@ private void initOptionsPanel() { contentPane.add(optionsPanel); } + /** + * 初始化Package组件 + */ private void initPackagePanel() { - JPanel projectRootPanel = new JPanel(); - projectRootPanel.setLayout(new BoxLayout(projectRootPanel, BoxLayout.X_AXIS)); - JBLabel projectRootLabel = new JBLabel("Project Root:"); - projectRootLabel.setPreferredSize(new Dimension(200, 20)); - projectRootField.addBrowseFolderListener(new TextBrowseFolderListener(FileChooserDescriptorFactory.createSingleFolderDescriptor()) { + JPanel moduleRootPanel = new JPanel(); + moduleRootPanel.setLayout(new BoxLayout(moduleRootPanel, BoxLayout.X_AXIS)); + JBLabel projectRootLabel = new JBLabel("Module Root:"); + projectRootLabel.setPreferredSize(new Dimension(150, 20)); + moduleRootField.addBrowseFolderListener(new TextBrowseFolderListener(FileChooserDescriptorFactory.createSingleFolderDescriptor()) { @Override public void actionPerformed(ActionEvent e) { super.actionPerformed(e); - projectRootField.setText(projectRootField.getText().replaceAll("\\\\", "/")); + moduleRootField.setText(moduleRootField.getText().replaceAll("\\\\", "/")); } }); - if (entityConfig != null && !StringUtils.isEmpty(entityConfig.getProjectRootPath())) { - projectRootField.setText(entityConfig.getProjectRootPath()); + if (entityConfig != null && !StringUtils.isEmpty(entityConfig.getModuleRootPath())) { + moduleRootField.setText(entityConfig.getModuleRootPath()); } else { - projectRootField.setText(project.getBasePath()); + moduleRootField.setText(project.getBasePath()); } - projectRootPanel.add(projectRootLabel); - projectRootPanel.add(projectRootField); + moduleRootPanel.add(projectRootLabel); + moduleRootPanel.add(moduleRootField); + + JPanel basePackagePanel = new JPanel(); + basePackagePanel.setLayout(new BoxLayout(basePackagePanel, BoxLayout.X_AXIS)); + JBLabel basePackageLabel = new JBLabel("Base Package:"); + basePackageLabel.setPreferredSize(new Dimension(150, 20)); + basePackageField.setEditable(false); + basePackageField.addActionListener(e -> { + final PackageChooserDialog chooser = new PackageChooserDialog("Select Base Package", project); + chooser.selectPackage(basePackageField.getText()); + chooser.show(); + final PsiPackage psiPackage = chooser.getSelectedPackage(); + String packageName = psiPackage == null ? null : psiPackage.getQualifiedName(); + basePackageField.setText(packageName); + entityPackageField.setText(packageName + ".domain"); + mapperPackageField.setText(packageName + "." + getMapperPostfix().toLowerCase()); + examplePackageField.setText(packageName + "." + getExamplePostfix().toLowerCase()); + }); + if (entityConfig != null && !StringUtils.isEmpty(entityConfig.getBasePackage())) { + basePackageField.setText(entityConfig.getBasePackage()); + } else { + basePackageField.setText(""); + } + basePackagePanel.add(basePackageLabel); + basePackagePanel.add(basePackageField); JPanel entityPackagePanel = new JPanel(); entityPackagePanel.setLayout(new BoxLayout(entityPackagePanel, BoxLayout.X_AXIS)); - JBLabel entityPackageLabel = new JBLabel("Entity Class Package:"); - entityPackageLabel.setPreferredSize(new Dimension(200, 20)); + JBLabel entityPackageLabel = new JBLabel("Entity Package:"); + entityPackageLabel.setPreferredSize(new Dimension(150, 20)); entityPackageField.addActionListener(e -> { final PackageChooserDialog chooser = new PackageChooserDialog("Select Entity Package", project); chooser.selectPackage(entityPackageField.getText()); @@ -347,13 +407,14 @@ public void actionPerformed(ActionEvent e) { JPanel mapperPackagePanel = new JPanel(); mapperPackagePanel.setLayout(new BoxLayout(mapperPackagePanel, BoxLayout.X_AXIS)); - JLabel mapperPackageLabel = new JLabel("Mapper Class Package:"); - mapperPackageLabel.setPreferredSize(new Dimension(200, 20)); - mapperPackageField.addActionListener(e -> { - final PackageChooserDialog chooser = new PackageChooserDialog("Select Mapper Package", project); - chooser.selectPackage(mapperPackageField.getText()); - chooser.show(); - final PsiPackage psiPackage = chooser.getSelectedPackage(); + JLabel mapperPackageLabel = new JLabel("Mapper Package:"); + mapperPackageLabel.setPreferredSize(new Dimension(150, 20)); + mapperPackageField.addActionListener(event -> { + final PackageChooserDialog packageChooserDialog = new PackageChooserDialog("Select Mapper Package", project); + packageChooserDialog.selectPackage(mapperPackageField.getText()); + packageChooserDialog.show(); + + final PsiPackage psiPackage = packageChooserDialog.getSelectedPackage(); String packageName = psiPackage == null ? null : psiPackage.getQualifiedName(); mapperPackageField.setText(packageName); }); @@ -365,35 +426,54 @@ public void actionPerformed(ActionEvent e) { mapperPackagePanel.add(mapperPackageLabel); mapperPackagePanel.add(mapperPackageField); + examplePackagePanel.setLayout(new BoxLayout(examplePackagePanel, BoxLayout.X_AXIS)); + + examplePackageField.addActionListener(e -> { + final PackageChooserDialog packageChooserDialog = new PackageChooserDialog("Select Example Package", project); + packageChooserDialog.selectPackage(examplePackageField.getText()); + packageChooserDialog.show(); + + final PsiPackage psiPackage = packageChooserDialog.getSelectedPackage(); + String packageName = psiPackage == null ? null : psiPackage.getQualifiedName(); + examplePackageField.setText(packageName); + }); + + JLabel examplePackageLabel = new JLabel("Example Package:"); + examplePackageLabel.setPreferredSize(new Dimension(150, 20)); + examplePackageField.setText(entityConfig.getExamplePackage()); + examplePackagePanel.add(examplePackageLabel); + examplePackagePanel.add(examplePackageField); + examplePackagePanel.setVisible(entityConfig.isUseExample()); + JPanel xmlPackagePanel = new JPanel(); xmlPackagePanel.setLayout(new BoxLayout(xmlPackagePanel, BoxLayout.X_AXIS)); - JLabel xmlPackageLabel = new JLabel("Mapper Xml Package:"); - xmlPackageLabel.setPreferredSize(new Dimension(200, 20)); - xmlPackageField.addBrowseFolderListener(new TextBrowseFolderListener(FileChooserDescriptorFactory.createSingleFolderDescriptor()) { - @Override - public void actionPerformed(ActionEvent e) { - super.actionPerformed(e); - xmlPackageField.setText(xmlPackageField.getText().replaceAll("\\\\", "/")); - } - }); + JLabel xmlPackageLabel = new JLabel("Xml Package:"); + xmlPackageLabel.setPreferredSize(new Dimension(150, 20)); xmlPackageField.setText(entityConfig.getXmlPackage()); xmlPackagePanel.add(xmlPackageLabel); xmlPackagePanel.add(xmlPackageField); JPanel packagePanel = new JPanel(); packagePanel.setLayout(new VerticalFlowLayout(VerticalFlowLayout.TOP)); - packagePanel.setBorder(BorderFactory.createTitledBorder("Package")); - packagePanel.add(projectRootPanel); + packagePanel.add(moduleRootPanel); + packagePanel.add(basePackagePanel); packagePanel.add(entityPackagePanel); packagePanel.add(mapperPackagePanel); + packagePanel.add(examplePackagePanel); packagePanel.add(xmlPackagePanel); + + TitledSeparator separator = new TitledSeparator(); + separator.setText("Package"); + contentPane.add(separator); contentPane.add(packagePanel); } private void initEntityPanel(String tableName, String modelName, String primaryKey) { JPanel entityPanel = new JPanel(); entityPanel.setLayout(new VerticalFlowLayout(VerticalFlowLayout.TOP)); - entityPanel.setBorder(BorderFactory.createTitledBorder("Entity")); + TitledSeparator separator = new TitledSeparator(); + separator.setText("Entity"); + contentPane.add(separator); contentPane.add(entityPanel); //Table @@ -401,7 +481,7 @@ private void initEntityPanel(String tableName, String modelName, String primaryK tableNamePanel.setLayout(new BoxLayout(tableNamePanel, BoxLayout.X_AXIS)); JLabel tableLabel = new JLabel("Table Name:"); tableLabel.setLabelFor(tableNameField); - tableLabel.setPreferredSize(new Dimension(200, 20)); + tableLabel.setPreferredSize(new Dimension(150, 20)); tableNamePanel.add(tableLabel); tableNamePanel.add(tableNameField); if (psiElements.length > 1) { @@ -416,28 +496,14 @@ public void keyReleased(KeyEvent e) { String entityName = StringUtils.dbStringToCamelStyle(tableNameField.getText()); entityNameField.setText(entityName); mapperNameField.setText(getMapperName(entityName)); + exampleNameField.setText(getExampleName(entityName)); } }); - //PrimaryKey - /*JPanel primaryKeyPanel = new JPanel(); - primaryKeyPanel.setLayout(new BoxLayout(primaryKeyPanel, BoxLayout.X_AXIS)); - JLabel primaryKeyLabel = new JLabel("Primary Key:"); - primaryKeyLabel.setPreferredSize(new Dimension(200, 20)); - primaryKeyLabel.setLabelFor(primaryKeyField); - primaryKeyPanel.add(primaryKeyLabel); - primaryKeyPanel.add(primaryKeyField); - if (psiElements.length > 1) { - primaryKeyField.addFocusListener(new JTextFieldHintListener(primaryKeyField, "eg:primary key")); - } else { - primaryKeyField.setText(primaryKey); - }*/ - - //EntityName JPanel entityNamePanel = new JPanel(); entityNamePanel.setLayout(new BoxLayout(entityNamePanel, BoxLayout.X_AXIS)); JLabel entityNameLabel = new JLabel("Entity Name:"); - entityNameLabel.setPreferredSize(new Dimension(200, 20)); + entityNameLabel.setPreferredSize(new Dimension(150, 20)); entityNamePanel.add(entityNameLabel); entityNamePanel.add(entityNameField); entityPanel.add(entityNamePanel); @@ -450,6 +516,7 @@ public void keyReleased(KeyEvent e) { @Override public void keyReleased(KeyEvent e) { mapperNameField.setText(getMapperName(entityNameField.getText())); + exampleNameField.setText(getExampleName(entityNameField.getText())); } }); @@ -457,7 +524,7 @@ public void keyReleased(KeyEvent e) { JPanel mapperNamePanel = new JPanel(); mapperNamePanel.setLayout(new BoxLayout(mapperNamePanel, BoxLayout.X_AXIS)); JLabel mapperNameLabel = new JLabel("Mapper Name:"); - mapperNameLabel.setPreferredSize(new Dimension(200, 20)); + mapperNameLabel.setPreferredSize(new Dimension(150, 20)); mapperNameLabel.setLabelFor(entityNameField); mapperNamePanel.add(mapperNameLabel); mapperNamePanel.add(mapperNameField); @@ -471,23 +538,45 @@ public void keyReleased(KeyEvent e) { mapperNameField.setText(getMapperName(modelName)); } + exampleNamePanel.setLayout(new BoxLayout(exampleNamePanel, BoxLayout.X_AXIS)); + JLabel exampleNameLabel = new JLabel("Example Name:"); + exampleNameLabel.setPreferredSize(new Dimension(150, 20)); + exampleNameLabel.setLabelFor(entityNameField); + exampleNamePanel.add(exampleNameLabel); + exampleNamePanel.add(exampleNameField); + if (psiElements.length > 1) { + if (entityConfig != null && !StringUtils.isEmpty(entityConfig.getExamplePostfix())) { + exampleNameField.addFocusListener(new JTextFieldHintListener(exampleNameField, "eg:DbTable" + entityConfig.getExamplePostfix())); + } else { + exampleNameField.addFocusListener(new JTextFieldHintListener(exampleNameField, "eg:DbTable" + "Example")); + } + } else { + exampleNameField.setText(getExampleName(modelName)); + } + + exampleNamePanel.setVisible(entityConfig.isUseExample()); + entityPanel.add(tableNamePanel); - //entityPanel.add(primaryKeyPanel); entityPanel.add(entityNamePanel); entityPanel.add(mapperNamePanel); + entityPanel.add(exampleNamePanel); } public void generate(RawConnectionConfig connectionConfig) { entityConfig.setName(tableNameField.getText()); entityConfig.setTableName(tableNameField.getText()); - entityConfig.setProjectRootPath(projectRootField.getText()); + entityConfig.setModuleRootPath(moduleRootField.getText()); + entityConfig.setBasePackage(basePackageField.getText()); entityConfig.setEntityPackage(entityPackageField.getText()); entityConfig.setMapperPackage(mapperPackageField.getText()); + entityConfig.setExamplePackage(examplePackageField.getText()); entityConfig.setXmlPackage(xmlPackageField.getText()); + entityConfig.setMapperName(mapperNameField.getText()); entityConfig.setEntityName(entityNameField.getText()); entityConfig.setPrimaryKey(primaryKeyField.getText()); + entityConfig.setExampleName(exampleNameField.getText()); entityConfig.setOffsetLimit(offsetLimitBox.getSelectedObjects() != null); entityConfig.setComment(commentBox.getSelectedObjects() != null); @@ -521,6 +610,30 @@ private String getMapperName(String entityName) { } } + private String getMapperPostfix() { + if (entityConfig != null && !StringUtils.isEmpty(entityConfig.getMapperPostfix())) { + return entityConfig.getMapperPostfix(); + } else { + return "Mapper"; + } + } + + private String getExamplePostfix() { + if (entityConfig != null && !StringUtils.isEmpty(entityConfig.getExamplePostfix())) { + return entityConfig.getExamplePostfix(); + } else { + return "Example"; + } + } + + private String getExampleName(String entityName) { + if (entityConfig != null && !StringUtils.isEmpty(entityConfig.getExamplePostfix())) { + return entityName + entityConfig.getExamplePostfix(); + } else { + return (entityName + "Example"); + } + } + @Nullable @Override protected JComponent createCenterPanel() { diff --git a/src/com/github/leecho/idea/plugin/mybatis/generator/ui/GeneratorSettingUI.java b/src/com/github/leecho/idea/plugin/mybatis/generator/ui/GeneratorSettingUI.java index 6b6638c..22d1be9 100644 --- a/src/com/github/leecho/idea/plugin/mybatis/generator/ui/GeneratorSettingUI.java +++ b/src/com/github/leecho/idea/plugin/mybatis/generator/ui/GeneratorSettingUI.java @@ -9,6 +9,7 @@ import com.intellij.openapi.ui.TextFieldWithBrowseButton; import com.intellij.openapi.ui.VerticalFlowLayout; import com.intellij.psi.PsiPackage; +import com.intellij.ui.TitledSeparator; import com.intellij.ui.components.JBLabel; import com.intellij.openapi.project.Project; import com.intellij.ui.components.JBPanel; @@ -29,12 +30,14 @@ public class GeneratorSettingUI extends JDialog { private TextFieldWithBrowseButton entityPackageField = new TextFieldWithBrowseButton(); private TextFieldWithBrowseButton mapperPackageField = new TextFieldWithBrowseButton(); private TextFieldWithBrowseButton xmlPackageField = new TextFieldWithBrowseButton(); - private TextFieldWithBrowseButton projectRootField = new TextFieldWithBrowseButton(); + private TextFieldWithBrowseButton examplePackageField = new TextFieldWithBrowseButton(); + private TextFieldWithBrowseButton moduleRootField = new TextFieldWithBrowseButton(); private JTextField sourcePathField = new JTextField(); private JTextField resourcePathField = new JTextField(); - private JTextField mapperNameField = new JTextField(10); + private JTextField mapperPostfixField = new JTextField(10); + private JTextField examplePostfixField = new JTextField(10); private JCheckBox offsetLimitBox = new JCheckBox("Page(分页)"); private JCheckBox commentBox = new JCheckBox("Comment(实体注释)"); @@ -67,11 +70,13 @@ public void createUI(Project project) { config = MyBatisGeneratorConfiguration.getInstance(project); this.initPathPanel(); + this.initPostfixPanel(); this.initPackagePanel(); this.initOptionsPanel(); GlobalConfig globalConfig = config.getGlobalConfig(); - mapperNameField.setText(globalConfig.getMapperPostfix()); + mapperPostfixField.setText(globalConfig.getMapperPostfix()); + examplePostfixField.setText(globalConfig.getExamplePostfix()); entityPackageField.setText(globalConfig.getEntityPackage()); mapperPackageField.setText(globalConfig.getMapperPackage()); xmlPackageField.setText(globalConfig.getXmlPackage()); @@ -100,7 +105,6 @@ public void createUI(Project project) { private void initOptionsPanel() { JBPanel optionsPanel = new JBPanel(new GridLayout(8, 2, 10, 10)); - optionsPanel.setBorder(BorderFactory.createTitledBorder("Options")); optionsPanel.add(offsetLimitBox); optionsPanel.add(commentBox); @@ -118,12 +122,15 @@ private void initOptionsPanel() { optionsPanel.add(mysql_8Box); optionsPanel.add(lombokAnnotationBox); optionsPanel.add(lombokBuilderAnnotationBox); + + TitledSeparator separator = new TitledSeparator(); + separator.setText("Options"); + contentPanel.add(separator); contentPanel.add(optionsPanel); } private void initPathPanel() { - JPanel sourcePathPanel = new JPanel(); sourcePathPanel.setLayout(new BoxLayout(sourcePathPanel, BoxLayout.X_AXIS)); JBLabel sourcePathLabel = new JBLabel("Source Path:"); @@ -140,34 +147,62 @@ private void initPathPanel() { JPanel pathPanel = new JPanel(); pathPanel.setLayout(new VerticalFlowLayout(VerticalFlowLayout.TOP)); - pathPanel.setBorder(BorderFactory.createTitledBorder("Path")); - pathPanel.add(resourcePathPanel); + TitledSeparator separator = new TitledSeparator(); + separator.setText("Path"); pathPanel.add(sourcePathPanel); + pathPanel.add(resourcePathPanel); + contentPanel.add(separator); contentPanel.add(pathPanel); } + private void initPostfixPanel() { + + JPanel mapperPostfixPanel = new JPanel(); + mapperPostfixPanel.setLayout(new BoxLayout(mapperPostfixPanel, BoxLayout.X_AXIS)); + JBLabel mapperPostfixLabel = new JBLabel("Mapper Postfix:"); + mapperPostfixLabel.setPreferredSize(new Dimension(200, 20)); + mapperPostfixPanel.add(mapperPostfixLabel); + mapperPostfixPanel.add(mapperPostfixField); + + JPanel examplePostfixPanel = new JPanel(); + examplePostfixPanel.setLayout(new BoxLayout(examplePostfixPanel, BoxLayout.X_AXIS)); + JBLabel examplePostfixLabel = new JBLabel("Example Postfix:"); + examplePostfixLabel.setPreferredSize(new Dimension(200, 20)); + examplePostfixPanel.add(examplePostfixLabel); + examplePostfixPanel.add(examplePostfixField); + + JPanel postfixPanel = new JPanel(); + postfixPanel.setLayout(new VerticalFlowLayout(VerticalFlowLayout.TOP)); + TitledSeparator separator = new TitledSeparator(); + separator.setText("Postfix"); + postfixPanel.add(mapperPostfixPanel); + postfixPanel.add(examplePostfixPanel); + contentPanel.add(separator); + contentPanel.add(postfixPanel); + } + private void initPackagePanel() { GlobalConfig globalConfig = config.getGlobalConfig(); JPanel projectRootPanel = new JPanel(); projectRootPanel.setLayout(new BoxLayout(projectRootPanel, BoxLayout.X_AXIS)); - JBLabel projectRootLabel = new JBLabel("Project Root:"); + JBLabel projectRootLabel = new JBLabel("Module Root:"); projectRootLabel.setPreferredSize(new Dimension(200, 20)); - projectRootField.addBrowseFolderListener(new TextBrowseFolderListener(FileChooserDescriptorFactory.createSingleFolderDescriptor()) { + moduleRootField.addBrowseFolderListener(new TextBrowseFolderListener(FileChooserDescriptorFactory.createSingleFolderDescriptor()) { @Override public void actionPerformed(ActionEvent e) { super.actionPerformed(e); - projectRootField.setText(projectRootField.getText().replaceAll("\\\\", "/")); + moduleRootField.setText(moduleRootField.getText().replaceAll("\\\\", "/")); } }); - if (globalConfig != null && !StringUtils.isEmpty(globalConfig.getProjectRootPath())) { - projectRootField.setText(globalConfig.getProjectRootPath()); + if (globalConfig != null && !StringUtils.isEmpty(globalConfig.getModuleRootPath())) { + moduleRootField.setText(globalConfig.getModuleRootPath()); } else { - projectRootField.setText(project.getBasePath()); + moduleRootField.setText(project.getBasePath()); } projectRootPanel.add(projectRootLabel); - projectRootPanel.add(projectRootField); + projectRootPanel.add(moduleRootField); JPanel entityPackagePanel = new JPanel(); entityPackagePanel.setLayout(new BoxLayout(entityPackagePanel, BoxLayout.X_AXIS)); @@ -199,6 +234,21 @@ public void actionPerformed(ActionEvent e) { mapperPackagePanel.add(mapperPackageLabel); mapperPackagePanel.add(mapperPackageField); + JPanel examplePackagePanel = new JPanel(); + examplePackagePanel.setLayout(new BoxLayout(examplePackagePanel, BoxLayout.X_AXIS)); + JLabel examplePackageLabel = new JLabel("Example Class Package:"); + examplePackageLabel.setPreferredSize(new Dimension(200, 20)); + examplePackageField.addActionListener(e -> { + final PackageChooserDialog chooser = new PackageChooserDialog("Select Example Package", project); + chooser.selectPackage(examplePackageField.getText()); + chooser.show(); + final PsiPackage psiPackage = chooser.getSelectedPackage(); + String packageName = psiPackage == null ? null : psiPackage.getQualifiedName(); + examplePackageField.setText(packageName); + }); + examplePackagePanel.add(examplePackageLabel); + examplePackagePanel.add(examplePackageField); + JPanel xmlPackagePanel = new JPanel(); xmlPackagePanel.setLayout(new BoxLayout(xmlPackagePanel, BoxLayout.X_AXIS)); JLabel xmlPackageLabel = new JLabel("Mapper Xml Package:"); @@ -216,11 +266,15 @@ public void actionPerformed(ActionEvent e) { JPanel packagePanel = new JPanel(); packagePanel.setLayout(new VerticalFlowLayout(VerticalFlowLayout.TOP)); - packagePanel.setBorder(BorderFactory.createTitledBorder("Package")); + //packagePanel.setBorder(BorderFactory.createTitledBorder("Package")); packagePanel.add(projectRootPanel); packagePanel.add(entityPackagePanel); - packagePanel.add(mapperPackagePanel); + packagePanel.add(examplePackagePanel); packagePanel.add(xmlPackagePanel); + + TitledSeparator separator = new TitledSeparator(); + separator.setText("Package"); + contentPanel.add(separator); contentPanel.add(packagePanel); } @@ -244,7 +298,8 @@ public boolean isModified() { public void apply() { GlobalConfig globalConfig = new GlobalConfig(); - globalConfig.setMapperPostfix(mapperNameField.getText()); + globalConfig.setMapperPostfix(mapperPostfixField.getText()); + globalConfig.setExamplePostfix(examplePostfixField.getText()); globalConfig.setEntityPackage(entityPackageField.getText()); globalConfig.setMapperPackage(mapperPackageField.getText()); globalConfig.setXmlPackage(xmlPackageField.getText());