Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JavaFX bindings: added property.fx() extension, deprecated bind(Bidir…
…ectionally)To methods
- Loading branch information
Showing
3 changed files
with
64 additions
and
44 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
fx-bindings/src/main/kotlin/net/aquadc/properties/fx/bind.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
|
30 changes: 0 additions & 30 deletions
30
fx-bindings/src/main/kotlin/net/aquadc/properties/fx/bindBidirectionally.kt
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
fx-bindings/src/main/kotlin/net/aquadc/properties/fx/bindTo.kt
This file was deleted.
Oops, something went wrong.