12
12
import com .intellij .openapi .application .ApplicationManager ;
13
13
import com .intellij .openapi .command .CommandProcessor ;
14
14
import com .intellij .openapi .project .Project ;
15
- import com .intellij .openapi .ui .Messages ;
16
15
import com .intellij .openapi .util .Ref ;
17
16
import com .intellij .openapi .util .text .StringUtil ;
18
17
import com .intellij .psi .PsiDirectory ;
31
30
import org .jetbrains .annotations .Nullable ;
32
31
33
32
public class FileFromTemplateGenerator {
34
- private final Project project ;
35
33
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 ) {
37
38
this .project = project ;
38
39
}
39
40
@@ -44,27 +45,30 @@ public FileFromTemplateGenerator(final Project project) {
44
45
* @param attributes Properties
45
46
* @param baseDir PsiDirectory
46
47
* @param actionName String
48
+ *
47
49
* @return PsiFile
48
50
*/
49
- @ Nullable
50
- public PsiFile generate (
51
+ public @ Nullable PsiFile generate (
51
52
final @ NotNull ModuleFileInterface moduleFile ,
52
53
final @ NotNull Properties attributes ,
53
54
final @ NotNull PsiDirectory baseDir ,
54
55
final @ NotNull String actionName
55
56
) {
56
57
final Ref <PsiFile > fileRef = new Ref <>(null );
57
58
final Ref <String > exceptionRef = new Ref <>(null );
59
+ exceptionMessage = null ;//NOPMD
58
60
final String filePath = baseDir .getText ().concat ("/" ).concat (moduleFile .getFileName ());
61
+
59
62
CommandProcessor .getInstance ().executeCommand (project , () -> {
60
63
final Runnable run = () -> {
61
64
try {
62
- PsiFile file = createFile (moduleFile , filePath , baseDir , attributes );
65
+ final PsiFile file = createFile (moduleFile , filePath , baseDir , attributes );
66
+
63
67
if (file != null ) {
64
68
fileRef .set (file );
65
69
}
66
- } catch (IncorrectOperationException | IOException var9 ) {
67
- exceptionRef .set (var9 .getMessage ());
70
+ } catch (IncorrectOperationException | IOException exception ) {
71
+ exceptionRef .set (exception .getMessage ());
68
72
}
69
73
};
70
74
ApplicationManager .getApplication ().runWriteAction (run );
@@ -73,11 +77,20 @@ public PsiFile generate(
73
77
if (exceptionRef .isNull ()) {
74
78
return fileRef .get ();
75
79
}
80
+ exceptionMessage = exceptionRef .get ();
76
81
77
- Messages .showErrorDialog (exceptionRef .get (), actionName );
78
82
return null ;
79
83
}
80
84
85
+ /**
86
+ * Get last thrown exception message if exists.
87
+ *
88
+ * @return String
89
+ */
90
+ public @ Nullable String getLastExceptionMessage () {
91
+ return exceptionMessage ;
92
+ }
93
+
81
94
@ Nullable
82
95
private PsiFile createFile (
83
96
final @ NotNull ModuleFileInterface moduleFile ,
@@ -89,13 +102,19 @@ private PsiFile createFile(
89
102
final String fileName = path .get (path .size () - 1 );
90
103
final PsiFile fileTemplate = createFileFromTemplate (
91
104
getTemplateManager (),
92
- baseDir , moduleFile .getTemplate (), attributes , fileName , moduleFile .getLanguage ());
105
+ baseDir ,
106
+ moduleFile .getTemplate (),
107
+ attributes ,
108
+ fileName ,
109
+ moduleFile .getLanguage ()
110
+ );
111
+
93
112
if (fileTemplate == null ) {
94
113
throw new IncorrectOperationException ("Template not found!" );
95
114
} else {
96
115
PsiElement file ;
97
-
98
116
file = baseDir .add (fileTemplate );
117
+
99
118
if (file instanceof PsiFile ) {
100
119
return (PsiFile )file ;
101
120
} else {
@@ -125,6 +144,7 @@ public PsiFile createFileFromTemplate(
125
144
final @ NotNull Language language
126
145
) throws IOException {
127
146
FileTemplate fileTemplate ;
147
+
128
148
try {
129
149
fileTemplate = templateManager .getInternalTemplate (templateName );
130
150
} catch (IllegalStateException e ) {
@@ -140,6 +160,7 @@ public PsiFile createFileFromTemplate(
140
160
true ,
141
161
false
142
162
);
163
+
143
164
if (fileTemplate .isReformatCode ()) {
144
165
CodeStyleManager .getInstance (project ).reformat (file );
145
166
}
0 commit comments