Created
February 20, 2020 14:48
-
-
Save andretietz/4b8308f2616e7419d3ca3871468fd651 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.beans.property.SimpleListProperty | |
import javafx.beans.property.SimpleObjectProperty | |
import javafx.collections.FXCollections | |
import javafx.collections.ObservableList | |
import javafx.scene.control.SelectionMode | |
import javafx.scene.control.TreeItem | |
import javafx.scene.text.Font | |
import tornadofx.* | |
fun main() { | |
launch<DemoApp>() | |
} | |
class DemoApp : App(CategoryMapperView::class) | |
data class DemoCategory( | |
val id: String, | |
val name: String, | |
val subcategories: List<DemoCategory>? = null | |
) | |
class CategoryMapperViewModel : ViewModel() { | |
val sourceProperty = SimpleListProperty<DemoCategory>() | |
val targetProperty = SimpleListProperty<DemoCategory>() | |
val sourceSelection = SimpleObjectProperty<DemoCategory>() | |
val targetSelection = SimpleListProperty<DemoCategory>(FXCollections.observableArrayList(listOf())) | |
init { | |
sourceSelection.onChange { | |
targetSelection.value.clear() | |
// TODO: load targetSelection | |
println("source selection changed... loading preselected target categories") | |
} | |
targetSelection.onChange { list: ObservableList<DemoCategory>? -> | |
// .. store selected if sourceSelection.value != null | |
list?.forEach { println(it) } | |
} | |
loadCategories() | |
} | |
private fun loadCategories() { | |
// load async (demo purpose) | |
sourceProperty.asyncItems { | |
listOf( | |
DemoCategory( | |
"1", "Cars", listOf( | |
DemoCategory("11", "BMW"), | |
DemoCategory("12", "Meredes"), | |
DemoCategory("13", "Ford") | |
) | |
), | |
DemoCategory( | |
"2", "Fruits", listOf( | |
DemoCategory("21", "Banana"), | |
DemoCategory("22", "Apple"), | |
DemoCategory("23", "Cherry") | |
) | |
), | |
DemoCategory( | |
"3", "Cities", listOf( | |
DemoCategory("31", "New York"), | |
DemoCategory("32", "London"), | |
DemoCategory("33", "Berlin") | |
) | |
) | |
) | |
} | |
targetProperty.asyncItems { | |
// load async (demo purpose) | |
listOf( | |
DemoCategory( | |
"1", "Cars", listOf( | |
DemoCategory("11", "BMW"), | |
DemoCategory("12", "Meredes"), | |
DemoCategory("13", "Ford") | |
) | |
), | |
DemoCategory( | |
"2", "Fruits", listOf( | |
DemoCategory("21", "Banana"), | |
DemoCategory("22", "Apple"), | |
DemoCategory("23", "Cherry") | |
) | |
), | |
DemoCategory( | |
"3", "Cities", listOf( | |
DemoCategory("31", "New York"), | |
DemoCategory("32", "London"), | |
DemoCategory("33", "Berlin") | |
) | |
) | |
) | |
} | |
} | |
} | |
class CategoryMapperView : View() { | |
private val viewmodel: CategoryMapperViewModel by inject() | |
override val root = vbox { | |
paddingAll = 8 | |
label("Categories") { font = Font.font(16.0) } | |
hbox { | |
treeview<DemoCategory> { | |
root = TreeItem(DemoCategory("", "root")) | |
isShowRoot = false | |
cellFormat { text = it.name } | |
populate { parent -> | |
when (parent) { | |
root -> viewmodel.sourceProperty | |
else -> parent.value.subcategories | |
} | |
} | |
root.isExpanded = true | |
root.children.forEach { it.isExpanded = true } | |
onUserSelect { viewmodel.sourceSelection.value = it } | |
} | |
treeview<DemoCategory> { | |
selectionModel.selectionMode = SelectionMode.MULTIPLE | |
root = TreeItem(DemoCategory("", "root")) | |
isShowRoot = false | |
cellFormat { text = "${it.name} (${it.id})" } | |
populate { parent -> | |
when (parent) { | |
root -> viewmodel.targetProperty | |
else -> parent.value.subcategories | |
} | |
} | |
root.isExpanded = true | |
root.children.forEach { it.isExpanded = true } | |
// TMP solution | |
onUserSelect { | |
viewmodel.targetSelection.value = FXCollections.observableArrayList( | |
selectionModel.selectedItems.map { it.value } | |
) | |
} | |
// TODO: HOW to bind viewmodel.targetSelection | |
// to selectionModel.selectedItems | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment