8061381: [macosx] Accelerators does not spelled for JMenuItems by Voice Over
authorserb
Mon, 04 Mar 2019 01:44:41 -0800
changeset 54233 9413f1a4dc2b
parent 54232 7c11a7cc7c1d
child 54234 6b2f2ec0f618
8061381: [macosx] Accelerators does not spelled for JMenuItems by Voice Over Reviewed-by: prr, kaddepalli
src/java.desktop/macosx/classes/sun/lwawt/macosx/CAccessibility.java
src/jdk.accessibility/windows/classes/com/sun/java/accessibility/internal/AccessBridge.java
--- a/src/java.desktop/macosx/classes/sun/lwawt/macosx/CAccessibility.java	Fri Mar 01 16:59:19 2019 -0800
+++ b/src/java.desktop/macosx/classes/sun/lwawt/macosx/CAccessibility.java	Mon Mar 04 01:44:41 2019 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2019, 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
@@ -31,6 +31,7 @@
 import java.awt.KeyboardFocusManager;
 import java.awt.Point;
 import java.awt.Window;
+import java.awt.event.KeyEvent;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.lang.reflect.InvocationTargetException;
@@ -54,7 +55,9 @@
 import javax.swing.JComponent;
 import javax.swing.JEditorPane;
 import javax.swing.JLabel;
+import javax.swing.JMenuItem;
 import javax.swing.JTextArea;
+import javax.swing.KeyStroke;
 
 import sun.awt.AWTAccessor;
 import sun.lwawt.LWWindowPeer;
@@ -355,6 +358,10 @@
                 if (accessibleName == null) {
                     return ac.getAccessibleDescription();
                 }
+                final String acceleratorText = getAcceleratorText(ac);
+                if (!acceleratorText.isEmpty()) {
+                    return accessibleName +' '+ acceleratorText;
+                }
                 return accessibleName;
             }
         }, c);
@@ -374,6 +381,41 @@
         }, c);
     }
 
+    /*
+     * Returns the JMenuItem accelerator. Implementation of this method is based
+     * on AccessBridge.getAccelerator(AccessibleContext) to access the KeyStroke
+     * and on AquaMenuPainter.paintMenuItem() to convert it to string.
+     */
+    @SuppressWarnings("deprecation")
+    private static String getAcceleratorText(AccessibleContext ac) {
+        String accText = "";
+        Accessible parent = ac.getAccessibleParent();
+        if (parent != null) {
+            // workaround for getAccessibleKeyBinding not returning the
+            // JMenuItem accelerator
+            int indexInParent = ac.getAccessibleIndexInParent();
+            Accessible child = parent.getAccessibleContext()
+                                     .getAccessibleChild(indexInParent);
+            if (child instanceof JMenuItem) {
+                JMenuItem menuItem = (JMenuItem) child;
+                KeyStroke keyStroke = menuItem.getAccelerator();
+                if (keyStroke != null) {
+                    int modifiers = keyStroke.getModifiers();
+                    if (modifiers > 0) {
+                        accText = KeyEvent.getKeyModifiersText(modifiers);
+                    }
+                    int keyCode = keyStroke.getKeyCode();
+                    if (keyCode != 0) {
+                        accText += KeyEvent.getKeyText(keyCode);
+                    } else {
+                        accText += keyStroke.getKeyChar();
+                    }
+                }
+            }
+        }
+        return accText;
+    }
+
     public static String getAccessibleDescription(final Accessible a, final Component c) {
         if (a == null) return null;
 
--- a/src/jdk.accessibility/windows/classes/com/sun/java/accessibility/internal/AccessBridge.java	Fri Mar 01 16:59:19 2019 -0800
+++ b/src/jdk.accessibility/windows/classes/com/sun/java/accessibility/internal/AccessBridge.java	Mon Mar 04 01:44:41 2019 -0800
@@ -3779,7 +3779,8 @@
     }
 
     /*
-     * returns the JMenuItem accelerator
+     * Returns the JMenuItem accelerator. Similar implementation is used on
+     * macOS, see CAccessibility.getAcceleratorText(AccessibleContext).
      */
     private KeyStroke getAccelerator(final AccessibleContext ac) {
         // workaround for getAccessibleKeyBinding not returning the