@@ -18,12 +18,10 @@ package com.stackspot.intellij.ui.toolwindow.panels
1818
1919import com.intellij.openapi.ui.ValidationInfo
2020import com.intellij.openapi.ui.validation.CHECK_NON_EMPTY
21- import com.intellij.openapi.ui.validation.validationTextErrorIf
2221import com.intellij.ui.UIBundle
2322import com.intellij.ui.components.JBTextField
2423import com.intellij.ui.dsl.builder.*
2524import com.intellij.util.MathUtil
26- import com.stackspot.model.Input
2725import com.stackspot.model.component.Helper
2826import org.apache.commons.lang3.StringUtils
2927import java.awt.event.KeyAdapter
@@ -63,34 +61,38 @@ class IntComponent : ComponentType {
6361 val field = textField()
6462 .bindText(getterString(helper)) { helper.addValues(it) }
6563 .comment(helper.input.help)
66- validatePattern(field, helper.input)
64+ validateRequired(helper, field)
65+ validatePattern(helper, field)
6766 validateIfInputIsNumber(field)
6867 field.columns(COLUMNS_SIZE )
69- field.component.addKeyListener(object : KeyAdapter () {
70- override fun keyPressed (e : KeyEvent ? ) {
71- val text = field.component.text
72- if (text.length >= MAX_TEXT_LENGTH && text.toIntOrNull() != null ) {
73- var value = text.toIntOrNull()
74- value = value?.let { MathUtil .clamp(it, 0 , 9999999 ) }
75- field.component.text = " "
76- field.component.text = value.toString()
77- e?.consume()
78- }
79- }
80- })
68+ validateNumericFieldSize(field)
8169 helper.components.add(field)
8270 }
8371 }
8472
85- private fun validateIfInputIsNumber (field : Cell <JBTextField >) {
73+ private fun validateNumericFieldSize (field : Cell <JBTextField >) {
74+ field.component.addKeyListener(object : KeyAdapter () {
75+ override fun keyPressed (e : KeyEvent ? ) {
76+ val text = field.component.text
77+ if (text.length >= MAX_TEXT_LENGTH && text.toIntOrNull() != null ) {
78+ var value = text.toIntOrNull()
79+ value = value?.let { MathUtil .clamp(it, 0 , 9999999 ) }
80+ field.component.text = " "
81+ field.component.text = value.toString()
82+ e?.consume()
83+ }
84+ }
85+ })
86+ }
8687
88+ private fun validateIfInputIsNumber (field : Cell <JBTextField >) {
8789 val regex = " ^[0-9]*\$ " .toRegex()
88-
89- val checkPattern = validationTextErrorIf(UIBundle .message(ENTER_A_NUMBER )) {
90- ! regex.matches(it) && field.component.isVisible
90+ field.validation {
91+ val text: String = field.component.text
92+ if (! regex.matches(text) && field.component.isVisible) {
93+ ValidationInfo (UIBundle .message(ENTER_A_NUMBER ), field.component)
94+ } else null
9195 }
92-
93- checkPattern.let { field.textValidation(it) }
9496 }
9597}
9698
@@ -114,26 +116,12 @@ class TextComponent : ComponentType {
114116 val field = textField()
115117 .bindText(getterString(helper)) { helper.addValues(it) }
116118 .comment(helper.input.help)
117- validatePattern(field, helper.input )
119+ validatePattern(helper, field )
118120 helper.components.add(field)
119121 }
120122 }
121123}
122124
123- private const val INPUT_INVALID_REGEX = " Input is invalid for regex:"
124-
125- private fun validatePattern (field : Cell <JTextField >, input : Input ) {
126- val pattern = input.pattern?.toRegex()
127- val checkPattern = pattern?.let {
128- validationTextErrorIf(" $INPUT_INVALID_REGEX $pattern " ) {
129- ! pattern.matches(it) && field.component.isVisible
130- }
131- }
132- checkPattern?.let { field.textValidation(it) }
133- if (input.required) field.textValidation(CHECK_NON_EMPTY )
134- }
135-
136-
137125class ListComponent : ComponentType {
138126 override fun create (helper : Helper ): Row {
139127 return helper.panel.row(helper.input.label) {
@@ -155,13 +143,29 @@ class PasswordComponent : ComponentType {
155143 val field = cell(passwordField)
156144 .bindText(getterString(helper)) { helper.addValues(it) }
157145 .comment(helper.input.help)
158- validatePattern(field, helper.input )
146+ validatePattern(helper, field )
159147 helper.components.add(field)
160148 }
161149 }
162150}
163151
152+ private const val INPUT_INVALID_REGEX = " Input is invalid for regex:"
153+
154+ private fun validateRequired (helper : Helper , field : Cell <JBTextField >) {
155+ if (helper.input.required) field.textValidation(CHECK_NON_EMPTY )
156+ }
164157
158+ private fun validatePattern (helper : Helper , field : Cell <JTextField >) {
159+ val pattern = helper.input.pattern?.toRegex()
160+ pattern?.let {
161+ field.validation {
162+ val text: String = field.component.text
163+ if (! pattern.matches(text) && field.component.isVisible) {
164+ ValidationInfo (" $INPUT_INVALID_REGEX $pattern " , field.component)
165+ } else null
166+ }
167+ }
168+ }
165169
166170private fun getterBoolean (
167171 helper : Helper ,
0 commit comments