|
1 | 1 | /* |
2 | | - * Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2011, 2025, 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 |
|
21 | 21 | * questions. |
22 | 22 | */ |
23 | 23 |
|
24 | | -import javax.swing.*; |
25 | | -import java.awt.*; |
26 | | -import java.awt.event.*; |
| 24 | +import java.awt.AWTException; |
| 25 | +import java.awt.BorderLayout; |
| 26 | +import java.awt.Component; |
| 27 | +import java.awt.Container; |
| 28 | +import java.awt.Dialog; |
| 29 | +import java.awt.Dimension; |
| 30 | +import java.awt.Point; |
| 31 | +import java.awt.Rectangle; |
| 32 | +import java.awt.Robot; |
| 33 | +import java.awt.event.ActionEvent; |
| 34 | +import java.awt.event.ActionListener; |
| 35 | +import java.awt.event.InputEvent; |
| 36 | +import java.awt.event.KeyEvent; |
27 | 37 | import java.awt.image.BufferedImage; |
28 | 38 | import java.util.ArrayList; |
29 | 39 | import java.util.LinkedList; |
30 | 40 | import java.util.List; |
31 | 41 | import java.util.concurrent.Callable; |
| 42 | +import java.util.function.Predicate; |
| 43 | + |
| 44 | +import javax.swing.Box; |
| 45 | +import javax.swing.JButton; |
| 46 | +import javax.swing.JDialog; |
| 47 | +import javax.swing.SwingUtilities; |
| 48 | + |
| 49 | +import static javax.swing.SwingUtilities.isEventDispatchThread; |
32 | 50 |
|
33 | 51 | /** |
34 | 52 | * <p>This class contains utilities useful for regression testing. |
@@ -123,26 +141,44 @@ public static void generateOOME() { |
123 | 141 | } |
124 | 142 |
|
125 | 143 | /** |
126 | | - * Find a sub component by class name. |
127 | | - * Always run this method on the EDT thread |
| 144 | + * Find a subcomponent by class name. |
128 | 145 | */ |
129 | 146 | public static Component findSubComponent(Component parent, String className) { |
130 | | - String parentClassName = parent.getClass().getName(); |
| 147 | + return findComponent((Container) parent, |
| 148 | + c -> c.getClass() |
| 149 | + .getName() |
| 150 | + .contains(className)); |
| 151 | + } |
131 | 152 |
|
132 | | - if (parentClassName.contains(className)) { |
133 | | - return parent; |
| 153 | + /** |
| 154 | + * Find a component based on predicate. |
| 155 | + */ |
| 156 | + public static Component findComponent(final Container container, |
| 157 | + final Predicate<Component> predicate) { |
| 158 | + try { |
| 159 | + if (isEventDispatchThread()) { |
| 160 | + return findComponentImpl(container, predicate); |
| 161 | + } else { |
| 162 | + return Util.invokeOnEDT(() -> findComponentImpl(container, predicate)); |
| 163 | + } |
| 164 | + } catch (Exception e) { |
| 165 | + throw new RuntimeException("Error occurred while finding component", e); |
134 | 166 | } |
| 167 | + } |
135 | 168 |
|
136 | | - if (parent instanceof Container) { |
137 | | - for (Component child : ((Container) parent).getComponents()) { |
138 | | - Component subComponent = findSubComponent(child, className); |
139 | | - |
140 | | - if (subComponent != null) { |
141 | | - return subComponent; |
| 169 | + private static Component findComponentImpl(final Container container, |
| 170 | + final Predicate<Component> predicate) { |
| 171 | + for (Component child : container.getComponents()) { |
| 172 | + if (predicate.test(child)) { |
| 173 | + return child; |
| 174 | + } |
| 175 | + if (child instanceof Container cont && cont.getComponentCount() > 0) { |
| 176 | + Component result = findComponentImpl(cont, predicate); |
| 177 | + if (result != null) { |
| 178 | + return result; |
142 | 179 | } |
143 | 180 | } |
144 | 181 | } |
145 | | - |
146 | 182 | return null; |
147 | 183 | } |
148 | 184 |
|
|
0 commit comments