diff --git a/src/main/java/de/neemann/gui/StringUtils.java b/src/main/java/de/neemann/gui/StringUtils.java index 33673b284..e6ecd4bfc 100644 --- a/src/main/java/de/neemann/gui/StringUtils.java +++ b/src/main/java/de/neemann/gui/StringUtils.java @@ -5,7 +5,7 @@ package de.neemann.gui; */ public final class StringUtils { - private static final int DEF_COLS = 60; + private static final int DEF_COLS = 70; private StringUtils() { } @@ -113,38 +113,49 @@ public final class StringUtils { if (text == null) return null; - StringBuilder sb = new StringBuilder(label); + StringBuilder outText = new StringBuilder(label); for (int i = 0; i < indent - label.length(); i++) - sb.append(" "); + outText.append(" "); + StringBuilder word = new StringBuilder(); + boolean isFirst = true; int pos = indent; - boolean wasBlank = false; for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); switch (c) { case '\n': case '\r': case ' ': - if (!wasBlank) { - if (pos > cols) { - sb.append('\n'); - for (int j = 0; j < indent; j++) - sb.append(" "); - - pos = indent; - } else { - sb.append(' '); - } - } - wasBlank = true; + pos = addWord(indent, cols, outText, word, pos, isFirst); + isFirst = false; break; default: - sb.append(c); - wasBlank = false; - pos++; + word.append(c); } } - return sb.toString(); + addWord(indent, cols, outText, word, pos, isFirst); + return outText.toString(); + } + + private static int addWord(int indent, int cols, StringBuilder outText, StringBuilder word, int pos, boolean isFirst) { + if (word.length() > 0) { + if (pos + (isFirst ? word.length() : word.length() + 1) > cols) { + outText.append('\n'); + for (int j = 0; j < indent; j++) + outText.append(" "); + + pos = indent; + } else { + if (!isFirst) { + outText.append(" "); + pos++; + } + } + outText.append(word); + pos += word.length(); + word.setLength(0); + } + return pos; } } diff --git a/src/test/java/de/neemann/gui/StringUtilsTest.java b/src/test/java/de/neemann/gui/StringUtilsTest.java index 95f7785ac..3f938a547 100644 --- a/src/test/java/de/neemann/gui/StringUtilsTest.java +++ b/src/test/java/de/neemann/gui/StringUtilsTest.java @@ -12,16 +12,17 @@ public class StringUtilsTest extends TestCase { public void testBreakLines() throws Exception { assertEquals("this is a test string", StringUtils.breakLines("this \n\n is \n a test \n\r string", 60)); - assertEquals("this is a test\nstring", StringUtils.breakLines("this \n\n is \n a test \n\r string", 10)); + assertEquals("this is a test\nstring", StringUtils.breakLines("this \n\n is \n a test \n\r string", 14)); assertEquals("This is a test string. This\n" + "is a test string. This is a\n" + - "test string.", StringUtils.breakLines("This is a test string. This is a test string. This is a test string.", 20)); + "test string.", StringUtils.breakLines("This is a test string. This is a test string. This is a test string.", 27)); + assertEquals("this is\naWordThatIsFarToLongToFitInASingleLine\nThis is a test string", StringUtils.breakLines("this is aWordThatIsFarToLongToFitInASingleLine This is a test string", 21)); } public void testBreakLinesLabel() throws Exception { assertEquals("a) This is a test string. This\n" + - " is a test string. This is\n" + - " a test string.", StringUtils.breakLinesLabel("a)", 3, "This is a test string. This is a test string. This is a test string.", 22)); + " is a test string. This is a\n" + + " test string.", StringUtils.breakLinesLabel("a)", 3, "This is a test string. This is a test string. This is a test string.", 30)); } } \ No newline at end of file