01: /*
02: * TextAreaLogAppender.java
03: *
04: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
05: *
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU General Public License
08: * as published by the Free Software Foundation; either version 2
09: * of the License, or any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU General Public License for more details.
15: *
16: * You should have received a copy of the GNU General Public License
17: * along with this program; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19: *
20: */
21:
22: package org.executequery.components;
23:
24: import java.util.regex.Matcher;
25: import java.util.regex.Pattern;
26: import javax.swing.JTextArea;
27: import org.apache.log4j.AppenderSkeleton;
28: import org.apache.log4j.Layout;
29: import org.apache.log4j.spi.LoggingEvent;
30: import org.executequery.Constants;
31:
32: /* ----------------------------------------------------------
33: * CVS NOTE: Changes to the CVS repository prior to the
34: * release of version 3.0.0beta1 has meant a
35: * resetting of CVS revision numbers.
36: * ----------------------------------------------------------
37: */
38:
39: /**
40: *
41: * @author Takis Diakoumis
42: * @version $Revision: 1.5 $
43: * @date $Date: 2006/05/14 06:56:53 $
44: */
45: public class TextAreaLogAppender extends AppenderSkeleton {
46:
47: /** the text area component to be appended to */
48: private JTextArea textArea;
49:
50: /** matcher to remove new lines from log messages */
51: private Matcher newLineMatcher;
52:
53: /** space string for new line replacement */
54: private static final String SPACE = " ";
55:
56: /** Creates a new instance of TextAreaLogAppender */
57: public TextAreaLogAppender(JTextArea textArea) {
58: this .textArea = textArea;
59: newLineMatcher = Pattern.compile("[\n\r]+").matcher("");
60: }
61:
62: public boolean requiresLayout() {
63: return true;
64: }
65:
66: public synchronized void append(LoggingEvent event) {
67: String text = null;
68: Object message = event.getMessage();
69: if (message instanceof String) {
70: text = (String) message;
71: if (text.length() == 1
72: && Character.isWhitespace(text.charAt(0))) {
73: return;
74: }
75: }
76:
77: text = layout.format(event);
78: newLineMatcher.reset(text);
79: text = newLineMatcher.replaceAll(SPACE).trim();
80:
81: textArea.append(text);
82: textArea.append(Layout.LINE_SEP);
83:
84: String[] s = event.getThrowableStrRep();
85: if (s != null) {
86: int len = s.length;
87: for (int i = 0; i < len; i++) {
88: textArea.append(s[i]);
89: textArea.append(Layout.LINE_SEP);
90: }
91: }
92:
93: textArea.setCaretPosition(textArea.getText().length());
94: }
95:
96: public void close() {
97: }
98:
99: }
|