changeset 6076:2f9b188da440

RT-35182: [Accelerators] Changing accelerator dynamically does not use the new accelerator
author jgiles
date Mon, 13 Jan 2014 14:45:46 +1300
parents dace65c4c5d7
children e0b13bbc1f8d
files modules/controls/src/main/java/com/sun/javafx/scene/control/skin/ContextMenuContent.java modules/controls/src/main/java/com/sun/javafx/scene/control/skin/MenuButtonSkinBase.java
diffstat 2 files changed, 30 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/ContextMenuContent.java	Mon Jan 13 14:11:27 2014 +1300
+++ b/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/ContextMenuContent.java	Mon Jan 13 14:45:46 2014 +1300
@@ -1305,6 +1305,10 @@
         
         private void updateAccelerator() {
             if (item.getAccelerator() != null) {
+                if (right != null) {
+                    getChildren().remove(right);
+                }
+
                 String text = KeystrokeUtils.toString(item.getAccelerator());
                 right = new Label(text);
                 right.setStyle(item.getStyle());
--- a/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/MenuButtonSkinBase.java	Mon Jan 13 14:11:27 2014 +1300
+++ b/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/MenuButtonSkinBase.java	Mon Jan 13 14:45:46 2014 +1300
@@ -38,11 +38,14 @@
 import javafx.scene.control.MenuButton;
 import javafx.scene.control.MenuItem;
 import javafx.scene.control.RadioMenuItem;
+import javafx.scene.input.KeyCombination;
 import javafx.scene.input.MouseEvent;
 import javafx.scene.layout.Region;
 import javafx.scene.layout.StackPane;
 import com.sun.javafx.scene.control.behavior.MenuButtonBehaviorBase;
 
+import java.util.Map;
+
 /**
  * Base class for MenuButtonSkin and SplitMenuButtonSkin. It consists of the
  * label, the arrowButton with its arrow shape, and the popup.
@@ -66,6 +69,18 @@
     protected boolean behaveLikeButton = false;
     private ListChangeListener<MenuItem> itemsChangedListener;
 
+    private final ChangeListener<KeyCombination> acceleratorListener = new ChangeListener<KeyCombination>() {
+        @Override public void changed(ObservableValue<? extends KeyCombination> observable, KeyCombination oldValue, KeyCombination newValue) {
+            Map<KeyCombination, Runnable> accelerators = getSkinnable().getScene().getAccelerators();
+            if (accelerators != null) {
+                Runnable acceleratorRunnable = accelerators.remove(oldValue);
+                accelerators.put(newValue, acceleratorRunnable);
+            }
+        }
+    };
+
+
+
     /***************************************************************************
      *                                                                         *
      * Constructors                                                            *
@@ -130,13 +145,12 @@
             addAccelerators(getSkinnable().getItems());
         }
         control.sceneProperty().addListener(new ChangeListener<Scene>() {
-                @Override
-                    public void changed(ObservableValue<? extends Scene> scene, Scene oldValue, Scene newValue) {
-                    if (getSkinnable() != null && getSkinnable().getScene() != null) {
-                        addAccelerators(getSkinnable().getItems());
-                    }
+            @Override public void changed(ObservableValue<? extends Scene> scene, Scene oldValue, Scene newValue) {
+                if (getSkinnable() != null && getSkinnable().getScene() != null) {
+                    addAccelerators(getSkinnable().getItems());
                 }
-            });
+            }
+        });
 
 //        If setOnAction() is overridden the code below causes the popup to show and hide.
 //        control.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {
@@ -298,8 +312,8 @@
                 ** check is there are any accelerators in this menuitem
                 */
                 if (menuitem.getAccelerator() != null) {
-                    if (getSkinnable().getScene().getAccelerators() != null) {
-
+                    Map<KeyCombination, Runnable> accelerators = getSkinnable().getScene().getAccelerators();
+                    if (accelerators != null) {
                         Runnable acceleratorRunnable = new Runnable() {
                             public void run() {
                                 if (menuitem.getOnMenuValidation() != null) {
@@ -321,16 +335,16 @@
                                 }
                             }
                         };
-                        getSkinnable().getScene().getAccelerators().put(menuitem.getAccelerator(), acceleratorRunnable);
+                        accelerators.put(menuitem.getAccelerator(), acceleratorRunnable);
+                        menuitem.acceleratorProperty().addListener(acceleratorListener);
                     }
                 }
             }
         }
     }
-   
-    
+
+
     private class MenuLabeledImpl extends LabeledImpl {
-
         MenuButton button;
         public MenuLabeledImpl(MenuButton b) {
             super(b);