Skip to content

Commit d8b2de8

Browse files
committed
8367348: Enhance PassFailJFrame to support links in HTML
Backport-of: 7a3025e3d7d33ed02db34c1485aa3c7b44b2d8ee
1 parent 9373d27 commit d8b2de8

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

test/jdk/java/awt/regtesthelpers/PassFailJFrame.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import javax.swing.JTextArea;
7373
import javax.swing.Timer;
7474
import javax.swing.border.Border;
75+
import javax.swing.event.HyperlinkListener;
7576
import javax.swing.text.JTextComponent;
7677
import javax.swing.text.html.HTMLEditorKit;
7778
import javax.swing.text.html.StyleSheet;
@@ -98,7 +99,8 @@
9899
* tester. The instructions can be either plain text or HTML. If the
99100
* text of the instructions starts with {@code "<html>"}, the
100101
* instructions are displayed as HTML, as supported by Swing, which
101-
* provides richer formatting options.
102+
* provides richer formatting options. To handle navigating links in the
103+
* instructions, call {@link Builder#hyperlinkListener} to install a listener.
102104
* <p>
103105
* The instructions are displayed in a text component with word-wrapping
104106
* so that there's no horizontal scroll bar. If the text doesn't fit, a
@@ -592,6 +594,7 @@ private static void createUI(String title, String instructions,
592594
frame.add(createInstructionUIPanel(instructions,
593595
testTimeOut,
594596
rows, columns,
597+
null,
595598
false,
596599
false, 0),
597600
BorderLayout.CENTER);
@@ -610,6 +613,7 @@ private static void createUI(Builder builder) {
610613
createInstructionUIPanel(builder.instructions,
611614
builder.testTimeOut,
612615
builder.rows, builder.columns,
616+
builder.hyperlinkListener,
613617
builder.screenCapture,
614618
builder.addLogArea,
615619
builder.logAreaRows);
@@ -631,6 +635,7 @@ private static void createUI(Builder builder) {
631635
private static JComponent createInstructionUIPanel(String instructions,
632636
long testTimeOut,
633637
int rows, int columns,
638+
HyperlinkListener hyperlinkListener,
634639
boolean enableScreenCapture,
635640
boolean addLogArea,
636641
int logAreaRows) {
@@ -643,6 +648,9 @@ private static JComponent createInstructionUIPanel(String instructions,
643648
JTextComponent text = instructions.startsWith("<html>")
644649
? configureHTML(instructions, rows, columns)
645650
: configurePlainText(instructions, rows, columns);
651+
if (hyperlinkListener != null && text instanceof JEditorPane ep) {
652+
ep.addHyperlinkListener(hyperlinkListener);
653+
}
646654
text.setEditable(false);
647655
text.setBorder(createTextBorder());
648656
text.setCaretPosition(0);
@@ -716,7 +724,7 @@ private static JTextComponent configureHTML(String instructions,
716724
// Reduce the list default margins
717725
styles.addRule("ol, ul { margin-left-ltr: 30; margin-left-rtl: 30 }");
718726
// Make the size of code (and other elements) the same as other text
719-
styles.addRule("code, kbd, samp, pre { font-size: inherit }");
727+
styles.addRule("code, kbd, samp, pre { font-size: inherit; background: #DDD; }");
720728

721729
return text;
722730
}
@@ -1398,6 +1406,7 @@ public static final class Builder {
13981406
private int rows;
13991407
private int columns;
14001408
private boolean screenCapture;
1409+
private HyperlinkListener hyperlinkListener;
14011410
private boolean addLogArea;
14021411
private int logAreaRows = 10;
14031412

@@ -1478,6 +1487,18 @@ public Builder columns(int columns) {
14781487
return this;
14791488
}
14801489

1490+
/**
1491+
* Sets a {@link HyperlinkListener} for navigating links inside
1492+
* the instructions pane.
1493+
*
1494+
* @param hyperlinkListener the listener
1495+
* @return this builder
1496+
*/
1497+
public Builder hyperlinkListener(HyperlinkListener hyperlinkListener) {
1498+
this.hyperlinkListener = hyperlinkListener;
1499+
return this;
1500+
}
1501+
14811502
public Builder screenCapture() {
14821503
this.screenCapture = true;
14831504
return this;

0 commit comments

Comments
 (0)