KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jaggenerator > ConsoleLogger


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.jaggenerator;
19
20 import java.awt.Color JavaDoc;
21 import java.util.HashMap JavaDoc;
22
23 import javax.swing.JTextArea JavaDoc;
24 import javax.swing.JTextPane JavaDoc;
25 import javax.swing.text.AttributeSet JavaDoc;
26 import javax.swing.text.BadLocationException JavaDoc;
27 import javax.swing.text.JTextComponent JavaDoc;
28 import javax.swing.text.SimpleAttributeSet JavaDoc;
29 import javax.swing.text.StyleConstants JavaDoc;
30
31 import org.apache.log4j.lf5.LogLevel;
32
33 /**
34  * Yet another logger implementation... ;) This logger logs to the JTextArea in
35  * the JAG gui.
36  *
37  * @author Michael O'Connor - Finalist IT Group
38  */

39 public class ConsoleLogger {
40
41     public static final HashMap JavaDoc styleMap = new HashMap JavaDoc();
42     
43     static SimpleAttributeSet JavaDoc BLACK = new SimpleAttributeSet JavaDoc();
44     static SimpleAttributeSet JavaDoc YELLOW = new SimpleAttributeSet JavaDoc();
45     static SimpleAttributeSet JavaDoc RED = new SimpleAttributeSet JavaDoc();
46
47     static {
48         styleMap.put(LogLevel.INFO, BLACK);
49         styleMap.put(LogLevel.DEBUG, YELLOW);
50         styleMap.put(LogLevel.ERROR, RED);
51         
52         StyleConstants.setForeground(BLACK, Color.BLACK);
53         StyleConstants.setFontFamily(BLACK, "Lucida");
54         StyleConstants.setFontSize(BLACK, 10);
55         
56         StyleConstants.setForeground(YELLOW, Color.YELLOW);
57         StyleConstants.setFontFamily(YELLOW, "Lucida");
58         StyleConstants.setFontSize(YELLOW, 10);
59         
60         StyleConstants.setForeground(RED, Color.RED);
61         StyleConstants.setFontFamily(RED, "Lucida");
62         StyleConstants.setFontSize(RED, 10);
63     }
64
65     private boolean colored;
66     
67     private JTextComponent JavaDoc genericConsole;
68     /**
69      * Creates a ConsoleLogger.
70      *
71      * @param console
72      */

73     public ConsoleLogger(JTextArea JavaDoc console) {
74         setGenericConsole(console);
75         setColored(false);
76     }
77     /**
78      * Creates a ConsoleLogger with color support.
79      *
80      * @param console
81      */

82     public ConsoleLogger(JTextPane JavaDoc console) {
83         setGenericConsole(console);
84         setColored(true);
85     }
86
87     /**
88      * Appends a log to the end of the console.
89      *
90      * @param message
91      * the log message.
92      */

93     public void log(String JavaDoc message) {
94         log(message, LogLevel.INFO);
95     }
96
97     /**
98      * Appends a log to the end of the console.
99      *
100      * @param message
101      * the log message.
102      * @param level
103      * the level, log4j style.
104      */

105     public void log(String JavaDoc message, LogLevel level) {
106         if(isColored())
107         {
108             insertText(message, getSyle4Level(level));
109         } else
110             insertText(message, BLACK);
111         insertText("\n", BLACK);
112         setEndSelection();
113     }
114
115     private SimpleAttributeSet JavaDoc getSyle4Level(LogLevel level) {
116         SimpleAttributeSet JavaDoc c = (SimpleAttributeSet JavaDoc ) ConsoleLogger.styleMap.get(level);
117         SimpleAttributeSet JavaDoc ret = BLACK;
118         if (c != null)
119             ret = c;
120
121         return ret;
122     }
123     public boolean isColored() {
124         return colored;
125     }
126     public void setColored(boolean colored) {
127         this.colored = colored;
128     }
129     public JTextComponent JavaDoc getGenericConsole() {
130         return genericConsole;
131     }
132     public void setGenericConsole(JTextComponent JavaDoc genericConsole) {
133         this.genericConsole = genericConsole;
134     }
135     
136     protected void insertText(String JavaDoc text, AttributeSet JavaDoc set) {
137         try {
138           genericConsole.getDocument().insertString(
139                   genericConsole.getDocument().getLength(), text, set);
140         } catch (BadLocationException JavaDoc e) {
141           e.printStackTrace();
142         }
143       }
144
145     // Needed for inserting icons in the right places
146
protected void setEndSelection() {
147       genericConsole.setSelectionStart(genericConsole.getDocument().getLength());
148       genericConsole.setSelectionEnd(genericConsole.getDocument().getLength());
149     }
150     
151     /**
152      * Appends an red error log to the end of the console.
153      *
154      * @param message
155      * the log message.
156      */

157     public void error(String JavaDoc message) {
158         log(message, LogLevel.ERROR);
159     }
160     
161     /**
162      * Appends an yellow debug log to the end of the console.
163      *
164      * @param message
165      * the log message.
166      */

167     public void debug(String JavaDoc message) {
168         log(message, LogLevel.DEBUG);
169     }
170 }
171
Popular Tags