All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.jetbrains.datalore.vis.svg.SvgEventPeer.kt Maven / Gradle / Ivy

/*
 * Copyright (c) 2019. 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.vis.svg

import jetbrains.datalore.base.event.Event
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.observable.property.PropertyChangeEvent
import jetbrains.datalore.base.observable.property.ReadableProperty
import jetbrains.datalore.base.registration.Registration
import jetbrains.datalore.vis.svg.event.SvgEventHandler
import jetbrains.datalore.vis.svg.event.SvgEventSpec

internal class SvgEventPeer {
    private var myEventHandlers: MutableMap>>? = null
    private var myListeners: Listeners>>>? = null

    fun handlersSet(): ReadableProperty> {
        return object : ReadableProperty> {
            override val propExpr: String
                get() = "$this.handlersProp"

            override fun get(): Set {
                return handlersKeySet()
            }

            override fun addHandler(handler: EventHandler>>): Registration {
                if (myListeners == null) {
                    myListeners = Listeners()
                }
                val addReg = myListeners!!.add(handler)
                return object : Registration() {
                    override fun doRemove() {
                        addReg.remove()
                        if (myListeners!!.isEmpty) {
                            myListeners = null
                        }
                    }
                }
            }
        }
    }

    private fun handlersKeySet(): Set {
        return if (myEventHandlers == null) emptySet() else myEventHandlers!!.keys
    }

    fun  addEventHandler(spec: SvgEventSpec, handler: SvgEventHandler): Registration {
        if (myEventHandlers == null) {
            myEventHandlers = HashMap()
        }
        val eventHandlers = myEventHandlers!!
        if (!eventHandlers.containsKey(spec)) {
            eventHandlers[spec] = Listeners()
        }

        val oldHandlersSet = eventHandlers.keys

        val specListeners = eventHandlers[spec]!!
        val addReg = specListeners.add(handler)
        val disposeReg = object : Registration() {
            override fun doRemove() {
                addReg.remove()
                if (specListeners.isEmpty) {
                    eventHandlers.remove(spec)
                }
            }
        }

        myListeners?.fire(object : ListenerCaller>>> {
            override fun call(l: EventHandler>>) {
                l.onEvent(PropertyChangeEvent(oldHandlersSet, handlersKeySet()))
            }
        })

        return disposeReg
    }

    fun  dispatch(spec: SvgEventSpec, event: EventT, target: SvgNode) {
        if (myEventHandlers != null && myEventHandlers!!.containsKey(spec)) {
            myEventHandlers!![spec]!!.fire(object : ListenerCaller> {
                override fun call(l: SvgEventHandler<*>) {
                    if (event.isConsumed) return
                    @Suppress("UNCHECKED_CAST")
                    val svgEventHandler = l as SvgEventHandler
                    svgEventHandler.handle(target, event)
                }
            })
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy