view scroll bars if error message is very long.

This commit is contained in:
hneemann 2019-05-14 17:23:59 +02:00
parent 0101548366
commit 1d1b4f60f1
3 changed files with 42 additions and 11 deletions

View File

@ -126,16 +126,26 @@ public class ErrorMessage implements Runnable {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
errorMessage = message; errorMessage = message;
message = new LineBreaker(80) final LineBreaker lineBreaker = new LineBreaker(80)
.toHTML() .toHTML()
.preserveContainedLineBreaks() .preserveContainedLineBreaks();
.breakLines(message); message = lineBreaker.breakLines(message);
int lines = lineBreaker.getLineCount();
int border;
if (lines <= 15) {
JLabel ta = new JLabel(message); JLabel ta = new JLabel(message);
int border = ta.getFont().getSize(); border = ta.getFont().getSize();
ta.setBorder(BorderFactory.createEmptyBorder(border, border, border, border)); ta.setBorder(BorderFactory.createEmptyBorder(border, border, border, border));
getContentPane().add(ta); getContentPane().add(ta);
} else {
JEditorPane ta = new JEditorPane("text/html", message);
ta.setBackground(getBackground());
border = ta.getFont().getSize();
final JScrollPane scrollPane = new JScrollPane(ta);
getContentPane().add(scrollPane);
scrollPane.setBorder(BorderFactory.createEmptyBorder(border, border, border, border));
}
JPanel buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT)); JPanel buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JButton button = new JButton(new AbstractAction(Lang.get("ok")) { JButton button = new JButton(new AbstractAction(Lang.get("ok")) {

View File

@ -20,7 +20,7 @@ public class LineBreaker {
private int pos; private int pos;
private boolean preserveLineBreaks = false; private boolean preserveLineBreaks = false;
private boolean toHTML = false; private boolean toHTML = false;
private boolean containsLineBreak = false; private int lines;
/** /**
* Creates a new instance * Creates a new instance
@ -105,7 +105,7 @@ public class LineBreaker {
if (toHTML) { if (toHTML) {
ret = "<html>" + ret.replace("<", "&lt;") ret = "<html>" + ret.replace("<", "&lt;")
.replace(">", "&gt;") + "</html>"; .replace(">", "&gt;") + "</html>";
if (containsLineBreak) if (lines > 1)
ret = ret.replace("\n", "<br>"); ret = ret.replace("\n", "<br>");
} }
@ -114,6 +114,8 @@ public class LineBreaker {
private void addWord(StringBuilder word) { private void addWord(StringBuilder word) {
if (word.length() > 0) { if (word.length() > 0) {
if (lines == 0)
lines = 1;
if (pos + (isFirst ? word.length() : word.length() + 1) > cols) { if (pos + (isFirst ? word.length() : word.length() + 1) > cols) {
lineBreak(); lineBreak();
} else { } else {
@ -135,8 +137,8 @@ public class LineBreaker {
for (int j = 0; j < indent; j++) for (int j = 0; j < indent; j++)
outText.append(" "); outText.append(" ");
pos = indent; pos = indent;
containsLineBreak = true;
isFirst = true; isFirst = true;
lines++;
} }
} }
@ -159,4 +161,11 @@ public class LineBreaker {
this.toHTML = true; this.toHTML = true;
return this; return this;
} }
/**
* @return the number of created lines
*/
public int getLineCount() {
return lines;
}
} }

View File

@ -39,5 +39,17 @@ public class LineBreakerTest extends TestCase {
assertEquals("<html>this is a<br>test string</html>", new LineBreaker(60).toHTML().preserveContainedLineBreaks().toHTML().breakLines("this is a\n test string")); assertEquals("<html>this is a<br>test string</html>", new LineBreaker(60).toHTML().preserveContainedLineBreaks().toHTML().breakLines("this is a\n test string"));
} }
public void testBreakLinesLineCount() throws Exception {
check("", 0, new LineBreaker(10),"");
check("This", 1, new LineBreaker(10),"This");
check("This is a\ntest", 2, new LineBreaker(10),"This is a test");
check("This is a\ntext used\nto check\nthe line\ncount", 5, new LineBreaker(10),"This is a text used to check the line count");
}
private void check(String should, int lineCount, LineBreaker lineBreaker, String textToBreak) {
assertEquals(should, lineBreaker.breakLines(textToBreak));
assertEquals(lineCount, lineBreaker.getLineCount());
}
} }