changeset 1312:0dc775249a62

RT-20582: MenuItem's styleable has a null Node
author jgiles
date Fri, 15 Jun 2012 14:13:27 +1200
parents adb8d50d8799
children 0f2fba17c263
files javafx-ui-controls/src/com/sun/javafx/scene/control/skin/ContextMenuContent.java javafx-ui-controls/src/javafx/scene/control/MenuItem.java
diffstat 2 files changed, 41 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/javafx-ui-controls/src/com/sun/javafx/scene/control/skin/ContextMenuContent.java	Fri Jun 15 13:48:56 2012 +1200
+++ b/javafx-ui-controls/src/com/sun/javafx/scene/control/skin/ContextMenuContent.java	Fri Jun 15 14:13:27 2012 +1200
@@ -130,8 +130,8 @@
         setUpBinds();
     }
     
-       //For testing purpose only
-    VBox getItemsContainer() {
+    //For access from controls
+    public VBox getItemsContainer() {
         return itemsContainer;
     }
     //For testing purpose only
@@ -198,7 +198,7 @@
             }
         }
     }
-
+    
     private void updateVisualItems() {
         itemsContainer.getChildren().clear();
         for (int row = 0; row < getItems().size(); row++) {
@@ -981,7 +981,7 @@
      * words, this contains and lays out a single MenuItem, regardless of it's 
      * specific subtype.
      */
-    class MenuItemContainer extends Pane {
+    public class MenuItemContainer extends Pane {
 
         private final MenuItem item;
 
@@ -994,7 +994,7 @@
             return (Label) label;
         }
         
-        protected MenuItem getItem() {
+        public MenuItem getItem() {
             return item;
         }
 
--- a/javafx-ui-controls/src/javafx/scene/control/MenuItem.java	Fri Jun 15 13:48:56 2012 +1200
+++ b/javafx-ui-controls/src/javafx/scene/control/MenuItem.java	Fri Jun 15 14:13:27 2012 +1200
@@ -47,12 +47,16 @@
 import javafx.scene.input.KeyCombination;
 
 import com.sun.javafx.event.EventHandlerManager;
+import com.sun.javafx.scene.control.skin.ContextMenuContent;
+import com.sun.javafx.scene.control.skin.ContextMenuSkin;
+import com.sun.webpane.platform.ContextMenuItem;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import javafx.beans.property.ReadOnlyObjectProperty;
 import javafx.beans.property.ReadOnlyObjectWrapper;
 import javafx.collections.ObservableMap;
+import javafx.scene.Parent;
 
 /**
  * <p>
@@ -572,13 +576,16 @@
 
                 @Override
                 public Styleable getStyleableParent() {
-                    if(MenuItem.this.getParentMenu() == null) {
-                        return MenuItem.this.getParentPopup() != null 
-                            ? MenuItem.this.getParentPopup().impl_getStyleable()
+                    Menu parentMenu = MenuItem.this.getParentMenu();
+                    ContextMenu parentPopup = MenuItem.this.getParentPopup();
+                    
+                    if(parentMenu == null) {
+                        return parentPopup != null 
+                            ? parentPopup.impl_getStyleable()
                             : null;
                     } else {
-                        return MenuItem.this.getParentMenu() != null 
-                            ? MenuItem.this.getParentMenu().impl_getStyleable()
+                        return parentMenu != null 
+                            ? parentMenu.impl_getStyleable()
                             : null;
                     }
                 }
@@ -591,6 +598,30 @@
 
                 @Override
                 public Node getNode() {
+                    // Fix for RT-20582. We dive into the visual representation
+                    // of this MenuItem so that we may return it to the caller.
+                    ContextMenu parentPopup = MenuItem.this.getParentPopup();
+                    if (! (parentPopup.getSkin() instanceof ContextMenuSkin)) return null;
+                    
+                    ContextMenuSkin skin = (ContextMenuSkin) parentPopup.getSkin();
+                    if (! (skin.getNode() instanceof ContextMenuContent)) return null;
+                    
+                    ContextMenuContent content = (ContextMenuContent) skin.getNode();
+                    Parent nodes = content.getItemsContainer();
+                    
+                    MenuItem desiredMenuItem = MenuItem.this;
+                    List<Node> childrenNodes = nodes.getChildrenUnmodifiable();
+                    for (int i = 0; i < childrenNodes.size(); i++) {
+                        if (! (childrenNodes.get(i) instanceof ContextMenuContent.MenuItemContainer)) continue;
+                        
+                        ContextMenuContent.MenuItemContainer MenuRow = 
+                                (ContextMenuContent.MenuItemContainer) childrenNodes.get(i);
+                        
+                        if (desiredMenuItem.equals(MenuRow.getItem())) {
+                            return MenuRow;
+                        }
+                    }
+                    
                     return null;
                 }