prototype Java log window and sticky configuration
[jalview.git] / src / jalview / gui / JalviewAppender.java
1 package jalview.gui;
2
3 import javax.swing.JTextArea;
4 import javax.swing.SwingUtilities;
5
6 import org.apache.log4j.WriterAppender;
7 import org.apache.log4j.spi.LoggingEvent;
8
9 /**
10  * From http://textareaappender.zcage.com/
11  * the means to capture the logs, too.
12  * Simple example of creating a Log4j appender that will
13  * write to a JTextArea. 
14  */
15 public class JalviewAppender extends WriterAppender {
16         
17         static private JTextArea jTextArea = null;
18         
19         /** Set the target JTextArea for the logging information to appear. */
20         static public void setTextArea(JTextArea jTextArea) {
21                 JalviewAppender.jTextArea = jTextArea;
22         }
23         /**
24          * Format and then append the loggingEvent to the stored
25          * JTextArea.
26          */
27         public void append(LoggingEvent loggingEvent) {
28                 final String message = this.layout.format(loggingEvent);
29
30                 // Append formatted message to textarea using the Swing Thread.
31                 SwingUtilities.invokeLater(new Runnable() {
32                         public void run() {
33                           if (jTextArea!=null) {
34                             jTextArea.append(message);
35                           }
36                         }
37                 });
38         }
39 }