changeset 1235:819d61e7effb

fix RT-20285 MemoryLeak in javafx.scene.controls.MenuBar
author Paru Somashekar <paru.somashekar@oracle.com>
date Tue, 05 Jun 2012 23:39:15 -0700
parents bbf040d2b20e
children a447a52d7abd
files javafx-ui-controls/src/com/sun/javafx/scene/control/skin/MenuBarSkin.java
diffstat 1 files changed, 22 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/javafx-ui-controls/src/com/sun/javafx/scene/control/skin/MenuBarSkin.java	Tue Jun 05 16:54:58 2012 -0700
+++ b/javafx-ui-controls/src/com/sun/javafx/scene/control/skin/MenuBarSkin.java	Tue Jun 05 23:39:15 2012 -0700
@@ -54,9 +54,11 @@
 import javafx.scene.input.MouseEvent;
 import javafx.scene.layout.HBox;
 import javafx.stage.Stage;
+import javafx.beans.value.WeakChangeListener;
 
 import com.sun.javafx.menu.MenuBase;
 import com.sun.javafx.scene.control.GlobalMenuAdapter;
+import com.sun.javafx.scene.control.WeakEventHandler;
 import com.sun.javafx.scene.control.behavior.BehaviorBase;
 import com.sun.javafx.scene.traversal.Direction;
 import com.sun.javafx.scene.traversal.TraversalEngine;
@@ -143,7 +145,10 @@
         });
     }
 
-
+    private WeakEventHandler weakSceneKeyEventHandler;
+    private WeakEventHandler weakSceneMouseEventHandler;
+    private EventHandler keyEventHandler;
+    private EventHandler mouseEventHandler;
 
     /***************************************************************************
      *                                                                         *
@@ -156,8 +161,9 @@
         
         container = new HBox();
         getChildren().add(container);
-     
-        control.getScene().addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
+        
+        // Key navigation 
+        keyEventHandler = new EventHandler<KeyEvent>() {
             @Override public void handle(KeyEvent event) {
                 // process right left and may be tab key events
                 if (openMenu != null) {
@@ -212,19 +218,25 @@
                     }
                 }
             }
-        });
+        };
+        weakSceneKeyEventHandler = new WeakEventHandler(control.getScene(), KeyEvent.KEY_PRESSED, 
+                keyEventHandler);
+        control.getScene().addEventFilter(KeyEvent.KEY_PRESSED, weakSceneKeyEventHandler);
+        
         // When we click else where in the scene - menu selection should be cleared.
-        control.getScene().addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
-            @Override
-            public void handle(MouseEvent t) {
+        mouseEventHandler = new EventHandler<MouseEvent>() {
+            @Override public void handle(MouseEvent t) {
                 if (!container.localToScene(container.getLayoutBounds()).contains(t.getX(), t.getY())) {
                     unSelectMenus();
                     firstF10 = true;
                 }
             }
-        });
+        };
+        weakSceneMouseEventHandler = new WeakEventHandler(control.getScene(), MouseEvent.MOUSE_CLICKED, 
+                mouseEventHandler);
+        
         // When the parent window looses focus - menu selection should be cleared
-        control.getScene().getWindow().focusedProperty().addListener(new ChangeListener<Boolean>() {
+        control.getScene().getWindow().focusedProperty().addListener(new WeakChangeListener(new ChangeListener<Boolean>() {
             @Override
             public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
               if (!t1) {
@@ -232,7 +244,7 @@
                   firstF10 = true;
               }
             }
-        });
+        }));
         
         rebuildUI();
         control.getMenus().addListener(new ListChangeListener<Menu>() {