001: /* Copyright (C) 2003 Finalist IT Group
002: *
003: * This file is part of JAG - the Java J2EE Application Generator
004: *
005: * JAG is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: * JAG is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: * You should have received a copy of the GNU General Public License
014: * along with JAG; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
016: */
017:
018: package com.finalist.jaggenerator;
019:
020: import java.awt.Color;
021: import java.util.HashMap;
022:
023: import javax.swing.JTextArea;
024: import javax.swing.JTextPane;
025: import javax.swing.text.AttributeSet;
026: import javax.swing.text.BadLocationException;
027: import javax.swing.text.JTextComponent;
028: import javax.swing.text.SimpleAttributeSet;
029: import javax.swing.text.StyleConstants;
030:
031: import org.apache.log4j.lf5.LogLevel;
032:
033: /**
034: * Yet another logger implementation... ;) This logger logs to the JTextArea in
035: * the JAG gui.
036: *
037: * @author Michael O'Connor - Finalist IT Group
038: */
039: public class ConsoleLogger {
040:
041: public static final HashMap styleMap = new HashMap();
042:
043: static SimpleAttributeSet BLACK = new SimpleAttributeSet();
044: static SimpleAttributeSet YELLOW = new SimpleAttributeSet();
045: static SimpleAttributeSet RED = new SimpleAttributeSet();
046:
047: static {
048: styleMap.put(LogLevel.INFO, BLACK);
049: styleMap.put(LogLevel.DEBUG, YELLOW);
050: styleMap.put(LogLevel.ERROR, RED);
051:
052: StyleConstants.setForeground(BLACK, Color.BLACK);
053: StyleConstants.setFontFamily(BLACK, "Lucida");
054: StyleConstants.setFontSize(BLACK, 10);
055:
056: StyleConstants.setForeground(YELLOW, Color.YELLOW);
057: StyleConstants.setFontFamily(YELLOW, "Lucida");
058: StyleConstants.setFontSize(YELLOW, 10);
059:
060: StyleConstants.setForeground(RED, Color.RED);
061: StyleConstants.setFontFamily(RED, "Lucida");
062: StyleConstants.setFontSize(RED, 10);
063: }
064:
065: private boolean colored;
066:
067: private JTextComponent genericConsole;
068:
069: /**
070: * Creates a ConsoleLogger.
071: *
072: * @param console
073: */
074: public ConsoleLogger(JTextArea console) {
075: setGenericConsole(console);
076: setColored(false);
077: }
078:
079: /**
080: * Creates a ConsoleLogger with color support.
081: *
082: * @param console
083: */
084: public ConsoleLogger(JTextPane console) {
085: setGenericConsole(console);
086: setColored(true);
087: }
088:
089: /**
090: * Appends a log to the end of the console.
091: *
092: * @param message
093: * the log message.
094: */
095: public void log(String message) {
096: log(message, LogLevel.INFO);
097: }
098:
099: /**
100: * Appends a log to the end of the console.
101: *
102: * @param message
103: * the log message.
104: * @param level
105: * the level, log4j style.
106: */
107: public void log(String message, LogLevel level) {
108: if (isColored()) {
109: insertText(message, getSyle4Level(level));
110: } else
111: insertText(message, BLACK);
112: insertText("\n", BLACK);
113: setEndSelection();
114: }
115:
116: private SimpleAttributeSet getSyle4Level(LogLevel level) {
117: SimpleAttributeSet c = (SimpleAttributeSet) ConsoleLogger.styleMap
118: .get(level);
119: SimpleAttributeSet ret = BLACK;
120: if (c != null)
121: ret = c;
122:
123: return ret;
124: }
125:
126: public boolean isColored() {
127: return colored;
128: }
129:
130: public void setColored(boolean colored) {
131: this .colored = colored;
132: }
133:
134: public JTextComponent getGenericConsole() {
135: return genericConsole;
136: }
137:
138: public void setGenericConsole(JTextComponent genericConsole) {
139: this .genericConsole = genericConsole;
140: }
141:
142: protected void insertText(String text, AttributeSet set) {
143: try {
144: genericConsole.getDocument()
145: .insertString(
146: genericConsole.getDocument().getLength(),
147: text, set);
148: } catch (BadLocationException e) {
149: e.printStackTrace();
150: }
151: }
152:
153: // Needed for inserting icons in the right places
154: protected void setEndSelection() {
155: genericConsole.setSelectionStart(genericConsole.getDocument()
156: .getLength());
157: genericConsole.setSelectionEnd(genericConsole.getDocument()
158: .getLength());
159: }
160:
161: /**
162: * Appends an red error log to the end of the console.
163: *
164: * @param message
165: * the log message.
166: */
167: public void error(String message) {
168: log(message, LogLevel.ERROR);
169: }
170:
171: /**
172: * Appends an yellow debug log to the end of the console.
173: *
174: * @param message
175: * the log message.
176: */
177: public void debug(String message) {
178: log(message, LogLevel.DEBUG);
179: }
180: }
|