You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
package nova.monadic_sfx.implicits
import scalafx.beans.property.ObjectProperty import scalafx.beans.property.ReadOnlyObjectProperty import scalafx.beans.value.ObservableValue
/**
* experimental implicits to be incorporated better later */ trait MySfxObservableImplicits { import nova.monadic_sfx.implicits.ExtraObservableImplicits._ implicit def myRichObservable[A, C]( observable: ObservableValue[A, C] ): MyRichObservable[A, C] = new MyRichObservable(observable) }
object ExtraObservableImplicits {
final class MyRichObservable[A, C](val self: ObservableValue[A, C]) extends AnyVal { def filter(f: A => Boolean): ReadOnlyObjectProperty[A] = Methods.filter(self)(f)
/**
* Simply creates a new observable that mirrors the source observable but * doesn't emit null values. JavaFX likes to work with null values in scene * nodes/properties (shrugs) and observables by default emit null values * that can cause crashes if you forget to null check. ScalaFX does not * offer any *fixes* for this. */ def filterNull: ReadOnlyObjectProperty[A] = Methods.filterNull(self) }
} object Types { type Observable[A] = ObservableValue[A, _] } object Methods { import Types._
def filter[B]( a: Observable[B] )(f: B => Boolean): ReadOnlyObjectProperty[B] = {
val prop = ObjectProperty[B](a.value)
def changeHandler() = prop.synchronized { if (f(a.value)) { prop.value = a.value } }
a onChange changeHandler() prop }
/**
* Simply creates a new observable that mirrors the source observable but * doesn't emit null values. JavaFX likes to work with null values in scene * nodes/properties (shrugs) and observables by default emit null values * that can cause crashes if you forget to null check. ScalaFX does not * offer any *fixes* for this. * * @param a * @return */ def filterNull[B]( a: Observable[B] ): ReadOnlyObjectProperty[B] = {
val prop = ObjectProperty[B](a.value)
def changeHandler() = prop.synchronized { if (a.value != null) { prop.value = a.value } }
a onChange changeHandler() prop }
}
|