8133039: Provide public API to sun.swing.UIAction#isEnabled(Object)
authoralexsch
Tue, 22 Dec 2015 05:34:49 +0400
changeset 35650 15f1f0ff7790
parent 35649 57120e827092
child 35651 42255542ef29
8133039: Provide public API to sun.swing.UIAction#isEnabled(Object) Reviewed-by: serb, azvegint
jdk/src/java.desktop/share/classes/javax/swing/Action.java
jdk/src/java.desktop/share/classes/javax/swing/SwingUtilities.java
jdk/src/java.desktop/share/classes/javax/swing/TransferHandler.java
jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicButtonListener.java
jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicComboBoxUI.java
jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicDesktopPaneUI.java
jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicInternalFrameUI.java
jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicListUI.java
jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicMenuUI.java
jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicRootPaneUI.java
jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTableHeaderUI.java
jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTableUI.java
jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTreeUI.java
jdk/src/java.desktop/share/classes/sun/swing/UIAction.java
jdk/test/javax/swing/Action/8133039/bug8133039.java
--- a/jdk/src/java.desktop/share/classes/javax/swing/Action.java	Fri Dec 18 17:02:42 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/javax/swing/Action.java	Tue Dec 22 05:34:49 2015 +0400
@@ -356,25 +356,42 @@
     public void putValue(String key, Object value);
 
     /**
-     * Sets the enabled state of the <code>Action</code>.  When enabled,
+     * Sets the enabled state of the {@code Action}.  When enabled,
      * any component associated with this object is active and
-     * able to fire this object's <code>actionPerformed</code> method.
-     * If the value has changed, a <code>PropertyChangeEvent</code> is sent
+     * able to fire this object's {@code actionPerformed} method.
+     * If the value has changed, a {@code PropertyChangeEvent} is sent
      * to listeners.
      *
-     * @param  b true to enable this <code>Action</code>, false to disable it
+     * @param  b true to enable this {@code Action}, false to disable it
+     * @see #accept
      */
     public void setEnabled(boolean b);
     /**
-     * Returns the enabled state of the <code>Action</code>. When enabled,
+     * Returns the enabled state of the {@code Action}. When enabled,
      * any component associated with this object is active and
-     * able to fire this object's <code>actionPerformed</code> method.
+     * able to fire this object's {@code actionPerformed} method.
      *
-     * @return true if this <code>Action</code> is enabled
+     * @return true if this {@code Action} is enabled
+     * @see #accept
      */
     public boolean isEnabled();
 
     /**
+     * Determines whether the action should be performed with the specified
+     * sender object. The {@code sender} can be {@code null}.
+     * The method must return false if the action is disabled.
+     * <p>
+     * @param sender the object to check, can be null
+     * @return {@code true} if the action should be performed with the sender
+     *         object, must be false if the action is disabled.
+     * @see isEnabled
+     * @see setEnabled
+     */
+    default boolean accept(Object sender) {
+        return isEnabled();
+    }
+
+    /**
      * Adds a <code>PropertyChange</code> listener. Containers and attached
      * components use these methods to register interest in this
      * <code>Action</code> object. When its enabled state or other property
--- a/jdk/src/java.desktop/share/classes/javax/swing/SwingUtilities.java	Fri Dec 18 17:02:42 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/javax/swing/SwingUtilities.java	Tue Dec 22 05:34:49 2015 +0400
@@ -1704,9 +1704,9 @@
     }
 
     /**
-     * Invokes <code>actionPerformed</code> on <code>action</code> if
-     * <code>action</code> is enabled (and non-{@code null}). The command for the
-     * ActionEvent is determined by:
+     * Invokes {@code actionPerformed} on {@code action} if {@code action}
+     * is non-{@code null} and accepts the sender object.
+     * The command for the ActionEvent is determined by:
      * <ol>
      *   <li>If the action was registered via
      *       <code>registerKeyboardAction</code>, then the command string
@@ -1725,23 +1725,18 @@
      * @param modifiers action modifiers
      * @return {@code true} if {@code action} is non-{@code null} and
      *         actionPerformed is invoked on it.
+     * @see javax.swing.Action#accept
      *
      * @since 1.3
      */
     public static boolean notifyAction(Action action, KeyStroke ks,
                                        KeyEvent event, Object sender,
                                        int modifiers) {
-        if (action == null) {
+
+        if (action == null || !action.accept(sender)) {
             return false;
         }
-        if (action instanceof UIAction) {
-            if (!((UIAction)action).isEnabled(sender)) {
-                return false;
-            }
-        }
-        else if (!action.isEnabled()) {
-            return false;
-        }
+
         Object commandO;
         boolean stayNull;
 
--- a/jdk/src/java.desktop/share/classes/javax/swing/TransferHandler.java	Fri Dec 18 17:02:42 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/javax/swing/TransferHandler.java	Tue Dec 22 05:34:49 2015 +0400
@@ -1695,13 +1695,10 @@
             super(name);
         }
 
-        public boolean isEnabled(Object sender) {
-            if (sender instanceof JComponent
-                && ((JComponent)sender).getTransferHandler() == null) {
-                    return false;
-            }
-
-            return true;
+        @Override
+        public boolean accept(Object sender) {
+            return !(sender instanceof JComponent
+                    && ((JComponent)sender).getTransferHandler() == null);
         }
 
         private static final JavaSecurityAccess javaSecurityAccess =
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicButtonListener.java	Fri Dec 18 17:02:42 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicButtonListener.java	Tue Dec 22 05:34:49 2015 +0400
@@ -319,13 +319,10 @@
             }
         }
 
-        public boolean isEnabled(Object sender) {
-            if(sender != null && (sender instanceof AbstractButton) &&
-                      !((AbstractButton)sender).getModel().isEnabled()) {
-                return false;
-            } else {
-                return true;
-            }
+        @Override
+        public boolean accept(Object sender) {
+            return !((sender instanceof AbstractButton) &&
+                    !((AbstractButton)sender).getModel().isEnabled());
         }
     }
-}
+}
\ No newline at end of file
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicComboBoxUI.java	Fri Dec 18 17:02:42 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicComboBoxUI.java	Tue Dec 22 05:34:49 2015 +0400
@@ -1719,7 +1719,8 @@
             return comboBox.getSelectedIndex();
         }
 
-        public boolean isEnabled(Object c) {
+        @Override
+        public boolean accept(Object c) {
             if (getName() == HIDE) {
                 return (c != null && ((JComboBox)c).isPopupVisible());
             }
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicDesktopPaneUI.java	Fri Dec 18 17:02:42 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicDesktopPaneUI.java	Tue Dec 22 05:34:49 2015 +0400
@@ -646,7 +646,8 @@
             }
         }
 
-        public boolean isEnabled(Object sender) {
+        @Override
+        public boolean accept(Object sender) {
             if (sender instanceof JDesktopPane) {
                 JDesktopPane dp = (JDesktopPane)sender;
                 String action = getName();
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicInternalFrameUI.java	Fri Dec 18 17:02:42 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicInternalFrameUI.java	Tue Dec 22 05:34:49 2015 +0400
@@ -200,7 +200,8 @@
                 }
             }
 
-            public boolean isEnabled(Object sender){
+            @Override
+            public boolean accept(Object sender){
                 if (sender instanceof JInternalFrame) {
                     JInternalFrame iFrame = (JInternalFrame)sender;
                     if (iFrame.getUI() instanceof BasicInternalFrameUI) {
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicListUI.java	Fri Dec 18 17:02:42 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicListUI.java	Tue Dec 22 05:34:49 2015 +0400
@@ -2060,7 +2060,8 @@
             }
         }
 
-        public boolean isEnabled(Object c) {
+        @Override
+        public boolean accept(Object c) {
             Object name = getName();
             if (name == SELECT_PREVIOUS_COLUMN_CHANGE_LEAD ||
                     name == SELECT_NEXT_COLUMN_CHANGE_LEAD ||
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicMenuUI.java	Fri Dec 18 17:02:42 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicMenuUI.java	Tue Dec 22 05:34:49 2015 +0400
@@ -312,7 +312,8 @@
             }
         }
 
-        public boolean isEnabled(Object c) {
+        @Override
+        public boolean accept(Object c) {
             if (c instanceof JMenu) {
                 return ((JMenu)c).isEnabled();
             }
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicRootPaneUI.java	Fri Dec 18 17:02:42 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicRootPaneUI.java	Tue Dec 22 05:34:49 2015 +0400
@@ -256,7 +256,8 @@
             }
         }
 
-        public boolean isEnabled(Object sender) {
+        @Override
+        public boolean accept(Object sender) {
             String key = getName();
             if(key == POST_POPUP) {
                 MenuElement[] elems = MenuSelectionManager
@@ -278,7 +279,7 @@
                 return false;
             }
 
-            if (sender != null && sender instanceof JRootPane) {
+            if (sender instanceof JRootPane) {
                 JButton owner = ((JRootPane)sender).getDefaultButton();
                 return (owner != null && owner.getModel().isEnabled());
             }
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTableHeaderUI.java	Fri Dec 18 17:02:42 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTableHeaderUI.java	Tue Dec 22 05:34:49 2015 +0400
@@ -855,7 +855,8 @@
             super(name);
         }
 
-        public boolean isEnabled(Object sender) {
+        @Override
+        public boolean accept(Object sender) {
             if (sender instanceof JTableHeader) {
                 JTableHeader th = (JTableHeader)sender;
                 TableColumnModel cm = th.getColumnModel();
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTableUI.java	Fri Dec 18 17:02:42 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTableUI.java	Tue Dec 22 05:34:49 2015 +0400
@@ -676,7 +676,8 @@
             }
         }
 
-        public boolean isEnabled(Object sender) {
+        @Override
+        public boolean accept(Object sender) {
             String key = getName();
 
             if (sender instanceof JTable &&
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTreeUI.java	Fri Dec 18 17:02:42 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTreeUI.java	Tue Dec 22 05:34:49 2015 +0400
@@ -4416,7 +4416,8 @@
             super(key);
         }
 
-        public boolean isEnabled(Object o) {
+        @Override
+        public boolean accept(Object o) {
             if (o instanceof JTree) {
                 if (getName() == CANCEL_EDITING) {
                     return ((JTree)o).isEditing();
--- a/jdk/src/java.desktop/share/classes/sun/swing/UIAction.java	Fri Dec 18 17:02:42 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/sun/swing/UIAction.java	Tue Dec 22 05:34:49 2015 +0400
@@ -87,7 +87,7 @@
      * Cover method for <code>isEnabled(null)</code>.
      */
     public final boolean isEnabled() {
-        return isEnabled(null);
+        return accept(null);
     }
 
     /**
@@ -96,7 +96,8 @@
      *
      * @param sender Widget enabled state is being asked for, may be null.
      */
-    public boolean isEnabled(Object sender) {
+    @Override
+    public boolean accept(Object sender) {
         return true;
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/Action/8133039/bug8133039.java	Tue Dec 22 05:34:49 2015 +0400
@@ -0,0 +1,180 @@
+/*
+ * Copyright (c) 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.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.awt.Robot;
+import java.awt.event.ActionEvent;
+import java.awt.event.KeyEvent;
+import javax.swing.AbstractAction;
+import javax.swing.Action;
+import javax.swing.JComboBox;
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.KeyStroke;
+import javax.swing.SwingUtilities;
+import sun.swing.UIAction;
+
+/**
+ * @test
+ * @bug 8133039
+ * @summary Provide public API to sun.swing.UIAction#isEnabled(Object)
+ * @author Alexander Scherbatiy
+ */
+public class bug8133039 {
+
+    private static volatile int ACTION_PERFORMED_CALLS = 0;
+    private static volatile int ACTION_ACCEPTED_CALLS = 0;
+
+    public static void main(String[] args) throws Exception {
+        testActionNotification();
+        testPopupAction();
+    }
+
+    private static void testActionNotification() {
+
+        KeyEvent keyEvent = new KeyEvent(new JLabel("Test"), 0, 0, 0, 0, 'A');
+        SenderObject rejectedSenderObject = new SenderObject();
+        SwingUtilities.notifyAction(new TestAction(false), null, keyEvent,
+                rejectedSenderObject, 0);
+
+        if (rejectedSenderObject.accepted) {
+            throw new RuntimeException("The sender is incorrectly accepted!");
+        }
+
+        if (rejectedSenderObject.performed) {
+            throw new RuntimeException("The action is incorrectly performed!");
+        }
+
+        SenderObject acceptedSenderObject = new SenderObject();
+        SwingUtilities.notifyAction(new TestAction(true), null, keyEvent,
+                acceptedSenderObject, 0);
+
+        if (!acceptedSenderObject.accepted) {
+            throw new RuntimeException("The sender is not accepted!");
+        }
+
+        if (!acceptedSenderObject.performed) {
+            throw new RuntimeException("The action is not performed!");
+        }
+    }
+
+    private static void testPopupAction() throws Exception {
+
+        SwingUtilities.invokeAndWait(bug8133039::createAndShowGUI);
+
+        Robot robot = new Robot();
+        robot.setAutoDelay(50);
+        robot.waitForIdle();
+
+        robot.keyPress(KeyEvent.VK_A);
+        robot.keyRelease(KeyEvent.VK_A);
+        robot.waitForIdle();
+
+        if (ACTION_ACCEPTED_CALLS != 1) {
+            throw new RuntimeException("Method accept is not invoked!");
+        }
+
+        if (ACTION_PERFORMED_CALLS != 1) {
+            throw new RuntimeException("Method actionPerformed is not invoked!");
+        }
+
+        robot.keyPress(KeyEvent.VK_A);
+        robot.keyRelease(KeyEvent.VK_A);
+        robot.waitForIdle();
+
+        if (ACTION_ACCEPTED_CALLS != 2) {
+            throw new RuntimeException("Method accept is not invoked!");
+        }
+
+        if (ACTION_PERFORMED_CALLS != 1) {
+            throw new RuntimeException("Method actionPerformed is invoked twice!");
+        }
+    }
+
+    private static void createAndShowGUI() {
+
+        JFrame frame = new JFrame();
+        frame.setSize(300, 300);
+        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+        JComboBox<String> comboBox = new JComboBox<>(new String[]{"1", "2", "3"});
+
+        Action showPopupAction = new ShowPopupAction();
+        comboBox.getInputMap().put(KeyStroke.getKeyStroke("A"), "showPopup");
+        comboBox.getActionMap().put("showPopup", showPopupAction);
+
+        frame.getContentPane().add(comboBox);
+        frame.setVisible(true);
+    }
+
+    private static class ShowPopupAction extends UIAction {
+
+        public ShowPopupAction() {
+            super("showPopup");
+        }
+
+        @Override
+        public void actionPerformed(ActionEvent e) {
+            ACTION_PERFORMED_CALLS++;
+            Object src = e.getSource();
+            if (src instanceof JComboBox) {
+                ((JComboBox) src).showPopup();
+            }
+        }
+
+        @Override
+        public boolean accept(Object sender) {
+            ACTION_ACCEPTED_CALLS++;
+            if (sender instanceof JComboBox) {
+                JComboBox c = (JComboBox) sender;
+                return !c.isPopupVisible();
+            }
+            return false;
+        }
+    }
+
+    private static class SenderObject {
+
+        private boolean accepted;
+        private boolean performed;
+    }
+
+    private static class TestAction extends AbstractAction {
+
+        private final boolean acceptSender;
+
+        public TestAction(boolean acceptSender) {
+            this.acceptSender = acceptSender;
+        }
+
+        @Override
+        public boolean accept(Object sender) {
+            ((SenderObject) sender).accepted = acceptSender;
+            return acceptSender;
+        }
+
+        @Override
+        public void actionPerformed(ActionEvent e) {
+            ((SenderObject) e.getSource()).performed = true;
+        }
+    }
+}