@@ -8,17 +8,14 @@ import com.intellij.openapi.actionSystem.*
88import com.intellij.openapi.application.WriteActionAware
99import com.intellij.openapi.fileEditor.FileEditorManager
1010import com.intellij.openapi.fileEditor.TextEditor
11+ import com.intellij.openapi.module.ModuleUtil
1112import com.intellij.openapi.project.Project
12- import com.intellij.psi.PsiClass
13- import com.intellij.psi.PsiDirectory
14- import com.intellij.psi.PsiFile
15- import com.intellij.psi.PsiJavaFile
13+ import com.intellij.psi.*
1614import com.intellij.util.PlatformIcons
17- import com.longforus.mvpautocodeplus.maker.TemplateMaker
18- import com.longforus.mvpautocodeplus.maker.TemplateParamFactory
19- import com.longforus.mvpautocodeplus.maker.createFileFromTemplate
20- import com.longforus.mvpautocodeplus.maker.overrideOrImplementMethods
15+ import com.longforus.mvpautocodeplus.config.ItemConfigBean
16+ import com.longforus.mvpautocodeplus.maker.*
2117import com.longforus.mvpautocodeplus.ui.EnterKeywordDialog
18+ import org.jetbrains.android.facet.AndroidFacet
2219import org.jetbrains.kotlin.psi.KtFile
2320import com.intellij.openapi.application.runWriteAction as runWriteAction1
2421
@@ -31,30 +28,31 @@ import com.intellij.openapi.application.runWriteAction as runWriteAction1
3128class MainAction : AnAction (" Generate MVP Code" , " auto make mvp code" , PlatformIcons .CLASS_ICON ), WriteActionAware {
3229 var project: Project ? = null
3330 lateinit var mSelectedState: PropertiesComponent
34- fun createFile (enterName : String , templateName : String , dir : PsiDirectory , superImplName : String , contract : PsiFile ? = null, fileName : String = enterName):
35- PsiFile ? {
36- val template = TemplateMaker .getTemplate(templateName, project!! ) ? : return null
31+ fun createFile (enterName : String , templateName : String , dir : PsiDirectory , superImplName : String , contract : PsiFile ? = null, fileName : String = enterName): Pair <PsiFile ?,
32+ PsiClass ?> {
33+ var clazz: PsiClass ? = null
34+ val template = TemplateMaker .getTemplate(templateName, project!! ) ? : return null to null
3735 val liveTemplateDefaultValues = TemplateParamFactory .getParam4TemplateName(templateName, enterName, superImplName, contract, mSelectedState)
3836 val psiFile = createFileFromTemplate(fileName, template, dir, null , false , liveTemplateDefaultValues, mSelectedState.getValue(COMMENT_AUTHOR ))
3937 if (! templateName.contains(" Contract" )) {
4038 val openFile = FileEditorManager .getInstance(project!! ).openFile(psiFile!! .virtualFile, false )
4139 val textEditor = openFile[0 ] as TextEditor
42- var clazz : PsiClass ? = null
40+
4341 if (psiFile is PsiJavaFile ) {
4442 if (psiFile.classes.isEmpty()) {
45- return psiFile
43+ return psiFile to null
4644 }
4745 clazz = psiFile.classes[0 ]
4846 } else if (psiFile is KtFile ) {
4947 if (psiFile.classes.isEmpty()) {
50- return psiFile
48+ return psiFile to null
5149 }
5250 clazz = psiFile.classes[0 ]
5351 }
5452 FeatureUsageTracker .getInstance().triggerFeatureUsed(ProductivityFeatureNames .CODEASSISTS_OVERRIDE_IMPLEMENT )
5553 overrideOrImplementMethods(project!! , textEditor.editor, clazz!! , true )
5654 }
57- return psiFile
55+ return psiFile to clazz
5856 }
5957
6058 override fun actionPerformed (e : AnActionEvent ) {
@@ -72,57 +70,74 @@ class MainAction : AnAction("Generate MVP Code", "auto make mvp code", PlatformI
7270// Messages.showErrorDialog("Super IView Interface name is null ! $GOTO_SETTING", "Error")
7371// return
7472// }
73+
74+ // NewAndroidComponentDialog
75+
7576 EnterKeywordDialog .getDialog(project) {
7677 mSelectedState = it.state
78+ val module = ModuleUtil .findModuleForFile(dir.virtualFile, project!! )
79+ val facet = AndroidFacet .getInstance(module!! )
7780 runWriteAction1 {
7881 if (it.isJava) {
79- val contractJ = createFile(it.name, if (it.generateModel) CONTRACT_TP_NAME_JAVA else CONTRACT_TP_NO_MODEL_NAME_JAVA , getSubDir(dir, CONTRACT ),
80- " " )
81- if (! it.vImpl.isEmpty() && ! it.vImpl.startsWith(IS_NOT_SET )) {
82- val sdV = getSubDir(dir, VIEW )
83- if (it.isActivity) {
84- createFile(it.name, VIEW_IMPL_TP_ACTIVITY_JAVA , sdV, it.vImpl, contractJ)
85- } else {
86- createFile(it.name, VIEW_IMPL_TP_FRAGMENT_JAVA , sdV, it.vImpl, contractJ)
87- }
88- }
89- if (! it.pImpl.isEmpty()) {
90- val sdP = getSubDir(dir, PRESENTER )
91- createFile(it.name, PRESENTER_IMPL_TP_JAVA , sdP, it.pImpl, contractJ)
92- }
93- if (! it.mImpl.isEmpty() && it.generateModel) {
94- val sdM = getSubDir(dir, MODEL )
95- createFile(it.name, MODEL_IMPL_TP_JAVA , sdM, it.mImpl, contractJ)
96- }
97-
82+ doJavaCreate(it, dir, facet)
9883 } else {
99- val contractK = createFile(it.name, if (it.generateModel) CONTRACT_TP_NAME_KOTLIN else CONTRACT_TP_NO_MODEL_NAME_KOTLIN , getSubDir(dir, CONTRACT ), " " ,
100- fileName = getContractName(it
101- .name))
102-
103- if (! it.vImpl.isEmpty() && ! it.vImpl.startsWith(IS_NOT_SET )) {
104- val sdV = getSubDir(dir, VIEW )
105- if (it.isActivity) {
106- createFile(it.name, VIEW_IMPL_TP_ACTIVITY_KOTLIN , sdV, it.vImpl, contractK, " ${it.name} Activity" )
107- } else {
108- createFile(it.name, VIEW_IMPL_TP_FRAGMENT_KOTLIN , sdV, it.vImpl, contractK, " ${it.name} Fragment" )
109- }
110- }
111- if (! it.pImpl.isEmpty()) {
112- val sdP = getSubDir(dir, PRESENTER )
113- createFile(it.name, PRESENTER_IMPL_TP_KOTLIN , sdP, it.pImpl, contractK, " ${it.name}${TemplateParamFactory .getPresenterOrViewModel(it.pImpl)} " )
114- }
115- if (! it.mImpl.isEmpty() && it.generateModel) {
116- val sdM = getSubDir(dir, MODEL )
117- createFile(it.name, MODEL_IMPL_TP_KOTLIN , sdM, it.mImpl, contractK, " ${it.name} Model" )
118- }
84+ doKtCreate(it, dir, facet)
11985 }
12086 }
87+ }
88+ }
12189
90+ private fun doKtCreate (it : ItemConfigBean , dir : PsiDirectory , facet : AndroidFacet ? ) {
91+ val contractK = createFile(it.name, if (it.generateModel) CONTRACT_TP_NAME_KOTLIN else CONTRACT_TP_NO_MODEL_NAME_KOTLIN , getSubDir(dir, CONTRACT ), " " ,
92+ fileName = getContractName(it
93+ .name))
94+
95+ if (it.vImpl.isNotEmpty() && ! it.vImpl.startsWith(IS_NOT_SET )) {
96+ val sdV = getSubDir(dir, VIEW )
97+ if (it.isActivity) {
98+ val activityKt = createFile(it.name, VIEW_IMPL_TP_ACTIVITY_KOTLIN , sdV, it.vImpl, contractK.first, " ${it.name} Activity" )
99+ CompleteRegister .registerActivity(activityKt.second, JavaDirectoryService .getInstance().getPackage(dir), facet!! , " " )
100+ doCreateLayoutFile(activityKt.second, dir, facet, false )
101+ } else {
102+ val fragmentKt = createFile(it.name, VIEW_IMPL_TP_FRAGMENT_KOTLIN , sdV, it.vImpl, contractK.first, " ${it.name} Fragment" )
103+ doCreateLayoutFile(fragmentKt.second, dir, facet!! , false )
104+ }
105+ }
106+ if (it.pImpl.isNotEmpty()) {
107+ val sdP = getSubDir(dir, PRESENTER )
108+ createFile(it.name, PRESENTER_IMPL_TP_KOTLIN , sdP, it.pImpl, contractK.first, " ${it.name}${TemplateParamFactory .getPresenterOrViewModel(it.pImpl)} " )
122109 }
110+ if (it.mImpl.isNotEmpty() && it.generateModel) {
111+ val sdM = getSubDir(dir, MODEL )
112+ createFile(it.name, MODEL_IMPL_TP_KOTLIN , sdM, it.mImpl, contractK.first, " ${it.name} Model" )
113+ }
114+ }
123115
116+ private fun doJavaCreate (it : ItemConfigBean , dir : PsiDirectory , facet : AndroidFacet ? ) {
117+ val contractJ = createFile(it.name, if (it.generateModel) CONTRACT_TP_NAME_JAVA else CONTRACT_TP_NO_MODEL_NAME_JAVA , getSubDir(dir, CONTRACT ),
118+ " " )
119+ if (it.vImpl.isNotEmpty() && ! it.vImpl.startsWith(IS_NOT_SET )) {
120+ val sdV = getSubDir(dir, VIEW )
121+ if (it.isActivity) {
122+ val activityJava = createFile(it.name, VIEW_IMPL_TP_ACTIVITY_JAVA , sdV, it.vImpl, contractJ.first)
123+ CompleteRegister .registerActivity(activityJava.second, JavaDirectoryService .getInstance().getPackage(dir), facet!! , " " )
124+ doCreateLayoutFile(activityJava.second, dir, facet, true )
125+ } else {
126+ val fragmentJava = createFile(it.name, VIEW_IMPL_TP_FRAGMENT_JAVA , sdV, it.vImpl, contractJ.first)
127+ doCreateLayoutFile(fragmentJava.second, dir, facet!! , true )
128+ }
129+ }
130+ if (it.pImpl.isNotEmpty()) {
131+ val sdP = getSubDir(dir, PRESENTER )
132+ createFile(it.name, PRESENTER_IMPL_TP_JAVA , sdP, it.pImpl, contractJ.first)
133+ }
134+ if (it.mImpl.isNotEmpty() && it.generateModel) {
135+ val sdM = getSubDir(dir, MODEL )
136+ createFile(it.name, MODEL_IMPL_TP_JAVA , sdM, it.mImpl, contractJ.first)
137+ }
124138 }
125139
140+
126141 fun getSubDir (dir : PsiDirectory , dirName : String ): PsiDirectory {
127142 return if (dir.name == CONTRACT ) {
128143 if (dirName == CONTRACT ) {
0 commit comments