KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > gui > TextPaneAppender


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j.gui;
18
19
20 import java.awt.Color JavaDoc;
21 import java.awt.Image JavaDoc;
22 import java.awt.Toolkit JavaDoc;
23 import java.io.*;
24 import java.net.URL JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27 import java.util.Hashtable JavaDoc;
28
29 import javax.swing.Icon JavaDoc;
30 import javax.swing.ImageIcon JavaDoc;
31 import javax.swing.JTextPane JavaDoc;
32 import javax.swing.text.AttributeSet JavaDoc;
33 import javax.swing.text.BadLocationException JavaDoc;
34 import javax.swing.text.MutableAttributeSet JavaDoc;
35 import javax.swing.text.SimpleAttributeSet JavaDoc;
36 import javax.swing.text.StyleConstants JavaDoc;
37 import javax.swing.text.StyledDocument JavaDoc;
38 import javax.swing.text.TabSet JavaDoc;
39 import javax.swing.text.TabStop JavaDoc;
40
41 import org.apache.log4j.*;
42
43 import org.apache.log4j.spi.LoggingEvent;
44 import org.apache.log4j.helpers.Loader;
45 import org.apache.log4j.helpers.QuietWriter;
46 import org.apache.log4j.helpers.TracerPrintWriter;
47 import org.apache.log4j.helpers.OptionConverter;
48
49
50 /**
51  * <b>Experimental</b> TextPaneAppender. <br>
52  *
53  *
54  * Created: Sat Feb 26 18:50:27 2000 <br>
55  *
56  * @author Sven Reimers
57  */

58
59 public class TextPaneAppender extends AppenderSkeleton {
60     
61   JTextPane JavaDoc textpane;
62   StyledDocument JavaDoc doc;
63   TracerPrintWriter tp;
64   StringWriter sw;
65   QuietWriter qw;
66   Hashtable JavaDoc attributes;
67   Hashtable JavaDoc icons;
68   
69   private String JavaDoc label;
70   
71   private boolean fancy;
72     
73   final String JavaDoc LABEL_OPTION = "Label";
74   final String JavaDoc COLOR_OPTION_FATAL = "Color.Emerg";
75   final String JavaDoc COLOR_OPTION_ERROR = "Color.Error";
76   final String JavaDoc COLOR_OPTION_WARN = "Color.Warn";
77   final String JavaDoc COLOR_OPTION_INFO = "Color.Info";
78   final String JavaDoc COLOR_OPTION_DEBUG = "Color.Debug";
79   final String JavaDoc COLOR_OPTION_BACKGROUND = "Color.Background";
80   final String JavaDoc FANCY_OPTION = "Fancy";
81   final String JavaDoc FONT_NAME_OPTION = "Font.Name";
82   final String JavaDoc FONT_SIZE_OPTION = "Font.Size";
83   
84   public static Image JavaDoc loadIcon ( String JavaDoc path ) {
85     Image JavaDoc img = null;
86     try {
87       URL JavaDoc url = ClassLoader.getSystemResource(path);
88       img = (Image JavaDoc) (Toolkit.getDefaultToolkit()).getImage(url);
89     } catch (Exception JavaDoc e) {
90       System.out.println("Exception occured: " + e.getMessage() +
91              " - " + e );
92     }
93     return (img);
94   }
95   
96   public TextPaneAppender(Layout layout, String JavaDoc name) {
97     this();
98     this.layout = layout;
99     this.name = name;
100     setTextPane(new JTextPane JavaDoc());
101     createAttributes();
102     createIcons();
103   }
104     
105   public TextPaneAppender() {
106     super();
107     setTextPane(new JTextPane JavaDoc());
108     createAttributes();
109     createIcons();
110     this.label="";
111     this.sw = new StringWriter();
112     this.qw = new QuietWriter(sw, errorHandler);
113     this.tp = new TracerPrintWriter(qw);
114     this.fancy =true;
115   }
116
117   public
118   void close() {
119     
120   }
121   
122   private void createAttributes() {
123     Priority prio[] = Priority.getAllPossiblePriorities();
124     
125     attributes = new Hashtable JavaDoc();
126     for (int i=0; i<prio.length;i++) {
127       MutableAttributeSet JavaDoc att = new SimpleAttributeSet JavaDoc();
128       attributes.put(prio[i], att);
129       StyleConstants.setFontSize(att,14);
130     }
131     StyleConstants.setForeground((MutableAttributeSet JavaDoc)attributes.get(Priority.ERROR),Color.red);
132     StyleConstants.setForeground((MutableAttributeSet JavaDoc)attributes.get(Priority.WARN),Color.orange);
133     StyleConstants.setForeground((MutableAttributeSet JavaDoc)attributes.get(Priority.INFO),Color.gray);
134     StyleConstants.setForeground((MutableAttributeSet JavaDoc)attributes.get(Priority.DEBUG),Color.black);
135   }
136
137   private void createIcons() {
138     Priority prio[] = Priority.getAllPossiblePriorities();
139     
140     icons = new Hashtable JavaDoc();
141     for (int i=0; i<prio.length;i++) {
142       if (prio[i].equals(Priority.FATAL))
143     icons.put(prio[i],new ImageIcon JavaDoc(loadIcon("icons/RedFlag.gif")));
144       if (prio[i].equals(Priority.ERROR))
145     icons.put(prio[i],new ImageIcon JavaDoc(loadIcon("icons/RedFlag.gif")));
146       if (prio[i].equals(Priority.WARN))
147     icons.put(prio[i],new ImageIcon JavaDoc(loadIcon("icons/BlueFlag.gif")));
148       if (prio[i].equals(Priority.INFO))
149     icons.put(prio[i],new ImageIcon JavaDoc(loadIcon("icons/GreenFlag.gif")));
150       if (prio[i].equals(Priority.DEBUG))
151     icons.put(prio[i],new ImageIcon JavaDoc(loadIcon("icons/GreenFlag.gif")));
152     }
153   }
154
155   public void append(LoggingEvent event) {
156     String JavaDoc text = this.layout.format(event);
157     String JavaDoc trace="";
158     // Print Stacktrace
159
// Quick Hack maybe there is a better/faster way?
160
if (event.throwable!=null) {
161       event.throwable.printStackTrace(tp);
162       for (int i=0; i< sw.getBuffer().length(); i++) {
163     if (sw.getBuffer().charAt(i)=='\t')
164       sw.getBuffer().replace(i,i+1," ");
165       }
166       trace = sw.toString();
167       sw.getBuffer().delete(0,sw.getBuffer().length());
168     }
169     try {
170       if (fancy) {
171     textpane.setEditable(true);
172     textpane.insertIcon((ImageIcon JavaDoc)icons.get(event.priority));
173     textpane.setEditable(false);
174       }
175       doc.insertString(doc.getLength(),text+trace,
176                (MutableAttributeSet JavaDoc)attributes.get(event.priority));
177     }
178     catch (BadLocationException JavaDoc badex) {
179       System.err.println(badex);
180     }
181     textpane.setCaretPosition(doc.getLength());
182   }
183   
184   public
185   JTextPane JavaDoc getTextPane() {
186     return textpane;
187   }
188   
189   private
190   static
191   Color JavaDoc parseColor (String JavaDoc v) {
192     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(v,",");
193     int val[] = {255,255,255,255};
194     int i=0;
195     while (st.hasMoreTokens()) {
196       val[i]=Integer.parseInt(st.nextToken());
197       i++;
198     }
199     return new Color JavaDoc(val[0],val[1],val[2],val[3]);
200   }
201   
202   private
203   static
204   String JavaDoc colorToString(Color JavaDoc c) {
205     // alpha component emitted only if not default (255)
206
String JavaDoc res = ""+c.getRed()+","+c.getGreen()+","+c.getBlue();
207     return c.getAlpha() >= 255 ? res : res + ","+c.getAlpha();
208   }
209
210   public
211   void setLayout(Layout layout) {
212     this.layout=layout;
213   }
214   
215   public
216   void setName(String JavaDoc name) {
217     this.name = name;
218   }
219   
220     
221   public
222   void setTextPane(JTextPane JavaDoc textpane) {
223     this.textpane=textpane;
224     textpane.setEditable(false);
225     textpane.setBackground(Color.lightGray);
226     this.doc=textpane.getStyledDocument();
227   }
228           
229   private
230   void setColor(Priority p, String JavaDoc v) {
231     StyleConstants.setForeground(
232               (MutableAttributeSet JavaDoc)attributes.get(p),parseColor(v));
233   }
234   
235   private
236   String JavaDoc getColor(Priority p) {
237     Color JavaDoc c = StyleConstants.getForeground(
238               (MutableAttributeSet JavaDoc)attributes.get(p));
239     return c == null ? null : colorToString(c);
240   }
241   
242   /////////////////////////////////////////////////////////////////////
243
// option setters and getters
244

245   public
246   void setLabel(String JavaDoc label) {
247     this.label = label;
248   }
249   public
250   String JavaDoc getLabel() {
251     return label;
252   }
253   
254   public
255   void setColorEmerg(String JavaDoc color) {
256     setColor(Priority.FATAL, color);
257   }
258   public
259   String JavaDoc getColorEmerg() {
260     return getColor(Priority.FATAL);
261   }
262   
263   public
264   void setColorError(String JavaDoc color) {
265     setColor(Priority.ERROR, color);
266   }
267   public
268   String JavaDoc getColorError() {
269     return getColor(Priority.ERROR);
270   }
271   
272   public
273   void setColorWarn(String JavaDoc color) {
274     setColor(Priority.WARN, color);
275   }
276   public
277   String JavaDoc getColorWarn() {
278     return getColor(Priority.WARN);
279   }
280   
281   public
282   void setColorInfo(String JavaDoc color) {
283     setColor(Priority.INFO, color);
284   }
285   public
286   String JavaDoc getColorInfo() {
287     return getColor(Priority.INFO);
288   }
289   
290   public
291   void setColorDebug(String JavaDoc color) {
292     setColor(Priority.DEBUG, color);
293   }
294   public
295   String JavaDoc getColorDebug() {
296     return getColor(Priority.DEBUG);
297   }
298   
299   public
300   void setColorBackground(String JavaDoc color) {
301     textpane.setBackground(parseColor(color));
302   }
303   public
304   String JavaDoc getColorBackground() {
305     return colorToString(textpane.getBackground());
306   }
307   
308   public
309   void setFancy(boolean fancy) {
310     this.fancy = fancy;
311   }
312   public
313   boolean getFancy() {
314     return fancy;
315   }
316   
317   public
318   void setFontSize(int size) {
319     Enumeration JavaDoc e = attributes.elements();
320     while (e.hasMoreElements()) {
321       StyleConstants.setFontSize((MutableAttributeSet JavaDoc)e.nextElement(),size);
322     }
323     return;
324   }
325   
326   public
327   int getFontSize() {
328     AttributeSet JavaDoc attrSet = (AttributeSet JavaDoc) attributes.get(Priority.INFO);
329     return StyleConstants.getFontSize(attrSet);
330   }
331   
332   public
333   void setFontName(String JavaDoc name) {
334     Enumeration JavaDoc e = attributes.elements();
335     while (e.hasMoreElements()) {
336       StyleConstants.setFontFamily((MutableAttributeSet JavaDoc)e.nextElement(),name);
337     }
338     return;
339   }
340   
341   public
342   String JavaDoc getFontName() {
343     AttributeSet JavaDoc attrSet = (AttributeSet JavaDoc) attributes.get(Priority.INFO);
344     return StyleConstants.getFontFamily(attrSet);
345   }
346
347   public
348   boolean requiresLayout() {
349     return true;
350   }
351 } // TextPaneAppender
352

353
354
355
Popular Tags