From f7bd816b79517f1697dbdc0352b9c3b39b06ecee Mon Sep 17 00:00:00 2001 From: Sarah Gerweck Date: Tue, 19 Dec 2017 23:00:06 -0800 Subject: [PATCH] Fix `ObservablePref` Scala 2.11 compatibility Scala 2.11 doesn't automatically lift functions into implementations of single-method interfaces, so we have to be a bit more explicit when creating the callback object. --- .../scalafx/util/prefs/ObservablePref.scala | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/main/scala/org/gerweck/scalafx/util/prefs/ObservablePref.scala b/src/main/scala/org/gerweck/scalafx/util/prefs/ObservablePref.scala index 167517b..bf2869b 100644 --- a/src/main/scala/org/gerweck/scalafx/util/prefs/ObservablePref.scala +++ b/src/main/scala/org/gerweck/scalafx/util/prefs/ObservablePref.scala @@ -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 } }