[Editor] Add support for manual committing in Parameters Table Fragment
This commit is contained in:
@@ -2,6 +2,8 @@ package com.bartlomiejpluta.base.editor.common.parameter.component
|
||||
|
||||
import com.bartlomiejpluta.base.editor.common.parameter.model.Parameter
|
||||
import javafx.scene.control.TableCell
|
||||
import javafx.scene.input.KeyCode
|
||||
import javafx.scene.input.KeyEvent
|
||||
|
||||
class ParameterValueEditingCell : TableCell<Parameter<*>, Any>() {
|
||||
|
||||
@@ -26,6 +28,11 @@ class ParameterValueEditingCell : TableCell<Parameter<*>, Any>() {
|
||||
}
|
||||
}
|
||||
|
||||
override fun commitEdit(newValue: Any?) {
|
||||
super.commitEdit(newValue)
|
||||
tableView.items[index]?.commit()
|
||||
}
|
||||
|
||||
override fun startEdit() {
|
||||
if (index < 0 || index >= tableView.items.size) {
|
||||
return
|
||||
@@ -37,7 +44,15 @@ class ParameterValueEditingCell : TableCell<Parameter<*>, Any>() {
|
||||
|
||||
super.startEdit()
|
||||
text = null
|
||||
graphic = tableView.items[index].editor
|
||||
val parameter = tableView.items[index]
|
||||
graphic = parameter.editor.apply {
|
||||
addEventHandler(KeyEvent.KEY_PRESSED) {
|
||||
if (it.code == KeyCode.ENTER) {
|
||||
commitEdit(parameter.value)
|
||||
it.consume()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun cancelEdit() {
|
||||
|
||||
@@ -1,14 +1,19 @@
|
||||
package com.bartlomiejpluta.base.editor.common.parameter.model
|
||||
|
||||
import javafx.beans.property.Property
|
||||
import javafx.scene.control.ComboBox
|
||||
|
||||
class EnumParameter<E : Enum<E>>(
|
||||
key: String,
|
||||
initialValue: E,
|
||||
editable: Boolean = true
|
||||
) : Parameter<E>(key, initialValue, editable) {
|
||||
override val editor = ComboBox<E>().apply {
|
||||
items.setAll(initialValue.javaClass.enumConstants.toList())
|
||||
valueProperty().bindBidirectional(valueProperty)
|
||||
editable: Boolean = true,
|
||||
autocommit: Boolean = false
|
||||
) : Parameter<E>(key, initialValue, editable, autocommit) {
|
||||
override val editor = ComboBox<E>().apply { items.setAll(initialValue.javaClass.enumConstants.toList()) }
|
||||
override val editorValueProperty: Property<E>
|
||||
get() = editor.valueProperty()
|
||||
|
||||
init {
|
||||
super.init()
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.bartlomiejpluta.base.editor.common.parameter.model
|
||||
|
||||
import javafx.beans.property.Property
|
||||
import javafx.scene.control.Spinner
|
||||
|
||||
class IntegerParameter(
|
||||
@@ -8,9 +9,13 @@ class IntegerParameter(
|
||||
minValue: Int,
|
||||
maxValue: Int,
|
||||
editable: Boolean = true,
|
||||
) : Parameter<Int>(key, initialValue, editable) {
|
||||
override val editor = Spinner<Int>(minValue, maxValue, initialValue).apply {
|
||||
isEditable = true
|
||||
valueFactory.valueProperty().bindBidirectional(valueProperty)
|
||||
autocommit: Boolean = false
|
||||
) : Parameter<Int>(key, initialValue, editable, autocommit) {
|
||||
override val editor = Spinner<Int>(minValue, maxValue, initialValue).apply { isEditable = true }
|
||||
override val editorValueProperty: Property<Int>
|
||||
get() = editor.valueFactory.valueProperty()
|
||||
|
||||
init {
|
||||
super.init()
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,12 @@ import javafx.scene.Node
|
||||
import tornadofx.getValue
|
||||
import tornadofx.setValue
|
||||
|
||||
abstract class Parameter<T>(key: String, initialValue: T? = null, editable: Boolean = true) {
|
||||
abstract class Parameter<T>(
|
||||
key: String,
|
||||
initialValue: T? = null,
|
||||
editable: Boolean = true,
|
||||
private val autocommit: Boolean = false
|
||||
) {
|
||||
val keyProperty = ReadOnlyStringWrapper(key)
|
||||
val key by keyProperty
|
||||
|
||||
@@ -15,6 +20,23 @@ abstract class Parameter<T>(key: String, initialValue: T? = null, editable: Bool
|
||||
val valueProperty: ObjectProperty<T> = SimpleObjectProperty(initialValue)
|
||||
var value by valueProperty
|
||||
|
||||
fun commit() {
|
||||
if (!autocommit) {
|
||||
value = editorValueProperty.value
|
||||
}
|
||||
}
|
||||
|
||||
protected fun init() {
|
||||
if (autocommit) {
|
||||
editorValueProperty.bindBidirectional(valueProperty)
|
||||
} else {
|
||||
editorValueProperty.value = value
|
||||
valueProperty.addListener { _, _, v -> editorValueProperty.value = v }
|
||||
}
|
||||
}
|
||||
|
||||
abstract val editorValueProperty: Property<T>
|
||||
|
||||
abstract val editor: Node
|
||||
|
||||
open val valueString: String
|
||||
|
||||
@@ -1,8 +1,20 @@
|
||||
package com.bartlomiejpluta.base.editor.common.parameter.model
|
||||
|
||||
import javafx.beans.property.Property
|
||||
import javafx.scene.control.TextField
|
||||
|
||||
class StringParameter(key: String, initialValue: String? = null, editable: Boolean = true) :
|
||||
Parameter<String>(key, initialValue, editable) {
|
||||
override val editor = TextField().apply { textProperty().bind(valueProperty) }
|
||||
class StringParameter(
|
||||
key: String,
|
||||
initialValue: String? = null,
|
||||
editable: Boolean = true,
|
||||
autocommit: Boolean = false
|
||||
) :
|
||||
Parameter<String>(key, initialValue, editable, autocommit) {
|
||||
override val editor = TextField()
|
||||
override val editorValueProperty: Property<String>
|
||||
get() = editor.textProperty()
|
||||
|
||||
init {
|
||||
super.init()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user