|
1 | 1 | /* |
2 | | - * Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
35 | 35 | import java.beans.PropertyChangeEvent; |
36 | 36 | import java.beans.PropertyChangeListener; |
37 | 37 | import java.lang.annotation.Native; |
38 | | -import java.lang.reflect.Constructor; |
39 | 38 | import java.lang.reflect.InvocationTargetException; |
40 | 39 | import java.util.ArrayList; |
| 40 | +import java.util.HashMap; |
41 | 41 | import java.util.HashSet; |
| 42 | +import java.util.List; |
42 | 43 | import java.util.Set; |
43 | 44 | import java.util.concurrent.Callable; |
44 | 45 | import java.util.Arrays; |
|
64 | 65 | import javax.swing.JList; |
65 | 66 | import javax.swing.JTree; |
66 | 67 | import javax.swing.KeyStroke; |
67 | | -import javax.swing.tree.TreePath; |
68 | 68 |
|
69 | 69 | import sun.awt.AWTAccessor; |
70 | 70 | import sun.lwawt.LWWindowPeer; |
@@ -742,84 +742,28 @@ private static Object[] getChildrenAndRolesImpl(Accessible a, Component c, int w |
742 | 742 | return new Object[]{childrenAndRoles.get(whichChildren * 2), childrenAndRoles.get((whichChildren * 2) + 1)}; |
743 | 743 | } |
744 | 744 |
|
745 | | - private static Accessible createAccessibleTreeNode(JTree t, TreePath p) { |
746 | | - Accessible a = null; |
747 | | - |
748 | | - try { |
749 | | - Class<?> accessibleJTreeNodeClass = Class.forName("javax.swing.JTree$AccessibleJTree$AccessibleJTreeNode"); |
750 | | - Constructor<?> constructor = accessibleJTreeNodeClass.getConstructor(t.getAccessibleContext().getClass(), JTree.class, TreePath.class, Accessible.class); |
751 | | - constructor.setAccessible(true); |
752 | | - a = ((Accessible) constructor.newInstance(t.getAccessibleContext(), t, p, null)); |
753 | | - } catch (Exception e) { |
754 | | - e.printStackTrace(); |
755 | | - } |
756 | | - |
757 | | - return a; |
758 | | - } |
759 | | - |
760 | 745 | // This method is called from the native |
761 | 746 | // Each child takes up three entries in the array: one for itself, one for its role, and one for the recursion level |
762 | 747 | private static Object[] getChildrenAndRolesRecursive(final Accessible a, final Component c, final int whichChildren, final boolean allowIgnored, final int level) { |
763 | 748 | if (a == null) return null; |
764 | 749 | return invokeAndWait(new Callable<Object[]>() { |
765 | 750 | public Object[] call() throws Exception { |
766 | 751 | ArrayList<Object> allChildren = new ArrayList<Object>(); |
767 | | - |
768 | | - Accessible at = null; |
769 | | - if (a instanceof CAccessible) { |
770 | | - at = CAccessible.getSwingAccessible(a); |
771 | | - } else { |
772 | | - at = a; |
773 | | - } |
774 | | - |
775 | | - if (at instanceof JTree) { |
776 | | - JTree tree = ((JTree) at); |
777 | | - |
778 | | - if (whichChildren == JAVA_AX_ALL_CHILDREN) { |
779 | | - int count = tree.getRowCount(); |
780 | | - for (int i = 0; i < count; i++) { |
781 | | - TreePath path = tree.getPathForRow(i); |
782 | | - Accessible an = createAccessibleTreeNode(tree, path); |
783 | | - if (an != null) { |
784 | | - AccessibleContext ac = an.getAccessibleContext(); |
785 | | - if (ac != null) { |
786 | | - allChildren.add(an); |
787 | | - allChildren.add(ac.getAccessibleRole());; |
788 | | - allChildren.add(String.valueOf((tree.isRootVisible() ? path.getPathCount() : path.getPathCount() - 1))); |
789 | | - } |
790 | | - } |
791 | | - } |
792 | | - } |
793 | | - |
794 | | - if (whichChildren == JAVA_AX_SELECTED_CHILDREN) { |
795 | | - int count = tree.getSelectionCount(); |
796 | | - for (int i = 0; i < count; i++) { |
797 | | - TreePath path = tree.getSelectionPaths()[i]; |
798 | | - Accessible an = createAccessibleTreeNode(tree, path); |
799 | | - if (an != null) { |
800 | | - AccessibleContext ac = an.getAccessibleContext(); |
801 | | - if (ac != null) { |
802 | | - allChildren.add(an); |
803 | | - allChildren.add(ac.getAccessibleRole()); |
804 | | - allChildren.add(String.valueOf((tree.isRootVisible() ? path.getPathCount() : path.getPathCount() - 1))); |
805 | | - } |
806 | | - } |
807 | | - } |
808 | | - } |
809 | | - |
810 | | - return allChildren.toArray(); |
811 | | - } |
812 | | - |
813 | 752 | ArrayList<Object> currentLevelChildren = new ArrayList<Object>(); |
814 | 753 | ArrayList<Accessible> parentStack = new ArrayList<Accessible>(); |
| 754 | + HashMap<Accessible, List<Object>> childrenOfParent = new HashMap<>(); |
815 | 755 | parentStack.add(a); |
816 | 756 | ArrayList<Integer> indexses = new ArrayList<Integer>(); |
817 | 757 | Integer index = 0; |
818 | 758 | int currentLevel = level; |
819 | 759 | while (!parentStack.isEmpty()) { |
820 | 760 | Accessible p = parentStack.get(parentStack.size() - 1); |
821 | | - |
822 | | - currentLevelChildren.addAll(Arrays.asList(getChildrenAndRolesImpl(p, c, JAVA_AX_ALL_CHILDREN, allowIgnored, ChildrenOperations.COMMON))); |
| 761 | + if (!childrenOfParent.containsKey(p)) { |
| 762 | + childrenOfParent.put(p, Arrays.asList(getChildrenAndRolesImpl(p, |
| 763 | + c, JAVA_AX_ALL_CHILDREN, allowIgnored, |
| 764 | + ChildrenOperations.COMMON))); |
| 765 | + } |
| 766 | + currentLevelChildren.addAll(childrenOfParent.get(p)); |
823 | 767 | if ((currentLevelChildren.size() == 0) || (index >= currentLevelChildren.size())) { |
824 | 768 | if (!parentStack.isEmpty()) parentStack.remove(parentStack.size() - 1); |
825 | 769 | if (!indexses.isEmpty()) index = indexses.remove(indexses.size() - 1); |
@@ -862,7 +806,6 @@ public Object[] call() throws Exception { |
862 | 806 | currentLevel += 1; |
863 | 807 | continue; |
864 | 808 | } |
865 | | - |
866 | 809 | } |
867 | 810 |
|
868 | 811 | return allChildren.toArray(); |
|
0 commit comments