Skip to content

Commit

Permalink
Merge pull request edvin#1338 from yamert89/master
Browse files Browse the repository at this point in the history
optional converter in extensions: useComboBox() and useChoiceBox()
  • Loading branch information
edvin authored Jul 19, 2021
2 parents 62e8a1b + c6ae18a commit 5d9efdd
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/main/java/tornadofx/ItemControls.kt
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,11 @@ fun <S, T> TreeTableView<S>.column(title: String, getter: KFunction<T>): TreeTab
return this.column(title, propName)
}

fun <S, T> TableColumn<S, T?>.useComboBox(items: ObservableList<T>, afterCommit: (TableColumn.CellEditEvent<S, T?>) -> Unit = {}) = apply {
cellFactory = ComboBoxTableCell.forTableColumn(items)
fun <S, T> TableColumn<S, T?>.useComboBox(
items: ObservableList<T>,
converter: StringConverter<T>? = null,
afterCommit: (TableColumn.CellEditEvent<S, T?>) -> Unit = {}) = apply {
cellFactory = if (converter == null) ComboBoxTableCell.forTableColumn(items) else ComboBoxTableCell.forTableColumn(converter, items)
setOnEditCommit {
val property = it.tableColumn.getCellObservableValue(it.rowValue) as Property<T?>
property.value = it.newValue
Expand Down Expand Up @@ -440,8 +443,11 @@ inline fun <S, reified T> TableColumn<S, T?>.useTextField(
}
}

fun <S, T> TableColumn<S, T?>.useChoiceBox(items: ObservableList<T>, afterCommit: (TableColumn.CellEditEvent<S, T?>) -> Unit = {}) = apply {
cellFactory = ChoiceBoxTableCell.forTableColumn(items)
fun <S, T> TableColumn<S, T?>.useChoiceBox(
items: ObservableList<T>,
converter: StringConverter<T>? = null,
afterCommit: (TableColumn.CellEditEvent<S, T?>) -> Unit = {}) = apply {
cellFactory = if (converter == null) ChoiceBoxTableCell.forTableColumn(items) else ChoiceBoxTableCell.forTableColumn(converter, items)
setOnEditCommit {
val property = it.tableColumn.getCellObservableValue(it.rowValue) as Property<T?>
property.value = it.newValue
Expand Down
45 changes: 45 additions & 0 deletions src/test/kotlin/tornadofx/testapps/UseComboBox.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package tornadofx.testapps

import javafx.beans.property.SimpleIntegerProperty
import javafx.util.StringConverter
import tornadofx.*

class UseComboBox: App(UseComboBoxView::class)

class UseComboBoxView: View() {
override val root = anchorpane{
val data = listOf(DataItem(1), DataItem(2), DataItem(3)).asObservable()
tableview(data) {
isEditable = true
column("1", DataItem::a).useComboBox(listOf(1, 2, 3).asObservable(), IntegerConverter())
column("2", DataItem::a).useChoiceBox(listOf(1, 2, 3).asObservable(), IntegerConverter())
column("3", DataItem::a).useComboBox(listOf(1, 2, 3).asObservable())
}
}
}

class DataItem(a: Int){
private val aProperty = SimpleIntegerProperty(a)
var a: Int? by aProperty
}

class IntegerConverter: StringConverter<Int>() {
override fun toString(`object`: Int): String {
return when(`object`){
1 -> "first"
2 -> "second"
3 -> "third"
else -> `object`.toString()
}

}

override fun fromString(string: String): Int {
return when(string){
"first" -> 1
"second" -> 2
"third" -> 3
else -> throw IllegalArgumentException("unsupported value")
}
}
}

0 comments on commit 5d9efdd

Please sign in to comment.