changeset 5217:ac845cbafb49

RT-27540: Swing application with JFXPanel does not terminate properly RT-21404: JFXPanel: If Platform implicitExit flag is false when the last window exits, setting it back to true will have no effect Reviewed-by: Kevin Rushforth
author art
date Tue, 01 Oct 2013 19:52:35 +0400
parents 604ebe0585ea
children 0eb503d2b567
files modules/graphics/src/main/java/com/sun/javafx/application/PlatformImpl.java modules/swing/src/main/java/javafx/embed/swing/JFXPanel.java tests/system/src/test/java/com/sun/javafx/application/SwingExitCommon.java tests/system/src/test/java/com/sun/javafx/application/SwingExitExplicit2Test.java tests/system/src/test/java/com/sun/javafx/application/SwingNoExit.java
diffstat 5 files changed, 179 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/modules/graphics/src/main/java/com/sun/javafx/application/PlatformImpl.java	Tue Oct 01 15:02:03 2013 +0100
+++ b/modules/graphics/src/main/java/com/sun/javafx/application/PlatformImpl.java	Tue Oct 01 19:52:35 2013 +0400
@@ -364,15 +364,23 @@
     public static void removeListener(FinishListener l) {
         finishListeners.remove(l);
         listenersRegistered.set(!finishListeners.isEmpty());
+        if (!listenersRegistered.get()) {
+            checkIdle();
+        }
     }
 
     private static void notifyFinishListeners(boolean exitCalled) {
-        for (FinishListener l : finishListeners) {
-            if (exitCalled) {
-                l.exitCalled();
-            } else {
-                l.idle(implicitExit);
+        // Notify listeners if any are registered, else exit directly
+        if (listenersRegistered.get()) {
+            for (FinishListener l : finishListeners) {
+                if (exitCalled) {
+                    l.exitCalled();
+                } else {
+                    l.idle(implicitExit);
+                }
             }
+        } else if (implicitExit || platformExit.get()) {
+            tkExit();
         }
     }
 
@@ -450,16 +458,8 @@
     }
 
     public static void exit() {
-//        System.err.println("PlatformImpl.exit");
         platformExit.set(true);
-
-        // Notify listeners if any are registered, else exit directly
-        if (listenersRegistered.get()) {
-            notifyFinishListeners(true);
-        } else {
-//            System.err.println("Platform.exit: calling doExit directly (no listeners)");
-            tkExit();
-        }
+        notifyFinishListeners(true);
     }
 
     private static Boolean checkForClass(String classname) {
--- a/modules/swing/src/main/java/javafx/embed/swing/JFXPanel.java	Tue Oct 01 15:02:03 2013 +0100
+++ b/modules/swing/src/main/java/javafx/embed/swing/JFXPanel.java	Tue Oct 01 19:52:35 2013 +0400
@@ -123,8 +123,8 @@
  */
 public class JFXPanel extends JComponent {
 
+    private static AtomicInteger instanceCount = new AtomicInteger(0);
     private static PlatformImpl.FinishListener finishListener;
-    private static boolean firstPanelShown = false;
 
     private HostContainer hostContainer;
 
@@ -160,28 +160,32 @@
 
     private boolean isCapturingMouse = false;
 
-    // Initialize FX runtime when the JFXPanel instance is constructed
-    private synchronized static void initFx() {
-        if (finishListener != null) {
+    private synchronized void registerFinishListener() {
+        if (instanceCount.getAndIncrement() > 0) {
             // Already registered
             return;
         }
         // Need to install a finish listener to catch calls to Platform.exit
         finishListener = new PlatformImpl.FinishListener() {
             @Override public void idle(boolean implicitExit) {
-                if (!firstPanelShown) {
-                    return;
-                }
-                PlatformImpl.removeListener(finishListener);
-                finishListener = null;
-                if (implicitExit) {
-                    Platform.exit();
-                }
             }
             @Override public void exitCalled() {
             }
         };
         PlatformImpl.addListener(finishListener);
+    }
+
+    private synchronized void deregisterFinishListener() {
+        if (instanceCount.decrementAndGet() > 0) {
+            // Other JFXPanels still alive
+            return;
+        }
+        PlatformImpl.removeListener(finishListener);
+        finishListener = null;
+    }
+
+    // Initialize FX runtime when the JFXPanel instance is constructed
+    private synchronized static void initFx() {
         // Note that calling PlatformImpl.startup more than once is OK
         PlatformImpl.startup(new Runnable() {
             @Override public void run() {
@@ -280,7 +284,6 @@
             stage.setScene(newScene);
             if (!stage.isShowing()) {
                 stage.show();
-                firstPanelShown = true;
             }
         }
     }
@@ -692,8 +695,8 @@
     public void addNotify() {
         super.addNotify();
 
+        registerFinishListener();
         dnd.addNotify();
-
         AccessController.doPrivileged(new PrivilegedAction<Void>() {
             public Void run() {
                 JFXPanel.this.getToolkit().addAWTEventListener(ungrabListener,
@@ -701,15 +704,12 @@
                 return null;
             }
         });
-
         updateComponentSize(); // see RT-23603
-
         SwingFXUtils.runOnFxThread(new Runnable() {
             @Override
             public void run() {
                 if ((stage != null) && !stage.isShowing()) {
                     stage.show();
-                    firstPanelShown = true;
                     sendMoveEventToFX();
                 }
             }
@@ -748,6 +748,8 @@
 
         /* see CR 4867453 */
         getInputContext().removeNotify(this);
+
+        deregisterFinishListener();
     }
 
     private class HostContainer implements HostInterface {
--- a/tests/system/src/test/java/com/sun/javafx/application/SwingExitCommon.java	Tue Oct 01 15:02:03 2013 +0100
+++ b/tests/system/src/test/java/com/sun/javafx/application/SwingExitCommon.java	Tue Oct 01 19:52:35 2013 +0400
@@ -57,7 +57,7 @@
     private static final CountDownLatch initialized = new CountDownLatch(1);
 
     // Value of the implicit exit flag for the given test
-    private static boolean implicitExit;
+    private static volatile boolean implicitExit;
 
     private JFrame frame;
     private JFXPanel fxPanel;
@@ -91,7 +91,7 @@
         });
 
         // show frame
-        frame.setLocation(0, 0);
+        frame.setLocationRelativeTo(null);
         frame.pack();
         frame.setVisible(true);
 
--- a/tests/system/src/test/java/com/sun/javafx/application/SwingExitExplicit2Test.java	Tue Oct 01 15:02:03 2013 +0100
+++ b/tests/system/src/test/java/com/sun/javafx/application/SwingExitExplicit2Test.java	Tue Oct 01 19:52:35 2013 +0400
@@ -33,8 +33,7 @@
  */
 public class SwingExitExplicit2Test extends SwingExitCommon {
 
-    // TODO: Re-enable this test when RT-21404 is fixed
-    @Ignore @Test
+    @Test
     public void testExplicitExitReEnable() {
         doTestExplicitExitReEnable();
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/system/src/test/java/com/sun/javafx/application/SwingNoExit.java	Tue Oct 01 19:52:35 2013 +0400
@@ -0,0 +1,143 @@
+/*
+ * Copyright (c) 2012, 2013, 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 com.sun.javafx.application;
+
+import javafx.application.Platform;
+import javafx.embed.swing.JFXPanel;
+import javafx.scene.Group;
+import javafx.scene.Scene;
+import javafx.scene.paint.Color;
+import junit.framework.AssertionFailedError;
+import util.Util;
+
+import javax.swing.JFrame;
+import javax.swing.SwingUtilities;
+import java.awt.BorderLayout;
+import java.awt.Dimension;
+import java.lang.reflect.InvocationTargetException;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.junit.Test;
+import org.junit.Assert;
+
+/**
+ * Test program for Platform.exit() behavior with an embedded JFXPanel.
+ */
+public class SwingNoExit {
+
+    // Sleep time showing/hiding window in milliseconds
+    private static final int SLEEP_TIME = 1000;
+
+    private JFrame frame;
+    private JFXPanel fxPanel;
+
+    public void init() {
+        // Create Swing window
+        frame = new JFrame("JFXPanel 1");
+        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
+        frame.setLayout(new BorderLayout());
+
+        // Create javafx panel
+        fxPanel = new JFXPanel();
+        fxPanel.setPreferredSize(new Dimension(210, 180));
+        frame.getContentPane().add(fxPanel, BorderLayout.CENTER);
+
+        // Create scene and add it to the panel
+        Util.runAndWait(new Runnable() {
+            public void run() {
+                Group root = new Group();
+                Scene scene = new Scene(root);
+                scene.setFill(Color.LIGHTYELLOW);
+                fxPanel.setScene(scene);
+            }
+        });
+
+        // show frame
+        frame.setLocationRelativeTo(null);
+        frame.pack();
+        frame.setVisible(true);
+    }
+
+    @Test
+    public void doTestImplicitExit() throws Throwable {
+        final AtomicReference<Throwable> error = new AtomicReference<>(null);
+
+        final CountDownLatch initLatch = new CountDownLatch(1);
+        SwingUtilities.invokeLater(new Runnable() {
+            public void run() {
+                try {
+                    init();
+                    initLatch.countDown();
+                } catch (Throwable th) {
+                    error.set(th);
+                }
+            }
+        });
+        if (!initLatch.await(Util.TIMEOUT, TimeUnit.MILLISECONDS)) {
+            throw new AssertionFailedError("Timeout waiting for JFXPanel to launch and initialize");
+        }
+        Throwable t = error.get();
+        if (t != null) {
+            throw t;
+        }
+
+        final CountDownLatch runAndWait = new CountDownLatch(1);
+        Platform.runLater(new Runnable() {
+            public void run() {
+                Platform.exit();
+                runAndWait.countDown();
+            }
+        });
+        if (!runAndWait.await(Util.TIMEOUT, TimeUnit.MILLISECONDS)) {
+            throw new AssertionFailedError("Timeout waiting for Platform.exit()");
+        }
+
+        final CountDownLatch exitLatch = PlatformImpl.test_getPlatformExitLatch();
+        Thread.sleep(SLEEP_TIME);
+        // Platform.exit() should not cause FX to exit, while JFXPanel is alive
+        Assert.assertEquals("Platform.exit() caused FX to exit, while JFXPanel is alive",
+                            1, exitLatch.getCount());
+
+        try {
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    frame.setVisible(false);
+                    frame.dispose();
+                }
+            });
+        }
+        catch (InvocationTargetException ex) {
+            throw new AssertionFailedError("Exception while disposing JFrame");
+        }
+
+        Thread.sleep(SLEEP_TIME);
+        // JFXPanel is gone, implicit exit is false, so FX should have exited now
+        Assert.assertEquals("FX is not exited, when the last JFXPanel is disposed",
+                            0, exitLatch.getCount());
+    }
+}