1212import com .intellij .openapi .application .ApplicationManager ;
1313import com .intellij .openapi .command .CommandProcessor ;
1414import com .intellij .openapi .project .Project ;
15- import com .intellij .openapi .ui .Messages ;
1615import com .intellij .openapi .util .Ref ;
1716import com .intellij .openapi .util .text .StringUtil ;
1817import com .intellij .psi .PsiDirectory ;
3130import org .jetbrains .annotations .Nullable ;
3231
3332public class FileFromTemplateGenerator {
34- private final Project project ;
3533
36- public FileFromTemplateGenerator (final Project project ) {
34+ private final @ NotNull Project project ;
35+ private @ Nullable String exceptionMessage ;
36+
37+ public FileFromTemplateGenerator (final @ NotNull Project project ) {
3738 this .project = project ;
3839 }
3940
@@ -44,27 +45,30 @@ public FileFromTemplateGenerator(final Project project) {
4445 * @param attributes Properties
4546 * @param baseDir PsiDirectory
4647 * @param actionName String
48+ *
4749 * @return PsiFile
4850 */
49- @ Nullable
50- public PsiFile generate (
51+ public @ Nullable PsiFile generate (
5152 final @ NotNull ModuleFileInterface moduleFile ,
5253 final @ NotNull Properties attributes ,
5354 final @ NotNull PsiDirectory baseDir ,
5455 final @ NotNull String actionName
5556 ) {
5657 final Ref <PsiFile > fileRef = new Ref <>(null );
5758 final Ref <String > exceptionRef = new Ref <>(null );
59+ exceptionMessage = null ;//NOPMD
5860 final String filePath = baseDir .getText ().concat ("/" ).concat (moduleFile .getFileName ());
61+
5962 CommandProcessor .getInstance ().executeCommand (project , () -> {
6063 final Runnable run = () -> {
6164 try {
62- PsiFile file = createFile (moduleFile , filePath , baseDir , attributes );
65+ final PsiFile file = createFile (moduleFile , filePath , baseDir , attributes );
66+
6367 if (file != null ) {
6468 fileRef .set (file );
6569 }
66- } catch (IncorrectOperationException | IOException var9 ) {
67- exceptionRef .set (var9 .getMessage ());
70+ } catch (IncorrectOperationException | IOException exception ) {
71+ exceptionRef .set (exception .getMessage ());
6872 }
6973 };
7074 ApplicationManager .getApplication ().runWriteAction (run );
@@ -73,11 +77,20 @@ public PsiFile generate(
7377 if (exceptionRef .isNull ()) {
7478 return fileRef .get ();
7579 }
80+ exceptionMessage = exceptionRef .get ();
7681
77- Messages .showErrorDialog (exceptionRef .get (), actionName );
7882 return null ;
7983 }
8084
85+ /**
86+ * Get last thrown exception message if exists.
87+ *
88+ * @return String
89+ */
90+ public @ Nullable String getLastExceptionMessage () {
91+ return exceptionMessage ;
92+ }
93+
8194 @ Nullable
8295 private PsiFile createFile (
8396 final @ NotNull ModuleFileInterface moduleFile ,
@@ -89,13 +102,19 @@ private PsiFile createFile(
89102 final String fileName = path .get (path .size () - 1 );
90103 final PsiFile fileTemplate = createFileFromTemplate (
91104 getTemplateManager (),
92- baseDir , moduleFile .getTemplate (), attributes , fileName , moduleFile .getLanguage ());
105+ baseDir ,
106+ moduleFile .getTemplate (),
107+ attributes ,
108+ fileName ,
109+ moduleFile .getLanguage ()
110+ );
111+
93112 if (fileTemplate == null ) {
94113 throw new IncorrectOperationException ("Template not found!" );
95114 } else {
96115 PsiElement file ;
97-
98116 file = baseDir .add (fileTemplate );
117+
99118 if (file instanceof PsiFile ) {
100119 return (PsiFile )file ;
101120 } else {
@@ -125,6 +144,7 @@ public PsiFile createFileFromTemplate(
125144 final @ NotNull Language language
126145 ) throws IOException {
127146 FileTemplate fileTemplate ;
147+
128148 try {
129149 fileTemplate = templateManager .getInternalTemplate (templateName );
130150 } catch (IllegalStateException e ) {
@@ -140,6 +160,7 @@ public PsiFile createFileFromTemplate(
140160 true ,
141161 false
142162 );
163+
143164 if (fileTemplate .isReformatCode ()) {
144165 CodeStyleManager .getInstance (project ).reformat (file );
145166 }
0 commit comments