11package com .mighty16 .json ;
22
3- import com .intellij .notification .*;
43import com .intellij .openapi .actionSystem .*;
5- import com .intellij .openapi .application .ApplicationManager ;
64import com .intellij .openapi .module .Module ;
75import com .intellij .openapi .project .Project ;
8- import com .intellij .openapi .project .ProjectManager ;
96import 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 ;
128import com .intellij .openapi .vfs .VirtualFile ;
13- import com .intellij .openapi .wm .StatusBar ;
149import com .intellij .pom .Navigatable ;
1510import com .intellij .psi .*;
11+ import com .intellij .psi .impl .file .PsiDirectoryFactory ;
12+ import com .intellij .util .ui .UIUtil ;
1613import com .mighty16 .json .annotations .*;
14+ import com .mighty16 .json .core .AnnotationGenerator ;
15+ import com .mighty16 .json .core .FileSaver ;
1716import com .mighty16 .json .generator .SingleFileGenerator ;
18- import com .mighty16 .json .generator .SourceFilesGenerator ;
17+ import com .mighty16 .json .core .SourceFilesGenerator ;
1918import com .mighty16 .json .generator .MultipleFilesGenerator ;
19+ import com .mighty16 .json .resolver .KotlinFileType ;
2020import 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 ;
2323import com .mighty16 .json .ui .JSONEditDialog ;
2424import com .mighty16 .json .ui .ModelTableDialog ;
25+ import com .mighty16 .json .ui .NotificationsHelper ;
26+ import com .mighty16 .json .ui .TextResources ;
2527
2628import java .awt .*;
2729import java .awt .event .ComponentAdapter ;
@@ -33,19 +35,20 @@ public class ClassFromJSONAction extends AnAction implements JSONEditDialog.JSON
3335 private PsiDirectory directory ;
3436 private Point lastDialogLocation ;
3537 private LanguageResolver languageResolver ;
36-
37- private DataContext dataContext ;
38+ private TextResources textResources ;
3839
3940 public ClassFromJSONAction () {
40- super ("JSON Kotlin Models" );
41+ super ();
4142 }
4243
4344 @ Override
4445 public void actionPerformed (AnActionEvent event ) {
4546 languageResolver = new KotlinResolver ();
47+ textResources = new TextResources ();
48+
4649 Project project = event .getProject ();
4750 if (project == null ) return ;
48- dataContext = event .getDataContext ();
51+ DataContext dataContext = event .getDataContext ();
4952 final Module module = DataKeys .MODULE .getData (dataContext );
5053 if (module == null ) return ;
5154 final Navigatable navigatable = DataKeys .NAVIGATABLE .getData (dataContext );
@@ -63,7 +66,7 @@ public void actionPerformed(AnActionEvent event) {
6366 }
6467 }
6568
66- JSONEditDialog dialog = new JSONEditDialog (this );
69+ JSONEditDialog dialog = new JSONEditDialog (this , textResources );
6770 dialog .addComponentListener (new ComponentAdapter () {
6871 public void componentMoved (ComponentEvent e ) {
6972 lastDialogLocation = dialog .getLocation ();
@@ -76,7 +79,7 @@ public void componentMoved(ComponentEvent e) {
7679
7780 @ Override
7881 public void onJsonParsed (List <ClassModel > classDataList ) {
79- ModelTableDialog tableDialog = new ModelTableDialog (classDataList , languageResolver , this );
82+ ModelTableDialog tableDialog = new ModelTableDialog (classDataList , languageResolver , textResources , this );
8083 if (lastDialogLocation != null ) {
8184 tableDialog .setLocation (lastDialogLocation );
8285 }
@@ -107,33 +110,34 @@ public void onModelsReady(List<ClassModel> data, String singleFileName, int anno
107110 annotations = new JacksonAnnotations ();
108111 break ;
109112 }
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+
110129 SourceFilesGenerator generator ;
111130 if (singleFileName == null ) {
112- generator = new MultipleFilesGenerator (languageResolver , annotations );
131+ generator = new MultipleFilesGenerator (fileSaver , languageResolver , annotations );
113132 } else {
114- generator = new SingleFileGenerator (languageResolver , annotations , singleFileName );
133+ generator = new SingleFileGenerator (singleFileName , languageResolver , annotations , fileSaver );
115134 }
116135
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+ );
136140
137- generator .generateFiles (directory , data );
141+ generator .generateFiles (packageName , data );
138142 }
139143}
0 commit comments