Search in sources :

Example 61 with StyledDocument

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());
}
Also used : SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) StringWriter(java.io.StringWriter) StyledDocument(javax.swing.text.StyledDocument) BadLocationException(javax.swing.text.BadLocationException) PrintWriter(java.io.PrintWriter)

Example 62 with StyledDocument

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());
}
Also used : JTextPane(javax.swing.JTextPane) LogRecord(java.util.logging.LogRecord) StyledDocument(javax.swing.text.StyledDocument) Test(org.junit.jupiter.api.Test)

Example 63 with StyledDocument

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());
}
Also used : SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) StyledDocument(javax.swing.text.StyledDocument) BadLocationException(javax.swing.text.BadLocationException)

Aggregations

StyledDocument (javax.swing.text.StyledDocument)63 BadLocationException (javax.swing.text.BadLocationException)29 Style (javax.swing.text.Style)18 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)12 Point (java.awt.Point)7 JTextPane (javax.swing.JTextPane)7 DefaultStyledDocument (javax.swing.text.DefaultStyledDocument)7 ArrayList (java.util.ArrayList)4 JLabel (javax.swing.JLabel)4 Test (org.junit.jupiter.api.Test)4 Font (java.awt.Font)3 LogRecord (java.util.logging.LogRecord)3 Matcher (java.util.regex.Matcher)3 ImageIcon (javax.swing.ImageIcon)3 JPanel (javax.swing.JPanel)3 PersistentArrayMap (clojure.lang.PersistentArrayMap)2 BorderLayout (java.awt.BorderLayout)2 Dimension (java.awt.Dimension)2 Rectangle (java.awt.Rectangle)2 IOException (java.io.IOException)2