├── .gitignore ├── ui.png ├── nutzwk-plugin.jar ├── README.md ├── src └── cn │ └── enilu │ └── common │ └── code │ └── plugin │ ├── ui │ ├── ErrorDialog.java │ └── ConfigDialog.java │ ├── utils │ └── GenerateConfig.java │ └── GeneratorAction.java └── META-INF └── plugin.xml /.gitignore: -------------------------------------------------------------------------------- 1 | /out/ 2 | /.idea/ 3 | *.iml 4 | -------------------------------------------------------------------------------- /ui.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enilu/nutzwk-plugin/master/ui.png -------------------------------------------------------------------------------- /nutzwk-plugin.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enilu/nutzwk-plugin/master/nutzwk-plugin.jar -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nutzwk-plugin 2 | 3 | nutzwk的idea intellij 的MVC代码生成插件 4 | 5 | ## 使用方法 6 | 7 | ### 下载安装插件 8 | 9 | - [github下载](nutzwk-plugin.jar) 10 | - IDEA插件库下载 11 | 12 | ### 依赖[nutzwk-code-generator](https://github.com/enilu/nutzwk-code-generator) 13 | 14 | 在自己的项目中添加依赖 15 | 16 | ``` 17 | 18 | cn.enilu.tools 19 | nutzwk-code-generator 20 | 1.2 21 | 22 | ``` 23 | 24 | ### 用法 25 | 26 | - 打开要生成model类,在代码中右键打开“Generate...”,选择“nutzwk mvc” 27 | - 在弹出窗口选择“ok”按钮即可,即可在相应位置生成代码 28 | 29 | ![使用界面](ui.png) 30 | 31 | **注意** 32 | 33 | - 没错,这个插件,其实就是给nutzwk-code-generator增加个配置界面而已^_^ 34 | - 需要依赖最新版的nutzwk-code-generator项目 35 | -------------------------------------------------------------------------------- /src/cn/enilu/common/code/plugin/ui/ErrorDialog.java: -------------------------------------------------------------------------------- 1 | package cn.enilu.common.code.plugin.ui; 2 | 3 | import com.intellij.openapi.project.Project; 4 | import com.intellij.openapi.ui.DialogWrapper; 5 | import com.intellij.ui.components.JBLabel; 6 | import com.intellij.ui.components.JBPanel; 7 | import com.intellij.ui.components.JBTextField; 8 | import org.jetbrains.annotations.Nullable; 9 | 10 | import javax.swing.*; 11 | 12 | /** 13 | * Created by ghost on 2016/4/1. 14 | */ 15 | public class ErrorDialog extends DialogWrapper { 16 | private String msg = ""; 17 | 18 | public ErrorDialog(@Nullable Project project) { 19 | super(project); 20 | setTitle("Error Occured, Please Retry!"); 21 | setOKActionEnabled(false); 22 | init(); 23 | } 24 | 25 | public ErrorDialog(@Nullable Project project, String title, String msg) { 26 | super(project); 27 | this.msg = msg; 28 | setTitle(title); 29 | setOKActionEnabled(false); 30 | init(); 31 | } 32 | 33 | @Nullable 34 | @Override 35 | protected JComponent createCenterPanel() { 36 | if (msg.length() > 0) { 37 | JComponent centerPanel = super.createContentPane(); 38 | JTextArea textArea = new JTextArea(msg,8,60); 39 | centerPanel.add(textArea); 40 | return centerPanel; 41 | } 42 | return null; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /META-INF/plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | cn.enilu 3 | nutzwk-plugin 4 | 1.6 5 | enilu 6 | 7 | 9 | 10 | ]]> 11 | 12 | 15 | 16 | 17 | 18 | 19 | 20 | 22 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /src/cn/enilu/common/code/plugin/utils/GenerateConfig.java: -------------------------------------------------------------------------------- 1 | package cn.enilu.common.code.plugin.utils; 2 | 3 | import com.intellij.openapi.project.Project; 4 | 5 | /** 6 | * 代码生成配置对象
7 | *

Copyright by easecredit.com
8 | * 作者: zhangtao
9 | * 创建日期: 16-7-23
10 | */ 11 | public class GenerateConfig { 12 | private boolean service; 13 | private boolean conroller; 14 | private boolean view; 15 | private String baseUri; 16 | private String basePackage; 17 | 18 | private String controllerPakName="controllers"; 19 | private String ServicePakName="services"; 20 | private String modelPakName="models"; 21 | private boolean force=false; 22 | private String pages; 23 | private Project project; 24 | 25 | public boolean isService() { 26 | return service; 27 | } 28 | 29 | public void setService(boolean service) { 30 | this.service = service; 31 | } 32 | 33 | public boolean isConroller() { 34 | return conroller; 35 | } 36 | 37 | public void setConroller(boolean conroller) { 38 | this.conroller = conroller; 39 | } 40 | 41 | public boolean isView() { 42 | return view; 43 | } 44 | 45 | public void setView(boolean view) { 46 | this.view = view; 47 | } 48 | 49 | public String getBaseUri() { 50 | return baseUri; 51 | } 52 | 53 | public void setBaseUri(String baseUri) { 54 | this.baseUri = baseUri; 55 | } 56 | 57 | public String getBasePackage() { 58 | return basePackage; 59 | } 60 | 61 | public void setBasePackage(String basePackage) { 62 | this.basePackage = basePackage; 63 | } 64 | 65 | public String getControllerPakName() { 66 | return controllerPakName; 67 | } 68 | 69 | public void setControllerPakName(String controllerPakName) { 70 | this.controllerPakName = controllerPakName; 71 | } 72 | 73 | public String getServicePakName() { 74 | return ServicePakName; 75 | } 76 | 77 | public void setServicePakName(String servicePakName) { 78 | ServicePakName = servicePakName; 79 | } 80 | 81 | public String getModelPakName() { 82 | return modelPakName; 83 | } 84 | 85 | public void setModelPakName(String modelPakName) { 86 | this.modelPakName = modelPakName; 87 | } 88 | 89 | public Project getProject() { 90 | return project; 91 | } 92 | 93 | public void setProject(Project project) { 94 | this.project = project; 95 | } 96 | 97 | public boolean isForce() { 98 | return force; 99 | } 100 | 101 | public void setForce(boolean force) { 102 | this.force = force; 103 | } 104 | 105 | public String getPages() { 106 | return pages; 107 | } 108 | 109 | public void setPages(String pages) { 110 | this.pages = pages; 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /src/cn/enilu/common/code/plugin/GeneratorAction.java: -------------------------------------------------------------------------------- 1 | package cn.enilu.common.code.plugin; 2 | 3 | import cn.enilu.common.code.plugin.ui.ErrorDialog; 4 | import cn.enilu.common.code.plugin.ui.ConfigDialog; 5 | import cn.enilu.common.code.plugin.utils.GenerateConfig; 6 | import com.google.common.io.Files; 7 | import com.intellij.execution.Executor; 8 | import com.intellij.execution.ExecutorRegistry; 9 | import com.intellij.execution.RunManager; 10 | import com.intellij.execution.RunnerAndConfigurationSettings; 11 | import com.intellij.execution.application.ApplicationConfiguration; 12 | import com.intellij.execution.application.ApplicationConfigurationType; 13 | import com.intellij.execution.runners.ExecutionUtil; 14 | import com.intellij.openapi.actionSystem.*; 15 | import com.intellij.openapi.editor.Document; 16 | import com.intellij.openapi.editor.Editor; 17 | import com.intellij.openapi.module.Module; 18 | import com.intellij.openapi.module.ModuleManager; 19 | import com.intellij.openapi.project.Project; 20 | import com.intellij.psi.*; 21 | import com.intellij.psi.util.PsiTreeUtil; 22 | 23 | import java.io.File; 24 | import java.nio.charset.Charset; 25 | 26 | 27 | /** 28 | * 入口类
29 | *

Copyright by easecredit.com
30 | * 作者: zhangtao
31 | * 创建日期: 16-7-24
32 | */ 33 | public class GeneratorAction extends AnAction { 34 | Project project=null; 35 | public void actionPerformed(AnActionEvent e) { 36 | project = e.getData(CommonDataKeys.PROJECT); 37 | 38 | String pomFile = project.getBasePath()+File.separator+"pom.xml"; 39 | try { 40 | String pomTxt = Files.toString(new File(pomFile), Charset.forName("utf8")); 41 | if(!pomTxt.contains("cn.enilu.tools")||!pomTxt.contains("nutzwk-code-generator")){ 42 | new ErrorDialog(project,"没有添加nutzwk-code-generator依赖","请在pom.xml文件中添加依赖:\r\n" + 43 | " \n" + 44 | " cn.enilu.tools\n" + 45 | " nutzwk-code-generator\n" + 46 | " 1.0\n" + 47 | " ").show(); 48 | return ; 49 | } 50 | }catch (Exception ex){ 51 | ex.printStackTrace(); 52 | new ErrorDialog(project,"error",ex.getMessage()+"\n该项目不是一个有效的maven项目,暂时不支持使用插件\n您以通过该链接:\n" + 53 | "http://mvnrepository.com/artifact/cn.enilu.tools/nutzwk-code-generator\n" + 54 | "下载nutzwk-code-generatorjar的包添加到项目中,\n" + 55 | "然后使用命令行生成代码").show(); 56 | return ; 57 | } 58 | final Editor editor = e.getData(CommonDataKeys.EDITOR); 59 | 60 | PsiClass rootClass = getTargetClass(e, editor); 61 | if (null == rootClass) { 62 | new ErrorDialog(project).show(); 63 | return; 64 | } 65 | ConfigDialog fieldDialog = null; 66 | try { 67 | fieldDialog = new ConfigDialog(rootClass); 68 | }catch (Exception ex){ 69 | new ErrorDialog(project,"error","\npackage名成不规范").show(); 70 | return ; 71 | } 72 | fieldDialog.show(); 73 | if (!fieldDialog.isOK()) { 74 | return; 75 | } 76 | 77 | generateCode(rootClass, fieldDialog.getGenerateConfig()); 78 | } 79 | 80 | private void generateCode(final PsiClass rootClass, 81 | final GenerateConfig generateConfig) { 82 | RunManager runManager = RunManager.getInstance(project); 83 | ApplicationConfiguration appConfig = new ApplicationConfiguration("generator", project, ApplicationConfigurationType.getInstance()); 84 | appConfig.MAIN_CLASS_NAME = "cn.enilu.common.code.Generator"; 85 | String entityClassName = rootClass.getName(); 86 | StringBuilder programArgs = new StringBuilder(); 87 | programArgs.append("-i ").append(entityClassName).append(" -u ").append(generateConfig.getBaseUri()).append(" -p ") 88 | .append(generateConfig.getBasePackage()) .append(generateConfig.isForce() ? " -f" : "") 89 | .append(generateConfig.getPages().length()>1?(" -v "+generateConfig.getPages()):"") 90 | .append(" -mod ").append(generateConfig.getModelPakName()) 91 | .append(" -ctr ").append(generateConfig.getControllerPakName()) 92 | .append(" -sev ").append(generateConfig.getServicePakName()) 93 | .append(generateConfig.isConroller() ? " controller" : "") 94 | .append(generateConfig.isService() ? " service" : "").append(generateConfig.isView()?" view":"") ; 95 | 96 | appConfig.PROGRAM_PARAMETERS = programArgs.toString(); 97 | 98 | appConfig.WORKING_DIRECTORY = project.getBasePath(); 99 | Module[] modules = ModuleManager.getInstance(project).getModules(); 100 | 101 | appConfig.setModule(modules[0]); 102 | RunnerAndConfigurationSettings configuration = runManager.createConfiguration(appConfig, appConfig.getFactory()); 103 | runManager.addConfiguration(configuration, true); 104 | Executor executor = ExecutorRegistry.getInstance().getExecutorById(com.intellij.openapi.wm.ToolWindowId.DEBUG); 105 | ExecutionUtil.runConfiguration(configuration, executor); 106 | 107 | } 108 | 109 | private PsiClass getTargetClass(AnActionEvent e, Editor editor) { 110 | final PsiFile file = e.getData(LangDataKeys.PSI_FILE); 111 | int offset = editor.getCaretModel().getOffset(); 112 | PsiElement element = file.findElementAt(offset); 113 | return PsiTreeUtil.getParentOfType(element, PsiClass.class); 114 | } 115 | 116 | @Override 117 | public void update(AnActionEvent e) { 118 | final Project project = e.getData(CommonDataKeys.PROJECT); 119 | final Editor editor = e.getData(CommonDataKeys.EDITOR); 120 | final PsiClass psiClass = getTargetClass(e, editor); 121 | e.getPresentation().setVisible((null != project && null != editor && null != psiClass && 122 | !psiClass.isEnum() && 0 != psiClass.getAllFields().length)); 123 | } 124 | 125 | } -------------------------------------------------------------------------------- /src/cn/enilu/common/code/plugin/ui/ConfigDialog.java: -------------------------------------------------------------------------------- 1 | package cn.enilu.common.code.plugin.ui; 2 | 3 | import cn.enilu.common.code.plugin.utils.GenerateConfig; 4 | import com.intellij.ide.util.DefaultPsiElementCellRenderer; 5 | import com.intellij.openapi.ui.DialogWrapper; 6 | import com.intellij.openapi.ui.LabeledComponent; 7 | import com.intellij.psi.PsiAnnotation; 8 | import com.intellij.psi.PsiClass; 9 | import com.intellij.psi.PsiField; 10 | import com.intellij.psi.PsiModifier; 11 | import com.intellij.ui.CollectionListModel; 12 | import com.intellij.ui.ToolbarDecorator; 13 | import com.intellij.ui.components.JBCheckBox; 14 | import com.intellij.ui.components.JBLabel; 15 | import com.intellij.ui.components.JBList; 16 | import com.intellij.ui.components.JBTextField; 17 | import com.intellij.ui.components.panels.HorizontalBox; 18 | import com.intellij.ui.components.panels.VerticalBox; 19 | import org.jetbrains.annotations.Nullable; 20 | 21 | import javax.swing.*; 22 | import java.awt.event.ActionEvent; 23 | import java.awt.event.ActionListener; 24 | import java.util.List; 25 | 26 | /** 27 | * Created by ghost on 2016/4/1. 28 | */ 29 | public class ConfigDialog extends DialogWrapper { 30 | private JBCheckBox controllerCheckBox; 31 | private JBCheckBox serviceCheckBox; 32 | private JBCheckBox viewCheckBox; 33 | 34 | private JBCheckBox viewAddCheckBox; 35 | private JBCheckBox viewDetailCheckBox; 36 | private JBCheckBox viewEditCheckBox; 37 | private JBCheckBox viewIndexCheckBox; 38 | 39 | private JBCheckBox forceCheckBox; 40 | 41 | private JBLabel baseUriLabel; 42 | private JBLabel basePackageLabel; 43 | private JBTextField baseUriTextField; 44 | private JBTextField basePackageTextField; 45 | 46 | 47 | private JBLabel modPackageLabel; 48 | private JBLabel serPackageLabel; 49 | private JBLabel ctrPackageLabel; 50 | private JBTextField modPackageTextField; 51 | private JBTextField serPackageTextField; 52 | private JBTextField ctrPackageTextField; 53 | 54 | private final PsiClass mClass; 55 | private final String basePackage; 56 | private final String baseUri; 57 | private final String modelPackageName; 58 | 59 | public ConfigDialog(final PsiClass psiClass) { 60 | super(psiClass.getProject()); 61 | String arr[] = psiClass.getQualifiedName().split("\\."); 62 | String modelName = psiClass.getName(); 63 | basePackage = psiClass.getQualifiedName().replace("." + arr[arr.length - 3] +"." + arr[arr.length - 2] + "." + arr[arr.length - 1], ""); 64 | modelPackageName = arr[arr.length - 2]; 65 | baseUri = "/platform/" + arr[arr.length - 2]; 66 | mClass = psiClass; 67 | setupViews(modelName); 68 | init(); 69 | 70 | 71 | } 72 | 73 | 74 | private void setupViews(String modelName) { 75 | 76 | setTitle("Generate Model:" + modelName); 77 | 78 | controllerCheckBox = new JBCheckBox("controllers", true); 79 | serviceCheckBox = new JBCheckBox("services", true); 80 | viewCheckBox = new JBCheckBox("views", true); 81 | 82 | viewAddCheckBox = new JBCheckBox("add", true); 83 | viewDetailCheckBox = new JBCheckBox("detail", true); 84 | viewEditCheckBox = new JBCheckBox("edit", true); 85 | viewIndexCheckBox = new JBCheckBox("index", true); 86 | 87 | forceCheckBox = new JBCheckBox("replace", false); 88 | 89 | baseUriTextField = new JBTextField(baseUri); 90 | basePackageTextField = new JBTextField(basePackage); 91 | baseUriLabel = new JBLabel("baseUri:"); 92 | basePackageLabel = new JBLabel("base Package:"); 93 | 94 | modPackageLabel = new JBLabel("models Package:"); 95 | serPackageLabel = new JBLabel("services Package:"); 96 | ctrPackageLabel = new JBLabel("controllers Package:"); 97 | modPackageTextField = new JBTextField("models."+modelPackageName); 98 | modPackageTextField.disable(); 99 | serPackageTextField = new JBTextField("services."+modelPackageName); 100 | ctrPackageTextField = new JBTextField("controllers.platform."+modelPackageName); 101 | 102 | viewCheckBox.addActionListener(new ActionListener() { 103 | @Override 104 | public void actionPerformed(ActionEvent actionEvent) { 105 | if (viewCheckBox.isSelected()) { 106 | 107 | viewAddCheckBox.setSelected(true); 108 | viewDetailCheckBox.setSelected(true); 109 | viewEditCheckBox.setSelected(true); 110 | viewIndexCheckBox.setSelected(true); 111 | 112 | viewAddCheckBox.setVisible(true); 113 | viewDetailCheckBox.setVisible(true); 114 | viewEditCheckBox.setVisible(true); 115 | viewIndexCheckBox.setVisible(true); 116 | } else { 117 | viewAddCheckBox.setSelected(false); 118 | viewDetailCheckBox.setSelected(false); 119 | viewEditCheckBox.setSelected(false); 120 | viewIndexCheckBox.setSelected(false); 121 | 122 | viewAddCheckBox.setVisible(false); 123 | viewDetailCheckBox.setVisible(false); 124 | viewEditCheckBox.setVisible(false); 125 | viewIndexCheckBox.setVisible(false); 126 | } 127 | } 128 | }); 129 | } 130 | 131 | @Nullable 132 | @Override 133 | protected JComponent createSouthPanel() { 134 | JComponent southPanel = super.createSouthPanel(); 135 | if (null == southPanel) { 136 | return null; 137 | } 138 | final VerticalBox root = new VerticalBox(); 139 | root.add(baseUriLabel); 140 | root.add(baseUriTextField); 141 | root.add(basePackageLabel); 142 | root.add(basePackageTextField); 143 | 144 | root.add(modPackageLabel); 145 | root.add(modPackageTextField); 146 | root.add(serPackageLabel); 147 | root.add(serPackageTextField); 148 | root.add(ctrPackageLabel); 149 | root.add(ctrPackageTextField); 150 | 151 | root.add(forceCheckBox); 152 | root.add(southPanel); 153 | return root; 154 | } 155 | 156 | @Nullable 157 | @Override 158 | protected JComponent createCenterPanel() { 159 | JComponent centerPanel = super.createContentPane(); 160 | final VerticalBox verticalBox = new VerticalBox(); 161 | final HorizontalBox horizontalBox1 = new HorizontalBox(); 162 | 163 | final HorizontalBox horizontalBox2 = new HorizontalBox(); 164 | 165 | horizontalBox1.add(controllerCheckBox); 166 | horizontalBox1.add(serviceCheckBox); 167 | horizontalBox1.add(viewCheckBox); 168 | 169 | horizontalBox2.add(viewAddCheckBox); 170 | horizontalBox2.add(viewDetailCheckBox); 171 | horizontalBox2.add(viewEditCheckBox); 172 | horizontalBox2.add(viewIndexCheckBox); 173 | verticalBox.add(horizontalBox1); 174 | verticalBox.add(horizontalBox2); 175 | centerPanel.add(verticalBox); 176 | return centerPanel; 177 | } 178 | 179 | public GenerateConfig getGenerateConfig() { 180 | GenerateConfig config = new GenerateConfig(); 181 | config.setBasePackage(basePackage); 182 | config.setBaseUri(baseUriTextField.getText().trim()); 183 | config.setConroller(controllerCheckBox.isSelected()); 184 | config.setService(serviceCheckBox.isSelected()); 185 | config.setView(viewCheckBox.isSelected()); 186 | config.setForce(forceCheckBox.isSelected()); 187 | config.setModelPakName(modPackageTextField.getText().trim()); 188 | config.setServicePakName(serPackageTextField.getText().trim()); 189 | config.setControllerPakName(ctrPackageTextField.getText().trim()); 190 | StringBuilder pages = new StringBuilder(); 191 | pages.append(viewIndexCheckBox.isSelected() ? "index_" : "").append(viewAddCheckBox.isSelected() ? "add_" : "") 192 | .append(viewDetailCheckBox.isSelected() ? "detail_" : "").append(viewEditCheckBox.isSelected() ? "edit_" : ""); 193 | config.setPages(pages.toString()); 194 | return config; 195 | } 196 | 197 | } 198 | --------------------------------------------------------------------------------