55import com .intellij .openapi .fileChooser .FileChooserDescriptorFactory ;
66import com .intellij .openapi .module .Module ;
77import com .intellij .openapi .vfs .VirtualFile ;
8+ import com .intellij .openapi .vfs .VirtualFileManager ;
89import com .intellij .psi .PsiPackage ;
910import com .sjhy .plugin .entity .TableInfo ;
1011import com .sjhy .plugin .entity .Template ;
@@ -48,7 +49,7 @@ public class SelectSavePath extends JDialog {
4849 /**
4950 * 模型下拉框
5051 */
51- private JComboBox moduleComboBox ;
52+ private JComboBox < String > moduleComboBox ;
5253 /**
5354 * 包字段
5455 */
@@ -128,16 +129,18 @@ public void windowClosing(WindowEvent e) {
128129 * @return 模板对象集合
129130 */
130131 private List <Template > getSelectTemplate () {
132+ // 获取到已选择的复选框
131133 List <String > selectTemplateNameList = new ArrayList <>();
132134 checkBoxList .forEach (jCheckBox -> {
133135 if (jCheckBox .isSelected ()) {
134136 selectTemplateNameList .add (jCheckBox .getText ());
135137 }
136138 });
137- List <Template > selectTemplateList = new ArrayList <>();
139+ List <Template > selectTemplateList = new ArrayList <>(selectTemplateNameList . size () );
138140 if (selectTemplateNameList .isEmpty ()) {
139141 return selectTemplateList ;
140142 }
143+ // 将复选框转换成对应的模板对象
141144 templateGroup .getElementList ().forEach (template -> {
142145 if (selectTemplateNameList .contains (template .getName ())) {
143146 selectTemplateList .add (template );
@@ -151,12 +154,13 @@ private List<Template> getSelectTemplate() {
151154 */
152155 private void onOK () {
153156 List <Template > selectTemplateList = getSelectTemplate ();
157+ // 如果选择的模板是空的
154158 if (selectTemplateList .isEmpty ()) {
155159 JOptionPane .showMessageDialog (null , "Can't Select Template!" );
156160 return ;
157161 }
158162 String savePath = pathField .getText ();
159- if (savePath .isEmpty ()) {
163+ if (StringUtils .isEmpty (savePath )) {
160164 JOptionPane .showMessageDialog (null , "Can't Select Save Path!" );
161165 return ;
162166 }
@@ -168,6 +172,7 @@ private void onOK() {
168172 cacheDataUtils .setUnifiedConfig (unifiedConfig .isSelected ());
169173 // 生成代码
170174 VelocityUtils .getInstance ().handler ();
175+ // 关闭窗口
171176 dispose ();
172177 }
173178
@@ -195,10 +200,15 @@ private void init() {
195200
196201 //初始化Module选择
197202 for (Module module : cacheDataUtils .getModules ()) {
198- //noinspection unchecked
199203 moduleComboBox .addItem (module .getName ());
200204 }
201205
206+ //监听module选择事件
207+ moduleComboBox .addActionListener (e -> {
208+ // 刷新路径
209+ refreshPath ();
210+ });
211+
202212 //添加包选择事件
203213 packageChooseButton .addActionListener (e -> {
204214 PackageChooserDialog dialog = new PackageChooserDialog ("Package Chooser" , cacheDataUtils .getProject ());
@@ -219,8 +229,8 @@ private void init() {
219229 //将当前选中的model设置为基础路径
220230 VirtualFile path = cacheDataUtils .getProject ().getBaseDir ();
221231 Module module = getSelectModule ();
222- if (module !=null && module . getModuleFile ()!= null ) {
223- path = module .getModuleFile () .getParent ();
232+ if (module !=null ) {
233+ path = VirtualFileManager . getInstance (). findFileByUrl ( "file://" + new File ( module .getModuleFilePath ()) .getParent () );
224234 }
225235 VirtualFile virtualFile = FileChooser .chooseFile (FileChooserDescriptorFactory .createSingleFolderDescriptor (), cacheDataUtils .getProject (), path );
226236 if (virtualFile != null ) {
@@ -264,8 +274,8 @@ private Module getSelectModule() {
264274 private String getBasePath () {
265275 Module module = getSelectModule ();
266276 String baseDir = cacheDataUtils .getProject ().getBasePath ();
267- if (module !=null && module . getModuleFile ()!= null ) {
268- baseDir = module .getModuleFile (). getParent (). getPath ();
277+ if (module !=null ) {
278+ baseDir = new File ( module .getModuleFilePath ()). getParent ();
269279 }
270280 // 针对Maven项目
271281 File file = new File (baseDir + "/src/main/java" );
0 commit comments