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

LogTextPanel

public class LogTextPanel extends JPanel
Title: Description: Copyright: Copyright (c) 2001 Company:
author
version
1.0

Fields Summary
private JScrollBar
scrollBar
private JTextPane
textPane
private JCheckBox
cbxTail
private StyledDocument
doc
private Hashtable
fontAttributes
private int
eventBufferMaxSize
private ArrayList
eventBuffer
private int
eventViewIndex
int
maxR
Constructors Summary
public LogTextPanel()


    
    constructComponents();
    createDefaultFontAttributes();
  
Methods Summary
private voidconstructComponents()

      // setup the panel's additional components...
    this.setLayout(new BorderLayout());

    cbxTail = new JCheckBox();
    cbxTail.setSelected(true);
    cbxTail.setText("Tail log events");

    JPanel bottomPanel = new JPanel();
    bottomPanel.add(cbxTail, null);

    textPane = new JTextPane();
    textPane.setEditable(false);
    textPane.setText("");
    doc = textPane.getStyledDocument();

    scrollBar = new JScrollBar(JScrollBar.VERTICAL);

    this.add(bottomPanel, BorderLayout.SOUTH);
    this.add(scrollBar, BorderLayout.EAST);
    this.add(textPane, BorderLayout.CENTER);
  
private voidcreateDefaultFontAttributes()

    Priority[] prio = Priority.getAllPossiblePriorities();

    fontAttributes = new Hashtable();
    for (int i=0; i<prio.length;i++) {
      MutableAttributeSet att = new SimpleAttributeSet();
      fontAttributes.put(prio[i], att);
      //StyleConstants.setFontSize(att,11);
    }

    setTextColor(Priority.FATAL, Color.red);
    setTextColor(Priority.ERROR, Color.magenta);
    setTextColor(Priority.WARN, Color.orange);
    setTextColor(Priority.INFO, Color.blue);
    setTextColor(Priority.DEBUG, Color.black);
  
voiddrawText()


    
    if(maxR < 0)
      maxR =  textPane.getHeight() / textPane.getFontMetrics(textPane.getFont()).getHeight();
    try {
      doc.remove(0, doc.getLength());
    } catch(Exception e) { e.printStackTrace(); }

    for(int i=eventViewIndex; (i < eventBuffer.size()) && (i < (eventViewIndex + maxR)); i++) {
      EventBufferElement evt = (EventBufferElement)eventBuffer.get(i);

      try {
        doc.insertString(doc.getLength(), evt.text, (MutableAttributeSet)fontAttributes.get(evt.prio));
      } catch(Exception e) { e.printStackTrace(); }
    }
  
voidnewEvents(EventBufferElement[] evts)


    if((eventBuffer.size() + evts.length) >= eventBufferMaxSize) {
      for(int i=0; i < evts.length; i++) {
        eventBuffer.remove(0);
      }
      eventViewIndex -= evts.length;
      if(eventViewIndex < 0)
        eventViewIndex = 0;
    }
    for(int i=0; i < evts.length; i++)
      eventBuffer.add(evts[i]);

    if((eventBuffer.size() > maxR) && cbxTail.isSelected()) {
      eventViewIndex = (eventBuffer.size() - maxR);
    }

    // only redraw if new line is visible...
    if((maxR < 0) || (eventBuffer.size() >= eventViewIndex && eventBuffer.size() <= (eventViewIndex + maxR)))
      drawText();
  
private 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]);
  
voidsetEventBufferSize(int bufferSize)

    eventBufferMaxSize = bufferSize;
  
public voidsetTextBackground(java.awt.Color color)

    textPane.setBackground(color);
  
public voidsetTextBackground(java.lang.String v)

    textPane.setBackground(parseColor(v));
  
voidsetTextColor(org.apache.log4j.Priority p, java.lang.String v)

    StyleConstants.setForeground(
          (MutableAttributeSet)fontAttributes.get(p),parseColor(v));
  
voidsetTextColor(org.apache.log4j.Priority p, java.awt.Color c)

    StyleConstants.setForeground(
          (MutableAttributeSet)fontAttributes.get(p),c);
  
voidsetTextFontName(java.lang.String name)

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

    Enumeration e = fontAttributes.elements();
    while (e.hasMoreElements()) {
      StyleConstants.setFontSize((MutableAttributeSet)e.nextElement(),size);
    }
    return;