8161483: Implement AccessibleAction interface in JList.AccessibleJList.AccessibleJListChild
authorptbrunet
Thu, 11 Aug 2016 21:48:53 -0500
changeset 40439 acd9a7547e59
parent 40438 4abe842e6c48
child 40440 81680d8e945e
8161483: Implement AccessibleAction interface in JList.AccessibleJList.AccessibleJListChild Summary: Move AccessibleAction implementation from subclass to AccessibleJListChild Reviewed-by: alexsch, prr, darcy
jdk/src/java.desktop/share/classes/javax/swing/JList.java
jdk/test/javax/swing/JList/8161483/Bug8161483.java
--- a/jdk/src/java.desktop/share/classes/javax/swing/JList.java	Thu Aug 11 11:35:47 2016 -0700
+++ b/jdk/src/java.desktop/share/classes/javax/swing/JList.java	Thu Aug 11 21:48:53 2016 -0500
@@ -3058,7 +3058,7 @@
         public Accessible getAccessibleAt(Point p) {
             int i = locationToIndex(p);
             if (i >= 0) {
-                return new ActionableAccessibleJListChild(JList.this, i);
+                return new AccessibleJListChild(JList.this, i);
             } else {
                 return null;
             }
@@ -3085,7 +3085,7 @@
             if (i >= getModel().getSize()) {
                 return null;
             } else {
-                return new ActionableAccessibleJListChild(JList.this, i);
+                return new AccessibleJListChild(JList.this, i);
             }
         }
 
@@ -3188,7 +3188,7 @@
            * for list children.
            */
         protected class AccessibleJListChild extends AccessibleContext
-                implements Accessible, AccessibleComponent {
+                implements Accessible, AccessibleComponent, AccessibleAction {
             private JList<E>     parent = null;
             int indexInParent;
             private Component component = null;
@@ -3743,16 +3743,17 @@
                 }
             }
 
-        } // inner class AccessibleJListChild
-
-        private class ActionableAccessibleJListChild
-            extends AccessibleJListChild
-            implements AccessibleAction {
-
-            ActionableAccessibleJListChild(JList<E> parent, int indexInParent) {
-                super(parent, indexInParent);
-            }
-
+            /**
+             * {@inheritDoc}
+             * @implSpec Returns the AccessibleAction for this AccessibleJListChild
+             * as follows:  First getListCellRendererComponent of the ListCellRenderer
+             * for the component at the "index in parent" of this child is called.
+             * Then its AccessibleContext is fetched and that AccessibleContext's
+             * AccessibleAction is returned.  Note that if an AccessibleAction
+             * is not found using this process then this object with its implementation
+             * of the AccessibleAction interface is returned.
+             * @since 9
+             */
             @Override
             public AccessibleAction getAccessibleAction() {
                 AccessibleContext ac = getCurrentAccessibleContext();
@@ -3768,6 +3769,13 @@
                 }
             }
 
+            /**
+             * {@inheritDoc}
+             * @implSpec If i == 0 selects this AccessibleJListChild by calling
+             * JList.this.setSelectedIndex(indexInParent) and then returns true;
+             * otherwise returns false.
+             * @since 9
+             */
             @Override
             public boolean doAccessibleAction(int i) {
                 if (i == 0) {
@@ -3778,6 +3786,13 @@
                 }
             }
 
+            /**
+             * {@inheritDoc}
+             * @implSpec If i == 0 returns the action description fetched from
+             * UIManager.getString("AbstractButton.clickText");
+             * otherwise returns null.
+             * @since 9
+             */
             @Override
             public String getAccessibleActionDescription(int i) {
                 if (i == 0) {
@@ -3787,12 +3802,17 @@
                 }
             }
 
+            /**
+             * {@inheritDoc}
+             * @implSpec Returns 1, i.e. there is only one action.
+             * @since 9
+             */
             @Override
             public int getAccessibleActionCount() {
                 return 1;
             }
 
-        } // inner class ActionableAccessibleJListChild
+        } // inner class AccessibleJListChild
 
     } // inner class AccessibleJList
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/JList/8161483/Bug8161483.java	Thu Aug 11 21:48:53 2016 -0500
@@ -0,0 +1,104 @@
+/*
+ * Copyright (c) 2016, 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.
+ */
+
+/*
+ * @test
+ * @bug 8161483
+ * @summary Implement AccessibleAction in JList.AccessibleJList.AccessibleJListChild
+ * @run main Bug8161483
+ */
+
+import javax.accessibility.Accessible;
+import javax.accessibility.AccessibleAction;
+import javax.accessibility.AccessibleContext;
+import javax.accessibility.AccessibleSelection;
+import javax.swing.DefaultListModel;
+import javax.swing.JFrame;
+import javax.swing.JList;
+import javax.swing.SwingUtilities;
+
+public class Bug8161483 extends JFrame {
+
+    private static JFrame frame;
+    private static volatile Exception exception = null;
+    private JList<String> countryList;
+
+    public static void main(String args[]) throws Exception {
+        try {
+            SwingUtilities.invokeAndWait(() -> {
+                DefaultListModel<String> listModel = new DefaultListModel<>();
+                listModel.addElement("one");
+                listModel.addElement("two");
+                listModel.addElement("three");
+                JList<String> list = new JList<>(listModel);
+                frame = new JFrame();
+                frame.add(list);
+                frame.pack();
+                try {
+                    AccessibleContext acList = list.getAccessibleContext();
+                    Accessible accChild = acList.getAccessibleChild(1);
+                    AccessibleContext acChild = accChild.getAccessibleContext();
+                    AccessibleAction aa = acChild.getAccessibleAction();
+                    int c = aa.getAccessibleActionCount();
+                    if (c != 1) {
+                        throw new RuntimeException("getAccessibleActionCount is not 1");
+                    }
+                    String s = aa.getAccessibleActionDescription(0);
+                    if (!s.equals("click")) {
+                        throw new RuntimeException("getAccessibleActionDescription is not click");
+                    }
+                    boolean b = aa.doAccessibleAction(0);
+                    if (!b) {
+                        throw new RuntimeException("doAccessibleAction did not return true");
+                    }
+                    AccessibleSelection as = acList.getAccessibleSelection();
+                    int asc = as.getAccessibleSelectionCount();
+                    if (asc != 1) {
+                        throw new RuntimeException("getAccessibleSelectionCount is not 1");
+                    }
+                    boolean isSelected = as.isAccessibleChildSelected(0);
+                    if (isSelected) {
+                        throw new RuntimeException("isAccessibleChildSelected(0) did not return false");
+                    }
+                    isSelected = as.isAccessibleChildSelected(1);
+                    if (!isSelected) {
+                        throw new RuntimeException("isAccessibleChildSelected(1) did not return true");
+                    }
+                } catch (Exception e) {
+                    exception = e;
+                }
+            });
+            if (exception != null) {
+                System.out.println("Test failed: " + exception.getMessage());
+                throw exception;
+            } else {
+                System.out.println("Test passed.");
+            }
+        } finally {
+            SwingUtilities.invokeAndWait(() -> {
+                frame.dispose();
+            });
+        }
+    }
+
+}