commonMain.jetbrains.datalore.base.observable.property.ValueProperty.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lets-plot-common Show documentation
Show all versions of lets-plot-common Show documentation
Lets-Plot JVM package without rendering part
/*
* Copyright (c) 2020. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
package jetbrains.datalore.base.observable.property
import jetbrains.datalore.base.observable.event.EventHandler
import jetbrains.datalore.base.observable.event.ListenerCaller
import jetbrains.datalore.base.observable.event.Listeners
import jetbrains.datalore.base.registration.Registration
/**
* A simple implementation of Read/Write property which stores the value in a field
*/
open class ValueProperty(private var myValue: ValueT) :
BaseReadableProperty(),
Property {
private var myHandlers: Listeners>>? = null
override val propExpr: String
get() = "valueProperty()"
override fun get(): ValueT {
return myValue
}
override fun set(value: ValueT) {
if (value == myValue) return
val oldValue = myValue
myValue = value
fireEvents(oldValue, myValue)
}
private fun fireEvents(oldValue: ValueT, newValue: ValueT) {
if (myHandlers != null) {
val event =
PropertyChangeEvent(oldValue, newValue)
myHandlers!!.fire(object : ListenerCaller>> {
override fun call(l: EventHandler>) {
l.onEvent(event)
}
})
}
}
override fun addHandler(handler: EventHandler>): Registration {
if (myHandlers == null) {
myHandlers = object : Listeners>>() {
override fun afterLastRemoved() {
myHandlers = null
}
}
}
return myHandlers!!.add(handler)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy