8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
authorazvegint
Thu, 31 Aug 2017 09:28:21 +0530
changeset 47192 d46b91465a81
parent 47191 8c95ea7056b7
child 47193 b12ec990a6b1
8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true Reviewed-by: kcr
jdk/src/java.desktop/share/classes/java/awt/EventDispatchThread.java
jdk/src/java.desktop/share/classes/java/awt/EventQueue.java
--- a/jdk/src/java.desktop/share/classes/java/awt/EventDispatchThread.java	Tue Aug 01 14:18:14 2017 +0800
+++ b/jdk/src/java.desktop/share/classes/java/awt/EventDispatchThread.java	Thu Aug 31 09:28:21 2017 +0530
@@ -161,6 +161,23 @@
         }
     }
 
+    boolean filterAndCheckEvent(AWTEvent event) {
+        boolean eventOK = true;
+        synchronized (eventFilters) {
+            for (int i = eventFilters.size() - 1; i >= 0; i--) {
+                EventFilter f = eventFilters.get(i);
+                EventFilter.FilterAction accept = f.acceptEvent(event);
+                if (accept == EventFilter.FilterAction.REJECT) {
+                    eventOK = false;
+                    break;
+                } else if (accept == EventFilter.FilterAction.ACCEPT_IMMEDIATELY) {
+                    break;
+                }
+            }
+        }
+        return eventOK && SunDragSourceContextPeer.checkEvent(event);
+    }
+
     void pumpOneEventForFilters(int id) {
         AWTEvent event = null;
         boolean eventOK = false;
@@ -172,20 +189,7 @@
 
                 event = (id == ANY_EVENT) ? eq.getNextEvent() : eq.getNextEvent(id);
 
-                eventOK = true;
-                synchronized (eventFilters) {
-                    for (int i = eventFilters.size() - 1; i >= 0; i--) {
-                        EventFilter f = eventFilters.get(i);
-                        EventFilter.FilterAction accept = f.acceptEvent(event);
-                        if (accept == EventFilter.FilterAction.REJECT) {
-                            eventOK = false;
-                            break;
-                        } else if (accept == EventFilter.FilterAction.ACCEPT_IMMEDIATELY) {
-                            break;
-                        }
-                    }
-                }
-                eventOK = eventOK && SunDragSourceContextPeer.checkEvent(event);
+                eventOK = filterAndCheckEvent(event);
                 if (!eventOK) {
                     event.consume();
                 }
--- a/jdk/src/java.desktop/share/classes/java/awt/EventQueue.java	Tue Aug 01 14:18:14 2017 +0800
+++ b/jdk/src/java.desktop/share/classes/java/awt/EventQueue.java	Thu Aug 31 09:28:21 2017 +0530
@@ -719,7 +719,9 @@
                     fwDispatcher.scheduleDispatch(new Runnable() {
                         @Override
                         public void run() {
-                            dispatchEventImpl(event, src);
+                            if (dispatchThread.filterAndCheckEvent(event)) {
+                                dispatchEventImpl(event, src);
+                            }
                         }
                     });
                 }
@@ -1008,6 +1010,32 @@
         return createSecondaryLoop(null, null, 0);
     }
 
+    private class FwSecondaryLoopWrapper implements SecondaryLoop {
+        final private SecondaryLoop loop;
+        final private EventFilter filter;
+
+        public FwSecondaryLoopWrapper(SecondaryLoop loop, EventFilter filter) {
+            this.loop = loop;
+            this.filter = filter;
+        }
+
+        @Override
+        public boolean enter() {
+            if (filter != null) {
+                dispatchThread.addEventFilter(filter);
+            }
+            return loop.enter();
+        }
+
+        @Override
+        public boolean exit() {
+            if (filter != null) {
+                dispatchThread.removeEventFilter(filter);
+            }
+            return loop.exit();
+        }
+    }
+
     SecondaryLoop createSecondaryLoop(Conditional cond, EventFilter filter, long interval) {
         pushPopLock.lock();
         try {
@@ -1016,7 +1044,7 @@
                 return nextQueue.createSecondaryLoop(cond, filter, interval);
             }
             if (fwDispatcher != null) {
-                return fwDispatcher.createSecondaryLoop();
+                return new FwSecondaryLoopWrapper(fwDispatcher.createSecondaryLoop(), filter);
             }
             if (dispatchThread == null) {
                 initDispatchThread();