1
1
package com .mighty16 .json ;
2
2
3
- import com .intellij .notification .*;
4
3
import com .intellij .openapi .actionSystem .*;
5
- import com .intellij .openapi .application .ApplicationManager ;
6
4
import com .intellij .openapi .module .Module ;
7
5
import com .intellij .openapi .project .Project ;
8
- import com .intellij .openapi .project .ProjectManager ;
9
6
import com .intellij .openapi .roots .ModuleRootManager ;
10
- import com .intellij .openapi .ui .popup .Balloon ;
11
- import com .intellij .openapi .ui .popup .JBPopupFactory ;
7
+ import com .intellij .openapi .ui .Messages ;
12
8
import com .intellij .openapi .vfs .VirtualFile ;
13
- import com .intellij .openapi .wm .StatusBar ;
14
9
import com .intellij .pom .Navigatable ;
15
10
import com .intellij .psi .*;
11
+ import com .intellij .psi .impl .file .PsiDirectoryFactory ;
12
+ import com .intellij .util .ui .UIUtil ;
16
13
import com .mighty16 .json .annotations .*;
14
+ import com .mighty16 .json .core .AnnotationGenerator ;
15
+ import com .mighty16 .json .core .FileSaver ;
17
16
import com .mighty16 .json .generator .SingleFileGenerator ;
18
- import com .mighty16 .json .generator .SourceFilesGenerator ;
17
+ import com .mighty16 .json .core .SourceFilesGenerator ;
19
18
import com .mighty16 .json .generator .MultipleFilesGenerator ;
19
+ import com .mighty16 .json .resolver .KotlinFileType ;
20
20
import com .mighty16 .json .resolver .KotlinResolver ;
21
- import com .mighty16 .json .models .ClassModel ;
22
- import com .mighty16 .json .resolver .LanguageResolver ;
21
+ import com .mighty16 .json .core . models .ClassModel ;
22
+ import com .mighty16 .json .core .LanguageResolver ;
23
23
import com .mighty16 .json .ui .JSONEditDialog ;
24
24
import com .mighty16 .json .ui .ModelTableDialog ;
25
+ import com .mighty16 .json .ui .NotificationsHelper ;
26
+ import com .mighty16 .json .ui .TextResources ;
25
27
26
28
import java .awt .*;
27
29
import java .awt .event .ComponentAdapter ;
@@ -33,19 +35,20 @@ public class ClassFromJSONAction extends AnAction implements JSONEditDialog.JSON
33
35
private PsiDirectory directory ;
34
36
private Point lastDialogLocation ;
35
37
private LanguageResolver languageResolver ;
36
-
37
- private DataContext dataContext ;
38
+ private TextResources textResources ;
38
39
39
40
public ClassFromJSONAction () {
40
- super ("JSON Kotlin Models" );
41
+ super ();
41
42
}
42
43
43
44
@ Override
44
45
public void actionPerformed (AnActionEvent event ) {
45
46
languageResolver = new KotlinResolver ();
47
+ textResources = new TextResources ();
48
+
46
49
Project project = event .getProject ();
47
50
if (project == null ) return ;
48
- dataContext = event .getDataContext ();
51
+ DataContext dataContext = event .getDataContext ();
49
52
final Module module = DataKeys .MODULE .getData (dataContext );
50
53
if (module == null ) return ;
51
54
final Navigatable navigatable = DataKeys .NAVIGATABLE .getData (dataContext );
@@ -63,7 +66,7 @@ public void actionPerformed(AnActionEvent event) {
63
66
}
64
67
}
65
68
66
- JSONEditDialog dialog = new JSONEditDialog (this );
69
+ JSONEditDialog dialog = new JSONEditDialog (this , textResources );
67
70
dialog .addComponentListener (new ComponentAdapter () {
68
71
public void componentMoved (ComponentEvent e ) {
69
72
lastDialogLocation = dialog .getLocation ();
@@ -76,7 +79,7 @@ public void componentMoved(ComponentEvent e) {
76
79
77
80
@ Override
78
81
public void onJsonParsed (List <ClassModel > classDataList ) {
79
- ModelTableDialog tableDialog = new ModelTableDialog (classDataList , languageResolver , this );
82
+ ModelTableDialog tableDialog = new ModelTableDialog (classDataList , languageResolver , textResources , this );
80
83
if (lastDialogLocation != null ) {
81
84
tableDialog .setLocation (lastDialogLocation );
82
85
}
@@ -107,33 +110,34 @@ public void onModelsReady(List<ClassModel> data, String singleFileName, int anno
107
110
annotations = new JacksonAnnotations ();
108
111
break ;
109
112
}
113
+
114
+ Project project = directory .getProject ();
115
+ PsiFileFactory factory = PsiFileFactory .getInstance (project );
116
+ PsiDirectoryFactory directoryFactory = PsiDirectoryFactory .getInstance (directory .getProject ());
117
+ String packageName = directoryFactory .getQualifiedName (directory , true );
118
+
119
+ FileSaver fileSaver = new IDEFileSaver (factory , directory , KotlinFileType .INSTANCE );
120
+
121
+ fileSaver .setListener (fileName -> {
122
+ int ok = Messages .showOkCancelDialog (
123
+ textResources .getReplaceDialogMessage (fileName ),
124
+ textResources .getReplaceDialogTitle (),
125
+ UIUtil .getQuestionIcon ());
126
+ return ok == 0 ;
127
+ });
128
+
110
129
SourceFilesGenerator generator ;
111
130
if (singleFileName == null ) {
112
- generator = new MultipleFilesGenerator (languageResolver , annotations );
131
+ generator = new MultipleFilesGenerator (fileSaver , languageResolver , annotations );
113
132
} else {
114
- generator = new SingleFileGenerator (languageResolver , annotations , singleFileName );
133
+ generator = new SingleFileGenerator (singleFileName , languageResolver , annotations , fileSaver );
115
134
}
116
135
117
- generator .setListener (new SourceFilesGenerator .Listener () {
118
- @ Override
119
- public void onFilesGenerated (int filesCount ) {
120
-
121
- String message = filesCount + " data " + ((filesCount ==1 )?"class" :"classes" )+ " generated from JSON" ;
122
-
123
- final NotificationGroup GROUP_DISPLAY_ID_INFO =
124
- new NotificationGroup ("JSON to data class" ,
125
- NotificationDisplayType .BALLOON , true );
126
-
127
- ApplicationManager .getApplication ().invokeLater (new Runnable () {
128
- @ Override
129
- public void run () {
130
- Notification notification = GROUP_DISPLAY_ID_INFO .createNotification (message , NotificationType .INFORMATION );
131
- Notifications .Bus .notify (notification , directory .getProject ());
132
- }
133
- });
134
- }
135
- });
136
+ generator .setListener (filesCount ->
137
+ NotificationsHelper .showNotification (directory .getProject (),
138
+ textResources .getGeneratedFilesMessage (filesCount ))
139
+ );
136
140
137
- generator .generateFiles (directory , data );
141
+ generator .generateFiles (packageName , data );
138
142
}
139
143
}
0 commit comments