use of javax.swing.text.StyledDocument in project litiengine by gurkenlabs.
the class LogHandler method publish.
@Override
public void publish(final LogRecord record) {
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, getColor(record.getLevel()));
StyleConstants.setBold(keyWord, true);
StyleConstants.setFontSize(keyWord, 12);
StyleConstants.setFontFamily(keyWord, Style.FONTNAME_CONSOLE);
SimpleAttributeSet text = new SimpleAttributeSet();
StyleConstants.setForeground(text, getColor(record.getLevel()));
StyleConstants.setFontFamily(text, Style.FONTNAME_CONSOLE);
String message;
if (record.getParameters() != null) {
message = MessageFormat.format(record.getMessage(), record.getParameters());
} else {
message = record.getMessage();
}
if (record.getLevel() == Level.SEVERE && record.getThrown() != null) {
StringWriter writer = new StringWriter();
record.getThrown().printStackTrace(new PrintWriter(writer));
message = writer.toString();
}
try {
doc.insertString(doc.getLength(), String.format("%1$-10s", record.getLevel()), keyWord);
doc.insertString(doc.getLength(), message, text);
doc.insertString(doc.getLength(), "\n", text);
} catch (BadLocationException e) {
// if an exception occurs while logging, just ignore it
}
textPane.setCaretPosition(doc.getLength());
}
use of javax.swing.text.StyledDocument in project litiengine by gurkenlabs.
the class LogHandlerTest method publish.
@Test
void publish() {
JTextPane textPane = new JTextPane();
LogHandler logHandler = new LogHandler(textPane);
StyledDocument styledDocument = textPane.getStyledDocument();
assertEquals(0, styledDocument.getLength());
assertEquals(0, textPane.getCaretPosition());
logHandler.publish(new LogRecord(Level.INFO, "Hello World"));
logHandler.publish(new LogRecord(Level.SEVERE, "This is a severe test!"));
assertEquals(55, styledDocument.getLength());
assertEquals(55, textPane.getCaretPosition());
}
use of javax.swing.text.StyledDocument in project ripme by RipMeApp.
the class MainWindow method appendLog.
/**
* Write a line to the Log section of the GUI
*
* @param text the string to log
* @param color the color of the line
*/
private void appendLog(final String text, final Color color) {
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setForeground(sas, color);
StyledDocument sd = logText.getStyledDocument();
try {
synchronized (this) {
sd.insertString(sd.getLength(), text + "\n", sas);
}
} catch (BadLocationException e) {
}
logText.setCaretPosition(sd.getLength());
}
Aggregations