changeset 1243:c03a7b34ec86

RT-22106: Cancel and Default buttons still receive onAction events triggered by ESC and Enter keys when they are removed from the scene.
author Kinsley Wong <kinsley.wong@oracle.com>
date Wed, 06 Jun 2012 16:37:04 -0700
parents b14d29378d60
children 60301b7fd01f
files javafx-ui-controls/src/com/sun/javafx/scene/control/skin/ButtonSkin.java javafx-ui-controls/test/javafx/scene/control/ButtonTest.java
diffstat 2 files changed, 61 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/javafx-ui-controls/src/com/sun/javafx/scene/control/skin/ButtonSkin.java	Wed Jun 06 13:44:06 2012 -0700
+++ b/javafx-ui-controls/src/com/sun/javafx/scene/control/skin/ButtonSkin.java	Wed Jun 06 16:37:04 2012 -0700
@@ -81,11 +81,11 @@
                     }
                 }
             }
-        });
+        });                
     }
 
 
-    @Override protected void handleControlPropertyChanged(String p) {
+    @Override protected void handleControlPropertyChanged(String p) {        
         super.handleControlPropertyChanged(p);
         if (p == "DEFAULT_BUTTON") {
             setDefaultButton(getSkinnable().isDefaultButton());
@@ -103,6 +103,11 @@
                     }
                 }
            }
+        } else if (p == "PARENT") {
+            if (getSkinnable().getParent() == null) {
+                getScene().getAccelerators().remove(defaultAcceleratorKeyCodeCombination);
+                getScene().getAccelerators().remove(cancelAcceleratorKeyCodeCombination);
+            }
         }
     }
 
@@ -115,67 +120,68 @@
         };
 
     Runnable cancelButtonRunnable = new Runnable() {
-            public void run() {                
+            public void run() {   
                 if (!getSkinnable().isDisabled()) {
                     getSkinnable().fire();
                 }
             }
         };
 
+    private KeyCodeCombination defaultAcceleratorKeyCodeCombination;
+    
     private void setDefaultButton(boolean value) {
 
         KeyCode acceleratorCode = KeyCode.ENTER;
-        KeyCodeCombination acceleratorKeyCombo = 
+        defaultAcceleratorKeyCodeCombination = 
                 new KeyCodeCombination(acceleratorCode);
 
         if (value == false) {
             /*
             ** first check of there's a default button already
             */
-            Runnable oldDefault = getSkinnable().getParent().getScene().getAccelerators().get(acceleratorKeyCombo);
+            Runnable oldDefault = getSkinnable().getParent().getScene().getAccelerators().get(defaultAcceleratorKeyCodeCombination);
             if (!defaultButtonRunnable.equals(oldDefault)) {
                 /*
                 ** is it us?
                 */
-                getSkinnable().getParent().getScene().getAccelerators().remove(acceleratorKeyCombo);
+                getSkinnable().getParent().getScene().getAccelerators().remove(defaultAcceleratorKeyCodeCombination);
             }
         }
         else {
             /*
             ** first check of there's a default button already
             */
-            Runnable oldDefault = getSkinnable().getParent().getScene().getAccelerators().get(acceleratorKeyCombo);
+            Runnable oldDefault = getSkinnable().getParent().getScene().getAccelerators().get(defaultAcceleratorKeyCodeCombination);
         }
-        getSkinnable().getParent().getScene().getAccelerators().put(acceleratorKeyCombo, defaultButtonRunnable);
+        getSkinnable().getParent().getScene().getAccelerators().put(defaultAcceleratorKeyCodeCombination, defaultButtonRunnable);
     }
 
-
+    private KeyCodeCombination cancelAcceleratorKeyCodeCombination;
 
     private void setCancelButton(boolean value) {
-
         KeyCode acceleratorCode = KeyCode.ESCAPE;
-        KeyCodeCombination acceleratorKeyCombo =
+        cancelAcceleratorKeyCodeCombination =
                 new KeyCodeCombination(acceleratorCode);
-
+        
         if (value == false) {
             /*
             ** first check of there's a default button already
             */
-            Runnable oldDefault = getSkinnable().getParent().getScene().getAccelerators().get(acceleratorKeyCombo);
+            Runnable oldDefault = getSkinnable().getParent().getScene().getAccelerators().get(cancelAcceleratorKeyCodeCombination);
             if (!defaultButtonRunnable.equals(oldDefault)) {
                 /*
                 ** is it us?
                 */
-                getSkinnable().getParent().getScene().getAccelerators().remove(acceleratorKeyCombo);
+                getSkinnable().getParent().getScene().getAccelerators().remove(cancelAcceleratorKeyCodeCombination);
             }
         }
         else {
             /*
             ** first check of there's a default button already
             */
-            Runnable oldDefault = getSkinnable().getParent().getScene().getAccelerators().get(acceleratorKeyCombo);
-        }
-        getSkinnable().getParent().getScene().getAccelerators().put(acceleratorKeyCombo, cancelButtonRunnable);
+            Runnable oldDefault = getSkinnable().getParent().getScene().getAccelerators().get(cancelAcceleratorKeyCodeCombination);
+        }        
+        getSkinnable().getParent().getScene().getAccelerators().put(cancelAcceleratorKeyCodeCombination, cancelButtonRunnable);
     }
 
 }
--- a/javafx-ui-controls/test/javafx/scene/control/ButtonTest.java	Wed Jun 06 13:44:06 2012 -0700
+++ b/javafx-ui-controls/test/javafx/scene/control/ButtonTest.java	Wed Jun 06 16:37:04 2012 -0700
@@ -189,6 +189,25 @@
         tk.firePulse();                
     }
 
+    @Test public void defaultButtonCanBeInvokeAfterRemovingFromTheScene_RT22106() {
+        btn.setDefaultButton(true);        
+        btn.setOnAction(new EventHandler<ActionEvent>() {
+            @Override
+            public void handle(ActionEvent actionEvent) {
+                fail();
+            }
+        });
+        root.getChildren().add(btn);
+        show();
+        
+        root.getChildren().remove(btn);        
+        
+        KeyEventFirer keyboard = new KeyEventFirer(root);        
+        keyboard.doKeyPress(KeyCode.ENTER);
+
+        tk.firePulse();                      
+    }
+    
     /*********************************************************************
      * Tests for the cancelButton state                                 *
      ********************************************************************/
@@ -258,6 +277,25 @@
         assertEquals("cancelButton", btn.cancelButtonProperty().getName());
     }
 
+    @Test public void cancelButtonCanBeInvokeAfterRemovingFromTheScene_RT22106() {
+        btn.setCancelButton(true);        
+        btn.setOnAction(new EventHandler<ActionEvent>() {
+            @Override
+            public void handle(ActionEvent actionEvent) {
+                fail();
+            }
+        });
+        root.getChildren().add(btn);
+        show();
+        
+        root.getChildren().remove(btn);        
+        
+        KeyEventFirer keyboard = new KeyEventFirer(root);        
+        keyboard.doKeyPress(KeyCode.ESCAPE);
+
+        tk.firePulse();                      
+    }
+    
 //  private Button button1;
 //  private Button button2;
 //