diff --git a/plot-config/src/jvmMain/kotlin/plot/AwtContainerDisposer.kt b/plot-config/src/jvmMain/kotlin/plot/AwtContainerDisposer.kt new file mode 100644 index 00000000000..1652db33c6d --- /dev/null +++ b/plot-config/src/jvmMain/kotlin/plot/AwtContainerDisposer.kt @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2021. 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.plot + +import jetbrains.datalore.base.registration.Disposable +import java.awt.Container + +class AwtContainerDisposer( + private val container: Container +) : Disposable { + private var isDisposed: Boolean = false + + override fun dispose() { + require(!isDisposed) { "Already disposed." } + isDisposed = true + container.components.forEach { + // We expect all children are disposable + (it as Disposable).dispose() + } + } +} \ No newline at end of file diff --git a/plot-config/src/jvmMain/kotlin/plot/AwtLiveMapFactoryUtil.kt b/plot-config/src/jvmMain/kotlin/plot/AwtLiveMapFactoryUtil.kt index d960ed62e51..0573abf9785 100644 --- a/plot-config/src/jvmMain/kotlin/plot/AwtLiveMapFactoryUtil.kt +++ b/plot-config/src/jvmMain/kotlin/plot/AwtLiveMapFactoryUtil.kt @@ -27,7 +27,7 @@ internal object AwtLiveMapFactoryUtil { preferredSize: ReadableProperty, svgComponentFactory: (svg: SvgSvgElement) -> JComponent, executor: (() -> Unit) -> Unit - ): DisposableJPanel { + ): JComponent { val cursorServiceConfig = CursorServiceConfig() injectLiveMapProvider(assembler, processedSpec, cursorServiceConfig) diff --git a/plot-config/src/jvmMain/kotlin/plot/AwtLiveMapPanel.kt b/plot-config/src/jvmMain/kotlin/plot/AwtLiveMapPanel.kt index 3ed8d9089a1..055fc137a69 100644 --- a/plot-config/src/jvmMain/kotlin/plot/AwtLiveMapPanel.kt +++ b/plot-config/src/jvmMain/kotlin/plot/AwtLiveMapPanel.kt @@ -23,16 +23,19 @@ import java.awt.event.ComponentEvent import java.awt.event.MouseAdapter import java.awt.event.MouseEvent import javax.swing.JComponent +import javax.swing.JLayeredPane import javax.swing.JPanel + class AwtLiveMapPanel( private val plotContainer: PlotContainer, private val plotComponent: JComponent, private val executor: (() -> Unit) -> Unit, private val cursorServiceConfig: CursorServiceConfig -) : DisposableJPanel(null) { +) : JLayeredPane(), Disposable { + private val awtContainerDisposer = AwtContainerDisposer(this) private val mappers: MutableList<() -> Unit> = ArrayList() - private val livemaps: MutableList = ArrayList() + private val registrations: MutableList = ArrayList() private val mouseMoutionListener = object : MouseAdapter() { override fun mouseDragged(e: MouseEvent) { super.mouseDragged(e) @@ -93,7 +96,7 @@ class AwtLiveMapPanel( timer ).let { mappers.add { - livemaps.add(canvasFigure.mapToCanvas(it)) + registrations.add(canvasFigure.mapToCanvas(it)) } } } @@ -111,8 +114,8 @@ class AwtLiveMapPanel( } override fun dispose() { - super.dispose() - livemaps.forEach(Disposable::dispose) + awtContainerDisposer.dispose() + registrations.forEach(Disposable::dispose) plotComponent.removeMouseMotionListener(mouseMoutionListener) plotContainer.clearContent() cursorServiceConfig.defaultSetter { } diff --git a/plot-config/src/jvmMain/kotlin/plot/DisposableJPanel.kt b/plot-config/src/jvmMain/kotlin/plot/DisposableJPanel.kt index 39e9bb1e154..08be1af9b25 100644 --- a/plot-config/src/jvmMain/kotlin/plot/DisposableJPanel.kt +++ b/plot-config/src/jvmMain/kotlin/plot/DisposableJPanel.kt @@ -10,13 +10,9 @@ import java.awt.LayoutManager import javax.swing.JPanel open class DisposableJPanel(layout: LayoutManager?) : JPanel(layout), Disposable { - private var isDisposed: Boolean = false + private val disposer = AwtContainerDisposer(this) + override fun dispose() { - require(!isDisposed) { "Already disposed." } - isDisposed = true - components.forEach { - // We a expect all children are disposable - (it as Disposable).dispose() - } + disposer.dispose() } } \ No newline at end of file diff --git a/vis-canvas/src/jvmMain/kotlin/vis/canvas/awt/AwtRepaintTimer.kt b/vis-canvas/src/jvmMain/kotlin/vis/canvas/awt/AwtRepaintTimer.kt index 14c290f1ec2..f9629176c44 100644 --- a/vis-canvas/src/jvmMain/kotlin/vis/canvas/awt/AwtRepaintTimer.kt +++ b/vis-canvas/src/jvmMain/kotlin/vis/canvas/awt/AwtRepaintTimer.kt @@ -17,16 +17,13 @@ class AwtRepaintTimer( ) : Disposable { private val myHandlers = ArrayList<(Long) -> Unit>() - init { - myHandlers.add { repaint() } - } - private var actionListener = ActionListener { myHandlers.forEach { executor { it(System.currentTimeMillis()) } } + repaint() } private val myTimer: Timer = Timer(1000 / 60, actionListener) @@ -54,6 +51,7 @@ class AwtRepaintTimer( override fun dispose() { myTimer.stop() + myTimer.removeActionListener(actionListener) actionListener = ActionListener {} myHandlers.clear() }