< prev index next >

src/java.desktop/share/classes/java/awt/Container.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 1995, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1995, 2015, 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

@@ -39,10 +39,11 @@
 import java.io.ObjectOutputStream;
 import java.io.ObjectStreamField;
 import java.io.PrintStream;
 import java.io.PrintWriter;
 
+import java.lang.ref.WeakReference;
 import java.security.AccessController;
 
 import java.util.EventListener;
 import java.util.HashSet;
 import java.util.Set;

@@ -3319,20 +3320,10 @@
         if (cont == this || isParentOf(cont)) {
             kfm.setGlobalCurrentFocusCycleRootPriv(null);
         }
     }
 
-    @Override
-    void clearLightweightDispatcherOnRemove(Component removedComponent) {
-        if (dispatcher != null) {
-            dispatcher.removeReferences(removedComponent);
-        } else {
-            //It is a Lightweight Container, should clear parent`s Dispatcher
-            super.clearLightweightDispatcherOnRemove(removedComponent);
-        }
-    }
-
     final Container getTraversalRoot() {
         if (isFocusCycleRoot()) {
             return findTraversalRoot();
         }
 

@@ -4429,24 +4420,26 @@
 
     private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.LightweightDispatcher");
 
     LightweightDispatcher(Container nativeContainer) {
         this.nativeContainer = nativeContainer;
-        mouseEventTarget = null;
+        mouseEventTarget = new WeakReference<>(null);
+        targetLastEntered = new WeakReference<>(null);
+        targetLastEnteredDT = new WeakReference<>(null);
         eventMask = 0;
     }
 
     /*
      * Clean up any resources allocated when dispatcher was created;
      * should be called from Container.removeNotify
      */
     void dispose() {
         //System.out.println("Disposing lw dispatcher");
         stopListeningForOtherDrags();
-        mouseEventTarget = null;
-        targetLastEntered = null;
-        targetLastEnteredDT = null;
+        mouseEventTarget.clear();
+        targetLastEntered.clear();
+        targetLastEnteredDT.clear();
     }
 
     /**
      * Enables events to subcomponents.
      */

@@ -4529,45 +4522,46 @@
             nativeContainer.getMouseEventTarget(e.getX(), e.getY(),
                                                 Container.INCLUDE_SELF);
 
         trackMouseEnterExit(mouseOver, e);
 
+        Component met = mouseEventTarget.get();
     // 4508327 : MOUSE_CLICKED should only go to the recipient of
     // the accompanying MOUSE_PRESSED, so don't reset mouseEventTarget on a
     // MOUSE_CLICKED.
     if (!isMouseGrab(e) && id != MouseEvent.MOUSE_CLICKED) {
-            mouseEventTarget = (mouseOver != nativeContainer) ? mouseOver: null;
-            isCleaned = false;
+            met = (mouseOver != nativeContainer) ? mouseOver : null;
+            mouseEventTarget = new WeakReference<>(met);
         }
 
-        if (mouseEventTarget != null) {
+        if (met != null) {
             switch (id) {
             case MouseEvent.MOUSE_ENTERED:
             case MouseEvent.MOUSE_EXITED:
                 break;
             case MouseEvent.MOUSE_PRESSED:
-                retargetMouseEvent(mouseEventTarget, id, e);
+                    retargetMouseEvent(met, id, e);
                 break;
         case MouseEvent.MOUSE_RELEASED:
-            retargetMouseEvent(mouseEventTarget, id, e);
+                    retargetMouseEvent(met, id, e);
         break;
         case MouseEvent.MOUSE_CLICKED:
         // 4508327: MOUSE_CLICKED should never be dispatched to a Component
         // other than that which received the MOUSE_PRESSED event.  If the
         // mouse is now over a different Component, don't dispatch the event.
         // The previous fix for a similar problem was associated with bug
         // 4155217.
-        if (mouseOver == mouseEventTarget) {
+                    if (mouseOver == met) {
             retargetMouseEvent(mouseOver, id, e);
         }
         break;
             case MouseEvent.MOUSE_MOVED:
-                retargetMouseEvent(mouseEventTarget, id, e);
+                    retargetMouseEvent(met, id, e);
                 break;
         case MouseEvent.MOUSE_DRAGGED:
             if (isMouseGrab(e)) {
-                retargetMouseEvent(mouseEventTarget, id, e);
+                        retargetMouseEvent(met, id, e);
             }
                 break;
         case MouseEvent.MOUSE_WHEEL:
             // This may send it somewhere that doesn't have MouseWheelEvents
             // enabled.  In this case, Component.dispatchEventImpl() will

@@ -4582,14 +4576,10 @@
             }
         //Consuming of wheel events is implemented in "retargetMouseEvent".
         if (id != MouseEvent.MOUSE_WHEEL) {
             e.consume();
         }
-    } else if (isCleaned && id != MouseEvent.MOUSE_WHEEL) {
-        //After mouseEventTarget was removed and cleaned should consume all events
-        //until new mouseEventTarget is found
-        e.consume();
     }
     return e.isConsumed();
     }
 
     private boolean processDropTargetEvent(SunDropTargetEvent e) {

@@ -4650,13 +4640,14 @@
         } else if (id == MouseEvent.MOUSE_ENTERED) {
             isMouseDTInNativeContainer = true;
         } else if (id == MouseEvent.MOUSE_EXITED) {
             isMouseDTInNativeContainer = false;
         }
-        targetLastEnteredDT = retargetMouseEnterExit(targetOver, e,
-                                                     targetLastEnteredDT,
+        Component tle = retargetMouseEnterExit(targetOver, e,
+                                                     targetLastEnteredDT.get(),
                                                      isMouseDTInNativeContainer);
+        targetLastEnteredDT = new WeakReference<>(tle);
     }
 
     /*
      * Generates enter/exit events as mouse moves over lw components
      * @param targetOver        Target mouse is over (including native container)

@@ -4678,13 +4669,14 @@
             startListeningForOtherDrags();
         } else if (id == MouseEvent.MOUSE_EXITED) {
             isMouseInNativeContainer = false;
             stopListeningForOtherDrags();
         }
-        targetLastEntered = retargetMouseEnterExit(targetOver, e,
-                                                   targetLastEntered,
+        Component tle = retargetMouseEnterExit(targetOver, e,
+                                                   targetLastEntered.get(),
                                                    isMouseInNativeContainer);
+        targetLastEntered = new WeakReference<>(tle);
     }
 
     private Component retargetMouseEnterExit(Component targetOver, MouseEvent e,
                                              Component lastEntered,
                                              boolean inNativeContainer) {

@@ -4942,26 +4934,21 @@
      * The current subcomponent being hosted by this windowed
      * component that has events being forwarded to it.  If this
      * is null, there are currently no events being forwarded to
      * a subcomponent.
      */
-    private transient Component mouseEventTarget;
+    private transient WeakReference<Component> mouseEventTarget;
 
     /**
      * The last component entered by the {@code MouseEvent}.
      */
-    private transient Component targetLastEntered;
+    private transient  WeakReference<Component> targetLastEntered;
 
     /**
      * The last component entered by the {@code SunDropTargetEvent}.
      */
-    private transient Component targetLastEnteredDT;
-
-    /**
-     * Indicates whether {@code mouseEventTarget} was removed and nulled
-     */
-    private transient boolean isCleaned;
+    private transient  WeakReference<Component> targetLastEnteredDT;
 
     /**
      * Is the mouse over the native container.
      */
     private transient boolean isMouseInNativeContainer = false;

@@ -4998,19 +4985,6 @@
 
     private static final long MOUSE_MASK =
         AWTEvent.MOUSE_EVENT_MASK |
         AWTEvent.MOUSE_MOTION_EVENT_MASK |
         AWTEvent.MOUSE_WHEEL_EVENT_MASK;
-
-    void removeReferences(Component removedComponent) {
-        if (mouseEventTarget == removedComponent) {
-            isCleaned = true;
-            mouseEventTarget = null;
-        }
-        if (targetLastEntered == removedComponent) {
-            targetLastEntered = null;
-        }
-        if (targetLastEnteredDT == removedComponent) {
-            targetLastEnteredDT = null;
-        }
-    }
 }
< prev index next >