11package wu.seal.jsontokotlin
22
3+ import com.google.gson.Gson
34import com.intellij.openapi.actionSystem.AnAction
45import com.intellij.openapi.actionSystem.AnActionEvent
56import com.intellij.openapi.actionSystem.PlatformDataKeys
67import com.intellij.openapi.application.ApplicationManager
78import com.intellij.openapi.command.CommandProcessor
89import com.intellij.openapi.project.Project
910import com.intellij.openapi.ui.Messages
11+ import wu.seal.jsontokotlin.statistics.StartAction
12+ import wu.seal.jsontokotlin.statistics.SuccessCompleteAction
13+ import wu.seal.jsontokotlin.statistics.handlerException
14+ import wu.seal.jsontokotlin.statistics.sendActionInfo
1015
1116import java.util.IllegalFormatFlagsException
1217
@@ -16,60 +21,72 @@ import java.util.IllegalFormatFlagsException
1621 */
1722class MakeKotlinClassAction : AnAction (" MakeKotlinClass" ) {
1823
19- override fun actionPerformed (event : AnActionEvent ) {
20- val project = event.getData(PlatformDataKeys .PROJECT )
21- val caret = event.getData(PlatformDataKeys .CARET )
22- val editor = event.getData(PlatformDataKeys .EDITOR_EVEN_IF_INACTIVE )
23- if (editor == null ) {
24- Messages .showWarningDialog(" Please open a file in editor state for insert Kotlin code!" , " No Editor File" )
25- return
26- }
27- val className = Messages .showInputDialog(project, " Please input the Class Name for Insert" , " Input ClassName" , Messages .getInformationIcon())
28- if (className == null || className.isEmpty()) {
29- return
30- }
31- val inputDialog = JsonInputDialog (project!! )
32- inputDialog.show()
33- val jsonString = inputDialog.inputString
34- if (jsonString == null || jsonString.isEmpty()) {
35- return
36- }
37- val document = editor.document
38- ImportClassWriter .insertImportClassCode(project, document)
24+ private val gson = Gson ()
3925
40- val maker: KotlinMaker
26+ override fun actionPerformed (event : AnActionEvent ) {
27+ var jsonString: String = " "
4128 try {
42- maker = KotlinMaker (className, jsonString)
43- } catch (e: IllegalFormatFlagsException ) {
44- e.printStackTrace()
45- Messages .showErrorDialog(e.message, " UnSupport Json" )
46- return
47- }
29+ sendActionInfo(gson.toJson(StartAction ()))
30+ val project = event.getData(PlatformDataKeys .PROJECT )
31+ val caret = event.getData(PlatformDataKeys .CARET )
32+ val editor = event.getData(PlatformDataKeys .EDITOR_EVEN_IF_INACTIVE )
33+ if (editor == null ) {
34+ Messages .showWarningDialog(" Please open a file in editor state for insert Kotlin code!" , " No Editor File" )
35+ return
36+ }
37+ val className = Messages .showInputDialog(project, " Please input the Class Name for Insert" , " Input ClassName" , Messages .getInformationIcon())
38+ if (className == null || className.isEmpty()) {
39+ return
40+ }
41+ val inputDialog = JsonInputDialog (project!! )
42+ inputDialog.show()
43+ val json = inputDialog.inputString
44+ if (json == null || json.isEmpty()) {
45+ return
46+ }
47+ jsonString = json
48+ val document = editor.document
49+ ImportClassWriter .insertImportClassCode(project, document)
50+
51+ val maker: KotlinMaker
52+ try {
53+ maker = KotlinMaker (className, jsonString)
54+ } catch (e: IllegalFormatFlagsException ) {
55+ e.printStackTrace()
56+ Messages .showErrorDialog(e.message, " UnSupport Json" )
57+ return
58+ }
4859
49- executeCouldRollBackAction(project){
50- var offset = 0
60+ executeCouldRollBackAction(project) {
61+ var offset = 0
5162
52- if (caret != null ) {
63+ if (caret != null ) {
5364
54- offset = caret.offset
55- if (offset == 0 ) {
65+ offset = caret.offset
66+ if (offset == 0 ) {
67+ offset = document.textLength - 1
68+ }
69+ } else {
5670 offset = document.textLength - 1
5771 }
58- } else {
59- offset = document.textLength - 1
72+ document.insertString(Math .max(offset, 0 ), maker.makeKotlinData())
6073 }
61- document.insertString(Math .max(offset, 0 ), maker.makeKotlinData())
62- }
6374
64- Messages .showMessageDialog(project, " Kotlin Code insert successfully!" , " Information" , Messages .getInformationIcon())
75+ Messages .showMessageDialog(project, " Kotlin Code insert successfully!" , " Information" , Messages .getInformationIcon())
76+ sendActionInfo(gson.toJson(SuccessCompleteAction ()))
77+ } catch (e: Exception ) {
78+ handlerException(jsonString) {
79+ Messages .showErrorDialog(" I am sorry,JsonToKotlin may occur a RuntimeException,You could try again later or recover to the old version" , " Occur a fatal error" )
80+ }.uncaughtException(Thread .currentThread(), e)
81+ }
6582 }
6683}
6784
6885
6986/* *
7087 * do the action that could be roll-back
7188 */
72- fun executeCouldRollBackAction (project : Project ? , action : (Project ? )-> Unit ) {
89+ fun executeCouldRollBackAction (project : Project ? , action : (Project ? ) -> Unit ) {
7390 CommandProcessor .getInstance().executeCommand(project, {
7491 ApplicationManager .getApplication().runWriteAction {
7592 action.invoke(project)
0 commit comments