Skip to content

Commit b60a8c2

Browse files
author
seal
committed
add property init with default value option in property tab
fix a bug when list property name is 'list' add statistics about exception log and use condition beautify dialog layout
1 parent ad48a6f commit b60a8c2

19 files changed

+525
-99
lines changed

resources/META-INF/plugin.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<idea-plugin>
22
<id>wu.seal.tool.jsontokotlin</id>
33
<name>JsonToKotlinClass</name>
4-
<version>1.2.1</version>
4+
<version>1.3</version>
55
<vendor email="[email protected]" url="https://www.github.com/wuseal">Seal</vendor>
66

77
<description><![CDATA[
@@ -41,7 +41,7 @@
4141

4242
<application-components>
4343
<component>
44-
<implementation-class>wu.seal.jsontokotlin.TestApplication</implementation-class>
44+
<implementation-class>wu.seal.jsontokotlin.JsonToKotlinApplication</implementation-class>
4545
</component>
4646
</application-components>
4747
</idea-plugin>

src/wu/seal/jsontokotlin/ConfigManager.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ interface IConfigManager {
2121
private val IS_PROPERTY_NULLABLE_KEY: String
2222
get() = "jsonToKotlin_is_property_nullable_key"
2323

24+
25+
private val INIT_WITH_DEFAULT_VALUE_KEY: String
26+
get() = "jsonToKotlin_init_with_default_value_key"
27+
28+
29+
private val USER_UUID_KEY: String
30+
get() = "jsonToKotlin_user_uuid_value_key"
31+
32+
2433
var isPropertiesVar: Boolean
2534
get() = if (isTestModel) TestConfig.isPropertiesVar else PropertiesComponent.getInstance().isTrueValue(IS_PROPERTIES_VAR_KEY)
2635
set(value) = if (isTestModel) {
@@ -42,9 +51,20 @@ interface IConfigManager {
4251
get() = if (isTestModel) TestConfig.isPropertyNullable else PropertiesComponent.getInstance().isTrueValue(IS_PROPERTY_NULLABLE_KEY)
4352
set(value) = if (isTestModel) {
4453
} else PropertiesComponent.getInstance().setValue(IS_PROPERTY_NULLABLE_KEY, value)
45-
}
4654

4755

56+
var initWithDefaultValue: Boolean
57+
get() = if (isTestModel) TestConfig.initWithDefaultValue else PropertiesComponent.getInstance().getBoolean(INIT_WITH_DEFAULT_VALUE_KEY)
58+
set(value) = if (isTestModel) {
59+
} else PropertiesComponent.getInstance().setValue(INIT_WITH_DEFAULT_VALUE_KEY, value)
60+
61+
var userUUID: String
62+
get() = if (isTestModel) "" else PropertiesComponent.getInstance().getValue(USER_UUID_KEY, "")
63+
set(value) = if (isTestModel) {
64+
} else PropertiesComponent.getInstance().setValue(USER_UUID_KEY, value)
65+
66+
}
67+
4868
/**
4969
* This means which Json convert library you are using in you project
5070
*/
@@ -68,4 +88,5 @@ object TestConfig {
6888
var isPropertiesVar = false
6989
var targetJsonConvertLib = TargetJsonConverter.Gson
7090
var isPropertyNullable = true
91+
var initWithDefaultValue = true
7192
}

src/wu/seal/jsontokotlin/ConfigSettingDialog.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ interface IConfigSettingDialog {
2020

2121
}
2222

23-
23+
//todo : change the tab of TargetJson Lib postion to make it more pre
2424
class ConfigSettingDialog(canBeParent: Boolean) : DialogWrapper(canBeParent), IConfigSettingDialog {
2525

2626
init {
@@ -39,12 +39,12 @@ class ConfigSettingDialog(canBeParent: Boolean) : DialogWrapper(canBeParent), IC
3939

4040
val targetJsonLibConfigPanel = createTargetJsonLibConfigPanel()
4141

42-
tabbedPane.add("Property Keyword", propertyPanel)
43-
44-
tabbedPane.add("Comment Config", commentConfigPanel)
42+
tabbedPane.add("Property", propertyPanel)
4543

4644
tabbedPane.add("Target Json Lib", targetJsonLibConfigPanel)
4745

46+
tabbedPane.add("Comment", commentConfigPanel)
47+
4848
tabbedPane.minimumSize = JBDimension(500, 300)
4949

5050
return tabbedPane
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package wu.seal.jsontokotlin
2+
3+
import com.google.gson.Gson
4+
import com.google.gson.JsonParser
5+
6+
/**
7+
* Default Value relative
8+
* Created by Seal.wu on 2017/9/25.
9+
*/
10+
11+
fun getDefaultValue(propertyType: String): String {
12+
13+
val rawType = getRawType(propertyType)
14+
15+
if (rawType == TYPE_INT) {
16+
return 0.toString()
17+
} else if (rawType == TYPE_LONG) {
18+
return 0L.toString()
19+
} else if (rawType == TYPE_STRING) {
20+
return "\"\""
21+
} else if (rawType == TYPE_DOUBLE) {
22+
return 0.0.toString()
23+
} else if (rawType == TYPE_BOOLEAN) {
24+
return false.toString()
25+
} else if (rawType.contains("List")) {
26+
return "listOf()"
27+
} else if (rawType == TYPE_ANY) {
28+
return "Any()"
29+
} else {
30+
return "$rawType()"
31+
}
32+
}
33+
34+
data class Test(
35+
val a: String = "A"
36+
)
37+
38+
fun main(args: Array<String>) {
39+
40+
isTestModel = true
41+
42+
val json = """{"a":null} """
43+
44+
val gson = Gson()
45+
46+
val newJson = gson.toJson(JsonParser().parse(json))
47+
48+
val obj = gson.fromJson(newJson, Test::class.java)
49+
50+
println(obj)
51+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package wu.seal.jsontokotlin
2+
3+
import java.awt.Component
4+
import java.awt.Container
5+
import javax.swing.Box
6+
import javax.swing.BoxLayout
7+
8+
/**
9+
*
10+
* Created by Seal.Wu on 2017/9/25.
11+
*/
12+
13+
fun Container.addComponentIntoVerticalBoxAlignmentLeft(component: Component) {
14+
if (layout is BoxLayout) {
15+
16+
val hBox = Box.createHorizontalBox()
17+
hBox.add(component)
18+
hBox.add(Box.createHorizontalGlue())
19+
add(hBox)
20+
}
21+
22+
}
23+
24+
fun Container.addComponentIntoVerticalBoxAlignmentLeft(component: Component,leftMargin:Int) {
25+
if (layout is BoxLayout) {
26+
27+
val hBox = Box.createHorizontalBox()
28+
hBox.add(Box.createHorizontalStrut(leftMargin))
29+
hBox.add(component)
30+
hBox.add(Box.createHorizontalGlue())
31+
add(hBox)
32+
}
33+
34+
}

src/wu/seal/jsontokotlin/GsonSupporter.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ object GsonSupporter : IGsonSupporter {
4949

5050
gsonSupportPropertyBuilder.append(propertyType)
5151

52+
if (ConfigManager.initWithDefaultValue) {
53+
gsonSupportPropertyBuilder.append(" = ").append(getDefaultValue(propertyType))
54+
}
55+
5256
gsonSupportPropertyBuilder.append(",")
5357

5458
return gsonSupportPropertyBuilder.toString()

src/wu/seal/jsontokotlin/JsonInputDialog.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ import javax.swing.text.JTextComponent
1717
* Created by Seal.wu on 2017/9/21.
1818
*/
1919

20-
/**
21-
* Json input Dialog
22-
*/
23-
class JsonInputDialog(project: Project) : Messages.InputDialog(project, "Please input the Json Data", "Input Json", Messages.getInformationIcon(), "", object : InputValidator {
20+
21+
class MyInputValidator : InputValidator {
2422
override fun checkInput(inputString: String): Boolean {
2523
try {
2624
val jsonElement = JsonParser().parse(inputString)
25+
2726
return jsonElement.isJsonObject || jsonElement.isJsonArray
2827
} catch (e: JsonSyntaxException) {
2928
return false
@@ -34,7 +33,13 @@ class JsonInputDialog(project: Project) : Messages.InputDialog(project, "Please
3433
override fun canClose(inputString: String): Boolean {
3534
return true
3635
}
37-
}) {
36+
}
37+
38+
/**
39+
* Json input Dialog
40+
*/
41+
class JsonInputDialog(project: Project) : Messages.InputDialog(project, "Please input the Json Data", "Input Json", Messages.getInformationIcon(), "", MyInputValidator()) {
42+
3843
override fun createMessagePanel(): JPanel {
3944
val messagePanel = JPanel(BorderLayout())
4045
if (myMessage != null) {
@@ -44,7 +49,6 @@ class JsonInputDialog(project: Project) : Messages.InputDialog(project, "Please
4449

4550
myField = createTextFieldComponent()
4651

47-
4852
messagePanel.add(createScrollableTextComponent(), BorderLayout.CENTER)
4953
val settingButton = JButton("Config Settings")
5054
settingButton.addActionListener(object : AbstractAction() {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package wu.seal.jsontokotlin
2+
3+
import com.intellij.openapi.components.ApplicationComponent
4+
import com.intellij.openapi.ui.Messages
5+
import wu.seal.jsontokotlin.statistics.handlerException
6+
import wu.seal.jsontokotlin.statistics.sendHistoryExceptionInfo
7+
import wu.seal.jsontokotlin.statistics.sendHistoryActionInfo
8+
9+
/**
10+
*
11+
* Created by Seal.wu on 2017/8/21.
12+
*/
13+
class JsonToKotlinApplication : ApplicationComponent {
14+
15+
override fun initComponent() {
16+
17+
println("init json to kotlin ")
18+
Thread() {
19+
sendHistoryExceptionInfo()
20+
sendHistoryActionInfo()
21+
}.start()
22+
}
23+
24+
override fun disposeComponent() {}
25+
26+
override fun getComponentName(): String {
27+
return "wu.seal.wu.seal.jsontokotlin.JsonToKotlinApplication"
28+
}
29+
}

src/wu/seal/jsontokotlin/KotlinMaker.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class KotlinMaker {
9696
fun main(args: Array<String>) {
9797
isTestModel = true
9898
val json1 = """{ "progr ammers": [
99-
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
99+
{ "FirstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
100100
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },
101101
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
102102
],

src/wu/seal/jsontokotlin/MakeKotlinClassAction.kt

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package wu.seal.jsontokotlin
22

3+
import com.google.gson.Gson
34
import com.intellij.openapi.actionSystem.AnAction
45
import com.intellij.openapi.actionSystem.AnActionEvent
56
import com.intellij.openapi.actionSystem.PlatformDataKeys
67
import com.intellij.openapi.application.ApplicationManager
78
import com.intellij.openapi.command.CommandProcessor
89
import com.intellij.openapi.project.Project
910
import 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

1116
import java.util.IllegalFormatFlagsException
1217

@@ -16,60 +21,72 @@ import java.util.IllegalFormatFlagsException
1621
*/
1722
class 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

Comments
 (0)