mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-16 16:34:47 -04:00
view scroll bars if error message is very long.
This commit is contained in:
parent
0101548366
commit
1d1b4f60f1
@ -126,16 +126,26 @@ public class ErrorMessage implements Runnable {
|
||||
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
|
||||
|
||||
errorMessage = message;
|
||||
message = new LineBreaker(80)
|
||||
final LineBreaker lineBreaker = new LineBreaker(80)
|
||||
.toHTML()
|
||||
.preserveContainedLineBreaks()
|
||||
.breakLines(message);
|
||||
.preserveContainedLineBreaks();
|
||||
message = lineBreaker.breakLines(message);
|
||||
int lines = lineBreaker.getLineCount();
|
||||
|
||||
|
||||
JLabel ta = new JLabel(message);
|
||||
int border = ta.getFont().getSize();
|
||||
ta.setBorder(BorderFactory.createEmptyBorder(border, border, border, border));
|
||||
getContentPane().add(ta);
|
||||
int border;
|
||||
if (lines <= 15) {
|
||||
JLabel ta = new JLabel(message);
|
||||
border = ta.getFont().getSize();
|
||||
ta.setBorder(BorderFactory.createEmptyBorder(border, border, border, border));
|
||||
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));
|
||||
JButton button = new JButton(new AbstractAction(Lang.get("ok")) {
|
||||
|
@ -20,7 +20,7 @@ public class LineBreaker {
|
||||
private int pos;
|
||||
private boolean preserveLineBreaks = false;
|
||||
private boolean toHTML = false;
|
||||
private boolean containsLineBreak = false;
|
||||
private int lines;
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
@ -105,7 +105,7 @@ public class LineBreaker {
|
||||
if (toHTML) {
|
||||
ret = "<html>" + ret.replace("<", "<")
|
||||
.replace(">", ">") + "</html>";
|
||||
if (containsLineBreak)
|
||||
if (lines > 1)
|
||||
ret = ret.replace("\n", "<br>");
|
||||
}
|
||||
|
||||
@ -114,6 +114,8 @@ public class LineBreaker {
|
||||
|
||||
private void addWord(StringBuilder word) {
|
||||
if (word.length() > 0) {
|
||||
if (lines == 0)
|
||||
lines = 1;
|
||||
if (pos + (isFirst ? word.length() : word.length() + 1) > cols) {
|
||||
lineBreak();
|
||||
} else {
|
||||
@ -135,8 +137,8 @@ public class LineBreaker {
|
||||
for (int j = 0; j < indent; j++)
|
||||
outText.append(" ");
|
||||
pos = indent;
|
||||
containsLineBreak = true;
|
||||
isFirst = true;
|
||||
lines++;
|
||||
}
|
||||
}
|
||||
|
||||
@ -159,4 +161,11 @@ public class LineBreaker {
|
||||
this.toHTML = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the number of created lines
|
||||
*/
|
||||
public int getLineCount() {
|
||||
return lines;
|
||||
}
|
||||
}
|
||||
|
@ -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"));
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user