From cc7178c9c979a7f2ea40c06b48bb05cab54ba9ad Mon Sep 17 00:00:00 2001 From: dragonmacher <48328597+dragonmacher@users.noreply.github.com> Date: Mon, 10 May 2021 13:59:56 -0400 Subject: [PATCH] GP-828 - fixed bad message dialog wrapping --- .../java/docking/DockingErrorDisplay.java | 30 ++++++++++++++++++- .../java/docking/widgets/MultiLineLabel.java | 12 ++------ 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/Ghidra/Framework/Docking/src/main/java/docking/DockingErrorDisplay.java b/Ghidra/Framework/Docking/src/main/java/docking/DockingErrorDisplay.java index 3fd195d9cc..f649a7301a 100644 --- a/Ghidra/Framework/Docking/src/main/java/docking/DockingErrorDisplay.java +++ b/Ghidra/Framework/Docking/src/main/java/docking/DockingErrorDisplay.java @@ -17,13 +17,16 @@ package docking; import java.awt.Component; import java.awt.Window; +import java.util.List; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.WordUtils; import docking.widgets.OkDialog; import docking.widgets.OptionDialog; import ghidra.util.*; import ghidra.util.exception.MultipleCauses; +import ghidra.util.html.HtmlLineSplitter; public class DockingErrorDisplay implements ErrorDisplay { @@ -60,6 +63,31 @@ public class DockingErrorDisplay implements ErrorDisplay { throwable); } + private static String wrap(String text) { + + StringBuilder buffy = new StringBuilder(); + List lines = HtmlLineSplitter.split(text, 100, true); + String newline = "\n"; + for (String line : lines) { + + if (buffy.length() != 0) { + buffy.append(newline); + } + + if (StringUtils.isBlank(line)) { + // this will trim all leading blank lines, but preserve internal blank lines, + // which clients may be providing for visual line separation + continue; + } + + // wrap any poorly formatted text that gets displayed in the label; 80-100 chars is + // a reasonable line length based on historical print margins + String wrapped = WordUtils.wrap(line, 100, null, true); + buffy.append(wrapped); + } + return buffy.toString(); + } + private void displayMessage(MessageType messageType, ErrorLogger errorLogger, Object originator, Component parent, String title, Object message, Throwable throwable) { @@ -73,7 +101,7 @@ public class DockingErrorDisplay implements ErrorDisplay { // wrap any poorly formatted text that gets displayed in the label; 80-100 chars is // a reasonable line length based on historical print margins - messageString = WordUtils.wrap(safeMessage, 100, null, true); + messageString = wrap(safeMessage); } String unformattedMessage = HTMLUtilities.fromHTML(messageString); diff --git a/Ghidra/Framework/Docking/src/main/java/docking/widgets/MultiLineLabel.java b/Ghidra/Framework/Docking/src/main/java/docking/widgets/MultiLineLabel.java index 49a006081c..a174cd81f9 100644 --- a/Ghidra/Framework/Docking/src/main/java/docking/widgets/MultiLineLabel.java +++ b/Ghidra/Framework/Docking/src/main/java/docking/widgets/MultiLineLabel.java @@ -16,7 +16,6 @@ package docking.widgets; import java.awt.*; -import java.util.StringTokenizer; import javax.swing.JFrame; import javax.swing.JPanel; @@ -103,15 +102,10 @@ public class MultiLineLabel extends JPanel { "exception with no message from the line of code below:\n\n" + getCallerString(); Msg.debug(label, label, new Throwable()); } - StringTokenizer t = new StringTokenizer(label, "\n"); - num_lines = t.countTokens(); - lines = new String[num_lines]; + lines = label.split("\n"); + num_lines = lines.length; line_widths = new int[num_lines]; - for (int i = 0; i < num_lines; i++) { - lines[i] = t.nextToken(); - } - } private String getCallerString() { @@ -331,7 +325,7 @@ public class MultiLineLabel extends JPanel { public static void main(String[] args) { MultiLineLabel mlab = new MultiLineLabel( - "This is a test\nof a multi-line label\nLine One\n" + "Line Two\nLine Three.", 20, 20, + "This is a test\nof a multi-line label\nLine One\n\nLine Two\nLine Three.", 20, 20, MultiLineLabel.CENTER); JFrame f = new JFrame("Test MultiLineLabel"); f.getContentPane().add(mlab);