001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.swing;
024:
025: import static org.isqlviewer.UserOptions.DEF_CONSOLE_ERR_COLOR;
026: import static org.isqlviewer.UserOptions.DEF_CONSOLE_FG_COLOR;
027: import static org.isqlviewer.UserOptions.DEF_CONSOLE_FONT;
028: import static org.isqlviewer.UserOptions.DEF_CONSOLE_SQL_COLOR;
029: import static org.isqlviewer.UserOptions.DEF_CONSOLE_TIME_FMT;
030: import static org.isqlviewer.UserOptions.KEY_CONSOLE_ERR_COLOR;
031: import static org.isqlviewer.UserOptions.KEY_CONSOLE_FG_COLOR;
032: import static org.isqlviewer.UserOptions.KEY_CONSOLE_FONT;
033: import static org.isqlviewer.UserOptions.KEY_CONSOLE_SQL_COLOR;
034: import static org.isqlviewer.UserOptions.KEY_CONSOLE_TIME_FMT;
035:
036: import java.awt.Color;
037: import java.awt.Font;
038: import java.text.SimpleDateFormat;
039: import java.util.Date;
040: import java.util.prefs.PreferenceChangeEvent;
041: import java.util.prefs.PreferenceChangeListener;
042: import java.util.prefs.Preferences;
043:
044: import javax.swing.text.BadLocationException;
045: import javax.swing.text.Document;
046: import javax.swing.text.JTextComponent;
047: import javax.swing.text.SimpleAttributeSet;
048: import javax.swing.text.StyleConstants;
049:
050: import org.apache.log4j.AppenderSkeleton;
051: import org.apache.log4j.Level;
052: import org.apache.log4j.spi.LoggingEvent;
053: import org.apache.log4j.spi.ThrowableInformation;
054: import org.isqlviewer.util.IsqlToolkit;
055: import org.isqlviewer.util.StringUtilities;
056:
057: /**
058: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
059: * @version 1.0
060: */
061: public class DocumentAppender extends AppenderSkeleton implements
062: PreferenceChangeListener {
063:
064: private Document document = null;
065: private JTextComponent view = null;
066: private SimpleDateFormat sdf = new SimpleDateFormat("");
067: private Preferences preferences = Preferences.userRoot().node(
068: IsqlToolkit.getRootPreferencesNode());
069:
070: public DocumentAppender(Document document, JTextComponent view) {
071:
072: super ();
073: this .document = document;
074: this .view = view;
075: sdf.applyPattern(preferences.get(KEY_CONSOLE_TIME_FMT,
076: DEF_CONSOLE_TIME_FMT));
077: String fontString = preferences.get(KEY_CONSOLE_FONT,
078: DEF_CONSOLE_FONT);
079: view.setFont(StringUtilities.parseFontString(fontString));
080: }
081:
082: @Override
083: protected void append(LoggingEvent event) {
084:
085: SimpleAttributeSet sas = new SimpleAttributeSet();
086: Font font = view.getFont();
087: StyleConstants.setFontFamily(sas, font.getFamily());
088: StyleConstants.setFontSize(sas, font.getSize());
089: StyleConstants.setBold(sas, font.isBold());
090: StyleConstants.setItalic(sas, font.isItalic());
091: StyleConstants.setBackground(sas, view.getBackground());
092:
093: int last = document.getLength();
094: try {
095: Color color = null;
096: Level level = event.getLevel();
097: switch (level.toInt()) {
098: case Level.DEBUG_INT:
099: case Level.WARN_INT:
100: color = Color.decode(preferences.get(
101: KEY_CONSOLE_SQL_COLOR, DEF_CONSOLE_SQL_COLOR));
102: break;
103: case Level.FATAL_INT:
104: case Level.ERROR_INT:
105: color = Color.decode(preferences.get(
106: KEY_CONSOLE_ERR_COLOR, DEF_CONSOLE_ERR_COLOR));
107: break;
108: default:
109: color = Color.decode(preferences.get(
110: KEY_CONSOLE_FG_COLOR, DEF_CONSOLE_FG_COLOR));
111: break;
112: }
113: StyleConstants.setForeground(sas, color);
114: document.insertString(last, format(event), sas);
115: document.insertString(last, "\n", null);
116: last = document.getLength();
117: } catch (BadLocationException ignored) {
118:
119: } finally {
120: view.setCaretPosition(last);
121: }
122: }
123:
124: public void close() {
125:
126: }
127:
128: public boolean requiresLayout() {
129:
130: return false;
131: }
132:
133: public void preferenceChange(PreferenceChangeEvent event) {
134:
135: if (event == null)
136: return;
137:
138: String key = event.getKey();
139: String val = event.getNewValue();
140:
141: if (key.equals(KEY_CONSOLE_TIME_FMT)) {
142: sdf.applyPattern(val);
143: }
144: }
145:
146: private String format(LoggingEvent event) {
147:
148: StringBuilder builder = new StringBuilder(32);
149:
150: builder.append(sdf.format(new Date(event.timeStamp)));
151: builder.append(' ');
152: builder.append(event.getRenderedMessage());
153:
154: ThrowableInformation errors = event.getThrowableInformation();
155: if (errors != null) {
156: String[] stackTrace = errors.getThrowableStrRep();
157: for (int i = 0; i < stackTrace.length; i++) {
158: builder.append('\n');
159: builder.append('\t');
160: builder.append(stackTrace[i].trim());
161: }
162: }
163: return builder.toString();
164: }
165: }
|