@ -1,7 +1,7 @@
package org.gerweck.scalafx.util
package prefs
import java.util.prefs.Preferences
import java.util.prefs._
import scalafx.application.Platform.runLater
import scalafx.beans.property.ObjectProperty
@ -10,7 +10,7 @@ import org.gerweck.scala.util.prefs._
/* TODO: take an implicit that will deteremine whether this is an `ObjectProperty` or what */
class ObservablePref [ A ] protected ( path : String ) ( implicit handler : Pref.AccessHandler [ A ] , prefs : Preferences )
extends Pref [ A ] ( path ) {
extends Pref [ A ] ( path ) { thisPref =>
lazy val observe : ObjectProperty [ A ] = {
val initialValue : A = this ( )
@ -23,23 +23,31 @@ class ObservablePref[A] protected (path: String)(implicit handler: Pref.AccessHa
* in a bidirectional bridge . */
/* Preferences => Property bridge */
prefs . addPreferenceChangeListener { pce =>
if ( pce . getKey == path ) {
runLater {
val currentValue = this ( )
if ( property . value != currentValue ) {
property . value = currentValue
/* In Scala 2.12, the callback could just be bare inside `addPreferenceChangeListener`.
* However , it must be created explicitly since we cross - compile against Scala 2.11 . */
val changeListener = new PreferenceChangeListener {
def preferenceChange ( pce : PreferenceChangeEvent ) : Unit = {
if ( pce . getKey == path ) {
runLater {
val currentValue = thisPref ( )
if ( property . value != currentValue ) {
property . value = currentValue
}
}
}
}
}
prefs . addPreferenceChangeListener ( changeListener )
/* Property => Preferences bridge */
property . onChange { ( _ , _ , newV ) =>
if ( newV != this ( ) ) {
this ( ) = newV
}
( )
}
/* Return the bridged property */
property
}
}