From a3cd34af29c52bd6ba4d98615bd14d0a772cb2ed Mon Sep 17 00:00:00 2001
From: Ioannis Kappas <ioannis.kappas@gmamil.com>
Date: Sat, 25 Jun 2022 11:23:57 +0100
Subject: [PATCH v6 2/6] New java-openjfx-swing package

---
 gnu/local.mk                                  |  1 +
 gnu/packages/java.scm                         | 31 +++++++++-
 ...va-openjfx-build-swing-JFXPanel-JDK8.patch | 60 +++++++++++++++++++
 3 files changed, 91 insertions(+), 1 deletion(-)
 create mode 100644 gnu/packages/patches/java-openjfx-build-swing-JFXPanel-JDK8.patch

diff --git a/gnu/local.mk b/gnu/local.mk
index 576c1a3eb6..fd3b992f33 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1320,6 +1320,7 @@ dist_patch_DATA =						\
   %D%/packages/patches/java-commons-lang-fix-dependency.patch \
   %D%/packages/patches/java-jeromq-fix-tests.patch		\
   %D%/packages/patches/java-openjfx-build-jdk_version.patch     \
+  %D%/packages/patches/java-openjfx-build-swing-JFXPanel-JDK8.patch     \
   %D%/packages/patches/java-powermock-fix-java-files.patch		\
   %D%/packages/patches/java-simple-xml-fix-tests.patch		\
   %D%/packages/patches/java-svg-salamander-Fix-non-det.patch	\
diff --git a/gnu/packages/java.scm b/gnu/packages/java.scm
index 40b88640a3..5f5e2cd00e 100644
--- a/gnu/packages/java.scm
+++ b/gnu/packages/java.scm
@@ -2524,7 +2524,8 @@ (define-public java-openjfx-build
               (sha256
                (base32
                 "0yg38mwpivswccv9n96k06x3iv82i4px1a9xg9l8dswzwmfj259f"))
-              (patches (search-patches "java-openjfx-build-jdk_version.patch"))))
+              (patches (search-patches "java-openjfx-build-jdk_version.patch"
+                                       "java-openjfx-build-swing-JFXPanel-JDK8.patch"))))
     (build-system ant-build-system)
     (arguments
      `(#:jar-name "java-openjfx.jar"
@@ -3272,6 +3273,34 @@ (class (match:suffix (regexp-exec rx class))))
 applications.  This package contains graphics-related classes for the
 OpenJFX distribution.")))
 
+(define-public java-openjfx-swing
+  (package (inherit java-openjfx-build)
+    (name "java-openjfx-swing")
+    (arguments
+     `(#:jar-name "java-openjfx-swing.jar"
+       #:source-dir "modules/swing/src/main/java"
+       #:test-dir "modules/swing/src/test"
+       #:test-include (list "**/*Test.*")
+       #:modules ((guix build ant-build-system)
+                  (guix build utils)
+                  (ice-9 regex))
+       #:phases
+       (modify-phases %standard-phases
+         (add-before 'check 'x-test-setup
+           (lambda _
+             (system "Xvfb :33 &")
+             (setenv "DISPLAY" ":33"))))))
+    (native-inputs
+     `(("java-junit" ,java-junit)
+       ("xorg-server" ,xorg-server-for-tests)))
+    (propagated-inputs
+     `(("java-openjfx-graphics" ,java-openjfx-graphics)))
+    (description "OpenJFX is a client application platform for desktop,
+mobile and embedded systems built on Java.  Its goal is to produce a modern,
+efficient, and fully featured toolkit for developing rich client applications.
+This package contains a set of classes to use JavaFX inside Swing
+applications.")))
+
 (define-public java-openjfx-media
   (package (inherit java-openjfx-build)
     (name "java-openjfx-media")
diff --git a/gnu/packages/patches/java-openjfx-build-swing-JFXPanel-JDK8.patch b/gnu/packages/patches/java-openjfx-build-swing-JFXPanel-JDK8.patch
new file mode 100644
index 0000000000..b540048621
--- /dev/null
+++ b/gnu/packages/patches/java-openjfx-build-swing-JFXPanel-JDK8.patch
@@ -0,0 +1,60 @@
+Subject: [PATCH] java-openjfx-build: Cannot compile JFXPanel with JDK 8
+
+In JDKu9 getDefaultScale method was removed and sun.java2d.SurfaceData class now
+has separate getDefaultScaleX() and getDefaultScaleY() methods. See
+https://bugs.openjdk.java.net/browse/JDK-8073320 for more info.
+
+Backport the missing port to build openjfx-swing package using JDKu8. For more
+info please see https://bugs.openjdk.java.net/browse/JDK-8149967
+
+---
+ .../java/javafx/embed/swing/JFXPanel.java     | 20 ++++++++++++++++++-
+ 1 file changed, 19 insertions(+), 1 deletion(-)
+
+diff --git a/modules/swing/src/main/java/javafx/embed/swing/JFXPanel.java b/modules/swing/src/main/java/javafx/embed/swing/JFXPanel.java
+index 97515e2290..614ed656b9 100644
+--- a/modules/swing/src/main/java/javafx/embed/swing/JFXPanel.java
++++ b/modules/swing/src/main/java/javafx/embed/swing/JFXPanel.java
+@@ -81,6 +81,7 @@ import sun.awt.AppContext;
+ import sun.awt.CausedFocusEvent;
+ import sun.awt.SunToolkit;
+ import sun.java2d.SunGraphics2D;
++import sun.java2d.SurfaceData;
+ import sun.util.logging.PlatformLogger;
+ import sun.util.logging.PlatformLogger.Level;
+
+@@ -681,6 +682,23 @@
+                 e.getCaret().getInsertionIndex());
+     }
+
++    // FIXME: once we move to JDK 9 as the boot JDK we should remove the
++    // reflection code from this method, consider changing it to
++    // use double rather than int, and account for the possibility of
++    // a different scale factor in X and Y.
++    private int getDefaultScale(SurfaceData surfaceData) {
++        /*
++          double scale = surfaceData.getDefaultScaleX();
++        */
++        double scale = 1;
++        try {
++            Method meth = SurfaceData.class.getMethod("getDefaultScaleX");
++            scale = (Double)meth.invoke(surfaceData);
++        } catch (Exception ex) {
++        }
++
++        return (int)Math.round(scale);
++    }
+
+     /**
+      * Overrides the {@link javax.swing.JComponent#paintComponent(Graphics)}
+@@ -720,7 +738,7 @@ public class JFXPanel extends JComponent {
+
+             int newScaleFactor = scaleFactor;
+             if (g instanceof SunGraphics2D) {
+-                newScaleFactor = ((SunGraphics2D)g).surfaceData.getDefaultScale();
++                newScaleFactor = getDefaultScale(((SunGraphics2D)g).surfaceData);
+             }
+             if (scaleFactor != newScaleFactor) {
+                 resizePixelBuffer(newScaleFactor);
+--
+2.24.1
-- 
2.34.0

