Skip to content

Commit 5165a56

Browse files
author
seal
committed
Change comment tab to other Tab
Add inner class model option in others tab Support auto get Class name when it has one in the edit file
1 parent 41ac966 commit 5165a56

File tree

12 files changed

+303
-34
lines changed

12 files changed

+303
-34
lines changed

.idea/uiDesigner.xml

Lines changed: 124 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/wu/seal/jsontokotlin/ConfigManager.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ interface IConfigManager {
3737
private val USER_CUSTOM_JSON_LIB_ANNOTATION_FORMAT_STRING: String
3838
get() = "jsontokotlin_user_custom_json_lib_annotation_format_string"
3939

40+
private val INNER_CLASS_MODEL_KEY: String
41+
get() = "jsontokotlin_inner_class_model_key"
42+
43+
4044
var isPropertiesVar: Boolean
4145
get() = if (isTestModel) TestConfig.isPropertiesVar else PropertiesComponent.getInstance().isTrueValue(IS_PROPERTIES_VAR_KEY)
4246
set(value) = if (isTestModel) {
@@ -96,6 +100,12 @@ interface IConfigManager {
96100
} else {
97101
PropertiesComponent.getInstance().setValue(USER_CUSTOM_JSON_LIB_ANNOTATION_FORMAT_STRING, value)
98102
}
103+
var isInnerClassModel: Boolean
104+
get() = if (isTestModel) TestConfig.isInnerClassModel else PropertiesComponent.getInstance().getBoolean(INNER_CLASS_MODEL_KEY, false)
105+
set(value) = if (isTestModel) {
106+
} else {
107+
PropertiesComponent.getInstance().setValue(INNER_CLASS_MODEL_KEY, value)
108+
}
99109
}
100110

101111
/**
@@ -122,4 +132,5 @@ object TestConfig {
122132
var targetJsonConvertLib = TargetJsonConverter.Gson
123133
var isPropertyNullable = true
124134
var initWithDefaultValue = true
135+
var isInnerClassModel = true
125136
}

src/wu/seal/jsontokotlin/KotlinMaker.kt renamed to src/wu/seal/jsontokotlin/KotlinCodeMaker.kt

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import java.util.HashSet
1010
* Kotlin code maker
1111
* Created by seal.wu on 2017/8/21.
1212
*/
13-
class KotlinMaker {
13+
class KotlinCodeMaker {
1414

1515
private var className: String? = null
1616
private var inputElement: JsonElement? = null
@@ -44,11 +44,39 @@ class KotlinMaker {
4444
stringBuilder.deleteCharAt(index)
4545
}
4646
stringBuilder.append(")")
47+
if (toBeAppend.isNotEmpty()) {
48+
appendSubClassCode(stringBuilder)
49+
}
50+
51+
return stringBuilder.toString()
52+
}
53+
54+
private fun appendSubClassCode(stringBuilder: StringBuilder) {
55+
if (ConfigManager.isInnerClassModel) {
56+
appendInnerClassModelSubClassCode(stringBuilder)
57+
} else {
58+
appendNormalSubClassCode(stringBuilder)
59+
}
60+
}
61+
62+
private fun appendInnerClassModelSubClassCode(stringBuilder: StringBuilder) {
63+
stringBuilder.append(" {")
64+
for (append in toBeAppend) {
65+
stringBuilder.append("\n")
66+
append.split("\n").filter { it.isNotEmpty() }.forEach {
67+
stringBuilder.append("\t")
68+
stringBuilder.append(it)
69+
stringBuilder.append("\n")
70+
}
71+
}
72+
stringBuilder.append("}")
73+
}
74+
75+
private fun appendNormalSubClassCode(stringBuilder: StringBuilder) {
4776
for (append in toBeAppend) {
4877
stringBuilder.append("\n")
4978
stringBuilder.append(append)
5079
}
51-
return stringBuilder.toString()
5280
}
5381

5482
private fun appClassName(stringBuilder: StringBuilder) {
@@ -67,7 +95,7 @@ class KotlinMaker {
6795
val type = getArrayType(property, jsonElementValue.asJsonArray)
6896

6997
if (isExpectedJsonObjArrayType(jsonElementValue.asJsonArray)) {
70-
toBeAppend.add(KotlinMaker(getChildType(getRawType(type)), jsonElementValue.asJsonArray.first()).makeKotlinData())
98+
toBeAppend.add(KotlinCodeMaker(getChildType(getRawType(type)), jsonElementValue.asJsonArray.first()).makeKotlinData())
7199
}
72100
addProperty(stringBuilder, property, type, "")
73101

@@ -77,7 +105,7 @@ class KotlinMaker {
77105

78106
} else if (jsonElementValue.isJsonObject) {
79107
val type = getJsonObjectType(property)
80-
toBeAppend.add(KotlinMaker(getRawType(type), jsonElementValue).makeKotlinData())
108+
toBeAppend.add(KotlinCodeMaker(getRawType(type), jsonElementValue).makeKotlinData())
81109
addProperty(stringBuilder, property, type, "")
82110

83111
} else if (jsonElementValue.isJsonNull) {

src/wu/seal/jsontokotlin/MakeKotlinClassAction.kt

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class MakeKotlinClassAction : AnAction("MakeKotlinClass") {
2727
override fun actionPerformed(event: AnActionEvent) {
2828
var jsonString: String = ""
2929
try {
30-
Thread() {
30+
Thread {
3131
sendActionInfo(gson.toJson(StartAction()))
3232
}.start()
3333
val project = event.getData(PlatformDataKeys.PROJECT)
@@ -37,21 +37,33 @@ class MakeKotlinClassAction : AnAction("MakeKotlinClass") {
3737
Messages.showWarningDialog("Please open a file in editor state for insert Kotlin code!", "No Editor File")
3838
return
3939
}
40-
41-
val inputDialog = JsonInputDialog(project!!)
40+
var tempClassName = ""
41+
val document = editor.document
42+
val editorText = document.text
43+
var needRewriteRootClassName = false
44+
if (editorText.indexOf("class") == editorText.lastIndexOf("class") && editorText.substringAfter("class").contains("(").not()) {
45+
tempClassName = editorText.substringAfter("class").substringBefore("{").trim()
46+
needRewriteRootClassName = true
47+
}
48+
val inputDialog = JsonInputDialog(tempClassName, project!!)
4249
inputDialog.show()
4350
val className = inputDialog.getClassName()
4451
val json = inputDialog.inputString
4552
if (json == null || json.isEmpty()) {
4653
return
4754
}
4855
jsonString = json
49-
val document = editor.document
56+
57+
if (needRewriteRootClassName) {
58+
executeCouldRollBackAction(project) {
59+
document.setText(editorText.substringBefore("class"))
60+
}
61+
}
5062
ImportClassWriter.insertImportClassCode(project, document)
5163

52-
val maker: KotlinMaker
64+
val codeMaker: KotlinCodeMaker
5365
try {
54-
maker = KotlinMaker(className, jsonString)
66+
codeMaker = KotlinCodeMaker(className, jsonString)
5567
} catch (e: IllegalFormatFlagsException) {
5668
e.printStackTrace()
5769
Messages.showErrorDialog(e.message, "UnSupport Json")
@@ -70,10 +82,9 @@ class MakeKotlinClassAction : AnAction("MakeKotlinClass") {
7082
} else {
7183
offset = document.textLength - 1
7284
}
73-
document.insertString(Math.max(offset, 0), maker.makeKotlinData())
85+
document.insertString(Math.max(offset, 0), codeMaker.makeKotlinData())
7486
}
7587

76-
// Messages.showMessageDialog(project, "Kotlin Code insert successfully!", "Information", Messages.getInformationIcon())
7788
Thread {
7889
sendActionInfo(gson.toJson(SuccessCompleteAction()))
7990
}.start()

src/wu/seal/jsontokotlin/statistics/Datas.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ data class ConfigInfo(
1919
val isPropertyNullable: Boolean = ConfigManager.isPropertyNullable,
2020
val initWithDefaultValue: Boolean = ConfigManager.initWithDefaultValue,
2121
val targetJsonConverterLib: String = ConfigManager.targetJsonConverterLib.name,
22+
val isInnerClassMode:Boolean = ConfigManager.isInnerClassModel,
2223
val timeStamp: String = Date().time.toString(),
2324
val daytime: String = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(Date())
2425

src/wu/seal/jsontokotlin/ui/ConfigSettingDialog.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,25 @@ class ConfigSettingDialog(canBeParent: Boolean) : DialogWrapper(canBeParent), IC
3232

3333
val tabbedPane = JTabbedPane()
3434

35-
val propertyPanel = createPropertyPane()
35+
val propertyPanelTab = createPropertyPane()
3636

37-
val commentConfigPanel = createCommentConfigPanel()
37+
val otherConfigTab = createOtherSettingTab()
3838

39-
val targetJsonLibConfigPanel = createTargetJsonLibConfigPanel()
39+
val JSONconverterTab = createTargetJsonLibConfigPanel()
4040

41-
tabbedPane.add("Property", propertyPanel)
41+
tabbedPane.add("Property", propertyPanelTab)
4242

43-
tabbedPane.add("JSON Converter", targetJsonLibConfigPanel)
43+
tabbedPane.add("JSON Converter", JSONconverterTab)
4444

45-
tabbedPane.add("Comment", commentConfigPanel)
45+
tabbedPane.add("Other", otherConfigTab)
4646

4747
tabbedPane.minimumSize = JBDimension(500, 300)
4848

4949
return tabbedPane
5050
}
5151

52+
private fun createOtherSettingTab() = ConfigSettingsOthersTab(true)
53+
5254
private fun createTargetJsonLibConfigPanel() = TargetJsonLibConfigPanelContainer(true)
5355

5456
private fun createCommentConfigPanel() = CommentConfigPanel(true)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package wu.seal.jsontokotlin.ui
2+
3+
import com.intellij.ui.components.JBCheckBox
4+
import com.intellij.util.ui.JBUI
5+
import wu.seal.jsontokotlin.ConfigManager
6+
import wu.seal.jsontokotlin.addComponentIntoVerticalBoxAlignmentLeft
7+
import java.awt.FlowLayout
8+
import java.awt.LayoutManager
9+
import javax.swing.Box
10+
import javax.swing.BoxLayout
11+
import javax.swing.JPanel
12+
import javax.swing.border.EmptyBorder
13+
14+
/**
15+
* others settings tab in config settings dialog
16+
* Created by Seal.Wu on 2018/2/6.
17+
*/
18+
class ConfigSettingsOthersTab(layout: LayoutManager?, isDoubleBuffered: Boolean) : JPanel(layout, isDoubleBuffered) {
19+
20+
constructor(layout: LayoutManager?) : this(layout, false)
21+
22+
constructor(isDoubleBuffered: Boolean) : this(FlowLayout(), isDoubleBuffered)
23+
24+
init {
25+
val boxLayout = BoxLayout(this, BoxLayout.PAGE_AXIS)
26+
setLayout(boxLayout)
27+
val bordWidth = JBUI.scale(10)
28+
border = EmptyBorder(bordWidth, bordWidth, bordWidth, bordWidth)
29+
30+
val enableComment = JBCheckBox("enable comment")
31+
enableComment.isSelected = ConfigManager.isCommentOff.not()
32+
enableComment.addActionListener { ConfigManager.isCommentOff = enableComment.isSelected.not() }
33+
34+
35+
36+
val enableInnerClassModel = JBCheckBox("enable inner class model")
37+
enableInnerClassModel.isSelected = ConfigManager.isInnerClassModel
38+
enableInnerClassModel.addActionListener { ConfigManager.isInnerClassModel = enableInnerClassModel.isSelected }
39+
40+
add(Box.createVerticalStrut(JBUI.scale(10)))
41+
42+
addComponentIntoVerticalBoxAlignmentLeft(enableComment)
43+
44+
add(Box.createVerticalStrut(JBUI.scale(20)))
45+
46+
addComponentIntoVerticalBoxAlignmentLeft(enableInnerClassModel)
47+
}
48+
49+
}

0 commit comments

Comments
 (0)