@@ -23,11 +23,11 @@ import java.awt.Dimension
2323import java.awt.Toolkit
2424import java.awt.datatransfer.DataFlavor
2525import java.awt.event.ActionEvent
26- import java.awt.event.KeyAdapter
27- import java.awt.event.KeyEvent
2826import java.net.URL
2927import javax.swing.*
28+ import javax.swing.text.AttributeSet
3029import javax.swing.text.JTextComponent
30+ import javax.swing.text.PlainDocument
3131
3232/* *
3333 * Dialog widget relative
@@ -74,71 +74,71 @@ class JsonInputDialog(classsName: String, private val project: Project) : Messag
7474 myInputValidator.jsonInputEditor = jsonContentEditor
7575
7676 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- }
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+ }
8484
8585
8686 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- }
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+ }
9595
9696
9797 val centerContainer = JPanel ()
98- .apply {
99- layout = BoxLayout (this , BoxLayout .PAGE_AXIS )
100- addComponentIntoVerticalBoxAlignmentLeft(classNameInputContainer)
101- addComponentIntoVerticalBoxAlignmentLeft(jsonInputContainer)
102- }
98+ .apply {
99+ layout = BoxLayout (this , BoxLayout .PAGE_AXIS )
100+ addComponentIntoVerticalBoxAlignmentLeft(classNameInputContainer)
101+ addComponentIntoVerticalBoxAlignmentLeft(jsonInputContainer)
102+ }
103103
104104 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- }
105+ .apply {
106+ horizontalAlignment = SwingConstants .CENTER
107+ addActionListener(object : AbstractAction () {
108+ override fun actionPerformed (e : ActionEvent ) {
109+ AdvancedDialog (false ).show()
110+ }
111+ })
112+ }
113113
114114 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- }
115+ .apply {
116+ horizontalAlignment = SwingConstants .CENTER
117+ addActionListener(object : AbstractAction () {
118+ override fun actionPerformed (p0 : ActionEvent ? ) {
119+ handleFormatJSONString()
120+ }
121+ })
122+ }
123123
124124 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- }
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+ }
132132
133133
134134 return JPanel (BorderLayout ())
135- .apply {
136- if (myMessage != null ) {
137- add(createTextComponent(), BorderLayout .NORTH )
135+ .apply {
136+ if (myMessage != null ) {
137+ add(createTextComponent(), BorderLayout .NORTH )
138+ }
139+ add(centerContainer, BorderLayout .CENTER )
140+ add(settingContainer, BorderLayout .SOUTH )
138141 }
139- add(centerContainer, BorderLayout .CENTER )
140- add(settingContainer, BorderLayout .SOUTH )
141- }
142142 }
143143
144144 private fun createJsonContentEditor (): Editor {
@@ -155,11 +155,11 @@ class JsonInputDialog(classsName: String, private val project: Project) : Messag
155155 val editor = editorFactory.createEditor(document, null , JsonFileType .INSTANCE , false )
156156
157157 editor.component
158- .apply {
159- isEnabled = true
160- preferredSize = Dimension (640 , 480 )
161- autoscrolls = true
162- }
158+ .apply {
159+ isEnabled = true
160+ preferredSize = Dimension (640 , 480 )
161+ autoscrolls = true
162+ }
163163
164164
165165 val contentComponent = editor.contentComponent
@@ -176,16 +176,15 @@ class JsonInputDialog(classsName: String, private val project: Project) : Messag
176176 override fun createTextFieldComponent (): JTextComponent {
177177
178178 return JTextField ()
179- .apply {
180- preferredSize = JBDimension (400 , 40 )
181- addKeyListener( object : KeyAdapter () {
182- override fun keyTyped ( e : KeyEvent ) {
183- if (e.keyChar == ' ˚ ' ) {
184- e.consume( )
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 )
185185 }
186186 }
187- })
188- }
187+ }
189188 }
190189
191190 private fun createPasteFromClipboardMenuItem () = JMenuItem (" Paste from clipboard" ).apply {
@@ -228,7 +227,12 @@ class JsonInputDialog(classsName: String, private val project: Project) : Messag
228227 /* *
229228 * get the user input class name
230229 */
231- fun getClassName (): String = if (exitCode == 0 ) this .myField.text.trim() else " "
230+ fun getClassName (): String {
231+ return if (exitCode == 0 ) {
232+ val name = myField.text.trim()
233+ name.let { if (it.first().isDigit() || it.contains(' $' )) " `$it `" else it }
234+ } else " "
235+ }
232236
233237 override fun getInputString (): String = if (exitCode == 0 ) jsonContentEditor.document.text.trim() else " "
234238
@@ -267,7 +271,7 @@ class JsonInputDialog(classsName: String, private val project: Project) : Messag
267271fun createLinearLayoutVertical (): JPanel {
268272
269273 return JPanel ()
270- .apply {
271- layout = BoxLayout (this , BoxLayout .PAGE_AXIS )
272- }
274+ .apply {
275+ layout = BoxLayout (this , BoxLayout .PAGE_AXIS )
276+ }
273277}
0 commit comments