Add observeFiltered methods on ObserableBuffer

These are convenience methods for constructing a `FilteredBuffer` with
either a static or dynamic filter.
This commit is contained in:
Sarah Gerweck 2016-06-12 08:11:24 -07:00
parent 99ed345be5
commit c525330b08
2 changed files with 35 additions and 13 deletions

View File

@ -220,6 +220,7 @@ object Dependencies {
final val commonsIoVersion = "2.4" final val commonsIoVersion = "2.4"
final val spireVersion = "0.11.0" final val spireVersion = "0.11.0"
final val groovyVersion = "2.4.4" final val groovyVersion = "2.4.4"
final val scalaJava8Version = "0.7.0"
final val scalaParserVersion = "1.0.4" final val scalaParserVersion = "1.0.4"
final val scalaXmlVersion = "1.0.5" final val scalaXmlVersion = "1.0.5"
final val gerweckUtilVersion = "2.0.0" final val gerweckUtilVersion = "2.0.0"
@ -237,6 +238,7 @@ object Dependencies {
val groovy = "org.codehaus.groovy" % "groovy-all" % groovyVersion val groovy = "org.codehaus.groovy" % "groovy-all" % groovyVersion
val gerweckUtil = "org.gerweck.scala" %% "gerweck-utils" % gerweckUtilVersion val gerweckUtil = "org.gerweck.scala" %% "gerweck-utils" % gerweckUtilVersion
val gerweckUtilAkka = "org.gerweck.scala" %% "gerweck-utils-akka" % gerweckUtilVersion val gerweckUtilAkka = "org.gerweck.scala" %% "gerweck-utils-akka" % gerweckUtilVersion
val scalaJava8 = "org.scala-lang.modules" %% "scala-java8-compat" % scalaJava8Version
val scalaz = "org.scalaz" %% "scalaz-core" % scalazVersion val scalaz = "org.scalaz" %% "scalaz-core" % scalazVersion
val shapeless = "com.chuusai" %% "shapeless" % shapelessVersion val shapeless = "com.chuusai" %% "shapeless" % shapelessVersion
val scallop = "org.rogach" %% "scallop" % scallopVersion val scallop = "org.rogach" %% "scallop" % scallopVersion
@ -325,6 +327,7 @@ object UtilsBuild extends Build {
log4jBridge % "runtime,optional", log4jBridge % "runtime,optional",
logback % "runtime,optional", logback % "runtime,optional",
gerweckUtil, gerweckUtil,
scalaJava8,
scalaFx, scalaFx,
scalaz, scalaz,
shapeless shapeless

View File

@ -2,8 +2,14 @@ package org.gerweck.scalafx.util
import language.implicitConversions import language.implicitConversions
import scala.compat.java8.FunctionConverters._
import java.util.function.{ Predicate => JPredicate }
import scalafx.beans.property._ import scalafx.beans.property._
import scalafx.beans.value._
import scalafx.collections._ import scalafx.collections._
import scalafx.collections.transformation._
sealed trait ToFlatObservable[-A, +B] extends Calculable[A, B] sealed trait ToFlatObservable[-A, +B] extends Calculable[A, B]
object ToFlatObservable extends CalculableObservable[ToFlatObservable[_, _]] { object ToFlatObservable extends CalculableObservable[ToFlatObservable[_, _]] {
@ -39,6 +45,19 @@ sealed trait RichObservableSeqLike[A] extends Any {
final class RichObservableBuffer[A](val obs: ObservableBuffer[A]) extends AnyVal with RichObservableSeqLike[A] { final class RichObservableBuffer[A](val obs: ObservableBuffer[A]) extends AnyVal with RichObservableSeqLike[A] {
def observableSeqValue: ReadOnlyObjectProperty[Seq[A]] = ToFlatObservable.toObservable(obs) def observableSeqValue: ReadOnlyObjectProperty[Seq[A]] = ToFlatObservable.toObservable(obs)
def observableSize = ObservableSized.toObservable(obs) def observableSize = ObservableSized.toObservable(obs)
def observeFiltered(predicate: A => Boolean) = {
new transformation.FilteredBuffer(obs, predicate)
}
/* The dummy implicit is here to ensure the `observeFiltered` methods all have different post-erasure types */
def observeFiltered[B >: A](predicate: ObservableValue[JPredicate[B], JPredicate[B]])(implicit dummy: DummyImplicit): FilteredBuffer[A] = {
val fb = new transformation.FilteredBuffer(obs)
fb.predicate <== predicate
fb
}
def observeFiltered[B >: A](predicate: Observable[B => Boolean]): FilteredBuffer[A] = {
observeFiltered(predicate.map[JPredicate[A]](asJavaPredicate))
}
} }
final class RichObservableArray[A, B <: ObservableArray[A, B, C], C <: javafx.collections.ObservableArray[C]](val oaa: ObservableArray[A, B, C]) extends AnyVal with RichObservableSeqLike[A] { final class RichObservableArray[A, B <: ObservableArray[A, B, C], C <: javafx.collections.ObservableArray[C]](val oaa: ObservableArray[A, B, C]) extends AnyVal with RichObservableSeqLike[A] {