FileDocCategorySizeDatePackage
TextPaneAppender.javaAPI DocApache log4j 1.2.159370Sat Aug 25 00:09:34 BST 2007org.apache.log4j.gui

TextPaneAppender

public class TextPaneAppender extends AppenderSkeleton
Experimental TextPaneAppender.
Created: Sat Feb 26 18:50:27 2000
author
Sven Reimers

Fields Summary
JTextPane
textpane
StyledDocument
doc
org.apache.log4j.helpers.TracerPrintWriter
tp
StringWriter
sw
QuietWriter
qw
Hashtable
attributes
Hashtable
icons
private String
label
private boolean
fancy
final String
LABEL_OPTION
final String
COLOR_OPTION_FATAL
final String
COLOR_OPTION_ERROR
final String
COLOR_OPTION_WARN
final String
COLOR_OPTION_INFO
final String
COLOR_OPTION_DEBUG
final String
COLOR_OPTION_BACKGROUND
final String
FANCY_OPTION
final String
FONT_NAME_OPTION
final String
FONT_SIZE_OPTION
Constructors Summary
public TextPaneAppender(Layout layout, String name)

    this();
    this.layout = layout;
    this.name = name;
    setTextPane(new JTextPane());
    createAttributes();
    createIcons();
  
public TextPaneAppender()

    super();
    setTextPane(new JTextPane());
    createAttributes();
    createIcons();
    this.label="";
    this.sw = new StringWriter();
    this.qw = new QuietWriter(sw, errorHandler);
    this.tp = new TracerPrintWriter(qw);
    this.fancy =true;
  
Methods Summary
public voidappend(org.apache.log4j.spi.LoggingEvent event)

    String text = this.layout.format(event);
    String trace="";
    // Print Stacktrace
    // Quick Hack maybe there is a better/faster way?
    if (event.throwable!=null) {
      event.throwable.printStackTrace(tp);
      for (int i=0; i< sw.getBuffer().length(); i++) {
	if (sw.getBuffer().charAt(i)=='\t")
	  sw.getBuffer().replace(i,i+1,"        ");
      }
      trace = sw.toString();
      sw.getBuffer().delete(0,sw.getBuffer().length());
    }
    try {
      if (fancy) {
	textpane.setEditable(true);
	textpane.insertIcon((ImageIcon)icons.get(event.priority));
	textpane.setEditable(false);
      }
      doc.insertString(doc.getLength(),text+trace,
		       (MutableAttributeSet)attributes.get(event.priority));
	}	
    catch (BadLocationException badex) {
      System.err.println(badex);
    }	
    textpane.setCaretPosition(doc.getLength());
  
public voidclose()

    
  
private static java.lang.StringcolorToString(java.awt.Color c)

    // alpha component emitted only if not default (255)
    String res = ""+c.getRed()+","+c.getGreen()+","+c.getBlue();
    return c.getAlpha() >= 255 ? res : res + ","+c.getAlpha();
  
private voidcreateAttributes()

	
    Priority prio[] = Priority.getAllPossiblePriorities();
    
    attributes = new Hashtable();
    for (int i=0; i<prio.length;i++) {
      MutableAttributeSet att = new SimpleAttributeSet();
      attributes.put(prio[i], att);
      StyleConstants.setFontSize(att,14);
    }
    StyleConstants.setForeground((MutableAttributeSet)attributes.get(Priority.ERROR),Color.red);
    StyleConstants.setForeground((MutableAttributeSet)attributes.get(Priority.WARN),Color.orange);
    StyleConstants.setForeground((MutableAttributeSet)attributes.get(Priority.INFO),Color.gray);
    StyleConstants.setForeground((MutableAttributeSet)attributes.get(Priority.DEBUG),Color.black);
  
private voidcreateIcons()

    Priority prio[] = Priority.getAllPossiblePriorities();
    
    icons = new Hashtable();
    for (int i=0; i<prio.length;i++) {
      if (prio[i].equals(Priority.FATAL))
	icons.put(prio[i],new ImageIcon(loadIcon("icons/RedFlag.gif")));
      if (prio[i].equals(Priority.ERROR))		
	icons.put(prio[i],new ImageIcon(loadIcon("icons/RedFlag.gif")));
      if (prio[i].equals(Priority.WARN))		
	icons.put(prio[i],new ImageIcon(loadIcon("icons/BlueFlag.gif")));
      if (prio[i].equals(Priority.INFO))		
	icons.put(prio[i],new ImageIcon(loadIcon("icons/GreenFlag.gif")));
      if (prio[i].equals(Priority.DEBUG))		
	icons.put(prio[i],new ImageIcon(loadIcon("icons/GreenFlag.gif")));
    }
  
private java.lang.StringgetColor(org.apache.log4j.Priority p)

    Color c =  StyleConstants.getForeground(
		      (MutableAttributeSet)attributes.get(p));
    return c == null ? null : colorToString(c);
  
public java.lang.StringgetColorBackground()

    return colorToString(textpane.getBackground());
  
public java.lang.StringgetColorDebug()

    return getColor(Priority.DEBUG);
  
public java.lang.StringgetColorEmerg()

    return getColor(Priority.FATAL);
  
public java.lang.StringgetColorError()

    return getColor(Priority.ERROR);
  
public java.lang.StringgetColorInfo()

    return getColor(Priority.INFO);
  
public java.lang.StringgetColorWarn()

    return getColor(Priority.WARN);
  
public booleangetFancy()

    return fancy;
  
public java.lang.StringgetFontName()

    AttributeSet attrSet = (AttributeSet) attributes.get(Priority.INFO);
    return StyleConstants.getFontFamily(attrSet);
  
public intgetFontSize()

    AttributeSet attrSet = (AttributeSet) attributes.get(Priority.INFO);
    return StyleConstants.getFontSize(attrSet);
  
public java.lang.StringgetLabel()

    return label;
  
public javax.swing.JTextPanegetTextPane()

    return textpane;
  
public static java.awt.ImageloadIcon(java.lang.String path)

  
          
    Image img = null;
    try {
      URL url = ClassLoader.getSystemResource(path);
      img = (Image) (Toolkit.getDefaultToolkit()).getImage(url);
    } catch (Exception e) {
      System.out.println("Exception occured: " + e.getMessage() + 
			 " - " + e );   
    }	
    return (img);
  
private static java.awt.ColorparseColor(java.lang.String v)

    StringTokenizer st = new StringTokenizer(v,",");
    int val[] = {255,255,255,255};
    int i=0;
    while (st.hasMoreTokens()) {
      val[i]=Integer.parseInt(st.nextToken());
      i++;
    }
    return new Color(val[0],val[1],val[2],val[3]);
  
public booleanrequiresLayout()

    return true;
  
private voidsetColor(org.apache.log4j.Priority p, java.lang.String v)

    StyleConstants.setForeground(
		      (MutableAttributeSet)attributes.get(p),parseColor(v));	
  
public voidsetColorBackground(java.lang.String color)

    textpane.setBackground(parseColor(color));
  
public voidsetColorDebug(java.lang.String color)

    setColor(Priority.DEBUG, color);
  
public voidsetColorEmerg(java.lang.String color)

    setColor(Priority.FATAL, color);
  
public voidsetColorError(java.lang.String color)

    setColor(Priority.ERROR, color);
  
public voidsetColorInfo(java.lang.String color)

    setColor(Priority.INFO, color);
  
public voidsetColorWarn(java.lang.String color)

    setColor(Priority.WARN, color);
  
public voidsetFancy(boolean fancy)

    this.fancy = fancy;
  
public voidsetFontName(java.lang.String name)

    Enumeration e = attributes.elements();
    while (e.hasMoreElements()) {
      StyleConstants.setFontFamily((MutableAttributeSet)e.nextElement(),name);
    }
    return;
  
public voidsetFontSize(int size)

    Enumeration e = attributes.elements();
    while (e.hasMoreElements()) {
      StyleConstants.setFontSize((MutableAttributeSet)e.nextElement(),size);
    }
    return;
  
public voidsetLabel(java.lang.String label)

    this.label = label;
  
public voidsetLayout(org.apache.log4j.Layout layout)

    this.layout=layout;
  
public voidsetName(java.lang.String name)

    this.name = name;
  
public voidsetTextPane(javax.swing.JTextPane textpane)

    this.textpane=textpane;
    textpane.setEditable(false);
    textpane.setBackground(Color.lightGray);
    this.doc=textpane.getStyledDocument();