[Editor] Create Parameters Table Fragment
This commit is contained in:
@@ -0,0 +1,48 @@
|
|||||||
|
package com.bartlomiejpluta.base.editor.common.parameter.component
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.editor.common.parameter.model.Parameter
|
||||||
|
import javafx.scene.control.TableCell
|
||||||
|
|
||||||
|
class ParameterValueEditingCell : TableCell<Parameter<*>, Any>() {
|
||||||
|
|
||||||
|
override fun updateItem(item: Any?, empty: Boolean) {
|
||||||
|
super.updateItem(item, empty)
|
||||||
|
|
||||||
|
when {
|
||||||
|
empty || item == null -> {
|
||||||
|
text = null
|
||||||
|
graphic = null
|
||||||
|
}
|
||||||
|
|
||||||
|
isEditing -> {
|
||||||
|
text = null
|
||||||
|
graphic = tableView.items[index].editor
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
text = tableView.items[index].valueString
|
||||||
|
graphic = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun startEdit() {
|
||||||
|
if (index < 0 || index >= tableView.items.size) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!tableView.items[index].editable) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
super.startEdit()
|
||||||
|
text = null
|
||||||
|
graphic = tableView.items[index].editor
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun cancelEdit() {
|
||||||
|
super.cancelEdit()
|
||||||
|
text = tableView.items[index]?.valueString
|
||||||
|
graphic = null
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.bartlomiejpluta.base.editor.common.parameter.model
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.bartlomiejpluta.base.editor.common.parameter.model
|
||||||
|
|
||||||
|
import javafx.scene.control.Spinner
|
||||||
|
|
||||||
|
class IntegerParameter(
|
||||||
|
key: String,
|
||||||
|
initialValue: Int,
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.bartlomiejpluta.base.editor.common.parameter.model
|
||||||
|
|
||||||
|
import javafx.beans.property.*
|
||||||
|
import javafx.scene.Node
|
||||||
|
import tornadofx.getValue
|
||||||
|
import tornadofx.setValue
|
||||||
|
|
||||||
|
abstract class Parameter<T>(key: String, initialValue: T? = null, editable: Boolean = true) {
|
||||||
|
val keyProperty = ReadOnlyStringWrapper(key)
|
||||||
|
val key by keyProperty
|
||||||
|
|
||||||
|
val editableProperty: BooleanProperty = SimpleBooleanProperty(editable)
|
||||||
|
var editable by editableProperty
|
||||||
|
|
||||||
|
val valueProperty: ObjectProperty<T> = SimpleObjectProperty(initialValue)
|
||||||
|
var value by valueProperty
|
||||||
|
|
||||||
|
abstract val editor: Node
|
||||||
|
|
||||||
|
open val valueString: String
|
||||||
|
get() = value.toString()
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.bartlomiejpluta.base.editor.common.parameter.model
|
||||||
|
|
||||||
|
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) }
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.bartlomiejpluta.base.editor.common.parameter.view
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.editor.common.parameter.component.ParameterValueEditingCell
|
||||||
|
import com.bartlomiejpluta.base.editor.common.parameter.model.Parameter
|
||||||
|
import javafx.beans.value.ObservableValue
|
||||||
|
import javafx.collections.ObservableList
|
||||||
|
import javafx.scene.control.TableColumn
|
||||||
|
import javafx.scene.control.TableView
|
||||||
|
import tornadofx.*
|
||||||
|
|
||||||
|
class ParametersTableFragment : Fragment() {
|
||||||
|
val parameters: ObservableList<Parameter<*>> by param()
|
||||||
|
|
||||||
|
override val root = tableview(parameters) {
|
||||||
|
isEditable = true
|
||||||
|
columnResizePolicy = TableView.CONSTRAINED_RESIZE_POLICY
|
||||||
|
|
||||||
|
column("Key", Parameter<*>::keyProperty)
|
||||||
|
TableColumn<Parameter<*>, Any>("Value").apply {
|
||||||
|
setCellValueFactory { it.value.valueProperty as ObservableValue<Any> }
|
||||||
|
setCellFactory { ParameterValueEditingCell() }
|
||||||
|
}.let { addColumnInternal(it) }
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user