|
1 | 1 | /* |
2 | | - * Copyright (c) 2011, 2022, 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 |
|
36 | 36 | import java.beans.PropertyChangeEvent; |
37 | 37 | import java.beans.PropertyChangeListener; |
38 | 38 | import java.lang.annotation.Native; |
39 | | -import java.lang.reflect.Constructor; |
40 | 39 | import java.lang.reflect.InvocationTargetException; |
41 | 40 | import java.util.ArrayList; |
| 41 | +import java.util.HashMap; |
42 | 42 | import java.util.HashSet; |
| 43 | +import java.util.List; |
43 | 44 | import java.util.Set; |
44 | 45 | import java.util.concurrent.Callable; |
45 | 46 | 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; |
@@ -759,84 +759,28 @@ private static Object[] getChildrenAndRolesImpl(Accessible a, Component c, int w |
759 | 759 | return new Object[]{childrenAndRoles.get(whichChildren * 2), childrenAndRoles.get((whichChildren * 2) + 1)}; |
760 | 760 | } |
761 | 761 |
|
762 | | - private static Accessible createAccessibleTreeNode(JTree t, TreePath p) { |
763 | | - Accessible a = null; |
764 | | - |
765 | | - try { |
766 | | - Class<?> accessibleJTreeNodeClass = Class.forName("javax.swing.JTree$AccessibleJTree$AccessibleJTreeNode"); |
767 | | - Constructor<?> constructor = accessibleJTreeNodeClass.getConstructor(t.getAccessibleContext().getClass(), JTree.class, TreePath.class, Accessible.class); |
768 | | - constructor.setAccessible(true); |
769 | | - a = ((Accessible) constructor.newInstance(t.getAccessibleContext(), t, p, null)); |
770 | | - } catch (Exception e) { |
771 | | - e.printStackTrace(); |
772 | | - } |
773 | | - |
774 | | - return a; |
775 | | - } |
776 | | - |
777 | 762 | // This method is called from the native |
778 | 763 | // Each child takes up three entries in the array: one for itself, one for its role, and one for the recursion level |
779 | 764 | private static Object[] getChildrenAndRolesRecursive(final Accessible a, final Component c, final int whichChildren, final boolean allowIgnored, final int level) { |
780 | 765 | if (a == null) return null; |
781 | 766 | return invokeAndWait(new Callable<Object[]>() { |
782 | 767 | public Object[] call() throws Exception { |
783 | 768 | ArrayList<Object> allChildren = new ArrayList<Object>(); |
784 | | - |
785 | | - Accessible at = null; |
786 | | - if (a instanceof CAccessible) { |
787 | | - at = CAccessible.getSwingAccessible(a); |
788 | | - } else { |
789 | | - at = a; |
790 | | - } |
791 | | - |
792 | | - if (at instanceof JTree) { |
793 | | - JTree tree = ((JTree) at); |
794 | | - |
795 | | - if (whichChildren == JAVA_AX_ALL_CHILDREN) { |
796 | | - int count = tree.getRowCount(); |
797 | | - for (int i = 0; i < count; i++) { |
798 | | - TreePath path = tree.getPathForRow(i); |
799 | | - Accessible an = createAccessibleTreeNode(tree, path); |
800 | | - if (an != null) { |
801 | | - AccessibleContext ac = an.getAccessibleContext(); |
802 | | - if (ac != null) { |
803 | | - allChildren.add(an); |
804 | | - allChildren.add(ac.getAccessibleRole());; |
805 | | - allChildren.add(String.valueOf((tree.isRootVisible() ? path.getPathCount() : path.getPathCount() - 1))); |
806 | | - } |
807 | | - } |
808 | | - } |
809 | | - } |
810 | | - |
811 | | - if (whichChildren == JAVA_AX_SELECTED_CHILDREN) { |
812 | | - int count = tree.getSelectionCount(); |
813 | | - for (int i = 0; i < count; i++) { |
814 | | - TreePath path = tree.getSelectionPaths()[i]; |
815 | | - Accessible an = createAccessibleTreeNode(tree, path); |
816 | | - if (an != null) { |
817 | | - AccessibleContext ac = an.getAccessibleContext(); |
818 | | - if (ac != null) { |
819 | | - allChildren.add(an); |
820 | | - allChildren.add(ac.getAccessibleRole()); |
821 | | - allChildren.add(String.valueOf((tree.isRootVisible() ? path.getPathCount() : path.getPathCount() - 1))); |
822 | | - } |
823 | | - } |
824 | | - } |
825 | | - } |
826 | | - |
827 | | - return allChildren.toArray(); |
828 | | - } |
829 | | - |
830 | 769 | ArrayList<Object> currentLevelChildren = new ArrayList<Object>(); |
831 | 770 | ArrayList<Accessible> parentStack = new ArrayList<Accessible>(); |
| 771 | + HashMap<Accessible, List<Object>> childrenOfParent = new HashMap<>(); |
832 | 772 | parentStack.add(a); |
833 | 773 | ArrayList<Integer> indexses = new ArrayList<Integer>(); |
834 | 774 | Integer index = 0; |
835 | 775 | int currentLevel = level; |
836 | 776 | while (!parentStack.isEmpty()) { |
837 | 777 | Accessible p = parentStack.get(parentStack.size() - 1); |
838 | | - |
839 | | - currentLevelChildren.addAll(Arrays.asList(getChildrenAndRolesImpl(p, c, JAVA_AX_ALL_CHILDREN, allowIgnored, ChildrenOperations.COMMON))); |
| 778 | + if (!childrenOfParent.containsKey(p)) { |
| 779 | + childrenOfParent.put(p, Arrays.asList(getChildrenAndRolesImpl(p, |
| 780 | + c, JAVA_AX_ALL_CHILDREN, allowIgnored, |
| 781 | + ChildrenOperations.COMMON))); |
| 782 | + } |
| 783 | + currentLevelChildren.addAll(childrenOfParent.get(p)); |
840 | 784 | if ((currentLevelChildren.size() == 0) || (index >= currentLevelChildren.size())) { |
841 | 785 | if (!parentStack.isEmpty()) parentStack.remove(parentStack.size() - 1); |
842 | 786 | if (!indexses.isEmpty()) index = indexses.remove(indexses.size() - 1); |
@@ -879,7 +823,6 @@ public Object[] call() throws Exception { |
879 | 823 | currentLevel += 1; |
880 | 824 | continue; |
881 | 825 | } |
882 | | - |
883 | 826 | } |
884 | 827 |
|
885 | 828 | return allChildren.toArray(); |
|
0 commit comments