Skip to content

Commit

Permalink
8294158: HTML formatting for PassFailJFrame instructions
Browse files Browse the repository at this point in the history
Backport-of: 14090ef6039ff2f3064f397a75219b2bc715cc27
  • Loading branch information
Andrew Lu committed Feb 26, 2024
1 parent 5de649a commit 95ca457
Showing 1 changed file with 40 additions and 5 deletions.
45 changes: 40 additions & 5 deletions test/jdk/java/awt/regtesthelpers/PassFailJFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,17 @@
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;
import javax.swing.text.JTextComponent;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

import static javax.swing.SwingUtilities.invokeAndWait;
import static javax.swing.SwingUtilities.isEventDispatchThread;
Expand All @@ -81,8 +85,11 @@ public class PassFailJFrame {
private static volatile boolean failed;
private static volatile boolean timeout;
private static volatile String testFailedReason;

private static final AtomicInteger imgCounter = new AtomicInteger(0);

private static JFrame frame;

private static Robot robot;

public enum Position {HORIZONTAL, VERTICAL, TOP_LEFT_CORNER}
Expand Down Expand Up @@ -169,7 +176,7 @@ public PassFailJFrame(String title, String instructions, long testTimeOut,
InvocationTargetException {
if (isEventDispatchThread()) {
createUI(title, instructions, testTimeOut, rows, columns,
enableScreenCapture);
enableScreenCapture);
} else {
invokeAndWait(() -> createUI(title, instructions, testTimeOut,
rows, columns, enableScreenCapture));
Expand All @@ -187,9 +194,11 @@ private static void createUI(String title, String instructions,
boolean enableScreenCapture) {
frame = new JFrame(title);
frame.setLayout(new BorderLayout());
JTextArea instructionsText = new JTextArea(instructions, rows, columns);
instructionsText.setEditable(false);
instructionsText.setLineWrap(true);

JTextComponent text = instructions.startsWith("<html>")
? configureHTML(instructions, rows, columns)
: configurePlainText(instructions, rows, columns);
text.setEditable(false);

long tTimeout = TimeUnit.MINUTES.toMillis(testTimeOut);

Expand All @@ -210,7 +219,7 @@ private static void createUI(String title, String instructions,
});
timer.start();
frame.add(testTimeoutLabel, BorderLayout.NORTH);
frame.add(new JScrollPane(instructionsText), BorderLayout.CENTER);
frame.add(new JScrollPane(text), BorderLayout.CENTER);

JButton btnPass = new JButton("Pass");
btnPass.addActionListener((e) -> {
Expand Down Expand Up @@ -249,6 +258,32 @@ public void windowClosing(WindowEvent e) {
windowList.add(frame);
}

private static JTextComponent configurePlainText(String instructions,
int rows, int columns) {
JTextArea text = new JTextArea(instructions, rows, columns);
text.setLineWrap(true);
text.setWrapStyleWord(true);
return text;
}

private static JTextComponent configureHTML(String instructions,
int rows, int columns) {
JEditorPane text = new JEditorPane("text/html", instructions);
text.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES,
Boolean.TRUE);
// Set preferred size as if it were JTextArea
text.setPreferredSize(new JTextArea(rows, columns).getPreferredSize());

HTMLEditorKit kit = (HTMLEditorKit) text.getEditorKit();
StyleSheet styles = kit.getStyleSheet();
// Reduce the default margins
styles.addRule("ol, ul { margin-left-ltr: 20; margin-left-rtl: 20 }");
// Make the size of code blocks the same as other text
styles.addRule("code { font-size: inherit }");

return text;
}

private static JComponent createCapturePanel() {
JComboBox<CaptureType> screenShortType = new JComboBox<>(CaptureType.values());

Expand Down

1 comment on commit 95ca457

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.