001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.toolkit.log4j;
025:
026: import jacareto.system.Customization;
027: import jacareto.system.Environment;
028:
029: import org.apache.log4j.AppenderSkeleton;
030: import org.apache.log4j.PatternLayout;
031: import org.apache.log4j.spi.LoggingEvent;
032:
033: import javax.swing.JTextArea;
034: import javax.swing.event.DocumentEvent;
035: import javax.swing.event.DocumentListener;
036:
037: /**
038: * A text area which may serve as a message display for logs.
039: *
040: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
041: * @version 0.76
042: */
043: public class TextAreaAppender extends AppenderSkeleton implements
044: DocumentListener {
045: /** The text area. */
046: private JTextArea textArea;
047:
048: /** The layout. */
049: private PatternLayout layout;
050:
051: /**
052: * Creates a new text area appender.
053: *
054: * @param env the environment
055: */
056: public TextAreaAppender(Environment env) {
057: super ();
058:
059: Customization customization = env.getCustomization();
060:
061: textArea = new JTextArea();
062: textArea.setRows(customization.getInt("TextAreaAppender.Rows",
063: 10));
064: textArea.setColumns(customization.getInt(
065: "TextAreaAppender.Columns", 40));
066: textArea.setLineWrap(customization.getBoolean(
067: "TextAreaAppender.LineWrap", true));
068: textArea.setEditable(false);
069: textArea.getDocument().addDocumentListener(this );
070:
071: layout = new PatternLayout("%-5p %3x - %m%n");
072: }
073:
074: /**
075: * Returns the text area this appender appends to.
076: *
077: * @return DOCUMENT ME!
078: */
079: public JTextArea getTextArea() {
080: return textArea;
081: }
082:
083: /**
084: * Appends the logging event information to the text area.
085: *
086: * @param loggingEvent the logging event
087: */
088: public void append(LoggingEvent loggingEvent) {
089: textArea.append(layout.format(loggingEvent));
090: }
091:
092: /**
093: * Returns <code>true</code>
094: *
095: * @return DOCUMENT ME!
096: */
097: public boolean requiresLayout() {
098: return true;
099: }
100:
101: /**
102: * Closes this appender (does nothing).
103: */
104: public void close() {
105: }
106:
107: /**
108: * Does nothing.
109: *
110: * @param event the document event.
111: */
112: public void changedUpdate(DocumentEvent event) {
113: }
114:
115: /**
116: * Does nothing.
117: *
118: * @param event the document event.
119: */
120: public void removeUpdate(DocumentEvent event) {
121: }
122:
123: /**
124: * Sets the caret position to the end of the text in the text component.
125: *
126: * @param event the document event.
127: */
128: public void insertUpdate(DocumentEvent event) {
129: textArea.setCaretPosition(textArea.getDocument().getLength());
130: }
131: }
|