From 49171c2806dd074d5894a1b71c30cb30a1656517 Mon Sep 17 00:00:00 2001 From: arapte Date: Sun, 15 Mar 2020 00:43:54 +0530 Subject: [PATCH] 8226253: JAWS reports wrong number of radio buttons when buttons are hidden. Reviewed-by: kizune, pbansal, andrew Contributed-by: Alex Kashchenko --- src/share/classes/javax/swing/JList.java | 8 +- .../sun/java/accessibility/AccessBridge.java | 88 +++++++++++++++++-- 2 files changed, 84 insertions(+), 12 deletions(-) diff --git a/src/share/classes/javax/swing/JList.java b/src/share/classes/javax/swing/JList.java index 7dbaec1ab..fa03ae97b 100644 --- a/src/share/classes/javax/swing/JList.java +++ b/src/share/classes/javax/swing/JList.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2020, 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 @@ -3308,6 +3308,7 @@ public class JList extends JComponent implements Scrollable, Accessible } s.add(AccessibleState.SELECTABLE); + s.add(AccessibleState.VISIBLE); if (parent.isFocusOwner() && (indexInParent == parent.getLeadSelectionIndex())) { s.add(AccessibleState.ACTIVE); @@ -3320,11 +3321,6 @@ public class JList extends JComponent implements Scrollable, Accessible } else if (s.contains(AccessibleState.SHOWING)) { s.remove(AccessibleState.SHOWING); } - if (this.isVisible()) { - s.add(AccessibleState.VISIBLE); - } else if (s.contains(AccessibleState.VISIBLE)) { - s.remove(AccessibleState.VISIBLE); - } s.add(AccessibleState.TRANSIENT); // cell-rendered return s; } diff --git a/src/windows/classes/com/sun/java/accessibility/AccessBridge.java b/src/windows/classes/com/sun/java/accessibility/AccessBridge.java index 11b6d001f..ebc904b0d 100644 --- a/src/windows/classes/com/sun/java/accessibility/AccessBridge.java +++ b/src/windows/classes/com/sun/java/accessibility/AccessBridge.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2020, 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 @@ -1550,6 +1550,38 @@ final public class AccessBridge extends AccessBridgeLoader { return null; } + private int getNonVisibleChildrenCountTillIndex(AccessibleContext parentAC, int index) { + if (parentAC != null && index >= 0 && index < parentAC.getAccessibleChildrenCount()) { + int nonVisibleChildrenCount = 0; + for (int i = 0; i <= index; i++) { + if (!parentAC.getAccessibleChild(i).getAccessibleContext().getAccessibleStateSet().contains(AccessibleState.VISIBLE)) { + nonVisibleChildrenCount++; + } + } + return nonVisibleChildrenCount; + } + return 0; + } + + private Accessible getVisibleChildAtIndex(AccessibleContext parentAC, int index) { + if (parentAC != null && index >= 0 && index < parentAC.getAccessibleChildrenCount()) { + int visibleIndex = -1; + int childrenCount = parentAC.getAccessibleChildrenCount(); + for (int i = 0; i <= childrenCount; i++) { + Accessible child = parentAC.getAccessibleChild(i); + if (child != null) { + AccessibleContext ac = child.getAccessibleContext(); + if (ac != null && ac.getAccessibleStateSet().contains(AccessibleState.VISIBLE)) { + visibleIndex++; + } + if (visibleIndex == index) { + return child; + } + } + } + } + return null; + } /** * returns the AccessibleParent from an AccessibleContext */ @@ -1580,7 +1612,12 @@ final public class AccessBridge extends AccessBridgeLoader { return InvocationUtils.invokeAndWait(new Callable() { @Override public Integer call() throws Exception { - return ac.getAccessibleIndexInParent(); + int indexInParent = ac.getAccessibleIndexInParent(); + Accessible parent = ac.getAccessibleParent(); + if (parent != null) { + indexInParent -= getNonVisibleChildrenCountTillIndex(parent.getAccessibleContext(), indexInParent); + } + return indexInParent; } }, ac); } @@ -1594,7 +1631,8 @@ final public class AccessBridge extends AccessBridgeLoader { return InvocationUtils.invokeAndWait(new Callable() { @Override public Integer call() throws Exception { - return ac.getAccessibleChildrenCount(); + int childrenCount = ac.getAccessibleChildrenCount(); + return childrenCount - getNonVisibleChildrenCountTillIndex(ac, childrenCount - 1); } }, ac); } @@ -1630,7 +1668,7 @@ final public class AccessBridge extends AccessBridgeLoader { return InvocationUtils.invokeAndWait(new Callable() { @Override public AccessibleContext call() throws Exception { - Accessible a = ac.getAccessibleChild(index); + Accessible a = getVisibleChildAtIndex(ac, index); if (a != null) { return a.getAccessibleContext(); } @@ -3615,7 +3653,11 @@ final public class AccessBridge extends AccessBridgeLoader { AccessibleRelation[] relations = ars.toArray(); if (relations != null && i >= 0 && i < relations.length) { Object[] targets = relations[i].getTarget(); - return targets.length; + if (targets != null) { + int targetCount = targets.length - + getNonVisibleTargetCountTillIndex(targets, targets.length - 1); + return targetCount; + } } } } @@ -3641,7 +3683,7 @@ final public class AccessBridge extends AccessBridgeLoader { if (relations != null && i >= 0 && i < relations.length) { Object[] targets = relations[i].getTarget(); if (targets != null && j >= 0 & j < targets.length) { - Object o = targets[j]; + Object o = getVisibleTargetAtIndex(targets, j); if (o instanceof Accessible) { return ((Accessible) o).getAccessibleContext(); } @@ -3654,6 +3696,40 @@ final public class AccessBridge extends AccessBridgeLoader { }, ac); } + private Object getVisibleTargetAtIndex(Object[] targets, int index) { + if (index >= 0 && index < targets.length) { + int visibleTargetIndex = -1; + for (int i = 0; i < targets.length; i++) { + if (targets[i] instanceof Accessible) { + AccessibleContext ac = ((Accessible) targets[i]).getAccessibleContext(); + if (ac != null && ac.getAccessibleStateSet().contains(AccessibleState.VISIBLE)) { + visibleTargetIndex++; + } + if (visibleTargetIndex == index) { + return targets[i]; + } + } + } + } + return null; + } + + private int getNonVisibleTargetCountTillIndex(Object[] targets, int index) { + if (index >= 0 && index < targets.length) { + int nonVisibleTargetsCount = 0; + for (int i = 0; i <= index; i++) { + if (targets[i] instanceof Accessible) { + AccessibleContext ac = ((Accessible) targets[i]).getAccessibleContext(); + if (ac != null && !ac.getAccessibleStateSet().contains(AccessibleState.VISIBLE)) { + nonVisibleTargetsCount++; + } + } + } + return nonVisibleTargetsCount; + } + return 0; + } + // ========= AccessibleHypertext ========= private Map hyperTextContextMap = new WeakHashMap<>(); -- GitLab