I have the following code to define a tableview with specific cells changing color depending on condition.
How I can define a typesafe stylesheet class and apply it to current code preventing the duplication in future?
class MonitorView : View("Monitor") {
override val root = borderpane {
center = tableview<ServerModel> {
items = readConfiguration().observable()
readonlyColumn("Environment", ServerModel::env)
readonlyColumn("Host", ServerModel::host)
readonlyColumn("Port", ServerModel::port)
readonlyColumn("Application Name", ServerModel::appName)
column("Is online", ServerModel::isReachable).cellFormat {
styleDependingOnBoolean(it)
graphic = hbox {
button("Restart").action {
isDisable = true
}
}
}
}
}
private fun TableCell<ServerModel, Boolean>.styleDependingOnBoolean(it: Boolean) {
if (it) {
style = "-fx-background-color:#00b200; -fx-text-fill:white"
text = it.toString()
} else {
style = "-fx-background-color:#8b0000; -fx-text-fill:white"
text = it.toString()
}
}
}