package de.gurkenlabs.utiliti.swing;

import java.awt.Color;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.MessageFormat;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import de.gurkenlabs.utiliti.Style;

@SuppressWarnings("serial")
public class ConsoleComponent extends JScrollPane {
  public ConsoleComponent() {
    Logger root = Logger.getLogger("");
    JTextPane consoleTextArea = new JTextPane();
    this.setViewportBorder(null);
    this.setViewportView(consoleTextArea);

    consoleTextArea.setEditable(false);
    consoleTextArea.setAutoscrolls(true);
    root.addHandler(new LogHandler(consoleTextArea));
  }

  private class LogHandler extends java.util.logging.Handler {
    private final JTextPane textPane;

    public LogHandler(final JTextPane textPane) {
      this.textPane = textPane;
    }

    @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());
    }

    @Override
    public void flush() {
      // nothing to flush here -> writing to a control
    }

    @Override
    public void close() {
      // nothing to close here -> writing to a control
    }

    private Color getColor(Level level) {
      if (level == Level.SEVERE) {
        return Color.RED;
      } else if (level == Level.WARNING) {
        return Color.ORANGE;
      }

      return Color.WHITE;
    }
  }
}

@@ -595,16 +595,60 @@
 lPane %7B%0A
+  private final  JTextPane consoleTextArea;%0A
   public
@@ -713,26 +713,21 @@
 %22);%0A    
-JTextPane 
+this.
 consoleT
@@ -812,16 +812,21 @@
 ortView(
+this.
 consoleT
@@ -836,24 +836,29 @@
 Area);%0A%0A    
+this.
 consoleTextA
@@ -885,16 +885,21 @@
 e);%0A    
+this.
 consoleT
@@ -963,16 +963,21 @@
 Handler(
+this.
 consoleT
@@ -3058,17 +3058,59 @@
 n Co
-lor.WHITE
+nsoleComponent.this.consoleTextArea.getForeground()
 ;%0A  

Use default foreground color for normal log messages