Skip to content

Commit 3549103

Browse files
author
Harshitha Onkar
committed
8353755: Add a helper method to Util - findComponent()
Reviewed-by: aivanov, tr
1 parent c865644 commit 3549103

File tree

1 file changed

+52
-16
lines changed

1 file changed

+52
-16
lines changed

test/jdk/javax/swing/regtesthelpers/Util.java

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,14 +21,32 @@
2121
* questions.
2222
*/
2323

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;
2737
import java.awt.image.BufferedImage;
2838
import java.util.ArrayList;
2939
import java.util.LinkedList;
3040
import java.util.List;
3141
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;
3250

3351
/**
3452
* <p>This class contains utilities useful for regression testing.
@@ -123,26 +141,44 @@ public static void generateOOME() {
123141
}
124142

125143
/**
126-
* Find a sub component by class name.
127-
* Always run this method on the EDT thread
144+
* Find a subcomponent by class name.
128145
*/
129146
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+
}
131152

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);
134166
}
167+
}
135168

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;
142179
}
143180
}
144181
}
145-
146182
return null;
147183
}
148184

0 commit comments

Comments
 (0)