Skip to content

Commit

Permalink
Allow overriding of Swing component painting behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
ileasile committed Mar 21, 2023
1 parent 91281b2 commit a3da4dc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
5 changes: 3 additions & 2 deletions jvm-package/jvm-publish-batik/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ task jarLetsPlotJvmBatikClasses(type: ShadowJar) {
exclude 'svgMapper/**/*'

// Fix for "META-INF/services files specify xalan" https://github.com/JetBrains/lets-plot/issues/576
// Get rid of "services" altogether.
exclude 'META-INF/services/**/*'
// Get rid only of harmful services.
exclude 'META-INF/services/org.apache.*'
exclude 'META-INF/services/javax.*'
}

task jarLetsPlotJvmBatikSources(type: ShadowJar) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2023. 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.swing

import org.apache.batik.gvt.GraphicsNode
import java.awt.Graphics2D
import java.util.*

interface BatikGraphicsNodeRenderer {
val priority: Int
fun paint(node: GraphicsNode, g: Graphics2D)

object Default : BatikGraphicsNodeRenderer {
override val priority: Int
get() = 0

override fun paint(node: GraphicsNode, g: Graphics2D) {
node.paint(g)
}
}

companion object {
fun getInstance(): BatikGraphicsNodeRenderer {
val serviceClass = BatikGraphicsNodeRenderer::class.java
val loader = ServiceLoader.load(serviceClass, serviceClass.classLoader)
return loader.maxByOrNull { it.priority } ?: Default
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class BatikMapperComponentHelper private constructor(
private val myMapper: SvgRootDocumentMapper
private val myUserAgent: UserAgent
private val myBridgeContext: BridgeContext
private val myRenderer: BatikGraphicsNodeRenderer

val preferredSize: Dimension
get() {
Expand Down Expand Up @@ -75,6 +76,8 @@ class BatikMapperComponentHelper private constructor(
myGraphicsNode = builder.build(myBridgeContext, myMapper.target)

myUserAgent.eventDispatcher.rootNode = myGraphicsNode

myRenderer = BatikGraphicsNodeRenderer.getInstance()
}

internal fun addSvgNodeContainerListener(l: SvgNodeContainerListener) {
Expand All @@ -99,7 +102,7 @@ class BatikMapperComponentHelper private constructor(
}

fun paint(g: Graphics2D) {
myGraphicsNode.paint(g)
myRenderer.paint(myGraphicsNode, g)
}

// fun handleMouseEvent(e: MouseEvent) {
Expand Down

0 comments on commit a3da4dc

Please sign in to comment.