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