changeset 9943:286fab2d6958

8088147: [SWT] FXCanvas: implement custom cursors Summary: Augmented implementation of SWTCursors to handle image cursors properly. Added unit test and manual test. Reviewed-by: azvegint Contributed-by: alexander@nyssen.org
author kcr
date Wed, 27 Jul 2016 13:04:23 -0700
parents 1156622f2b9f
children e22e0a5cd61e
files build.gradle gradle.properties.template modules/graphics/src/main/java/com/sun/javafx/application/PlatformImpl.java modules/graphics/src/main/java/javafx/scene/Scene.java modules/swt/src/main/java/javafx/embed/swt/SWTCursors.java modules/swt/src/test/java/test/javafx/embed/swt/SWTCursorsTest.java modules/swt/src/test/resources/test/javafx/embed/swt/cursor.png tests/manual/swt/SWTImageCursorTest.java tests/manual/swt/cursor.png
diffstat 9 files changed, 257 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/build.gradle	Tue Jul 26 12:03:52 2016 +1200
+++ b/build.gradle	Wed Jul 27 13:04:23 2016 -0700
@@ -425,6 +425,10 @@
 defineProperty("AWT_TEST", "true")
 ext.IS_AWT_TEST = Boolean.parseBoolean(AWT_TEST);
 
+// Specifies whether to run system tests that depend on SWT (only used when FULL_TEST is also enabled)
+defineProperty("SWT_TEST", "true")
+ext.IS_SWT_TEST = Boolean.parseBoolean(SWT_TEST);
+
 // Specifies whether to run unstable tests (true) - tests that don't run well with Hudson builds
 // These tests should be protected with :
 //    assumeTrue(Boolean.getBoolean("unstable.test"));
@@ -1850,6 +1854,22 @@
             }
         }
     }
+
+    test {
+        if (IS_JIGSAW_TEST) {
+            enabled = false // FIXME: JIGSAW -- support this with modules
+            logger.info("JIGSAW Testing disabled for swt")
+        } else {
+            enabled = IS_FULL_TEST && IS_SWT_TEST
+            if (IS_MAC) {
+                enabled = false
+                logger.info("SWT tests are disabled on MAC, because Gradle test runner does not handle -XstartOnFirstThread properly (https://issues.gradle.org/browse/GRADLE-3290).")
+            }
+            if (IS_LINUX) {
+                logger.info("SWT tests may cause 'Gtk-CRITICAL **: IA__gtk_main_quit: assertion 'main_loops != NULL' failed' LINUX (https://bugs.eclipse.org/bugs/show_bug.cgi?id=435066). This happens on shutdown, the tests are not affected.")
+            }
+        }
+    }
 }
 
 project(":fxml") {
--- a/gradle.properties.template	Tue Jul 26 12:03:52 2016 +1200
+++ b/gradle.properties.template	Wed Jul 27 13:04:23 2016 -0700
@@ -115,6 +115,11 @@
 
 #AWT_TEST = false
 
+# Specifies whether to run system tests that depend on SWT.
+# This flag is ignored if FULL_TEST is false.
+
+#SWT_TEST = false
+
 # Specifies whether or not the results of the packager tests should be
 # retained.  If not they will be automatically deleted.
 
--- a/modules/graphics/src/main/java/com/sun/javafx/application/PlatformImpl.java	Tue Jul 26 12:03:52 2016 +1200
+++ b/modules/graphics/src/main/java/com/sun/javafx/application/PlatformImpl.java	Wed Jul 27 13:04:23 2016 -0700
@@ -290,7 +290,8 @@
             "com.sun.glass.ui",
             "com.sun.javafx.cursor",
             "com.sun.javafx.embed",
-            "com.sun.javafx.stage"
+            "com.sun.javafx.stage",
+            "com.sun.javafx.tk"
         };
 
         if (DEBUG) {
--- a/modules/graphics/src/main/java/javafx/scene/Scene.java	Tue Jul 26 12:03:52 2016 +1200
+++ b/modules/graphics/src/main/java/javafx/scene/Scene.java	Wed Jul 27 13:04:23 2016 -0700
@@ -2439,6 +2439,7 @@
 
             if (isDirty(DirtyBits.CURSOR_DIRTY)) {
                 mouseHandler.updateCursor(getCursor());
+                mouseHandler.updateCursorFrame();
             }
 
             clearDirty();
--- a/modules/swt/src/main/java/javafx/embed/swt/SWTCursors.java	Tue Jul 26 12:03:52 2016 +1200
+++ b/modules/swt/src/main/java/javafx/embed/swt/SWTCursors.java	Wed Jul 27 13:04:23 2016 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,8 +25,11 @@
 
 package javafx.embed.swt;
 
+import com.sun.javafx.tk.Toolkit;
+import javafx.scene.image.Image;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.graphics.Cursor;
+import org.eclipse.swt.graphics.ImageData;
 import org.eclipse.swt.widgets.Display;
 
 import com.sun.javafx.cursor.CursorFrame;
@@ -39,31 +42,21 @@
  */
 class SWTCursors {
 
-    private static Cursor createCustomCursor(ImageCursorFrame cursorFrame) {
-        /*
-        Toolkit awtToolkit = Toolkit.getDefaultToolkit();
-
-        double imageWidth = cursorFrame.getWidth();
-        double imageHeight = cursorFrame.getHeight();
-        Dimension nativeSize = awtToolkit.getBestCursorSize((int)imageWidth, (int)imageHeight);
-
-        double scaledHotspotX = cursorFrame.getHotspotX() * nativeSize.getWidth() / imageWidth;
-        double scaledHotspotY = cursorFrame.getHotspotY() * nativeSize.getHeight() / imageHeight;
-        Point hotspot = new Point((int)scaledHotspotX, (int)scaledHotspotY);
-
-        final com.sun.javafx.tk.Toolkit fxToolkit =
-                com.sun.javafx.tk.Toolkit.getToolkit();
-        BufferedImage awtImage =
-                (BufferedImage) fxToolkit.toExternalImage(
-                                              cursorFrame.getPlatformImage(),
-                                              BufferedImage.class);
-
-        return awtToolkit.createCustomCursor(awtImage, hotspot, null);
-        */
-        return null;
+    private static Cursor createCustomCursor(Display display, ImageCursorFrame cursorFrame) {
+        // custom cursor, convert image
+        Image image = Toolkit.getImageAccessor().fromPlatformImage(cursorFrame.getPlatformImage());
+        ImageData imageData = SWTFXUtils.fromFXImage(image, null);
+        return new org.eclipse.swt.graphics.Cursor(
+                display, imageData, (int) cursorFrame.getHotspotX(), (int) cursorFrame.getHotspotY());
     }
 
     static Cursor embedCursorToCursor(CursorFrame cursorFrame) {
+        Display display = Display.getCurrent();
+
+        if (display == null) {
+            return  null;
+        }
+
         int id = SWT.CURSOR_ARROW;
         switch (cursorFrame.getCursorType()) {
             case DEFAULT:   id = SWT.CURSOR_ARROW; break;
@@ -88,12 +81,11 @@
             case H_RESIZE:  id = SWT.CURSOR_SIZEWE; break;
             case V_RESIZE:  id = SWT.CURSOR_SIZENS; break;
             case NONE:
+                // TODO: check if SWT.CURSOR_NO would be more appropriate here
                 return null;
             case IMAGE:
-                // RT-27939: custom cursors are not implemented
-                // return createCustomCursor((ImageCursorFrame) cursorFrame);
+                return createCustomCursor(display, (ImageCursorFrame) cursorFrame);
         }
-        Display display = Display.getCurrent();
-        return display != null ? display.getSystemCursor(id) : null;
+        return display.getSystemCursor(id);
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/swt/src/test/java/test/javafx/embed/swt/SWTCursorsTest.java	Wed Jul 27 13:04:23 2016 -0700
@@ -0,0 +1,115 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package test.javafx.embed.swt;
+
+import javafx.embed.swt.FXCanvas;
+import javafx.scene.Group;
+import javafx.scene.ImageCursor;
+import javafx.scene.Scene;
+import javafx.scene.image.Image;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
+import org.junit.Test;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicReference;
+
+import static org.junit.Assert.assertNotNull;
+
+public class SWTCursorsTest {
+
+    @Test(timeout=10000)
+    public void testImageCursor() throws Throwable {
+        // create display on first thread (required when using SWT on Mac OS X)
+        final Display display = new Display();
+        final Shell shell = new Shell(display);
+        final FXCanvas canvas = new FXCanvas(shell, SWT.NONE);
+        shell.open();
+
+        // keep track of exceptions thrown in UI thread
+        final AtomicReference<Throwable> throwableRef = new AtomicReference<>();
+
+        final CountDownLatch latch = new CountDownLatch(2);
+        display.asyncExec(() -> {
+            try {
+                // create and hook scene
+                Scene scene = new Scene(new Group());
+                canvas.setScene(scene);
+
+                // set image cursor to scene
+                Image cursorImage = new Image("test/javafx/embed/swt/cursor.png");
+                scene.setCursor(new ImageCursor(cursorImage));
+
+            } catch (Throwable throwable) {
+                throwableRef.set(throwable);
+            } finally {
+                latch.countDown();
+            }
+        });
+
+        while (latch.getCount() > 1) {
+            // run SWT event loop
+            if (!display.readAndDispatch()) {
+                display.sleep();
+            }
+        }
+
+        rethrow(throwableRef);
+
+        // ensure at least one pulse has passed before asserting the cursor is set,
+        // as the scene property synchronization is triggered by the pulse listener
+        display.asyncExec(() -> {
+            try {
+                assertNotNull(canvas.getCursor());
+            } catch (Throwable throwable) {
+                throwableRef.set(throwable);
+            } finally {
+                latch.countDown();
+            }
+        });
+
+        while (latch.getCount() > 0) {
+            // run SWT event loop (again)
+            if (!display.readAndDispatch()) {
+                display.sleep();
+            }
+        }
+
+        shell.close();
+        shell.dispose();
+        display.dispose();
+
+        rethrow(throwableRef);
+    }
+
+    private void rethrow(AtomicReference<Throwable> throwableRef) throws Throwable {
+        Throwable thrown = throwableRef.get();
+        if (thrown != null) {
+            throw thrown;
+        }
+    }
+}
Binary file modules/swt/src/test/resources/test/javafx/embed/swt/cursor.png has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/manual/swt/SWTImageCursorTest.java	Wed Jul 27 13:04:23 2016 -0700
@@ -0,0 +1,95 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import javafx.embed.swt.FXCanvas;
+import javafx.event.EventHandler;
+import javafx.scene.Group;
+import javafx.scene.ImageCursor;
+import javafx.scene.Scene;
+import javafx.scene.control.TextArea;
+import javafx.scene.image.Image;
+import javafx.scene.input.MouseEvent;
+import javafx.scene.paint.Color;
+import javafx.scene.shape.Rectangle;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.layout.FillLayout;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
+
+public class SWTImageCursorTest {
+
+    static final String instructions =
+            "This tests that an image cursor applied to a scene embedded within an FXCanvas is properly transferred to SWT. " +
+                    "This test passes if the cursor changes to the provided cursor.png image when entering the rectangle, and back to the default cursor when leaving it again.";
+
+    private static TextArea createInfo(String msg) {
+        TextArea t = new TextArea(msg);
+        t.setWrapText(true);
+        t.setEditable(false);
+        t.setMaxWidth(400);
+        t.setMaxHeight(200);
+        return t;
+    }
+
+    public static void main(String[] args) {
+        final Display display = new Display();
+        final Shell shell = new Shell(display);
+        shell.setText("SWTImageCursorTest");
+        shell.setSize(400, 200);
+        shell.setLayout(new FillLayout());
+        final FXCanvas canvas = new FXCanvas(shell, SWT.NONE);
+        shell.open();
+
+        // create and hook scene
+        Group root = new Group();
+
+        TextArea info = createInfo(instructions);
+        root.getChildren().add(info);
+
+        Rectangle rect = new Rectangle(100, 100, 100, 50);
+        rect.setStroke(Color.BLACK);
+        rect.setFill(Color.WHITE);
+        root.getChildren().add(rect);
+
+        final Scene scene = new Scene(root, 200, 200);
+        rect.setOnMouseEntered(mouseEvent -> {
+            Image cursorImage = new Image("cursor.png");
+            scene.setCursor(new ImageCursor(cursorImage));
+        });
+        rect.setOnMouseExited(mouseEvent -> {
+           scene.setCursor(null);
+        });
+        canvas.setScene(scene);
+        canvas.pack();
+
+        while (!shell.isDisposed()) {
+            // run SWT event loop
+            if (!display.readAndDispatch()) {
+                display.sleep();
+            }
+        }
+        display.dispose();
+    }
+}
Binary file tests/manual/swt/cursor.png has changed