package org.nield.demo.app import javafx.beans.property.SimpleStringProperty import javafx.collections.FXCollections import javafx.collections.ObservableList import rx.javafx.kt.actionEvents import rx.javafx.kt.addTo import rx.javafx.kt.onChangedObservable import rx.javafx.sources.CompositeObservable import rx.lang.kotlin.toObservable import tornadofx.* class MyApp: App(MainView::class) class MainView : View() { val personList: PersonList by inject() val anotherPersonList: AnotherPersonList by inject() override val root = hbox { this += personList this += anotherPersonList } } class PersonList : View() { val ctrl: PersonController by inject() override val root = vbox { val table = tableview(ctrl.persons) { column("First Name", Person::firstNameProperty) column("Last Name", Person::lastNameProperty) //broadcast selections selectionModel.selectedIndices.onChangedObservable() .addTo(ctrl.selectedLeft) columnResizePolicy = SmartResize.POLICY } button("SYNC").actionEvents() .flatMap { ctrl.selectedRight.toObservable() .take(1) .flatMap { it.toObservable() } }.subscribe { table.selectionModel.select(it) } } } class AnotherPersonList : View() { val ctrl: PersonController by inject() override val root = vbox { val table = tableview(ctrl.newPersons) { column("First Name", Person::firstNameProperty) column("Last Name", Person::lastNameProperty) //broadcast selections selectionModel.selectedIndices.onChangedObservable() .addTo(ctrl.selectedRight) columnResizePolicy = SmartResize.POLICY } button("SYNC").actionEvents() .flatMap { ctrl.selectedLeft.toObservable() .take(1) .flatMap { it.toObservable() } }.subscribe { table.selectionModel.select(it) } } } class Person(firstName: String = "", lastName: String = "") { val firstNameProperty = SimpleStringProperty(firstName) var firstName by firstNameProperty val lastNameProperty = SimpleStringProperty(lastName) var lastName by lastNameProperty } class PersonController : Controller(){ val selectedLeft = CompositeObservable> { it.replay(1).autoConnect().apply { subscribe() } } val selectedRight = CompositeObservable> { it.replay(1).autoConnect().apply { subscribe() } } val persons = FXCollections.observableArrayList() val newPersons = FXCollections.observableArrayList() init { persons += Person("Dead", "Stark") persons += Person("Tyrion", "Lannister") persons += Person("Arya", "Stark") persons += Person("Daenerys", "Targaryen") newPersons += Person("Ned", "Stark") newPersons += Person("Tyrion", "Janitor") newPersons += Person("Arya", "Stark") newPersons += Person("Taenerys", "Dargaryen") } }