[Editor] Add support for commit handler in Parameters Table Fragment
This commit is contained in:
@@ -1,11 +1,21 @@
|
||||
package com.bartlomiejpluta.base.editor.common.parameter.component
|
||||
|
||||
import com.bartlomiejpluta.base.editor.common.parameter.model.Parameter
|
||||
import javafx.event.EventHandler
|
||||
import javafx.scene.control.TableCell
|
||||
import javafx.scene.input.KeyCode
|
||||
import javafx.scene.input.KeyEvent
|
||||
|
||||
class ParameterValueEditingCell : TableCell<Parameter<*>, Any>() {
|
||||
private val parameter: Parameter<*>?
|
||||
get() = tableView.items.getOrNull(index)
|
||||
|
||||
private val commitKeyHandler = EventHandler<KeyEvent> {
|
||||
if (it.code == KeyCode.ENTER) {
|
||||
commitEdit(null)
|
||||
it.consume()
|
||||
}
|
||||
}
|
||||
|
||||
override fun updateItem(item: Any?, empty: Boolean) {
|
||||
super.updateItem(item, empty)
|
||||
@@ -18,46 +28,41 @@ class ParameterValueEditingCell : TableCell<Parameter<*>, Any>() {
|
||||
|
||||
isEditing -> {
|
||||
text = null
|
||||
graphic = tableView.items[index].editor
|
||||
graphic = parameter?.editor
|
||||
}
|
||||
|
||||
else -> {
|
||||
text = tableView.items[index].valueString
|
||||
text = parameter?.valueString
|
||||
graphic = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun commitEdit(newValue: Any?) {
|
||||
super.commitEdit(newValue)
|
||||
tableView.items[index]?.commit()
|
||||
}
|
||||
|
||||
override fun startEdit() {
|
||||
if (index < 0 || index >= tableView.items.size) {
|
||||
return
|
||||
}
|
||||
|
||||
if (!tableView.items[index].editable) {
|
||||
if (parameter?.editable?.let { !it } == true) {
|
||||
return
|
||||
}
|
||||
|
||||
super.startEdit()
|
||||
text = null
|
||||
val parameter = tableView.items[index]
|
||||
graphic = parameter.editor.apply {
|
||||
addEventHandler(KeyEvent.KEY_PRESSED) {
|
||||
if (it.code == KeyCode.ENTER) {
|
||||
commitEdit(parameter.value)
|
||||
it.consume()
|
||||
}
|
||||
}
|
||||
}
|
||||
graphic = parameter?.editor
|
||||
parameter?.editor?.addEventHandler(KeyEvent.KEY_PRESSED, commitKeyHandler)
|
||||
}
|
||||
|
||||
override fun commitEdit(newValue: Any?) {
|
||||
parameter?.commit()
|
||||
super.commitEdit(parameter?.value)
|
||||
parameter?.editor?.removeEventFilter(KeyEvent.KEY_PRESSED, commitKeyHandler)
|
||||
}
|
||||
|
||||
override fun cancelEdit() {
|
||||
super.cancelEdit()
|
||||
text = tableView.items[index]?.valueString
|
||||
text = parameter?.valueString
|
||||
graphic = null
|
||||
parameter?.editor?.removeEventFilter(KeyEvent.KEY_PRESSED, commitKeyHandler)
|
||||
}
|
||||
}
|
||||
@@ -7,8 +7,9 @@ class EnumParameter<E : Enum<E>>(
|
||||
key: String,
|
||||
initialValue: E,
|
||||
editable: Boolean = true,
|
||||
autocommit: Boolean = false
|
||||
) : Parameter<E>(key, initialValue, editable, autocommit) {
|
||||
autocommit: Boolean = false,
|
||||
onCommit: (oldValue: E, newValue: E, submit: () -> Unit) -> Unit = { _, _, submit -> submit() }
|
||||
) : Parameter<E>(key, initialValue, editable, autocommit, onCommit) {
|
||||
override val editor = ComboBox<E>().apply { items.setAll(initialValue.javaClass.enumConstants.toList()) }
|
||||
override val editorValueProperty: Property<E>
|
||||
get() = editor.valueProperty()
|
||||
|
||||
@@ -9,8 +9,9 @@ class IntegerParameter(
|
||||
minValue: Int,
|
||||
maxValue: Int,
|
||||
editable: Boolean = true,
|
||||
autocommit: Boolean = false
|
||||
) : Parameter<Int>(key, initialValue, editable, autocommit) {
|
||||
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 editorValueProperty: Property<Int>
|
||||
get() = editor.valueFactory.valueProperty()
|
||||
|
||||
@@ -9,7 +9,8 @@ abstract class Parameter<T>(
|
||||
key: String,
|
||||
initialValue: T? = null,
|
||||
editable: Boolean = true,
|
||||
private val autocommit: Boolean = false
|
||||
private val autocommit: Boolean = false,
|
||||
private val onCommit: (oldValue: T, newValue: T, submit: () -> Unit) -> Unit = { _, _, submit -> submit() }
|
||||
) {
|
||||
val keyProperty = ReadOnlyStringWrapper(key)
|
||||
val key by keyProperty
|
||||
@@ -22,13 +23,18 @@ abstract class Parameter<T>(
|
||||
|
||||
fun commit() {
|
||||
if (!autocommit) {
|
||||
value = editorValueProperty.value
|
||||
onCommit(value, editorValueProperty.value, this::submit)
|
||||
}
|
||||
}
|
||||
|
||||
private fun submit() {
|
||||
value = editorValueProperty.value
|
||||
}
|
||||
|
||||
protected fun init() {
|
||||
if (autocommit) {
|
||||
editorValueProperty.bindBidirectional(valueProperty)
|
||||
valueProperty.addListener { _, oldValue, newValue -> onCommit(oldValue, newValue, NOOP) }
|
||||
} else {
|
||||
editorValueProperty.value = value
|
||||
valueProperty.addListener { _, _, v -> editorValueProperty.value = v }
|
||||
@@ -41,4 +47,8 @@ abstract class Parameter<T>(
|
||||
|
||||
open val valueString: String
|
||||
get() = value.toString()
|
||||
|
||||
companion object {
|
||||
private val NOOP = {}
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,12 @@ import javafx.scene.control.TextField
|
||||
|
||||
class StringParameter(
|
||||
key: String,
|
||||
initialValue: String? = null,
|
||||
initialValue: String = "",
|
||||
editable: Boolean = true,
|
||||
autocommit: Boolean = false
|
||||
autocommit: Boolean = false,
|
||||
onCommit: (oldValue: String, newValue: String, submit: () -> Unit) -> Unit = { _, _, submit -> submit() }
|
||||
) :
|
||||
Parameter<String>(key, initialValue, editable, autocommit) {
|
||||
Parameter<String>(key, initialValue, editable, autocommit, onCommit) {
|
||||
override val editor = TextField()
|
||||
override val editorValueProperty: Property<String>
|
||||
get() = editor.textProperty()
|
||||
|
||||
Reference in New Issue
Block a user