Skip to content

Commit

Permalink
JavaFX bindings: added property.fx() extension, deprecated bind(Bidir…
Browse files Browse the repository at this point in the history
…ectionally)To methods
  • Loading branch information
Miha-x64 committed Jul 29, 2018
1 parent 7820751 commit be9fe05
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 44 deletions.
64 changes: 64 additions & 0 deletions fx-bindings/src/main/kotlin/net/aquadc/properties/fx/bind.kt
@@ -0,0 +1,64 @@
package net.aquadc.properties.fx

import javafx.beans.property.SimpleObjectProperty
import javafx.beans.value.ObservableValue
import net.aquadc.properties.MutableProperty
import net.aquadc.properties.Property
import javafx.beans.property.Property as FxProperty

/**
* Binds [this] and [that] property values.
* [this] takes [that] value first.
*/
@Deprecated("use prop.fx()", replaceWith = ReplaceWith("this.bindBidirectional(that.fx())"))
fun <T> FxProperty<T>.bindBidirectionally(that: MutableProperty<T>) {
this.value = that.value
var mutatingThis = false
var mutatingThat = false

this.addListener { _, _, new ->
if (!mutatingThis) {
mutatingThat = true
that.value = new
mutatingThat = false
}
}

that.addChangeListener { _, new ->
if (!mutatingThat) {
mutatingThis = true
this.value = new
mutatingThis = false
}
}
}

/**
* Binds [this] property value to [that].
*/
@Deprecated("use prop.fx()", replaceWith = ReplaceWith("this.bind(that.fx())"))
fun <T> FxProperty<in T>.bindTo(that: Property<T>) {
this.value = that.value
that.addChangeListener { _, new ->
this.value = new
}
}

/**
* Returns [FxProperty] view on this [MutableProperty].
*/
fun <T> MutableProperty<T>.fx(): FxProperty<T> {
val prop = SimpleObjectProperty<T>()
prop.bindBidirectionally(this)
return prop
}

/**
* Returns [ObservableValue] view on this [Property].
*/
fun <T> Property<T>.fx(): ObservableValue<T> {
val prop = SimpleObjectProperty<T>()
prop.bindTo(this)
return prop
}

This file was deleted.

14 changes: 0 additions & 14 deletions fx-bindings/src/main/kotlin/net/aquadc/properties/fx/bindTo.kt

This file was deleted.

0 comments on commit be9fe05

Please sign in to comment.