[Editor] Fix possibility to provide non-numeric characters in IntegerParameter Spinner
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.bartlomiejpluta.base.editor.common.parameter.model
|
||||
|
||||
import com.bartlomiejpluta.base.editor.util.fx.TextFieldUtil
|
||||
import javafx.beans.property.Property
|
||||
import javafx.scene.control.Spinner
|
||||
|
||||
@@ -12,7 +13,11 @@ class IntegerParameter(
|
||||
autocommit: Boolean = false,
|
||||
onCommit: (oldValue: Int, newValue: Int, submit: () -> Unit) -> Unit = { _, _, submit -> submit() },
|
||||
) : Parameter<Int>(key, initialValue, editable, autocommit, onCommit) {
|
||||
override val editor = Spinner<Int>(minValue, maxValue, initialValue).apply { isEditable = true }
|
||||
override val editor = Spinner<Int>(minValue, maxValue, initialValue).apply {
|
||||
isEditable = true
|
||||
editor.textFormatter = TextFieldUtil.integerFormatter(initialValue)
|
||||
}
|
||||
|
||||
override val editorValueProperty: Property<Int>
|
||||
get() = editor.valueFactory.valueProperty()
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.bartlomiejpluta.base.editor.util.fx
|
||||
|
||||
import javafx.scene.control.TextFormatter
|
||||
import javafx.util.converter.IntegerStringConverter
|
||||
import java.util.function.UnaryOperator
|
||||
|
||||
object TextFieldUtil {
|
||||
fun integerFormatter(initialValue: Int) = TextFormatter(IntegerStringConverter(), initialValue, filter)
|
||||
|
||||
private val intRegex = "-?[0-9]*".toRegex()
|
||||
private val filter = UnaryOperator<TextFormatter.Change> { if (it.controlNewText.matches(intRegex)) it else null }
|
||||
}
|
||||
Reference in New Issue
Block a user