Skip to content

Commit f78cb31

Browse files
authored
Merge pull request #202 from wuseal/3.4.0-optimize/JSON_Input_UI
Optimize UI&UX
2 parents 6185a06 + 9d7ab2b commit f78cb31

File tree

6 files changed

+168
-82
lines changed

6 files changed

+168
-82
lines changed

src/main/kotlin/wu/seal/jsontokotlin/ui/JsonInputDialog.kt

Lines changed: 148 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ import com.intellij.util.ui.JBEmptyBorder
1818
import wu.seal.jsontokotlin.feedback.FormatJSONAction
1919
import wu.seal.jsontokotlin.feedback.sendActionInfo
2020
import wu.seal.jsontokotlin.utils.addComponentIntoVerticalBoxAlignmentLeft
21-
import java.awt.BorderLayout
22-
import java.awt.Dimension
23-
import java.awt.Toolkit
21+
import java.awt.*
2422
import java.awt.datatransfer.DataFlavor
2523
import java.awt.event.ActionEvent
24+
import java.awt.event.MouseAdapter
25+
import java.awt.event.MouseEvent
26+
import java.net.URI
2627
import java.net.URL
2728
import javax.swing.*
2829
import javax.swing.text.AttributeSet
@@ -57,7 +58,14 @@ val myInputValidator = MyInputValidator()
5758
/**
5859
* Json input Dialog
5960
*/
60-
class JsonInputDialog(classsName: String, private val project: Project) : Messages.InputDialog(project, "Please input the class name and JSON String to generate Kotlin data class", "Generate Kotlin Data Class Code", IconLoader.getIcon("/icons/logo_96x96.png"), "", myInputValidator) {
61+
class JsonInputDialog(classsName: String, private val project: Project) : Messages.InputDialog(
62+
project,
63+
"Please input the JSON String and class name to generate Kotlin data class",
64+
"Generate Kotlin Data Class Code",
65+
null,
66+
"",
67+
myInputValidator
68+
) {
6169
private lateinit var jsonContentEditor: Editor
6270

6371
private val prettyGson: Gson = GsonBuilder().setPrettyPrinting().serializeNulls().disableHtmlEscaping().create()
@@ -67,78 +75,136 @@ class JsonInputDialog(classsName: String, private val project: Project) : Messag
6775
myField.text = classsName
6876
}
6977

70-
override fun createMessagePanel(): JPanel {
78+
override fun createNorthPanel(): JComponent? {
79+
val jsonTitle = JBLabel("JSON Text: ").apply {
80+
font = font.deriveFont(14f)
81+
}
82+
val jsonTip =
83+
JBLabel("Tips: you can use JSON string、http urls or local file just right click on text area").apply {
84+
font = font.deriveFont(12f)
85+
}
86+
87+
val formatButton = JButton("JSON Format")
88+
.apply {
89+
addActionListener(object : AbstractAction() {
90+
override fun actionPerformed(p0: ActionEvent?) {
91+
handleFormatJSONString()
92+
}
93+
})
94+
}
95+
val jsonInputTitleContainer = JPanel()
96+
.apply {
97+
border = JBEmptyBorder(0, 0, 5, 0)
98+
layout = BoxLayout(this, BoxLayout.LINE_AXIS)
99+
add(jsonTitle)
100+
add(jsonTip)
101+
add(Box.createHorizontalGlue())
102+
add(formatButton)
103+
}
104+
val rightContainer = JPanel(BorderLayout()).apply {
71105

106+
add(JLabel(myMessage).apply {
107+
font = font.deriveFont(12f)
108+
}, BorderLayout.NORTH)
109+
add(jsonInputTitleContainer, BorderLayout.CENTER)
110+
111+
}
112+
113+
val icon = IconLoader.getIcon("/icons/icon_json_input_dialog.png")
114+
115+
val iconLabel = JBLabel(icon).apply {
116+
minimumSize = JBDimension(42, 42)
117+
verticalAlignment = SwingConstants.TOP
118+
border = JBEmptyBorder(0, 0, 0, 5)
119+
}
120+
return JPanel(BorderLayout())
121+
.apply {
122+
border = JBEmptyBorder(0, 0, 5, 0)
123+
add(iconLabel, BorderLayout.WEST)
124+
add(rightContainer, BorderLayout.CENTER)
125+
}
126+
127+
}
128+
129+
override fun createCenterPanel(): JComponent? {
72130
jsonContentEditor = createJsonContentEditor()
73-
myField = createTextFieldComponent()
74131
myInputValidator.jsonInputEditor = jsonContentEditor
75132

76-
val classNameInputContainer = createLinearLayoutVertical()
77-
.apply {
78-
val classNameTitle = JBLabel("Class Name:")
79-
classNameTitle.border = JBEmptyBorder(5, 0, 5, 0)
80-
addComponentIntoVerticalBoxAlignmentLeft(classNameTitle)
81-
addComponentIntoVerticalBoxAlignmentLeft(myField)
82-
preferredSize = JBDimension(500, 56)
83-
}
84133

134+
val jsonInputContainer = JPanel(BorderLayout())
135+
.apply {
136+
preferredSize = JBDimension(700, 400)
137+
border = JBEmptyBorder(5, 0, 5, 5)
138+
add(jsonContentEditor.component, BorderLayout.CENTER)
139+
}
85140

86-
val jsonInputContainer = createLinearLayoutVertical()
87-
.apply {
88-
preferredSize = JBDimension(700, 400)
89-
border = JBEmptyBorder(5, 0, 5, 5)
90-
val jsonTitle = JBLabel("JSON Text:")
91-
jsonTitle.border = JBEmptyBorder(5, 0, 5, 0)
92-
addComponentIntoVerticalBoxAlignmentLeft(jsonTitle)
93-
addComponentIntoVerticalBoxAlignmentLeft(jsonContentEditor.component)
94-
}
141+
myField = createTextFieldComponent()
95142

143+
val classNameInputContainer = createLinearLayoutVertical()
144+
.apply {
145+
val classNameTitle = JBLabel("Class Name:")
146+
classNameTitle.border = JBEmptyBorder(5, 0, 5, 0)
147+
addComponentIntoVerticalBoxAlignmentLeft(classNameTitle)
148+
addComponentIntoVerticalBoxAlignmentLeft(myField)
149+
preferredSize = JBDimension(500, 56)
150+
font = font.deriveFont(14f)
151+
}
96152

97-
val centerContainer = JPanel()
98-
.apply {
99-
layout = BoxLayout(this, BoxLayout.PAGE_AXIS)
100-
addComponentIntoVerticalBoxAlignmentLeft(classNameInputContainer)
101-
addComponentIntoVerticalBoxAlignmentLeft(jsonInputContainer)
102-
}
153+
val centerInputContainer = JPanel(BorderLayout())
154+
.apply {
155+
add(jsonInputContainer, BorderLayout.CENTER)
156+
add(classNameInputContainer, BorderLayout.SOUTH)
157+
}
158+
159+
val settingContainer = createAdvancedPanel()
160+
161+
return JPanel(BorderLayout())
162+
.apply {
163+
add(centerInputContainer, BorderLayout.CENTER)
164+
add(settingContainer, BorderLayout.SOUTH)
165+
}
166+
}
103167

168+
private fun createAdvancedPanel(): JPanel {
104169
val advancedButton = JButton("Advanced")
105-
.apply {
106-
horizontalAlignment = SwingConstants.CENTER
107-
addActionListener(object : AbstractAction() {
108-
override fun actionPerformed(e: ActionEvent) {
109-
AdvancedDialog(false).show()
110-
}
111-
})
112-
}
170+
.apply {
171+
horizontalAlignment = SwingConstants.CENTER
172+
addActionListener(object : AbstractAction() {
173+
override fun actionPerformed(e: ActionEvent) {
174+
AdvancedDialog(false).show()
175+
}
176+
})
177+
}
113178

114-
val formatButton = JButton("Format")
115-
.apply {
116-
horizontalAlignment = SwingConstants.CENTER
117-
addActionListener(object : AbstractAction() {
118-
override fun actionPerformed(p0: ActionEvent?) {
119-
handleFormatJSONString()
120-
}
121-
})
122-
}
179+
val tip = JLabel("Like this version? Please star here: ")
180+
val projectLink =
181+
JLabel("<html><a href='https://github.com/wuseal/JsonToKotlinClass'>https://github.com/wuseal/JsonToKotlinClass</a></html>")
123182

124-
val settingContainer = JPanel()
125-
.apply {
126-
border = JBEmptyBorder(0, 5, 5, 7)
127-
layout = BoxLayout(this, BoxLayout.LINE_AXIS)
128-
add(advancedButton)
129-
add(Box.createHorizontalGlue())
130-
add(formatButton)
131-
}
183+
projectLink.maximumSize =
184+
JBDimension(210, 30)//if not add this line code,the `add(Box.createHorizontalGlue())`code will not do work
132185

186+
projectLink.addMouseListener(object : MouseAdapter() {
187+
override fun mouseClicked(e: MouseEvent?) {
188+
Desktop.getDesktop().browse(URI("https://github.com/wuseal/JsonToKotlinClass"))
189+
}
133190

134-
return JPanel(BorderLayout())
135-
.apply {
136-
if (myMessage != null) {
137-
add(createTextComponent(), BorderLayout.NORTH)
138-
}
139-
add(centerContainer, BorderLayout.CENTER)
140-
add(settingContainer, BorderLayout.SOUTH)
141-
}
191+
override fun mouseEntered(e: MouseEvent?) {
192+
projectLink.cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)
193+
}
194+
195+
override fun mouseExited(e: MouseEvent?) {
196+
projectLink.cursor = Cursor.getDefaultCursor()
197+
}
198+
})
199+
return JPanel()
200+
.apply {
201+
border = JBEmptyBorder(0, 0, 10, 7)
202+
layout = BoxLayout(this, BoxLayout.LINE_AXIS)
203+
add(advancedButton)
204+
add(Box.createHorizontalGlue())
205+
add(tip)
206+
add(projectLink)
207+
}
142208
}
143209

144210
private fun createJsonContentEditor(): Editor {
@@ -155,11 +221,11 @@ class JsonInputDialog(classsName: String, private val project: Project) : Messag
155221
val editor = editorFactory.createEditor(document, null, JsonFileType.INSTANCE, false)
156222

157223
editor.component
158-
.apply {
159-
isEnabled = true
160-
preferredSize = Dimension(640, 480)
161-
autoscrolls = true
162-
}
224+
.apply {
225+
isEnabled = true
226+
preferredSize = Dimension(640, 480)
227+
autoscrolls = true
228+
}
163229

164230

165231
val contentComponent = editor.contentComponent
@@ -176,15 +242,19 @@ class JsonInputDialog(classsName: String, private val project: Project) : Messag
176242
override fun createTextFieldComponent(): JTextComponent {
177243

178244
return JTextField()
179-
.apply {
180-
preferredSize = JBDimension(400, 40)
181-
document = object : PlainDocument() {
182-
override fun insertString(offs: Int, str: String?, a: AttributeSet?) {
183-
str ?: return
184-
super.insertString(offs, str.filter { it.isLetterOrDigit() || it in listOf('_', '$') }.take(252), a)
185-
}
245+
.apply {
246+
preferredSize = JBDimension(400, 40)
247+
document = object : PlainDocument() {
248+
override fun insertString(offs: Int, str: String?, a: AttributeSet?) {
249+
str ?: return
250+
super.insertString(
251+
offs,
252+
str.filter { it.isLetterOrDigit() || it in listOf('_', '$') }.take(252),
253+
a
254+
)
186255
}
187256
}
257+
}
188258
}
189259

190260
private fun createPasteFromClipboardMenuItem() = JMenuItem("Paste from clipboard").apply {
@@ -237,11 +307,7 @@ class JsonInputDialog(classsName: String, private val project: Project) : Messag
237307
override fun getInputString(): String = if (exitCode == 0) jsonContentEditor.document.text.trim() else ""
238308

239309
override fun getPreferredFocusedComponent(): JComponent? {
240-
return if (this.myField.text.isNullOrEmpty()) {
241-
this.myField
242-
} else {
243-
jsonContentEditor.contentComponent
244-
}
310+
return jsonContentEditor.contentComponent
245311
}
246312

247313
fun handleFormatJSONString() {
@@ -271,7 +337,7 @@ class JsonInputDialog(classsName: String, private val project: Project) : Messag
271337
fun createLinearLayoutVertical(): JPanel {
272338

273339
return JPanel()
274-
.apply {
275-
layout = BoxLayout(this, BoxLayout.PAGE_AXIS)
276-
}
340+
.apply {
341+
layout = BoxLayout(this, BoxLayout.PAGE_AXIS)
342+
}
277343
}
Lines changed: 20 additions & 0 deletions
Loading
-935 Bytes
Loading
1.53 KB
Loading
1.79 KB
Loading
4.45 KB
Loading

0 commit comments

Comments
 (0)